0
|
1 /**
|
59
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
0
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
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
|
|
35 #include <vector>
|
|
36 #include <stdio.h>
|
|
37 #include <boost/lexical_cast.hpp>
|
59
|
38 #include "../OrthancException.h"
|
0
|
39 #include "../Toolbox.h"
|
59
|
40 #include "../../OrthancCppClient/HttpException.h"
|
0
|
41
|
59
|
42 namespace Orthanc
|
0
|
43 {
|
|
44 void HttpOutput::SendString(const std::string& s)
|
|
45 {
|
|
46 if (s.size() > 0)
|
|
47 Send(&s[0], s.size());
|
|
48 }
|
|
49
|
|
50 void HttpOutput::SendOkHeader(const std::string& contentType)
|
|
51 {
|
|
52 SendOkHeader(contentType.c_str(), false, 0);
|
|
53 }
|
|
54
|
|
55 void HttpOutput::SendOkHeader()
|
|
56 {
|
|
57 SendOkHeader(NULL, false, 0);
|
|
58 }
|
|
59
|
|
60 void HttpOutput::SendOkHeader(uint64_t contentLength)
|
|
61 {
|
|
62 SendOkHeader(NULL, true, contentLength);
|
|
63 }
|
|
64
|
|
65 void HttpOutput::SendOkHeader(const std::string& contentType,
|
|
66 uint64_t contentLength)
|
|
67 {
|
|
68 SendOkHeader(contentType.c_str(), true, contentLength);
|
|
69 }
|
|
70
|
|
71
|
|
72 void HttpOutput::SendOkHeader(const char* contentType,
|
|
73 bool hasContentLength,
|
8
|
74 uint64_t contentLength)
|
0
|
75 {
|
|
76 std::string s = "HTTP/1.1 200 OK\r\n";
|
|
77
|
|
78 if (contentType)
|
|
79 {
|
|
80 s += "Content-Type: " + std::string(contentType) + "\r\n";
|
|
81 }
|
|
82
|
|
83 if (hasContentLength)
|
|
84 {
|
|
85 s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n";
|
|
86 }
|
|
87
|
|
88 s += "\r\n";
|
|
89
|
|
90 Send(&s[0], s.size());
|
|
91 }
|
|
92
|
|
93
|
|
94 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed)
|
|
95 {
|
|
96 std::string s =
|
59
|
97 "HTTP/1.1 405 " + std::string(HttpException::GetDescription(Orthanc_HttpStatus_405_MethodNotAllowed)) +
|
0
|
98 "\r\nAllow: " + allowed +
|
|
99 "\r\n\r\n";
|
|
100 Send(&s[0], s.size());
|
|
101 }
|
|
102
|
|
103
|
59
|
104 void HttpOutput::SendHeader(Orthanc_HttpStatus status)
|
0
|
105 {
|
59
|
106 if (status == Orthanc_HttpStatus_200_Ok ||
|
|
107 status == Orthanc_HttpStatus_405_MethodNotAllowed)
|
0
|
108 {
|
59
|
109 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput");
|
0
|
110 }
|
|
111
|
|
112 SendHeaderInternal(status);
|
|
113 }
|
|
114
|
|
115
|
59
|
116 void HttpOutput::SendHeaderInternal(Orthanc_HttpStatus status)
|
0
|
117 {
|
|
118 std::string s = "HTTP/1.1 " +
|
|
119 boost::lexical_cast<std::string>(status) +
|
|
120 " " + std::string(HttpException::GetDescription(status)) +
|
|
121 "\r\n\r\n";
|
|
122 Send(&s[0], s.size());
|
|
123 }
|
|
124
|
|
125
|
|
126 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer,
|
|
127 const std::string& contentType)
|
|
128 {
|
|
129 SendOkHeader(contentType.c_str(), true, buffer.size());
|
|
130 SendString(buffer);
|
|
131 }
|
|
132
|
|
133
|
|
134 void HttpOutput::AnswerBufferWithContentType(const void* buffer,
|
|
135 size_t size,
|
|
136 const std::string& contentType)
|
|
137 {
|
|
138 SendOkHeader(contentType.c_str(), true, size);
|
|
139 Send(buffer, size);
|
|
140 }
|
|
141
|
|
142
|
|
143 void HttpOutput::AnswerFileWithContentType(const std::string& path,
|
|
144 const std::string& contentType)
|
|
145 {
|
|
146 uint64_t fileSize = Toolbox::GetFileSize(path);
|
|
147
|
|
148 FILE* fp = fopen(path.c_str(), "rb");
|
|
149 if (!fp)
|
|
150 {
|
59
|
151 SendHeaderInternal(Orthanc_HttpStatus_500_InternalServerError);
|
0
|
152 return;
|
|
153 }
|
|
154
|
|
155 SendOkHeader(contentType.c_str(), true, fileSize);
|
|
156
|
|
157 std::vector<uint8_t> buffer(1024 * 1024); // Chunks of 1MB
|
|
158
|
|
159 for (;;)
|
|
160 {
|
|
161 size_t nbytes = fread(&buffer[0], 1, buffer.size(), fp);
|
|
162 if (nbytes == 0)
|
|
163 {
|
|
164 break;
|
|
165 }
|
|
166 else
|
|
167 {
|
|
168 Send(&buffer[0], nbytes);
|
|
169 }
|
|
170 }
|
|
171
|
|
172 fclose(fp);
|
|
173 }
|
|
174
|
|
175
|
|
176 void HttpOutput::AnswerFileAutodetectContentType(const std::string& path)
|
|
177 {
|
|
178 AnswerFileWithContentType(path, Toolbox::AutodetectMimeType(path));
|
|
179 }
|
|
180
|
|
181
|
|
182 void HttpOutput::AnswerFile(const FileStorage& storage,
|
|
183 const std::string& uuid,
|
|
184 const std::string& contentType)
|
|
185 {
|
|
186 boost::filesystem::path p(storage.GetPath(uuid));
|
|
187 AnswerFileWithContentType(p.string(), contentType);
|
|
188 }
|
|
189
|
|
190
|
|
191
|
|
192 void HttpOutput::Redirect(const std::string& path)
|
|
193 {
|
|
194 std::string s =
|
59
|
195 "HTTP/1.1 301 " + std::string(HttpException::GetDescription(Orthanc_HttpStatus_301_MovedPermanently)) +
|
0
|
196 "\r\nLocation: " + path +
|
|
197 "\r\n\r\n";
|
|
198 Send(&s[0], s.size());
|
|
199 }
|
|
200 }
|