changeset 1521:3606278d305e

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2015 13:37:24 +0200
parents 4a503a8c7749
children f938f7779bcb
files Core/HttpServer/EmbeddedResourceHttpHandler.cpp Core/HttpServer/FilesystemHttpHandler.cpp Core/HttpServer/HttpOutput.cpp Core/HttpServer/HttpOutput.h Core/HttpServer/MongooseServer.cpp Core/RestApi/RestApiOutput.cpp OrthancServer/ParsedDicomFile.cpp Plugins/Engine/OrthancPlugins.cpp
diffstat 8 files changed, 70 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/Core/HttpServer/EmbeddedResourceHttpHandler.cpp	Tue Aug 11 13:26:42 2015 +0200
+++ b/Core/HttpServer/EmbeddedResourceHttpHandler.cpp	Tue Aug 11 13:37:24 2015 +0200
@@ -81,7 +81,7 @@
       size_t size = EmbeddedResources::GetDirectoryResourceSize(resourceId_, resourcePath.c_str());
 
       output.SetContentType(contentType.c_str());
-      output.SendBody(buffer, size);
+      output.Answer(buffer, size);
     }
     catch (OrthancException&)
     {
--- a/Core/HttpServer/FilesystemHttpHandler.cpp	Tue Aug 11 13:26:42 2015 +0200
+++ b/Core/HttpServer/FilesystemHttpHandler.cpp	Tue Aug 11 13:37:24 2015 +0200
@@ -104,7 +104,7 @@
     s += "</html>";
 
     output.SetContentType("text/html");
-    output.SendBody(s);
+    output.Answer(s);
   }
 
 
--- a/Core/HttpServer/HttpOutput.cpp	Tue Aug 11 13:26:42 2015 +0200
+++ b/Core/HttpServer/HttpOutput.cpp	Tue Aug 11 13:37:24 2015 +0200
@@ -320,73 +320,75 @@
     stateMachine_.SendBody(NULL, 0);
   }
 
-  void HttpOutput::SendBody(const void* buffer, 
-                            size_t length)
+  void HttpOutput::Answer(const void* buffer, 
+                          size_t length)
   {
     if (length == 0)
     {
-      stateMachine_.SendBody(NULL, 0);
+      AnswerEmpty();
+      return;
+    }
+
+    HttpCompression compression = GetPreferredCompression(length);
+
+    if (compression == HttpCompression_None)
+    {
+      stateMachine_.SetContentLength(length);
+      stateMachine_.SendBody(buffer, length);
+      return;
+    }
+
+    std::string compressed, encoding;
+
+    switch (compression)
+    {
+      case HttpCompression_Deflate:
+      {
+        encoding = "deflate";
+        ZlibCompressor compressor;
+        // Do not prefix the buffer with its uncompressed size, to be compatible with "deflate"
+        compressor.SetPrefixWithUncompressedSize(false);  
+        compressor.Compress(compressed, buffer, length);
+        break;
+      }
+
+      case HttpCompression_Gzip:
+      {
+        encoding = "gzip";
+        GzipCompressor compressor;
+        compressor.Compress(compressed, buffer, length);
+        break;
+      }
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+    }
+
+    LOG(TRACE) << "Compressing a HTTP answer using " << encoding;
+
+    // The body is empty, do not use HTTP compression
+    if (compressed.size() == 0)
+    {
+      AnswerEmpty();
     }
     else
     {
-      HttpCompression compression = GetPreferredCompression(length);
-
-      switch (compression)
-      {
-        case HttpCompression_None:
-        {
-          stateMachine_.SendBody(buffer, length);
-          break;
-        }
-
-        case HttpCompression_Gzip:
-        case HttpCompression_Deflate:
-        {
-          std::string compressed, encoding;
+      stateMachine_.AddHeader("Content-Encoding", encoding);
+      stateMachine_.SetContentLength(compressed.size());
+      stateMachine_.SendBody(compressed.c_str(), compressed.size());
+    }
 
-          if (compression == HttpCompression_Deflate)
-          {
-            encoding = "deflate";
-            ZlibCompressor compressor;
-            // Do not prefix the buffer with its uncompressed size, to be compatible with "deflate"
-            compressor.SetPrefixWithUncompressedSize(false);  
-            compressor.Compress(compressed, buffer, length);
-          }
-          else
-          {
-            encoding = "gzip";
-            GzipCompressor compressor;
-            compressor.Compress(compressed, buffer, length);
-          }
-
-          LOG(TRACE) << "Compressing a HTTP answer using " << encoding;
-
-          // The body is empty, do not use Deflate compression
-          if (compressed.size() == 0)
-          {
-            stateMachine_.SendBody(NULL, 0);
-          }
-          else
-          {
-            stateMachine_.AddHeader("Content-Encoding", encoding);
-            stateMachine_.SendBody(compressed.c_str(), compressed.size());
-          }
-
-          break;
-        }
-
-        default:
-          throw OrthancException(ErrorCode_NotImplemented);
-      }
-    }
+    stateMachine_.CloseBody();
   }
 
-  void HttpOutput::SendBody(const std::string& str)
+
+  void HttpOutput::Answer(const std::string& str)
   {
-    SendBody(str.size() == 0 ? NULL : str.c_str(), str.size());
+    Answer(str.size() == 0 ? NULL : str.c_str(), str.size());
   }
 
-  void HttpOutput::SendEmptyBody()
+
+  void HttpOutput::AnswerEmpty()
   {
     stateMachine_.SetContentLength(0);
     stateMachine_.SendBody(NULL, 0);
--- a/Core/HttpServer/HttpOutput.h	Tue Aug 11 13:26:42 2015 +0200
+++ b/Core/HttpServer/HttpOutput.h	Tue Aug 11 13:37:24 2015 +0200
@@ -170,12 +170,12 @@
       stateMachine_.AddHeader(key, value);
     }
 
-    void SendBody(const void* buffer, 
-                  size_t length);
+    void Answer(const void* buffer, 
+                size_t length);
 
-    void SendBody(const std::string& str);
+    void Answer(const std::string& str);
 
-    void SendEmptyBody();
+    void AnswerEmpty();
 
     void SendMethodNotAllowed(const std::string& allowed);
 
--- a/Core/HttpServer/MongooseServer.cpp	Tue Aug 11 13:26:42 2015 +0200
+++ b/Core/HttpServer/MongooseServer.cpp	Tue Aug 11 13:37:24 2015 +0200
@@ -695,7 +695,7 @@
           return;
 
         case PostDataStatus_Pending:
-          output.SendEmptyBody();
+          output.AnswerEmpty();
           return;
 
         default:
--- a/Core/RestApi/RestApiOutput.cpp	Tue Aug 11 13:26:42 2015 +0200
+++ b/Core/RestApi/RestApiOutput.cpp	Tue Aug 11 13:37:24 2015 +0200
@@ -95,7 +95,7 @@
       std::string s;
       Toolbox::JsonToXml(s, value);
       output_.SetContentType("application/xml");
-      output_.SendBody(s);
+      output_.Answer(s);
 #else
       LOG(ERROR) << "Orthanc was compiled without XML support";
       throw OrthancException(ErrorCode_InternalError);
@@ -105,7 +105,7 @@
     {
       Json::StyledWriter writer;
       output_.SetContentType("application/json");
-      output_.SendBody(writer.write(value));
+      output_.Answer(writer.write(value));
     }
 
     alreadySent_ = true;
@@ -124,7 +124,7 @@
   {
     CheckStatus();
     output_.SetContentType(contentType.c_str());
-    output_.SendBody(buffer, length);
+    output_.Answer(buffer, length);
     alreadySent_ = true;
   }
 
--- a/OrthancServer/ParsedDicomFile.cpp	Tue Aug 11 13:26:42 2015 +0200
+++ b/OrthancServer/ParsedDicomFile.cpp	Tue Aug 11 13:37:24 2015 +0200
@@ -273,7 +273,7 @@
         length_(element.getLength(transferSyntax)),
         offset_(0)
       {
-        static const size_t CHUNK_SIZE = 64 * 1024;  // Use a 64KB chunk
+        static const size_t CHUNK_SIZE = 64 * 1024;  // Use chunks of max 64KB
         chunk_.resize(CHUNK_SIZE);
       }
 
--- a/Plugins/Engine/OrthancPlugins.cpp	Tue Aug 11 13:26:42 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Tue Aug 11 13:37:24 2015 +0200
@@ -477,7 +477,7 @@
 
     HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(p.output);
     translatedOutput->SetContentType(p.mimeType);
-    translatedOutput->SendBody(p.answer, p.answerSize);
+    translatedOutput->Answer(p.answer, p.answerSize);
   }
 
 
@@ -583,7 +583,7 @@
     writer.WriteToMemory(png, accessor);
 
     translatedOutput->SetContentType("image/png");
-    translatedOutput->SendBody(png);
+    translatedOutput->Answer(png);
   }