diff Framework/Oracle/WebAssemblyOracle.cpp @ 1242:b9b5d4378874 broker

working of WebAssemblyOracle
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 06 Jan 2020 18:08:05 +0100
parents b9f2a111c5b9
children 608983cc2512
line wrap: on
line diff
--- a/Framework/Oracle/WebAssemblyOracle.cpp	Mon Dec 30 10:54:26 2019 +0100
+++ b/Framework/Oracle/WebAssemblyOracle.cpp	Mon Jan 06 18:08:05 2020 +0100
@@ -338,6 +338,9 @@
     HttpHeaders                    headers_;
     unsigned int                   timeout_;
     std::string                    expectedContentType_;
+    bool                           hasCredentials_;
+    std::string                    username_;
+    std::string                    password_;
 
   public:
     FetchCommand(WebAssemblyOracle& oracle,
@@ -347,7 +350,8 @@
       receiver_(receiver),
       command_(command),
       method_(Orthanc::HttpMethod_Get),
-      timeout_(0)
+      timeout_(0),
+      hasCredentials_(false)
     {
       if (command == NULL)
       {
@@ -360,11 +364,6 @@
       method_ = method;
     }
 
-    void SetOrthancUri(const std::string& uri)
-    {
-      url_ = oracle_.orthancRoot_ + uri;
-    }
-
     void SetUrl(const std::string& url)
     {
       url_ = url;
@@ -375,9 +374,12 @@
       body_.swap(body);
     }
 
-    void SetHttpHeaders(const HttpHeaders& headers)
+    void AddHttpHeaders(const HttpHeaders& headers)
     {
-      headers_ = headers;
+      for (HttpHeaders::const_iterator it = headers.begin(); it != headers.end(); ++it)
+      {
+        headers_[it->first] = it->second;
+      }
     }
 
     void SetTimeout(unsigned int timeout)
@@ -385,6 +387,14 @@
       timeout_ = timeout;
     }
 
+    void SetCredentials(const std::string& username,
+                        const std::string& password)
+    {
+      hasCredentials_ = true;
+      username_ = username;
+      password_ = password;
+    }
+
     void Execute()
     {
 #if 0
@@ -435,6 +445,13 @@
       attr.onerror = FetchContext::FailureCallback;
       attr.timeoutMSecs = timeout_ * 1000;
 
+      if (hasCredentials_)
+      {
+        attr.withCredentials = EM_TRUE;
+        attr.userName = username_.c_str();
+        attr.password = password_.c_str();
+      }
+      
       std::vector<const char*> headers;
       headers.reserve(2 * headers_.size() + 1);
 
@@ -510,6 +527,26 @@
 #endif
 
   
+  void WebAssemblyOracle::SetOrthancUrl(FetchCommand& command,
+                                        const std::string& uri) const
+  {
+    if (isLocalOrthanc_)
+    {
+      command.SetUrl(localOrthancRoot_ + uri);
+    }
+    else
+    {
+      command.SetUrl(remoteOrthanc_.GetUrl() + uri);
+      command.AddHttpHeaders(remoteOrthanc_.GetHttpHeaders());
+      
+      if (!remoteOrthanc_.GetUsername().empty())
+      {
+        command.SetCredentials(remoteOrthanc_.GetUsername(), remoteOrthanc_.GetPassword());
+      }
+    }
+  }
+    
+
   void WebAssemblyOracle::Execute(boost::weak_ptr<IObserver> receiver,
                                   HttpCommand* command)
   {
@@ -517,7 +554,7 @@
     
     fetch.SetMethod(command->GetMethod());
     fetch.SetUrl(command->GetUrl());
-    fetch.SetHttpHeaders(command->GetHttpHeaders());
+    fetch.AddHttpHeaders(command->GetHttpHeaders());
     fetch.SetTimeout(command->GetTimeout());
     
     if (command->GetMethod() == Orthanc::HttpMethod_Post ||
@@ -552,8 +589,8 @@
       FetchCommand fetch(*this, receiver, command);
 
       fetch.SetMethod(command->GetMethod());
-      fetch.SetOrthancUri(command->GetUri());
-      fetch.SetHttpHeaders(command->GetHttpHeaders());
+      SetOrthancUrl(fetch, command->GetUri());
+      fetch.AddHttpHeaders(command->GetHttpHeaders());
       fetch.SetTimeout(command->GetTimeout());
 
       if (command->GetMethod() == Orthanc::HttpMethod_Post ||
@@ -608,8 +645,8 @@
 
     FetchCommand fetch(*this, receiver, command);
 
-    fetch.SetOrthancUri(command->GetUri());
-    fetch.SetHttpHeaders(command->GetHttpHeaders());
+    SetOrthancUrl(fetch, command->GetUri());
+    fetch.AddHttpHeaders(command->GetHttpHeaders());
     fetch.SetTimeout(command->GetTimeout());
       
     fetch.Execute();
@@ -628,8 +665,8 @@
 
     FetchCommand fetch(*this, receiver, command);
 
-    fetch.SetOrthancUri(command->GetUri());
-    fetch.SetHttpHeaders(command->GetHttpHeaders());
+    SetOrthancUrl(fetch, command->GetUri());
+    fetch.AddHttpHeaders(command->GetHttpHeaders());
     fetch.SetTimeout(command->GetTimeout());
       
     fetch.Execute();