diff Core/HttpServer/HttpOutput.cpp @ 1521:3606278d305e

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2015 13:37:24 +0200
parents 8bd0d897763f
children f938f7779bcb
line wrap: on
line diff
--- 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);