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