Mercurial > hg > orthanc
annotate Core/HttpServer/HttpOutput.cpp @ 910:28a52982196e plugins
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 20 Jun 2014 13:38:19 +0200 |
parents | e078ea944089 |
children | 306afd58a0b3 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
0 | 34 #include "HttpOutput.h" |
35 | |
324 | 36 #include <iostream> |
0 | 37 #include <vector> |
38 #include <stdio.h> | |
39 #include <boost/lexical_cast.hpp> | |
59 | 40 #include "../OrthancException.h" |
0 | 41 #include "../Toolbox.h" |
42 | |
59 | 43 namespace Orthanc |
0 | 44 { |
910 | 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) | |
78 { | |
79 if (state_ != State_WaitingHttpStatus) | |
80 { | |
81 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
82 } | |
83 | |
84 OnHttpStatusReceived(status); | |
85 state_ = State_WritingHeader; | |
86 | |
87 std::string s = "HTTP/1.1 " + | |
88 boost::lexical_cast<std::string>(status) + | |
89 " " + std::string(EnumerationToString(status)) + | |
90 "\r\n"; | |
91 | |
92 Send(true, &s[0], s.size()); | |
93 } | |
94 | |
95 void HttpOutput::StateMachine::SendHeaderData(const void* buffer, size_t length) | |
96 { | |
97 if (state_ != State_WritingHeader) | |
98 { | |
99 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
100 } | |
101 | |
102 Send(true, buffer, length); | |
103 } | |
104 | |
105 void HttpOutput::StateMachine::SendHeaderString(const std::string& str) | |
106 { | |
107 if (str.size() > 0) | |
108 { | |
109 SendHeaderData(&str[0], str.size()); | |
110 } | |
111 } | |
112 | |
113 void HttpOutput::StateMachine::SendBodyData(const void* buffer, size_t length) | |
114 { | |
115 if (state_ == State_WaitingHttpStatus) | |
116 { | |
117 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
118 } | |
119 | |
120 if (state_ == State_WritingHeader) | |
121 { | |
122 // Close the HTTP header before writing the body | |
123 Send(true, "\r\n", 2); | |
124 state_ = State_WritingBody; | |
125 } | |
126 | |
127 if (length > 0) | |
128 { | |
129 Send(false, buffer, length); | |
130 } | |
131 } | |
132 | |
133 void HttpOutput::StateMachine::SendBodyString(const std::string& str) | |
134 { | |
135 if (str.size() > 0) | |
136 { | |
137 SendBodyData(&str[0], str.size()); | |
138 } | |
139 } | |
140 | |
141 | |
330 | 142 void HttpOutput::PrepareOkHeader(Header& header, |
143 const char* contentType, | |
144 bool hasContentLength, | |
145 uint64_t contentLength, | |
146 const char* contentFilename) | |
147 { | |
148 header.clear(); | |
149 | |
150 if (contentType && contentType[0] != '\0') | |
151 { | |
152 header.push_back(std::make_pair("Content-Type", std::string(contentType))); | |
153 } | |
154 | |
155 if (hasContentLength) | |
156 { | |
157 header.push_back(std::make_pair("Content-Length", boost::lexical_cast<std::string>(contentLength))); | |
158 } | |
159 | |
160 if (contentFilename && contentFilename[0] != '\0') | |
161 { | |
162 std::string attachment = "attachment; filename=\"" + std::string(contentFilename) + "\""; | |
163 header.push_back(std::make_pair("Content-Disposition", attachment)); | |
164 } | |
165 } | |
166 | |
0 | 167 void HttpOutput::SendOkHeader(const char* contentType, |
168 bool hasContentLength, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
169 uint64_t contentLength, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
170 const char* contentFilename) |
0 | 171 { |
330 | 172 Header header; |
173 PrepareOkHeader(header, contentType, hasContentLength, contentLength, contentFilename); | |
174 SendOkHeader(header); | |
0 | 175 } |
176 | |
330 | 177 void HttpOutput::SendOkHeader(const Header& header) |
324 | 178 { |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
179 stream_.SendHttpStatus(HttpStatus_200_Ok); |
0 | 180 |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
181 std::string s; |
330 | 182 for (Header::const_iterator |
656 | 183 it = header.begin(); it != header.end(); ++it) |
324 | 184 { |
185 s += it->first + ": " + it->second + "\r\n"; | |
186 } | |
187 | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
188 stream_.SendHeaderString(s); |
207 | 189 } |
190 | |
191 | |
0 | 192 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) |
193 { | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
194 stream_.SendHttpStatus(HttpStatus_405_MethodNotAllowed); |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
195 stream_.SendHeaderString("Allow: " + allowed + "\r\n"); |
0 | 196 } |
197 | |
198 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
199 void HttpOutput::SendHeader(HttpStatus status) |
0 | 200 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
201 if (status == HttpStatus_200_Ok || |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
202 status == HttpStatus_301_MovedPermanently || |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
203 status == HttpStatus_401_Unauthorized || |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
204 status == HttpStatus_405_MethodNotAllowed) |
0 | 205 { |
59 | 206 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); |
0 | 207 } |
208 | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
209 stream_.SendHttpStatus(status); |
0 | 210 } |
211 | |
212 | |
213 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, | |
214 const std::string& contentType) | |
215 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
216 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
217 SendBodyString(buffer); |
0 | 218 } |
219 | |
220 | |
339 | 221 void HttpOutput::PrepareCookies(Header& header, |
222 const HttpHandler::Arguments& cookies) | |
223 { | |
224 for (HttpHandler::Arguments::const_iterator it = cookies.begin(); | |
656 | 225 it != cookies.end(); ++it) |
339 | 226 { |
227 header.push_back(std::make_pair("Set-Cookie", it->first + "=" + it->second)); | |
228 } | |
229 } | |
230 | |
231 | |
330 | 232 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, |
233 const std::string& contentType, | |
234 const HttpHandler::Arguments& cookies) | |
235 { | |
236 Header header; | |
237 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL); | |
339 | 238 PrepareCookies(header, cookies); |
330 | 239 SendOkHeader(header); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
240 SendBodyString(buffer); |
330 | 241 } |
242 | |
243 | |
0 | 244 void HttpOutput::AnswerBufferWithContentType(const void* buffer, |
245 size_t size, | |
246 const std::string& contentType) | |
247 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
248 SendOkHeader(contentType.c_str(), true, size, NULL); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
249 SendBodyData(buffer, size); |
0 | 250 } |
251 | |
339 | 252 |
253 void HttpOutput::AnswerBufferWithContentType(const void* buffer, | |
254 size_t size, | |
255 const std::string& contentType, | |
256 const HttpHandler::Arguments& cookies) | |
257 { | |
258 Header header; | |
259 PrepareOkHeader(header, contentType.c_str(), true, size, NULL); | |
260 PrepareCookies(header, cookies); | |
261 SendOkHeader(header); | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
262 SendBodyData(buffer, size); |
339 | 263 } |
264 | |
265 | |
0 | 266 void HttpOutput::Redirect(const std::string& path) |
267 { | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
268 stream_.SendHttpStatus(HttpStatus_301_MovedPermanently); |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
269 stream_.SendHeaderString("Location: " + path + "\r\n"); |
0 | 270 } |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
271 |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
272 |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
273 void HttpOutput::SendUnauthorized(const std::string& realm) |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
274 { |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
275 stream_.SendHttpStatus(HttpStatus_401_Unauthorized); |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
276 stream_.SendHeaderString("WWW-Authenticate: Basic realm=\"" + realm + "\"\r\n"); |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
277 } |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
278 |
0 | 279 } |