changeset 355:753e69f9326e

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Jan 2013 17:56:44 +0100
parents 4d76fce206ef
children d1f82cf3ae35
files Core/HttpServer/EmbeddedResourceHttpHandler.cpp Core/HttpServer/EmbeddedResourceHttpHandler.h Core/HttpServer/FilesystemHttpHandler.cpp Core/HttpServer/FilesystemHttpHandler.h Core/HttpServer/HttpHandler.h Core/HttpServer/MongooseServer.cpp Core/RestApi/RestApi.cpp Core/RestApi/RestApi.h
diffstat 8 files changed, 42 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/Core/HttpServer/EmbeddedResourceHttpHandler.cpp	Wed Jan 30 14:34:36 2013 +0100
+++ b/Core/HttpServer/EmbeddedResourceHttpHandler.cpp	Wed Jan 30 17:56:44 2013 +0100
@@ -58,13 +58,13 @@
 
   void EmbeddedResourceHttpHandler::Handle(
     HttpOutput& output,
-    const std::string& method,
+    Orthanc_HttpMethod method,
     const UriComponents& uri,
     const Arguments& headers,
     const Arguments& arguments,
     const std::string&)
   {
-    if (method != "GET")
+    if (method != Orthanc_HttpMethod_Get)
     {
       output.SendMethodNotAllowedError("GET");
       return;
--- a/Core/HttpServer/EmbeddedResourceHttpHandler.h	Wed Jan 30 14:34:36 2013 +0100
+++ b/Core/HttpServer/EmbeddedResourceHttpHandler.h	Wed Jan 30 17:56:44 2013 +0100
@@ -54,7 +54,7 @@
 
     virtual void Handle(
       HttpOutput& output,
-      const std::string& method,
+      Orthanc_HttpMethod method,
       const UriComponents& uri,
       const Arguments& headers,
       const Arguments& arguments,
--- a/Core/HttpServer/FilesystemHttpHandler.cpp	Wed Jan 30 14:34:36 2013 +0100
+++ b/Core/HttpServer/FilesystemHttpHandler.cpp	Wed Jan 30 17:56:44 2013 +0100
@@ -127,13 +127,13 @@
 
   void FilesystemHttpHandler::Handle(
     HttpOutput& output,
-    const std::string& method,
+    Orthanc_HttpMethod method,
     const UriComponents& uri,
     const Arguments& headers,
     const Arguments& arguments,
     const std::string&)
   {
-    if (method != "GET")
+    if (method != Orthanc_HttpMethod_Get)
     {
       output.SendMethodNotAllowedError("GET");
       return;
--- a/Core/HttpServer/FilesystemHttpHandler.h	Wed Jan 30 14:34:36 2013 +0100
+++ b/Core/HttpServer/FilesystemHttpHandler.h	Wed Jan 30 17:56:44 2013 +0100
@@ -56,7 +56,7 @@
 
     virtual void Handle(
       HttpOutput& output,
-      const std::string& method,
+      Orthanc_HttpMethod method,
       const UriComponents& uri,
       const Arguments& headers,
       const Arguments& arguments,
--- a/Core/HttpServer/HttpHandler.h	Wed Jan 30 14:34:36 2013 +0100
+++ b/Core/HttpServer/HttpHandler.h	Wed Jan 30 17:56:44 2013 +0100
@@ -36,6 +36,7 @@
 #include <vector>
 #include <stdint.h>
 #include "../Toolbox.h"
+#include "../../OrthancCppClient/HttpEnumerations.h"
 
 namespace Orthanc
 {
@@ -53,7 +54,7 @@
     virtual bool IsServedUri(const UriComponents& uri) = 0;
 
     virtual void Handle(HttpOutput& output,
-                        const std::string& method,
+                        Orthanc_HttpMethod method,
                         const UriComponents& uri,
                         const Arguments& headers,
                         const Arguments& getArguments,
--- a/Core/HttpServer/MongooseServer.cpp	Wed Jan 30 14:34:36 2013 +0100
+++ b/Core/HttpServer/MongooseServer.cpp	Wed Jan 30 17:56:44 2013 +0100
@@ -464,6 +464,30 @@
       MongooseServer* that = (MongooseServer*) (request->user_data);
       MongooseOutput output(connection);
 
+      // Compute the method
+      Orthanc_HttpMethod method;
+      if (!strcmp(request->request_method, "GET"))
+      {
+        method = Orthanc_HttpMethod_Get;
+      }
+      else if (!strcmp(request->request_method, "POST"))
+      {
+        method = Orthanc_HttpMethod_Post;
+      }
+      else if (!strcmp(request->request_method, "DELETE"))
+      {
+        method = Orthanc_HttpMethod_Delete;
+      }
+      else if (!strcmp(request->request_method, "PUT"))
+      {
+        method = Orthanc_HttpMethod_Put;
+      }
+      else
+      {
+        output.SendHeader(Orthanc_HttpStatus_405_MethodNotAllowed);
+        return (void*) "";
+      }      
+
       if (!that->IsRemoteAccessAllowed() &&
           request->remote_ip != LOCALHOST)
       {
@@ -489,12 +513,12 @@
 
       std::string postData;
 
-      if (!strcmp(request->request_method, "GET"))
+      if (method == Orthanc_HttpMethod_Get)
       {
         HttpHandler::ParseGetQuery(arguments, request->query_string);
       }
-      else if (!strcmp(request->request_method, "POST") ||
-               !strcmp(request->request_method, "PUT"))
+      else if (method == Orthanc_HttpMethod_Post ||
+               method == Orthanc_HttpMethod_Put)
       {
         HttpHandler::Arguments::const_iterator ct = headers.find("content-type");
         if (ct == headers.end())
@@ -543,8 +567,7 @@
       {
         try
         {
-          handler->Handle(output, std::string(request->request_method),
-                          uri, headers, arguments, postData);
+          handler->Handle(output, method, uri, headers, arguments, postData);
         }
         catch (OrthancException& e)
         {
--- a/Core/RestApi/RestApi.cpp	Wed Jan 30 14:34:36 2013 +0100
+++ b/Core/RestApi/RestApi.cpp	Wed Jan 30 17:56:44 2013 +0100
@@ -180,7 +180,7 @@
   }
 
   void RestApi::Handle(HttpOutput& output,
-                       const std::string& method,
+                       Orthanc_HttpMethod method,
                        const UriComponents& uri,
                        const Arguments& headers,
                        const Arguments& getArguments,
@@ -191,7 +191,7 @@
     RestApiPath::Components components;
     UriComponents trailing;
 
-    if (method == "GET")
+    if (method == Orthanc_HttpMethod_Get)
     {
       for (GetHandlers::const_iterator it = getHandlers_.begin();
            it != getHandlers_.end(); it++)
@@ -213,7 +213,7 @@
         }
       }
     }
-    else if (method == "PUT")
+    else if (method == Orthanc_HttpMethod_Put)
     {
       for (PutHandlers::const_iterator it = putHandlers_.begin();
            it != putHandlers_.end(); it++)
@@ -235,7 +235,7 @@
         }
       }
     }
-    else if (method == "POST")
+    else if (method == Orthanc_HttpMethod_Post)
     {
       for (PostHandlers::const_iterator it = postHandlers_.begin();
            it != postHandlers_.end(); it++)
@@ -257,7 +257,7 @@
         }
       }
     }
-    else if (method == "DELETE")
+    else if (method == Orthanc_HttpMethod_Delete)
     {
       for (DeleteHandlers::const_iterator it = deleteHandlers_.begin();
            it != deleteHandlers_.end(); it++)
--- a/Core/RestApi/RestApi.h	Wed Jan 30 14:34:36 2013 +0100
+++ b/Core/RestApi/RestApi.h	Wed Jan 30 17:56:44 2013 +0100
@@ -212,7 +212,7 @@
     virtual bool IsServedUri(const UriComponents& uri);
 
     virtual void Handle(HttpOutput& output,
-                        const std::string& method,
+                        Orthanc_HttpMethod method,
                         const UriComponents& uri,
                         const Arguments& headers,
                         const Arguments& getArguments,