diff Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 3416:541c787e2230

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Jun 2019 21:38:34 +0200
parents b9cba6a91780
children e2b032584a83
line wrap: on
line diff
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Tue Jun 11 21:06:57 2019 +0200
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Tue Jun 11 21:38:34 2019 +0200
@@ -2604,4 +2604,223 @@
   }
 
 #endif  /* HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 */
+
+
+
+
+
+  /******************************************************************
+   ** CHUNKED HTTP SERVER
+   ******************************************************************/
+
+  namespace Internals
+  {
+    void NullRestCallback(OrthancPluginRestOutput* output,
+                          const char* url,
+                          const OrthancPluginHttpRequest* request)
+    {
+    }
+  
+    IChunkedRequestReader *NullChunkedRestCallback(const char* url,
+                                                   const OrthancPluginHttpRequest* request)
+    {
+      return NULL;
+    }
+
+
+#if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER == 1
+
+    OrthancPluginErrorCode ChunkedRequestReaderAddChunk(
+      OrthancPluginServerChunkedRequestReader* reader,
+      const void*                              data,
+      uint32_t                                 size)
+    {
+      try
+      {
+        if (reader == NULL)
+        {
+          return OrthancPluginErrorCode_InternalError;
+        }
+
+        reinterpret_cast<IChunkedRequestReader*>(reader)->AddChunk(data, size);
+        return OrthancPluginErrorCode_Success;
+      }
+      catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
+      {
+        return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+        return OrthancPluginErrorCode_BadFileFormat;
+      }
+      catch (...)
+      {
+        return OrthancPluginErrorCode_Plugin;
+      }
+    }
+
+    
+    OrthancPluginErrorCode ChunkedRequestReaderExecute(
+      OrthancPluginServerChunkedRequestReader* reader,
+      OrthancPluginRestOutput*                 output)
+    {
+      try
+      {
+        if (reader == NULL)
+        {
+          return OrthancPluginErrorCode_InternalError;
+        }
+
+        reinterpret_cast<IChunkedRequestReader*>(reader)->Execute(output);
+        return OrthancPluginErrorCode_Success;
+      }
+      catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e)
+      {
+        return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+        return OrthancPluginErrorCode_BadFileFormat;
+      }
+      catch (...)
+      {
+        return OrthancPluginErrorCode_Plugin;
+      }
+    }
+
+    
+    void ChunkedRequestReaderFinalize(
+      OrthancPluginServerChunkedRequestReader* reader)
+    {
+      if (reader != NULL)
+      {
+        delete reinterpret_cast<IChunkedRequestReader*>(reader);
+      }
+    }
+
+#else
+    
+    void ChunkedRestCompatibility(OrthancPluginRestOutput* output,
+                                  const char* url,
+                                  const OrthancPluginHttpRequest* request,
+                                  RestCallback         GetHandler,
+                                  ChunkedRestCallback  PostHandler,
+                                  RestCallback         DeleteHandler,
+                                  ChunkedRestCallback  PutHandler)
+    {
+      std::string allowed;
+
+      if (GetHandler != Internals::NullRestCallback)
+      {
+        allowed += "GET";
+      }
+
+      if (PostHandler != Internals::NullChunkedRestCallback)
+      {
+        if (!allowed.empty())
+        {
+          allowed += ",";
+        }
+        
+        allowed += "POST";
+      }
+
+      if (DeleteHandler != Internals::NullRestCallback)
+      {
+        if (!allowed.empty())
+        {
+          allowed += ",";
+        }
+        
+        allowed += "DELETE";
+      }
+
+      if (PutHandler != Internals::NullChunkedRestCallback)
+      {
+        if (!allowed.empty())
+        {
+          allowed += ",";
+        }
+        
+        allowed += "PUT";
+      }
+      
+      switch (request->method)
+      {
+        case OrthancPluginHttpMethod_Get:
+        {
+          if (GetHandler == Internals::NullRestCallback)
+          {
+            OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
+          }
+          else
+          {
+            GetHandler(output, url, request);
+          }
+          return;
+        }
+
+        case OrthancPluginHttpMethod_Post:
+        {
+          if (PostHandler == Internals::NullChunkedRestCallback)
+          {
+            OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
+          }
+          else
+          {
+            std::auto_ptr<IChunkedRequestReader> reader(PostHandler(url, request));
+            if (reader.get() == NULL)
+            {
+              ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
+            }
+            else
+            {
+              reader->AddChunk(request->body, request->bodySize);
+              reader->Execute(output);
+            }
+          }
+          return;
+        }
+
+        case OrthancPluginHttpMethod_Delete:
+        {
+          if (DeleteHandler == Internals::NullRestCallback)
+          {
+            OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
+          }
+          else
+          {
+            DeleteHandler(output, url, request);
+          }
+          return;
+        }
+
+        case OrthancPluginHttpMethod_Put:
+        {
+          if (PutHandler == Internals::NullChunkedRestCallback)
+          {
+            OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str());
+          }
+          else
+          {
+            std::auto_ptr<IChunkedRequestReader> reader(PutHandler(url, request));
+            if (reader.get() == NULL)
+            {
+              ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin);
+            }
+            else
+            {
+              reader->AddChunk(request->body, request->bodySize);
+              reader->Execute(output);
+            }
+          }
+          return;
+        }
+
+        default:
+          ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
+      }
+    }
+#endif
+  }
 }