changeset 992:a9f5d0742e22

implementation of HttpCommand in WebAssemblyOracle
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 11 Sep 2019 12:31:49 +0200
parents 3ea70a62ca00
children f270852d3180
files Framework/Oracle/WebAssemblyOracle.cpp Framework/Oracle/WebAssemblyOracle.h
diffstat 2 files changed, 74 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Oracle/WebAssemblyOracle.cpp	Wed Sep 11 10:43:21 2019 +0200
+++ b/Framework/Oracle/WebAssemblyOracle.cpp	Wed Sep 11 12:31:49 2019 +0200
@@ -306,32 +306,39 @@
           {
             switch (context->GetCommand().GetType())
             {
-            case IOracleCommand::Type_OrthancRestApi:
-            {
-              LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);";
-              OrthancRestApiCommand::SuccessMessage message
-              (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer);
-              context->EmitMessage(message);
-              break;
-            }
+              case IOracleCommand::Type_Http:
+              {
+                HttpCommand::SuccessMessage message(context->GetTypedCommand<HttpCommand>(), headers, answer);
+                context->EmitMessage(message);
+                break;
+              }
+
+              case IOracleCommand::Type_OrthancRestApi:
+              {
+                LOG(TRACE) << "WebAssemblyOracle::FetchContext::SuccessCallback. About to call context->EmitMessage(message);";
+                OrthancRestApiCommand::SuccessMessage message
+                  (context->GetTypedCommand<OrthancRestApiCommand>(), headers, answer);
+                context->EmitMessage(message);
+                break;
+              }
 
-            case IOracleCommand::Type_GetOrthancImage:
-            {
-              context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer
-              (context->GetEmitter(), context->GetReceiver(), answer, headers);
-              break;
-            }
+              case IOracleCommand::Type_GetOrthancImage:
+              {
+                context->GetTypedCommand<GetOrthancImageCommand>().ProcessHttpAnswer
+                  (context->GetEmitter(), context->GetReceiver(), answer, headers);
+                break;
+              }
 
-            case IOracleCommand::Type_GetOrthancWebViewerJpeg:
-            {
-              context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer
-              (context->GetEmitter(), context->GetReceiver(), answer);
-              break;
-            }
+              case IOracleCommand::Type_GetOrthancWebViewerJpeg:
+              {
+                context->GetTypedCommand<GetOrthancWebViewerJpegCommand>().ProcessHttpAnswer
+                  (context->GetEmitter(), context->GetReceiver(), answer);
+                break;
+              }
 
-            default:
-              LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: "
-                << context->GetCommand().GetType();
+              default:
+                LOG(ERROR) << "Command type not implemented by the WebAssembly Oracle: "
+                           << context->GetCommand().GetType();
             }
           }
         }
@@ -375,7 +382,7 @@
     const IObserver&               receiver_;
     std::auto_ptr<IOracleCommand>  command_;
     Orthanc::HttpMethod            method_;
-    std::string                    uri_;
+    std::string                    url_;
     std::string                    body_;
     HttpHeaders                    headers_;
     unsigned int                   timeout_;
@@ -402,9 +409,14 @@
       method_ = method;
     }
 
-    void SetUri(const std::string& uri)
+    void SetOrthancUri(const std::string& uri)
     {
-      uri_ = oracle_.orthancRoot_ + uri;
+      url_ = oracle_.orthancRoot_ + uri;
+    }
+
+    void SetUrl(const std::string& url)
+    {
+      url_ = url;
     }
 
     void SetBody(std::string& body /* will be swapped */)
@@ -514,9 +526,9 @@
 
         // Must be the last call to prevent memory leak on error
 #if 0
-        LOG(TRACE) << "Performing " << method << " request on URI: \"" << uri_ << "\"";
+        LOG(TRACE) << "Performing " << method << " request on URI: \"" << url_ << "\"";
 #endif
-        emscripten_fetch(&attr, uri_.c_str());
+        emscripten_fetch(&attr, url_.c_str());
       }        
       catch(...)
       {
@@ -546,6 +558,29 @@
   }
 #endif
 
+  
+  void WebAssemblyOracle::Execute(const IObserver& receiver,
+                                  HttpCommand* command)
+  {
+    FetchCommand fetch(*this, receiver, command);
+    
+    fetch.SetMethod(command->GetMethod());
+    fetch.SetUrl(command->GetUrl());
+    fetch.SetHttpHeaders(command->GetHttpHeaders());
+    fetch.SetTimeout(command->GetTimeout());
+    
+    if (command->GetMethod() == Orthanc::HttpMethod_Post ||
+        command->GetMethod() == Orthanc::HttpMethod_Put)
+    {
+      std::string body;
+      command->SwapBody(body);
+      fetch.SetBody(body);
+    }
+    
+    fetch.Execute();
+  }
+  
+
   void WebAssemblyOracle::Execute(const IObserver& receiver,
                                   OrthancRestApiCommand* command)
   {
@@ -566,7 +601,7 @@
       FetchCommand fetch(*this, receiver, command);
 
       fetch.SetMethod(command->GetMethod());
-      fetch.SetUri(command->GetUri());
+      fetch.SetOrthancUri(command->GetUri());
       fetch.SetHttpHeaders(command->GetHttpHeaders());
       fetch.SetTimeout(command->GetTimeout());
 
@@ -622,7 +657,7 @@
 
     FetchCommand fetch(*this, receiver, command);
 
-    fetch.SetUri(command->GetUri());
+    fetch.SetOrthancUri(command->GetUri());
     fetch.SetHttpHeaders(command->GetHttpHeaders());
     fetch.SetTimeout(command->GetTimeout());
       
@@ -642,7 +677,7 @@
 
     FetchCommand fetch(*this, receiver, command);
 
-    fetch.SetUri(command->GetUri());
+    fetch.SetOrthancUri(command->GetUri());
     fetch.SetHttpHeaders(command->GetHttpHeaders());
     fetch.SetTimeout(command->GetTimeout());
       
@@ -667,6 +702,10 @@
 
     switch (command->GetType())
     {
+      case IOracleCommand::Type_Http:
+        Execute(receiver, dynamic_cast<HttpCommand*>(protection.release()));
+        break;
+        
       case IOracleCommand::Type_OrthancRestApi:
         //// DIAGNOSTIC. PLEASE REMOVE IF IT HAS BEEN COMMITTED BY MISTAKE
         //{
--- a/Framework/Oracle/WebAssemblyOracle.h	Wed Sep 11 10:43:21 2019 +0200
+++ b/Framework/Oracle/WebAssemblyOracle.h	Wed Sep 11 12:31:49 2019 +0200
@@ -32,6 +32,7 @@
 #include "../Messages/IObservable.h"
 #include "GetOrthancImageCommand.h"
 #include "GetOrthancWebViewerJpegCommand.h"
+#include "HttpCommand.h"
 #include "IOracle.h"
 #include "OrthancRestApiCommand.h"
 
@@ -51,6 +52,9 @@
     class FetchCommand;    
     
     void Execute(const IObserver& receiver,
+                 HttpCommand* command);    
+    
+    void Execute(const IObserver& receiver,
                  OrthancRestApiCommand* command);    
     
     void Execute(const IObserver& receiver,