Mercurial > hg > orthanc
annotate Core/HttpServer/HttpOutput.cpp @ 217:1ac3aacd10a5
simplifications
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 29 Nov 2012 12:22:23 +0100 |
parents | 7f74209ea0f8 |
children | 64925c94825c |
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) | |
217 | 47 { |
0 | 48 Send(&s[0], s.size()); |
217 | 49 } |
0 | 50 } |
51 | |
52 void HttpOutput::SendOkHeader(const char* contentType, | |
53 bool hasContentLength, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
54 uint64_t contentLength, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
55 const char* contentFilename) |
0 | 56 { |
57 std::string s = "HTTP/1.1 200 OK\r\n"; | |
58 | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
59 if (contentType && contentType[0] != '\0') |
0 | 60 { |
61 s += "Content-Type: " + std::string(contentType) + "\r\n"; | |
62 } | |
63 | |
64 if (hasContentLength) | |
65 { | |
66 s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n"; | |
67 } | |
68 | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
69 if (contentFilename && contentFilename[0] != '\0') |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
70 { |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
71 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
|
72 } |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
73 |
0 | 74 s += "\r\n"; |
75 | |
76 Send(&s[0], s.size()); | |
77 } | |
78 | |
79 | |
207 | 80 void HttpOutput::SendCustomOkHeader(const std::string& customHeader) |
81 { | |
82 std::string s = "HTTP/1.1 200 OK\r\n" + customHeader + "\r\n"; | |
83 Send(&s[0], s.size()); | |
84 } | |
85 | |
86 | |
0 | 87 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) |
88 { | |
89 std::string s = | |
59 | 90 "HTTP/1.1 405 " + std::string(HttpException::GetDescription(Orthanc_HttpStatus_405_MethodNotAllowed)) + |
0 | 91 "\r\nAllow: " + allowed + |
92 "\r\n\r\n"; | |
93 Send(&s[0], s.size()); | |
94 } | |
95 | |
96 | |
59 | 97 void HttpOutput::SendHeader(Orthanc_HttpStatus status) |
0 | 98 { |
59 | 99 if (status == Orthanc_HttpStatus_200_Ok || |
100 status == Orthanc_HttpStatus_405_MethodNotAllowed) | |
0 | 101 { |
59 | 102 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); |
0 | 103 } |
104 | |
105 SendHeaderInternal(status); | |
106 } | |
107 | |
108 | |
59 | 109 void HttpOutput::SendHeaderInternal(Orthanc_HttpStatus status) |
0 | 110 { |
111 std::string s = "HTTP/1.1 " + | |
112 boost::lexical_cast<std::string>(status) + | |
113 " " + std::string(HttpException::GetDescription(status)) + | |
114 "\r\n\r\n"; | |
115 Send(&s[0], s.size()); | |
116 } | |
117 | |
118 | |
119 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, | |
120 const std::string& contentType) | |
121 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
122 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); |
0 | 123 SendString(buffer); |
124 } | |
125 | |
126 | |
127 void HttpOutput::AnswerBufferWithContentType(const void* buffer, | |
128 size_t size, | |
129 const std::string& contentType) | |
130 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
131 SendOkHeader(contentType.c_str(), true, size, NULL); |
0 | 132 Send(buffer, size); |
133 } | |
134 | |
135 void HttpOutput::Redirect(const std::string& path) | |
136 { | |
137 std::string s = | |
59 | 138 "HTTP/1.1 301 " + std::string(HttpException::GetDescription(Orthanc_HttpStatus_301_MovedPermanently)) + |
0 | 139 "\r\nLocation: " + path + |
140 "\r\n\r\n"; | |
141 Send(&s[0], s.size()); | |
142 } | |
143 } |