Mercurial > hg > orthanc
annotate Core/HttpServer/HttpOutput.cpp @ 827:3d6f9b7d0add
precompiled headers in unit tests
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 02 Jun 2014 11:49:09 +0200 |
parents | a811bdf8b8eb |
children | e078ea944089 |
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 { |
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 |
656 | 93 it = header.begin(); it != header.end(); ++it) |
324 | 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 = | |
477 | 107 "HTTP/1.1 405 " + std::string(EnumerationToString(HttpStatus_405_MethodNotAllowed)) + |
0 | 108 "\r\nAllow: " + allowed + |
109 "\r\n\r\n"; | |
110 Send(&s[0], s.size()); | |
111 } | |
112 | |
113 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
114 void HttpOutput::SendHeader(HttpStatus status) |
0 | 115 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
116 if (status == HttpStatus_200_Ok || |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
117 status == 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 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
126 void HttpOutput::SendHeaderInternal(HttpStatus status) |
0 | 127 { |
128 std::string s = "HTTP/1.1 " + | |
129 boost::lexical_cast<std::string>(status) + | |
477 | 130 " " + std::string(EnumerationToString(status)) + |
0 | 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(); | |
656 | 148 it != cookies.end(); ++it) |
339 | 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 = | |
477 | 193 "HTTP/1.1 301 " + std::string(EnumerationToString(HttpStatus_301_MovedPermanently)) + |
0 | 194 "\r\nLocation: " + path + |
195 "\r\n\r\n"; | |
196 Send(&s[0], s.size()); | |
197 } | |
198 } |