diff Core/HttpServer/HttpOutput.h @ 1113:ba5c0908600c

Refactoring of HttpOutput ("Content-Length" header is now always sent)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Sep 2014 15:51:20 +0200
parents 8d1845feb277
children da56a7916e8a
line wrap: on
line diff
--- a/Core/HttpServer/HttpOutput.h	Mon Sep 01 12:20:26 2014 +0200
+++ b/Core/HttpServer/HttpOutput.h	Tue Sep 02 15:51:20 2014 +0200
@@ -51,84 +51,84 @@
     private:
       enum State
       {
-        State_WaitingHttpStatus,
         State_WritingHeader,      
-        State_WritingBody
+        State_WritingBody,
+        State_Done
       };
 
       IHttpOutputStream& stream_;
       State state_;
 
+      HttpStatus status_;
+      bool hasContentLength_;
+      uint64_t contentLength_;
+      uint64_t contentPosition_;
+      std::list<std::string> headers_;
+
     public:
-      StateMachine(IHttpOutputStream& stream) : 
-        stream_(stream),
-        state_(State_WaitingHttpStatus)
-      {
-      }
+      StateMachine(IHttpOutputStream& stream);
+
+      ~StateMachine();
+
+      void SetHttpStatus(HttpStatus status);
 
-      void SendHttpStatus(HttpStatus status);
+      void SetContentLength(uint64_t length);
 
-      void SendHeaderData(const void* buffer, size_t length);
+      void SetContentType(const char* contentType);
+
+      void SetContentFilename(const char* filename);
 
-      void SendHeaderString(const std::string& str);
+      void SetCookie(const std::string& cookie,
+                     const std::string& value);
 
-      void SendBodyData(const void* buffer, size_t length);
+      void AddHeader(const std::string& header,
+                     const std::string& value);
 
-      void SendBodyString(const std::string& str);
+      void ClearHeaders();
+
+      void SendBody(const void* buffer, size_t length);
     };
 
-    void PrepareOkHeader(Header& header,
-                         const char* contentType,
-                         bool hasContentLength,
-                         uint64_t contentLength,
-                         const char* contentFilename);
-
-    void SendOkHeader(const Header& header);
-
     StateMachine stateMachine_;
-    HttpHandler::Arguments cookies_;
 
   public:
     HttpOutput(IHttpOutputStream& stream) : stateMachine_(stream)
     {
     }
 
-    void SendOkHeader(const char* contentType,
-                      bool hasContentLength,
-                      uint64_t contentLength,
-                      const char* contentFilename);
+    void SendStatus(HttpStatus status);
 
-    void SendBodyData(const void* buffer, size_t length)
+    void SetContentType(const char* contentType)
     {
-      stateMachine_.SendBodyData(buffer, length);
+      stateMachine_.SetContentType(contentType);
+    }
+
+    void SetContentFilename(const char* filename)
+    {
+      stateMachine_.SetContentFilename(filename);
     }
 
-    void SendBodyString(const std::string& str)
+    void SetContentLength(uint64_t length)
     {
-      stateMachine_.SendBodyString(str);
+      stateMachine_.SetContentLength(length);
     }
 
-    void SendMethodNotAllowed(const std::string& allowed);
+    void SetCookie(const std::string& cookie,
+                   const std::string& value)
+    {
+      stateMachine_.SetCookie(cookie, value);
+    }
 
-    void SendHeader(HttpStatus status);
+    void SendBody(const void* buffer, size_t length);
+
+    void SendBody(const std::string& str);
+
+    void SendBody();
+
+    void SendMethodNotAllowed(const std::string& allowed);
 
     void Redirect(const std::string& path);
 
     void SendUnauthorized(const std::string& realm);
-
-    void SetCookie(const std::string& cookie,
-                   const std::string& value)
-    {
-      cookies_[cookie] = value;
-    }
-
-    // Higher-level constructs to send entire buffers ----------------------------
-
-    void AnswerBufferWithContentType(const std::string& buffer,
-                                     const std::string& contentType);
-
-    void AnswerBufferWithContentType(const void* buffer,
-                                     size_t size,
-                                     const std::string& contentType);
   };
 }