comparison Core/HttpServer/HttpOutput.cpp @ 911:306afd58a0b3 plugins

fix build
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 20 Jun 2014 13:45:22 +0200
parents 28a52982196e
children 8d1845feb277
comparison
equal deleted inserted replaced
910:28a52982196e 911:306afd58a0b3
40 #include "../OrthancException.h" 40 #include "../OrthancException.h"
41 #include "../Toolbox.h" 41 #include "../Toolbox.h"
42 42
43 namespace Orthanc 43 namespace Orthanc
44 { 44 {
45 class HttpOutput::StateMachine : public boost::noncopyable
46 {
47 protected:
48 enum State
49 {
50 State_WaitingHttpStatus,
51 State_WritingHeader,
52 State_WritingBody
53 };
54
55 private:
56 IHttpOutputStream& stream_;
57 State state_;
58
59 public:
60 HttpStateMachine() :
61 state_(State_WaitingHttpStatus)
62 {
63 }
64
65 void SendHttpStatus(HttpStatus status);
66
67 void SendHeaderData(const void* buffer, size_t length);
68
69 void SendHeaderString(const std::string& str);
70
71 void SendBodyData(const void* buffer, size_t length);
72
73 void SendBodyString(const std::string& str);
74 };
75
76
77 void HttpOutput::StateMachine::SendHttpStatus(HttpStatus status) 45 void HttpOutput::StateMachine::SendHttpStatus(HttpStatus status)
78 { 46 {
79 if (state_ != State_WaitingHttpStatus) 47 if (state_ != State_WaitingHttpStatus)
80 { 48 {
81 throw OrthancException(ErrorCode_BadSequenceOfCalls); 49 throw OrthancException(ErrorCode_BadSequenceOfCalls);
82 } 50 }
83 51
84 OnHttpStatusReceived(status); 52 stream_.OnHttpStatusReceived(status);
85 state_ = State_WritingHeader; 53 state_ = State_WritingHeader;
86 54
87 std::string s = "HTTP/1.1 " + 55 std::string s = "HTTP/1.1 " +
88 boost::lexical_cast<std::string>(status) + 56 boost::lexical_cast<std::string>(status) +
89 " " + std::string(EnumerationToString(status)) + 57 " " + std::string(EnumerationToString(status)) +
90 "\r\n"; 58 "\r\n";
91 59
92 Send(true, &s[0], s.size()); 60 stream_.Send(true, &s[0], s.size());
93 } 61 }
94 62
95 void HttpOutput::StateMachine::SendHeaderData(const void* buffer, size_t length) 63 void HttpOutput::StateMachine::SendHeaderData(const void* buffer, size_t length)
96 { 64 {
97 if (state_ != State_WritingHeader) 65 if (state_ != State_WritingHeader)
98 { 66 {
99 throw OrthancException(ErrorCode_BadSequenceOfCalls); 67 throw OrthancException(ErrorCode_BadSequenceOfCalls);
100 } 68 }
101 69
102 Send(true, buffer, length); 70 stream_.Send(true, buffer, length);
103 } 71 }
104 72
105 void HttpOutput::StateMachine::SendHeaderString(const std::string& str) 73 void HttpOutput::StateMachine::SendHeaderString(const std::string& str)
106 { 74 {
107 if (str.size() > 0) 75 if (str.size() > 0)
118 } 86 }
119 87
120 if (state_ == State_WritingHeader) 88 if (state_ == State_WritingHeader)
121 { 89 {
122 // Close the HTTP header before writing the body 90 // Close the HTTP header before writing the body
123 Send(true, "\r\n", 2); 91 stream_.Send(true, "\r\n", 2);
124 state_ = State_WritingBody; 92 state_ = State_WritingBody;
125 } 93 }
126 94
127 if (length > 0) 95 if (length > 0)
128 { 96 {
129 Send(false, buffer, length); 97 stream_.Send(false, buffer, length);
130 } 98 }
131 } 99 }
132 100
133 void HttpOutput::StateMachine::SendBodyString(const std::string& str) 101 void HttpOutput::StateMachine::SendBodyString(const std::string& str)
134 { 102 {
174 SendOkHeader(header); 142 SendOkHeader(header);
175 } 143 }
176 144
177 void HttpOutput::SendOkHeader(const Header& header) 145 void HttpOutput::SendOkHeader(const Header& header)
178 { 146 {
179 stream_.SendHttpStatus(HttpStatus_200_Ok); 147 stateMachine_.SendHttpStatus(HttpStatus_200_Ok);
180 148
181 std::string s; 149 std::string s;
182 for (Header::const_iterator 150 for (Header::const_iterator
183 it = header.begin(); it != header.end(); ++it) 151 it = header.begin(); it != header.end(); ++it)
184 { 152 {
185 s += it->first + ": " + it->second + "\r\n"; 153 s += it->first + ": " + it->second + "\r\n";
186 } 154 }
187 155
188 stream_.SendHeaderString(s); 156 stateMachine_.SendHeaderString(s);
189 } 157 }
190 158
191 159
192 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) 160 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed)
193 { 161 {
194 stream_.SendHttpStatus(HttpStatus_405_MethodNotAllowed); 162 stateMachine_.SendHttpStatus(HttpStatus_405_MethodNotAllowed);
195 stream_.SendHeaderString("Allow: " + allowed + "\r\n"); 163 stateMachine_.SendHeaderString("Allow: " + allowed + "\r\n");
196 } 164 }
197 165
198 166
199 void HttpOutput::SendHeader(HttpStatus status) 167 void HttpOutput::SendHeader(HttpStatus status)
200 { 168 {
204 status == HttpStatus_405_MethodNotAllowed) 172 status == HttpStatus_405_MethodNotAllowed)
205 { 173 {
206 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); 174 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput");
207 } 175 }
208 176
209 stream_.SendHttpStatus(status); 177 stateMachine_.SendHttpStatus(status);
210 } 178 }
211 179
212 180
213 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, 181 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer,
214 const std::string& contentType) 182 const std::string& contentType)
263 } 231 }
264 232
265 233
266 void HttpOutput::Redirect(const std::string& path) 234 void HttpOutput::Redirect(const std::string& path)
267 { 235 {
268 stream_.SendHttpStatus(HttpStatus_301_MovedPermanently); 236 stateMachine_.SendHttpStatus(HttpStatus_301_MovedPermanently);
269 stream_.SendHeaderString("Location: " + path + "\r\n"); 237 stateMachine_.SendHeaderString("Location: " + path + "\r\n");
270 } 238 }
271 239
272 240
273 void HttpOutput::SendUnauthorized(const std::string& realm) 241 void HttpOutput::SendUnauthorized(const std::string& realm)
274 { 242 {
275 stream_.SendHttpStatus(HttpStatus_401_Unauthorized); 243 stateMachine_.SendHttpStatus(HttpStatus_401_Unauthorized);
276 stream_.SendHeaderString("WWW-Authenticate: Basic realm=\"" + realm + "\"\r\n"); 244 stateMachine_.SendHeaderString("WWW-Authenticate: Basic realm=\"" + realm + "\"\r\n");
277 } 245 }
278 246
279 } 247 }