changeset 4329:9dc0e42f868b

moving static methods from HttpToolbox to IHttpHandler
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Nov 2020 13:46:49 +0100
parents ce9284aebd40
children a01b1c9cbef4
files OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake OrthancFramework/Sources/HttpServer/HttpToolbox.cpp OrthancFramework/Sources/HttpServer/HttpToolbox.h OrthancFramework/Sources/HttpServer/IHttpHandler.cpp OrthancFramework/Sources/HttpServer/IHttpHandler.h OrthancServer/Plugins/Engine/OrthancPlugins.cpp OrthancServer/Sources/LuaScripting.cpp
diffstat 7 files changed, 172 insertions(+), 146 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake	Wed Nov 25 13:34:58 2020 +0100
+++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake	Wed Nov 25 13:46:49 2020 +0100
@@ -307,6 +307,7 @@
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/HttpServer/HttpServer.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/HttpServer/HttpStreamTranscoder.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/HttpServer/HttpToolbox.cpp
+    ${CMAKE_CURRENT_LIST_DIR}/../../Sources/HttpServer/IHttpHandler.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/HttpServer/StringHttpOutput.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/RestApi/RestApi.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/RestApi/RestApiCall.cpp
--- a/OrthancFramework/Sources/HttpServer/HttpToolbox.cpp	Wed Nov 25 13:34:58 2020 +0100
+++ b/OrthancFramework/Sources/HttpServer/HttpToolbox.cpp	Wed Nov 25 13:46:49 2020 +0100
@@ -23,15 +23,7 @@
 #include "../PrecompiledHeaders.h"
 #include "HttpToolbox.h"
 
-#include "HttpOutput.h"
-#include "StringHttpOutput.h"
-
-#include <stdio.h>
 #include <string.h>
-#include <iostream>
-
-
-static const char* LOCALHOST = "127.0.0.1";
 
 
 namespace Orthanc
@@ -185,102 +177,4 @@
       compiled[source[i].first] = source[i].second;
     }
   }
-
-
-  bool HttpToolbox::SimpleGet(std::string& result,
-                              IHttpHandler& handler,
-                              RequestOrigin origin,
-                              const std::string& uri,
-                              const IHttpHandler::Arguments& httpHeaders)
-  {
-    UriComponents curi;
-    IHttpHandler::GetArguments getArguments;
-    ParseGetQuery(curi, getArguments, uri.c_str());
-
-    StringHttpOutput stream;
-    HttpOutput http(stream, false /* no keep alive */);
-
-    if (handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Get, curi, 
-                       httpHeaders, getArguments, NULL /* no body for GET */, 0))
-    {
-      stream.GetOutput(result);
-      return true;
-    }
-    else
-    {
-      return false;
-    }
-  }
-
-
-  static bool SimplePostOrPut(std::string& result,
-                              IHttpHandler& handler,
-                              RequestOrigin origin,
-                              HttpMethod method,
-                              const std::string& uri,
-                              const void* bodyData,
-                              size_t bodySize,
-                              const IHttpHandler::Arguments& httpHeaders)
-  {
-    IHttpHandler::GetArguments getArguments;  // No GET argument for POST/PUT
-
-    UriComponents curi;
-    Toolbox::SplitUriComponents(curi, uri);
-
-    StringHttpOutput stream;
-    HttpOutput http(stream, false /* no keep alive */);
-
-    if (handler.Handle(http, origin, LOCALHOST, "", method, curi, 
-                       httpHeaders, getArguments, bodyData, bodySize))
-    {
-      stream.GetOutput(result);
-      return true;
-    }
-    else
-    {
-      return false;
-    }
-  }
-
-
-  bool HttpToolbox::SimplePost(std::string& result,
-                               IHttpHandler& handler,
-                               RequestOrigin origin,
-                               const std::string& uri,
-                               const void* bodyData,
-                               size_t bodySize,
-                               const IHttpHandler::Arguments& httpHeaders)
-  {
-    return SimplePostOrPut(result, handler, origin, HttpMethod_Post, uri, bodyData, bodySize, httpHeaders);
-  }
-
-
-  bool HttpToolbox::SimplePut(std::string& result,
-                              IHttpHandler& handler,
-                              RequestOrigin origin,
-                              const std::string& uri,
-                              const void* bodyData,
-                              size_t bodySize,
-                              const IHttpHandler::Arguments& httpHeaders)
-  {
-    return SimplePostOrPut(result, handler, origin, HttpMethod_Put, uri, bodyData, bodySize, httpHeaders);
-  }
-
-
-  bool HttpToolbox::SimpleDelete(IHttpHandler& handler,
-                                 RequestOrigin origin,
-                                 const std::string& uri,
-                                 const IHttpHandler::Arguments& httpHeaders)
-  {
-    UriComponents curi;
-    Toolbox::SplitUriComponents(curi, uri);
-
-    IHttpHandler::GetArguments getArguments;  // No GET argument for DELETE
-
-    StringHttpOutput stream;
-    HttpOutput http(stream, false /* no keep alive */);
-
-    return handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Delete, curi, 
-                          httpHeaders, getArguments, NULL /* no body for DELETE */, 0);
-  }
 }
--- a/OrthancFramework/Sources/HttpServer/HttpToolbox.h	Wed Nov 25 13:34:58 2020 +0100
+++ b/OrthancFramework/Sources/HttpServer/HttpToolbox.h	Wed Nov 25 13:46:49 2020 +0100
@@ -50,32 +50,5 @@
 
     static void CompileGetArguments(IHttpHandler::Arguments& compiled,
                                     const IHttpHandler::GetArguments& source);
-
-    static bool SimpleGet(std::string& result,
-                          IHttpHandler& handler,
-                          RequestOrigin origin,
-                          const std::string& uri,
-                          const IHttpHandler::Arguments& httpHeaders);
-
-    static bool SimplePost(std::string& result,
-                           IHttpHandler& handler,
-                           RequestOrigin origin,
-                           const std::string& uri,
-                           const void* bodyData,
-                           size_t bodySize,
-                           const IHttpHandler::Arguments& httpHeaders);
-
-    static bool SimplePut(std::string& result,
-                          IHttpHandler& handler,
-                          RequestOrigin origin,
-                          const std::string& uri,
-                          const void* bodyData,
-                          size_t bodySize,
-                          const IHttpHandler::Arguments& httpHeaders);
-
-    static bool SimpleDelete(IHttpHandler& handler,
-                             RequestOrigin origin,
-                             const std::string& uri,
-                             const IHttpHandler::Arguments& httpHeaders);
   };
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancFramework/Sources/HttpServer/IHttpHandler.cpp	Wed Nov 25 13:46:49 2020 +0100
@@ -0,0 +1,131 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2020 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "../PrecompiledHeaders.h"
+#include "IHttpHandler.h"
+
+#include "HttpOutput.h"
+#include "HttpToolbox.h"
+#include "StringHttpOutput.h"
+
+static const char* LOCALHOST = "127.0.0.1";
+
+
+namespace Orthanc
+{
+  bool IHttpHandler::SimpleGet(std::string& result,
+                               IHttpHandler& handler,
+                               RequestOrigin origin,
+                               const std::string& uri,
+                               const IHttpHandler::Arguments& httpHeaders)
+  {
+    UriComponents curi;
+    IHttpHandler::GetArguments getArguments;
+    HttpToolbox::ParseGetQuery(curi, getArguments, uri.c_str());
+
+    StringHttpOutput stream;
+    HttpOutput http(stream, false /* no keep alive */);
+
+    if (handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Get, curi, 
+                       httpHeaders, getArguments, NULL /* no body for GET */, 0))
+    {
+      stream.GetOutput(result);
+      return true;
+    }
+    else
+    {
+      return false;
+    }
+  }
+
+
+  static bool SimplePostOrPut(std::string& result,
+                              IHttpHandler& handler,
+                              RequestOrigin origin,
+                              HttpMethod method,
+                              const std::string& uri,
+                              const void* bodyData,
+                              size_t bodySize,
+                              const IHttpHandler::Arguments& httpHeaders)
+  {
+    IHttpHandler::GetArguments getArguments;  // No GET argument for POST/PUT
+
+    UriComponents curi;
+    Toolbox::SplitUriComponents(curi, uri);
+
+    StringHttpOutput stream;
+    HttpOutput http(stream, false /* no keep alive */);
+
+    if (handler.Handle(http, origin, LOCALHOST, "", method, curi, 
+                       httpHeaders, getArguments, bodyData, bodySize))
+    {
+      stream.GetOutput(result);
+      return true;
+    }
+    else
+    {
+      return false;
+    }
+  }
+
+
+  bool IHttpHandler::SimplePost(std::string& result,
+                                IHttpHandler& handler,
+                                RequestOrigin origin,
+                                const std::string& uri,
+                                const void* bodyData,
+                                size_t bodySize,
+                                const IHttpHandler::Arguments& httpHeaders)
+  {
+    return SimplePostOrPut(result, handler, origin, HttpMethod_Post, uri, bodyData, bodySize, httpHeaders);
+  }
+
+
+  bool IHttpHandler::SimplePut(std::string& result,
+                               IHttpHandler& handler,
+                               RequestOrigin origin,
+                               const std::string& uri,
+                               const void* bodyData,
+                               size_t bodySize,
+                               const IHttpHandler::Arguments& httpHeaders)
+  {
+    return SimplePostOrPut(result, handler, origin, HttpMethod_Put, uri, bodyData, bodySize, httpHeaders);
+  }
+
+
+  bool IHttpHandler::SimpleDelete(IHttpHandler& handler,
+                                  RequestOrigin origin,
+                                  const std::string& uri,
+                                  const IHttpHandler::Arguments& httpHeaders)
+  {
+    UriComponents curi;
+    Toolbox::SplitUriComponents(curi, uri);
+
+    IHttpHandler::GetArguments getArguments;  // No GET argument for DELETE
+
+    StringHttpOutput stream;
+    HttpOutput http(stream, false /* no keep alive */);
+
+    return handler.Handle(http, origin, LOCALHOST, "", HttpMethod_Delete, curi, 
+                          httpHeaders, getArguments, NULL /* no body for DELETE */, 0);
+  }
+}
--- a/OrthancFramework/Sources/HttpServer/IHttpHandler.h	Wed Nov 25 13:34:58 2020 +0100
+++ b/OrthancFramework/Sources/HttpServer/IHttpHandler.h	Wed Nov 25 13:46:49 2020 +0100
@@ -81,5 +81,32 @@
                         const GetArguments& getArguments,
                         const void* bodyData,
                         size_t bodySize) = 0;
+
+    static bool SimpleGet(std::string& result,
+                          IHttpHandler& handler,
+                          RequestOrigin origin,
+                          const std::string& uri,
+                          const Arguments& httpHeaders);
+
+    static bool SimplePost(std::string& result,
+                           IHttpHandler& handler,
+                           RequestOrigin origin,
+                           const std::string& uri,
+                           const void* bodyData,
+                           size_t bodySize,
+                           const Arguments& httpHeaders);
+
+    static bool SimplePut(std::string& result,
+                          IHttpHandler& handler,
+                          RequestOrigin origin,
+                          const std::string& uri,
+                          const void* bodyData,
+                          size_t bodySize,
+                          const Arguments& httpHeaders);
+
+    static bool SimpleDelete(IHttpHandler& handler,
+                             RequestOrigin origin,
+                             const std::string& uri,
+                             const Arguments& httpHeaders);
   };
 }
--- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp	Wed Nov 25 13:34:58 2020 +0100
+++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp	Wed Nov 25 13:46:49 2020 +0100
@@ -2398,7 +2398,7 @@
     std::map<std::string, std::string> httpHeaders;
 
     std::string result;
-    if (HttpToolbox::SimpleGet(result, *handler, RequestOrigin_Plugins, p.uri, httpHeaders))
+    if (IHttpHandler::SimpleGet(result, *handler, RequestOrigin_Plugins, p.uri, httpHeaders))
     {
       CopyToMemoryBuffer(*p.target, result);
     }
@@ -2434,7 +2434,7 @@
     }
       
     std::string result;
-    if (HttpToolbox::SimpleGet(result, *handler, RequestOrigin_Plugins, p.uri, headers))
+    if (IHttpHandler::SimpleGet(result, *handler, RequestOrigin_Plugins, p.uri, headers))
     {
       CopyToMemoryBuffer(*p.target, result);
     }
@@ -2466,8 +2466,8 @@
 
     std::string result;
     if (isPost ? 
-        HttpToolbox::SimplePost(result, *handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize, httpHeaders) :
-        HttpToolbox::SimplePut (result, *handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize, httpHeaders))
+        IHttpHandler::SimplePost(result, *handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize, httpHeaders) :
+        IHttpHandler::SimplePut (result, *handler, RequestOrigin_Plugins, p.uri, p.body, p.bodySize, httpHeaders))
     {
       CopyToMemoryBuffer(*p.target, result);
     }
@@ -2494,7 +2494,7 @@
       
     std::map<std::string, std::string> httpHeaders;
 
-    if (!HttpToolbox::SimpleDelete(*handler, RequestOrigin_Plugins, uri, httpHeaders))
+    if (!IHttpHandler::SimpleDelete(*handler, RequestOrigin_Plugins, uri, httpHeaders))
     {
       throw OrthancException(ErrorCode_UnknownResource);
     }
--- a/OrthancServer/Sources/LuaScripting.cpp	Wed Nov 25 13:34:58 2020 +0100
+++ b/OrthancServer/Sources/LuaScripting.cpp	Wed Nov 25 13:46:49 2020 +0100
@@ -405,8 +405,8 @@
     try
     {
       std::string result;
-      if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
-                                 RequestOrigin_Lua, uri, headers))
+      if (IHttpHandler::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
+                                  RequestOrigin_Lua, uri, headers))
       {
         lua_pushlstring(state, result.c_str(), result.size());
         return 1;
@@ -458,10 +458,10 @@
     {
       std::string result;
       if (isPost ?
-          HttpToolbox::SimplePost(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
-                                  RequestOrigin_Lua, uri, bodyData, bodySize, headers) :
-          HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
-                                 RequestOrigin_Lua, uri, bodyData, bodySize, headers))
+          IHttpHandler::SimplePost(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
+                                   RequestOrigin_Lua, uri, bodyData, bodySize, headers) :
+          IHttpHandler::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
+                                  RequestOrigin_Lua, uri, bodyData, bodySize, headers))
       {
         lua_pushlstring(state, result.c_str(), result.size());
         return 1;
@@ -522,8 +522,8 @@
     
     try
     {
-      if (HttpToolbox::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
-                                    RequestOrigin_Lua, uri, headers))
+      if (IHttpHandler::SimpleDelete(serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), 
+                                     RequestOrigin_Lua, uri, headers))
       {
         lua_pushboolean(state, 1);
         return 1;