diff OrthancFramework/Sources/HttpServer/StringHttpOutput.cpp @ 4605:c8f444e8556d

new function in the plugin SDK: OrthancPluginCallRestApi()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Mar 2021 16:34:02 +0200
parents d9473bd5ed43
children 7053502fbf97
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpServer/StringHttpOutput.cpp	Wed Mar 17 15:48:31 2021 +0100
+++ b/OrthancFramework/Sources/HttpServer/StringHttpOutput.cpp	Tue Mar 30 16:34:02 2021 +0200
@@ -24,43 +24,97 @@
 #include "StringHttpOutput.h"
 
 #include "../OrthancException.h"
+#include "../Toolbox.h"
 
 namespace Orthanc
 {
-  void StringHttpOutput::OnHttpStatusReceived(HttpStatus status)
+  StringHttpOutput::StringHttpOutput() :
+    status_(HttpStatus_404_NotFound),
+    validBody_(true),
+    validHeaders_(true)
   {
-    switch (status)
+  }
+
+
+  void StringHttpOutput::Send(bool isHeader, const void* buffer, size_t length)
+  {
+    if (isHeader)
     {
-      case HttpStatus_200_Ok:
-        found_ = true;
-        break;
-
-      case HttpStatus_404_NotFound:
-        found_ = false;
-        break;
-
-      default:
-        throw OrthancException(ErrorCode_BadRequest);
+      if (validHeaders_)
+      {
+        headers_.AddChunk(buffer, length);
+      }
+      else
+      {
+        throw OrthancException(ErrorCode_BadSequenceOfCalls);
+      }
+    }
+    else
+    {
+      if (validBody_)
+      {
+        body_.AddChunk(buffer, length);
+      }
+      else
+      {
+        throw OrthancException(ErrorCode_BadSequenceOfCalls);
+      }
     }
   }
 
-  void StringHttpOutput::Send(bool isHeader, const void* buffer, size_t length)
+  
+  void StringHttpOutput::GetBody(std::string& output)
   {
-    if (!isHeader)
+    if (!validBody_)
     {
-      buffer_.AddChunk(buffer, length);
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
     }
-  }
-
-  void StringHttpOutput::GetOutput(std::string& output)
-  {
-    if (found_)
+    else if (status_ == HttpStatus_200_Ok)
     {
-      buffer_.Flatten(output);
+      body_.Flatten(output);
+      validBody_ = false;
     }
     else
     {
       throw OrthancException(ErrorCode_UnknownResource);
     }
   }
+
+
+  void StringHttpOutput::GetHeaders(std::map<std::string, std::string>& target,
+                                    bool keyToLowerCase)
+  {
+    if (!validHeaders_)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      std::string s;
+      headers_.Flatten(s);
+      validHeaders_ = false;
+
+      std::vector<std::string> lines;
+      Orthanc::Toolbox::TokenizeString(lines, s, '\n');
+
+      target.clear();
+
+      for (size_t i = 1 /* skip the HTTP status line */; i < lines.size(); i++)
+      {
+        size_t colon = lines[i].find(':');
+        if (colon != std::string::npos)
+        {
+          std::string key = lines[i].substr(0, colon);
+
+          if (keyToLowerCase)
+          {
+            Toolbox::ToLowerCase(key);
+          }
+          
+          const std::string value = lines[i].substr(colon + 1);
+          target[Toolbox::StripSpaces(key)] = Toolbox::StripSpaces(value);
+        }
+      }
+    }
+  }
 }