Mercurial > hg > orthanc
annotate Core/HttpServer/HttpOutput.cpp @ 599:e51543cdbffd
fix mainline version
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 04 Oct 2013 16:55:53 +0200 |
parents | 6f8ae46ed90e |
children | 08eca5d86aad |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
398 | 3 * Copyright (C) 2012-2013 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 | |
33 #include "HttpOutput.h" | |
34 | |
324 | 35 #include <iostream> |
0 | 36 #include <vector> |
37 #include <stdio.h> | |
38 #include <boost/lexical_cast.hpp> | |
59 | 39 #include "../OrthancException.h" |
0 | 40 #include "../Toolbox.h" |
41 | |
59 | 42 namespace Orthanc |
0 | 43 { |
44 void HttpOutput::SendString(const std::string& s) | |
45 { | |
46 if (s.size() > 0) | |
217 | 47 { |
0 | 48 Send(&s[0], s.size()); |
217 | 49 } |
0 | 50 } |
51 | |
330 | 52 void HttpOutput::PrepareOkHeader(Header& header, |
53 const char* contentType, | |
54 bool hasContentLength, | |
55 uint64_t contentLength, | |
56 const char* contentFilename) | |
57 { | |
58 header.clear(); | |
59 | |
60 if (contentType && contentType[0] != '\0') | |
61 { | |
62 header.push_back(std::make_pair("Content-Type", std::string(contentType))); | |
63 } | |
64 | |
65 if (hasContentLength) | |
66 { | |
67 header.push_back(std::make_pair("Content-Length", boost::lexical_cast<std::string>(contentLength))); | |
68 } | |
69 | |
70 if (contentFilename && contentFilename[0] != '\0') | |
71 { | |
72 std::string attachment = "attachment; filename=\"" + std::string(contentFilename) + "\""; | |
73 header.push_back(std::make_pair("Content-Disposition", attachment)); | |
74 } | |
75 } | |
76 | |
0 | 77 void HttpOutput::SendOkHeader(const char* contentType, |
78 bool hasContentLength, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
79 uint64_t contentLength, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
80 const char* contentFilename) |
0 | 81 { |
330 | 82 Header header; |
83 PrepareOkHeader(header, contentType, hasContentLength, contentLength, contentFilename); | |
84 SendOkHeader(header); | |
0 | 85 } |
86 | |
330 | 87 void HttpOutput::SendOkHeader(const Header& header) |
324 | 88 { |
89 std::string s = "HTTP/1.1 200 OK\r\n"; | |
0 | 90 |
330 | 91 for (Header::const_iterator |
324 | 92 it = header.begin(); it != header.end(); it++) |
93 { | |
94 s += it->first + ": " + it->second + "\r\n"; | |
95 } | |
96 | |
97 s += "\r\n"; | |
98 | |
207 | 99 Send(&s[0], s.size()); |
100 } | |
101 | |
102 | |
0 | 103 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) |
104 { | |
105 std::string s = | |
477 | 106 "HTTP/1.1 405 " + std::string(EnumerationToString(HttpStatus_405_MethodNotAllowed)) + |
0 | 107 "\r\nAllow: " + allowed + |
108 "\r\n\r\n"; | |
109 Send(&s[0], s.size()); | |
110 } | |
111 | |
112 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
113 void HttpOutput::SendHeader(HttpStatus status) |
0 | 114 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
115 if (status == HttpStatus_200_Ok || |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
116 status == HttpStatus_405_MethodNotAllowed) |
0 | 117 { |
59 | 118 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); |
0 | 119 } |
120 | |
121 SendHeaderInternal(status); | |
122 } | |
123 | |
124 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
125 void HttpOutput::SendHeaderInternal(HttpStatus status) |
0 | 126 { |
127 std::string s = "HTTP/1.1 " + | |
128 boost::lexical_cast<std::string>(status) + | |
477 | 129 " " + std::string(EnumerationToString(status)) + |
0 | 130 "\r\n\r\n"; |
131 Send(&s[0], s.size()); | |
132 } | |
133 | |
134 | |
135 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, | |
136 const std::string& contentType) | |
137 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
138 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); |
0 | 139 SendString(buffer); |
140 } | |
141 | |
142 | |
339 | 143 void HttpOutput::PrepareCookies(Header& header, |
144 const HttpHandler::Arguments& cookies) | |
145 { | |
146 for (HttpHandler::Arguments::const_iterator it = cookies.begin(); | |
147 it != cookies.end(); it++) | |
148 { | |
149 header.push_back(std::make_pair("Set-Cookie", it->first + "=" + it->second)); | |
150 } | |
151 } | |
152 | |
153 | |
330 | 154 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, |
155 const std::string& contentType, | |
156 const HttpHandler::Arguments& cookies) | |
157 { | |
158 Header header; | |
159 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL); | |
339 | 160 PrepareCookies(header, cookies); |
330 | 161 SendOkHeader(header); |
162 SendString(buffer); | |
163 } | |
164 | |
165 | |
0 | 166 void HttpOutput::AnswerBufferWithContentType(const void* buffer, |
167 size_t size, | |
168 const std::string& contentType) | |
169 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
170 SendOkHeader(contentType.c_str(), true, size, NULL); |
0 | 171 Send(buffer, size); |
172 } | |
173 | |
339 | 174 |
175 void HttpOutput::AnswerBufferWithContentType(const void* buffer, | |
176 size_t size, | |
177 const std::string& contentType, | |
178 const HttpHandler::Arguments& cookies) | |
179 { | |
180 Header header; | |
181 PrepareOkHeader(header, contentType.c_str(), true, size, NULL); | |
182 PrepareCookies(header, cookies); | |
183 SendOkHeader(header); | |
184 Send(buffer, size); | |
185 } | |
186 | |
187 | |
188 | |
0 | 189 void HttpOutput::Redirect(const std::string& path) |
190 { | |
191 std::string s = | |
477 | 192 "HTTP/1.1 301 " + std::string(EnumerationToString(HttpStatus_301_MovedPermanently)) + |
0 | 193 "\r\nLocation: " + path + |
194 "\r\n\r\n"; | |
195 Send(&s[0], s.size()); | |
196 } | |
197 } |