Mercurial > hg > orthanc
annotate Core/HttpServer/HttpOutput.cpp @ 1021:9e8ae1466c4b lua-scripting
close branch lua-scripting
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 10 Jul 2014 11:39:26 +0200 |
parents | 306afd58a0b3 |
children | 8d1845feb277 |
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 void HttpOutput::StateMachine::SendHttpStatus(HttpStatus status) |
46 { | |
47 if (state_ != State_WaitingHttpStatus) | |
48 { | |
49 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
50 } | |
51 | |
911 | 52 stream_.OnHttpStatusReceived(status); |
910 | 53 state_ = State_WritingHeader; |
54 | |
55 std::string s = "HTTP/1.1 " + | |
56 boost::lexical_cast<std::string>(status) + | |
57 " " + std::string(EnumerationToString(status)) + | |
58 "\r\n"; | |
59 | |
911 | 60 stream_.Send(true, &s[0], s.size()); |
910 | 61 } |
62 | |
63 void HttpOutput::StateMachine::SendHeaderData(const void* buffer, size_t length) | |
64 { | |
65 if (state_ != State_WritingHeader) | |
66 { | |
67 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
68 } | |
69 | |
911 | 70 stream_.Send(true, buffer, length); |
910 | 71 } |
72 | |
73 void HttpOutput::StateMachine::SendHeaderString(const std::string& str) | |
74 { | |
75 if (str.size() > 0) | |
76 { | |
77 SendHeaderData(&str[0], str.size()); | |
78 } | |
79 } | |
80 | |
81 void HttpOutput::StateMachine::SendBodyData(const void* buffer, size_t length) | |
82 { | |
83 if (state_ == State_WaitingHttpStatus) | |
84 { | |
85 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
86 } | |
87 | |
88 if (state_ == State_WritingHeader) | |
89 { | |
90 // Close the HTTP header before writing the body | |
911 | 91 stream_.Send(true, "\r\n", 2); |
910 | 92 state_ = State_WritingBody; |
93 } | |
94 | |
95 if (length > 0) | |
96 { | |
911 | 97 stream_.Send(false, buffer, length); |
910 | 98 } |
99 } | |
100 | |
101 void HttpOutput::StateMachine::SendBodyString(const std::string& str) | |
102 { | |
103 if (str.size() > 0) | |
104 { | |
105 SendBodyData(&str[0], str.size()); | |
106 } | |
107 } | |
108 | |
109 | |
330 | 110 void HttpOutput::PrepareOkHeader(Header& header, |
111 const char* contentType, | |
112 bool hasContentLength, | |
113 uint64_t contentLength, | |
114 const char* contentFilename) | |
115 { | |
116 header.clear(); | |
117 | |
118 if (contentType && contentType[0] != '\0') | |
119 { | |
120 header.push_back(std::make_pair("Content-Type", std::string(contentType))); | |
121 } | |
122 | |
123 if (hasContentLength) | |
124 { | |
125 header.push_back(std::make_pair("Content-Length", boost::lexical_cast<std::string>(contentLength))); | |
126 } | |
127 | |
128 if (contentFilename && contentFilename[0] != '\0') | |
129 { | |
130 std::string attachment = "attachment; filename=\"" + std::string(contentFilename) + "\""; | |
131 header.push_back(std::make_pair("Content-Disposition", attachment)); | |
132 } | |
133 } | |
134 | |
0 | 135 void HttpOutput::SendOkHeader(const char* contentType, |
136 bool hasContentLength, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
137 uint64_t contentLength, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
138 const char* contentFilename) |
0 | 139 { |
330 | 140 Header header; |
141 PrepareOkHeader(header, contentType, hasContentLength, contentLength, contentFilename); | |
142 SendOkHeader(header); | |
0 | 143 } |
144 | |
330 | 145 void HttpOutput::SendOkHeader(const Header& header) |
324 | 146 { |
911 | 147 stateMachine_.SendHttpStatus(HttpStatus_200_Ok); |
0 | 148 |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
149 std::string s; |
330 | 150 for (Header::const_iterator |
656 | 151 it = header.begin(); it != header.end(); ++it) |
324 | 152 { |
153 s += it->first + ": " + it->second + "\r\n"; | |
154 } | |
155 | |
911 | 156 stateMachine_.SendHeaderString(s); |
207 | 157 } |
158 | |
159 | |
0 | 160 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) |
161 { | |
911 | 162 stateMachine_.SendHttpStatus(HttpStatus_405_MethodNotAllowed); |
163 stateMachine_.SendHeaderString("Allow: " + allowed + "\r\n"); | |
0 | 164 } |
165 | |
166 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
167 void HttpOutput::SendHeader(HttpStatus status) |
0 | 168 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
169 if (status == HttpStatus_200_Ok || |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
170 status == HttpStatus_301_MovedPermanently || |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
171 status == HttpStatus_401_Unauthorized || |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
172 status == HttpStatus_405_MethodNotAllowed) |
0 | 173 { |
59 | 174 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); |
0 | 175 } |
176 | |
911 | 177 stateMachine_.SendHttpStatus(status); |
0 | 178 } |
179 | |
180 | |
181 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, | |
182 const std::string& contentType) | |
183 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
184 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
185 SendBodyString(buffer); |
0 | 186 } |
187 | |
188 | |
339 | 189 void HttpOutput::PrepareCookies(Header& header, |
190 const HttpHandler::Arguments& cookies) | |
191 { | |
192 for (HttpHandler::Arguments::const_iterator it = cookies.begin(); | |
656 | 193 it != cookies.end(); ++it) |
339 | 194 { |
195 header.push_back(std::make_pair("Set-Cookie", it->first + "=" + it->second)); | |
196 } | |
197 } | |
198 | |
199 | |
330 | 200 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, |
201 const std::string& contentType, | |
202 const HttpHandler::Arguments& cookies) | |
203 { | |
204 Header header; | |
205 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL); | |
339 | 206 PrepareCookies(header, cookies); |
330 | 207 SendOkHeader(header); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
208 SendBodyString(buffer); |
330 | 209 } |
210 | |
211 | |
0 | 212 void HttpOutput::AnswerBufferWithContentType(const void* buffer, |
213 size_t size, | |
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, size, NULL); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
217 SendBodyData(buffer, size); |
0 | 218 } |
219 | |
339 | 220 |
221 void HttpOutput::AnswerBufferWithContentType(const void* buffer, | |
222 size_t size, | |
223 const std::string& contentType, | |
224 const HttpHandler::Arguments& cookies) | |
225 { | |
226 Header header; | |
227 PrepareOkHeader(header, contentType.c_str(), true, size, NULL); | |
228 PrepareCookies(header, cookies); | |
229 SendOkHeader(header); | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
230 SendBodyData(buffer, size); |
339 | 231 } |
232 | |
233 | |
0 | 234 void HttpOutput::Redirect(const std::string& path) |
235 { | |
911 | 236 stateMachine_.SendHttpStatus(HttpStatus_301_MovedPermanently); |
237 stateMachine_.SendHeaderString("Location: " + path + "\r\n"); | |
0 | 238 } |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
239 |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
240 |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
241 void HttpOutput::SendUnauthorized(const std::string& realm) |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
242 { |
911 | 243 stateMachine_.SendHttpStatus(HttpStatus_401_Unauthorized); |
244 stateMachine_.SendHeaderString("WWW-Authenticate: Basic realm=\"" + realm + "\"\r\n"); | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
245 } |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
246 |
0 | 247 } |