changeset 1445:d26c8a93d05a

refactoring: SimpleGet
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 01 Jul 2015 12:30:19 +0200
parents b2b09a3dbd8e
children 8dc80ba768aa
files OrthancServer/LuaScripting.cpp OrthancServer/OrthancHttpHandler.cpp OrthancServer/OrthancHttpHandler.h Plugins/Engine/OrthancPlugins.cpp
diffstat 4 files changed, 24 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/LuaScripting.cpp	Wed Jul 01 12:09:58 2015 +0200
+++ b/OrthancServer/LuaScripting.cpp	Wed Jul 01 12:30:19 2015 +0200
@@ -82,14 +82,10 @@
     const char* uri = lua_tostring(state, 1);
     bool builtin = (nArgs == 2 ? lua_toboolean(state, 2) : false);
 
-    IHttpHandler& handler = (builtin ? 
-                             serverContext->GetHttpHandler().GetOrthancRestApi() : 
-                             serverContext->GetHttpHandler());
-    
-    std::string str;
-    if (HttpToolbox::SimpleGet(str, handler, uri))
+    std::string result;
+    if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), uri))
     {
-      lua_pushstring(state, str.c_str());
+      lua_pushstring(state, result.c_str());
     }
     else
     {
--- a/OrthancServer/OrthancHttpHandler.cpp	Wed Jul 01 12:09:58 2015 +0200
+++ b/OrthancServer/OrthancHttpHandler.cpp	Wed Jul 01 12:30:19 2015 +0200
@@ -69,13 +69,20 @@
   }
 
 
-  IHttpHandler& OrthancHttpHandler::GetOrthancRestApi() const
+  IHttpHandler& OrthancHttpHandler::RestrictToOrthancRestApi(bool restrict)
   {
-    if (orthancRestApi_ == NULL)
+    if (restrict)
     {
-      throw OrthancException(ErrorCode_InternalError);
+      if (orthancRestApi_ == NULL)
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+
+      return *orthancRestApi_;
     }
-
-    return *orthancRestApi_;
+    else
+    {
+      return *this;
+    }
   }
 }
--- a/OrthancServer/OrthancHttpHandler.h	Wed Jul 01 12:09:58 2015 +0200
+++ b/OrthancServer/OrthancHttpHandler.h	Wed Jul 01 12:30:19 2015 +0200
@@ -64,6 +64,6 @@
       return orthancRestApi_ != NULL;
     }
 
-    IHttpHandler& GetOrthancRestApi() const;
+    IHttpHandler& RestrictToOrthancRestApi(bool restrict);
   };
 }
--- a/Plugins/Engine/OrthancPlugins.cpp	Wed Jul 01 12:09:58 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Wed Jul 01 12:30:19 2015 +0200
@@ -616,38 +616,15 @@
     const _OrthancPluginRestApiGet& p = 
       *reinterpret_cast<const _OrthancPluginRestApiGet*>(parameters);
         
-    // TODO : Use "HttpToolbox::SimpleGet()"
-
-    IHttpHandler::Arguments headers;  // No HTTP header
-    std::string body;  // No body for a GET request
-
-    UriComponents uri;
-    IHttpHandler::GetArguments getArguments;
-    HttpToolbox::ParseGetQuery(uri, getArguments, p.uri);
-
-    StringHttpOutput stream;
-    HttpOutput http(stream, false /* no keep alive */);
-
     LOG(INFO) << "Plugin making REST GET call on URI " << p.uri
               << (afterPlugins ? " (after plugins)" : " (built-in API)");
 
-    bool ok = false;
-    std::string result;
-
-    if (afterPlugins)
-    {
-      ok = Handle(http, HttpMethod_Get, uri, headers, getArguments, body);
-    }
+    CheckContextAvailable();
+    IHttpHandler& handler = pimpl_->context_->GetHttpHandler().RestrictToOrthancRestApi(!afterPlugins);
 
-    if (!ok)
+    std::string result;
+    if (HttpToolbox::SimpleGet(result, handler, p.uri))
     {
-      ok = (pimpl_->restApi_ != NULL &&
-            pimpl_->restApi_->Handle(http, HttpMethod_Get, uri, headers, getArguments, body));
-    }
-
-    if (ok)
-    {
-      stream.GetOutput(result);
       CopyToMemoryBuffer(*p.target, result);
     }
     else
@@ -664,6 +641,8 @@
     const _OrthancPluginRestApiPostPut& p = 
       *reinterpret_cast<const _OrthancPluginRestApiPostPut*>(parameters);
 
+    // TODO : Use "HttpToolbox::SimplePost()"
+
     IHttpHandler::Arguments headers;  // No HTTP header
     IHttpHandler::GetArguments getArguments;  // No GET argument for POST/PUT
 
@@ -713,6 +692,8 @@
     UriComponents uri;
     Toolbox::SplitUriComponents(uri, reinterpret_cast<const char*>(parameters));
 
+    // TODO : Use "HttpToolbox::SimpleDelete()"
+
     IHttpHandler::Arguments headers;  // No HTTP header
     IHttpHandler::GetArguments getArguments;  // No GET argument for POST/PUT
     std::string body;  // No body for DELETE