Mercurial > hg > orthanc
annotate Core/HttpServer/HttpOutput.cpp @ 182:93ff5babcaf8
children public id
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 12 Nov 2012 10:36:58 +0100 |
parents | 93e1b0e3b83a |
children | 7f74209ea0f8 |
rev | line source |
---|---|
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 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
52 SendOkHeader(contentType.c_str(), false, 0, NULL); |
0 | 53 } |
54 | |
55 void HttpOutput::SendOkHeader(const char* contentType, | |
56 bool hasContentLength, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
57 uint64_t contentLength, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
58 const char* contentFilename) |
0 | 59 { |
60 std::string s = "HTTP/1.1 200 OK\r\n"; | |
61 | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
62 if (contentType && contentType[0] != '\0') |
0 | 63 { |
64 s += "Content-Type: " + std::string(contentType) + "\r\n"; | |
65 } | |
66 | |
67 if (hasContentLength) | |
68 { | |
69 s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n"; | |
70 } | |
71 | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
72 if (contentFilename && contentFilename[0] != '\0') |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
73 { |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
74 s += "Content-Disposition: attachment; filename=\"" + std::string(contentFilename) + "\"\r\n"; |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
75 } |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
76 |
0 | 77 s += "\r\n"; |
78 | |
79 Send(&s[0], s.size()); | |
80 } | |
81 | |
82 | |
83 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) | |
84 { | |
85 std::string s = | |
59 | 86 "HTTP/1.1 405 " + std::string(HttpException::GetDescription(Orthanc_HttpStatus_405_MethodNotAllowed)) + |
0 | 87 "\r\nAllow: " + allowed + |
88 "\r\n\r\n"; | |
89 Send(&s[0], s.size()); | |
90 } | |
91 | |
92 | |
59 | 93 void HttpOutput::SendHeader(Orthanc_HttpStatus status) |
0 | 94 { |
59 | 95 if (status == Orthanc_HttpStatus_200_Ok || |
96 status == Orthanc_HttpStatus_405_MethodNotAllowed) | |
0 | 97 { |
59 | 98 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); |
0 | 99 } |
100 | |
101 SendHeaderInternal(status); | |
102 } | |
103 | |
104 | |
59 | 105 void HttpOutput::SendHeaderInternal(Orthanc_HttpStatus status) |
0 | 106 { |
107 std::string s = "HTTP/1.1 " + | |
108 boost::lexical_cast<std::string>(status) + | |
109 " " + std::string(HttpException::GetDescription(status)) + | |
110 "\r\n\r\n"; | |
111 Send(&s[0], s.size()); | |
112 } | |
113 | |
114 | |
115 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, | |
116 const std::string& contentType) | |
117 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
118 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); |
0 | 119 SendString(buffer); |
120 } | |
121 | |
122 | |
123 void HttpOutput::AnswerBufferWithContentType(const void* buffer, | |
124 size_t size, | |
125 const std::string& contentType) | |
126 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
127 SendOkHeader(contentType.c_str(), true, size, NULL); |
0 | 128 Send(buffer, size); |
129 } | |
130 | |
131 | |
132 void HttpOutput::AnswerFileWithContentType(const std::string& path, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
133 const std::string& contentType, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
134 const char* filename) |
0 | 135 { |
136 uint64_t fileSize = Toolbox::GetFileSize(path); | |
137 | |
138 FILE* fp = fopen(path.c_str(), "rb"); | |
139 if (!fp) | |
140 { | |
59 | 141 SendHeaderInternal(Orthanc_HttpStatus_500_InternalServerError); |
0 | 142 return; |
143 } | |
144 | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
145 SendOkHeader(contentType.c_str(), true, fileSize, filename); |
0 | 146 |
147 std::vector<uint8_t> buffer(1024 * 1024); // Chunks of 1MB | |
148 | |
149 for (;;) | |
150 { | |
151 size_t nbytes = fread(&buffer[0], 1, buffer.size(), fp); | |
152 if (nbytes == 0) | |
153 { | |
154 break; | |
155 } | |
156 else | |
157 { | |
158 Send(&buffer[0], nbytes); | |
159 } | |
160 } | |
161 | |
162 fclose(fp); | |
163 } | |
164 | |
165 | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
166 void HttpOutput::AnswerFileAutodetectContentType(const std::string& path, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
167 const char* filename) |
0 | 168 { |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
169 AnswerFileWithContentType(path, Toolbox::AutodetectMimeType(path), filename); |
0 | 170 } |
171 | |
172 | |
173 void HttpOutput::AnswerFile(const FileStorage& storage, | |
174 const std::string& uuid, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
175 const std::string& contentType, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
176 const char* filename) |
0 | 177 { |
178 boost::filesystem::path p(storage.GetPath(uuid)); | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
179 AnswerFileWithContentType(p.string(), contentType, filename); |
0 | 180 } |
181 | |
182 | |
183 | |
184 void HttpOutput::Redirect(const std::string& path) | |
185 { | |
186 std::string s = | |
59 | 187 "HTTP/1.1 301 " + std::string(HttpException::GetDescription(Orthanc_HttpStatus_301_MovedPermanently)) + |
0 | 188 "\r\nLocation: " + path + |
189 "\r\n\r\n"; | |
190 Send(&s[0], s.size()); | |
191 } | |
192 } |