comparison 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
comparison
equal deleted inserted replaced
1112:a119f9ae3640 1113:ba5c0908600c
49 class StateMachine : public boost::noncopyable 49 class StateMachine : public boost::noncopyable
50 { 50 {
51 private: 51 private:
52 enum State 52 enum State
53 { 53 {
54 State_WaitingHttpStatus,
55 State_WritingHeader, 54 State_WritingHeader,
56 State_WritingBody 55 State_WritingBody,
56 State_Done
57 }; 57 };
58 58
59 IHttpOutputStream& stream_; 59 IHttpOutputStream& stream_;
60 State state_; 60 State state_;
61 61
62 HttpStatus status_;
63 bool hasContentLength_;
64 uint64_t contentLength_;
65 uint64_t contentPosition_;
66 std::list<std::string> headers_;
67
62 public: 68 public:
63 StateMachine(IHttpOutputStream& stream) : 69 StateMachine(IHttpOutputStream& stream);
64 stream_(stream),
65 state_(State_WaitingHttpStatus)
66 {
67 }
68 70
69 void SendHttpStatus(HttpStatus status); 71 ~StateMachine();
70 72
71 void SendHeaderData(const void* buffer, size_t length); 73 void SetHttpStatus(HttpStatus status);
72 74
73 void SendHeaderString(const std::string& str); 75 void SetContentLength(uint64_t length);
74 76
75 void SendBodyData(const void* buffer, size_t length); 77 void SetContentType(const char* contentType);
76 78
77 void SendBodyString(const std::string& str); 79 void SetContentFilename(const char* filename);
80
81 void SetCookie(const std::string& cookie,
82 const std::string& value);
83
84 void AddHeader(const std::string& header,
85 const std::string& value);
86
87 void ClearHeaders();
88
89 void SendBody(const void* buffer, size_t length);
78 }; 90 };
79 91
80 void PrepareOkHeader(Header& header,
81 const char* contentType,
82 bool hasContentLength,
83 uint64_t contentLength,
84 const char* contentFilename);
85
86 void SendOkHeader(const Header& header);
87
88 StateMachine stateMachine_; 92 StateMachine stateMachine_;
89 HttpHandler::Arguments cookies_;
90 93
91 public: 94 public:
92 HttpOutput(IHttpOutputStream& stream) : stateMachine_(stream) 95 HttpOutput(IHttpOutputStream& stream) : stateMachine_(stream)
93 { 96 {
94 } 97 }
95 98
96 void SendOkHeader(const char* contentType, 99 void SendStatus(HttpStatus status);
97 bool hasContentLength,
98 uint64_t contentLength,
99 const char* contentFilename);
100 100
101 void SendBodyData(const void* buffer, size_t length) 101 void SetContentType(const char* contentType)
102 { 102 {
103 stateMachine_.SendBodyData(buffer, length); 103 stateMachine_.SetContentType(contentType);
104 } 104 }
105 105
106 void SendBodyString(const std::string& str) 106 void SetContentFilename(const char* filename)
107 { 107 {
108 stateMachine_.SendBodyString(str); 108 stateMachine_.SetContentFilename(filename);
109 } 109 }
110 110
111 void SetContentLength(uint64_t length)
112 {
113 stateMachine_.SetContentLength(length);
114 }
115
116 void SetCookie(const std::string& cookie,
117 const std::string& value)
118 {
119 stateMachine_.SetCookie(cookie, value);
120 }
121
122 void SendBody(const void* buffer, size_t length);
123
124 void SendBody(const std::string& str);
125
126 void SendBody();
127
111 void SendMethodNotAllowed(const std::string& allowed); 128 void SendMethodNotAllowed(const std::string& allowed);
112
113 void SendHeader(HttpStatus status);
114 129
115 void Redirect(const std::string& path); 130 void Redirect(const std::string& path);
116 131
117 void SendUnauthorized(const std::string& realm); 132 void SendUnauthorized(const std::string& realm);
118
119 void SetCookie(const std::string& cookie,
120 const std::string& value)
121 {
122 cookies_[cookie] = value;
123 }
124
125 // Higher-level constructs to send entire buffers ----------------------------
126
127 void AnswerBufferWithContentType(const std::string& buffer,
128 const std::string& contentType);
129
130 void AnswerBufferWithContentType(const void* buffer,
131 size_t size,
132 const std::string& contentType);
133 }; 133 };
134 } 134 }