Mercurial > hg > orthanc
annotate Core/HttpServer/HttpOutput.cpp @ 2753:078e43c749d2 Orthanc-0.8.1
close old branch
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jul 2018 09:40:53 +0200 |
parents | 8d1845feb277 |
children | ba5c0908600c |
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> | |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
39 #include <glog/logging.h> |
0 | 40 #include <boost/lexical_cast.hpp> |
59 | 41 #include "../OrthancException.h" |
0 | 42 #include "../Toolbox.h" |
43 | |
59 | 44 namespace Orthanc |
0 | 45 { |
910 | 46 void HttpOutput::StateMachine::SendHttpStatus(HttpStatus status) |
47 { | |
48 if (state_ != State_WaitingHttpStatus) | |
49 { | |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
50 LOG(ERROR) << "Sending twice an HTTP status"; |
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
51 return; |
910 | 52 } |
53 | |
911 | 54 stream_.OnHttpStatusReceived(status); |
910 | 55 state_ = State_WritingHeader; |
56 | |
57 std::string s = "HTTP/1.1 " + | |
58 boost::lexical_cast<std::string>(status) + | |
59 " " + std::string(EnumerationToString(status)) + | |
60 "\r\n"; | |
61 | |
911 | 62 stream_.Send(true, &s[0], s.size()); |
910 | 63 } |
64 | |
65 void HttpOutput::StateMachine::SendHeaderData(const void* buffer, size_t length) | |
66 { | |
67 if (state_ != State_WritingHeader) | |
68 { | |
69 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
70 } | |
71 | |
911 | 72 stream_.Send(true, buffer, length); |
910 | 73 } |
74 | |
75 void HttpOutput::StateMachine::SendHeaderString(const std::string& str) | |
76 { | |
77 if (str.size() > 0) | |
78 { | |
79 SendHeaderData(&str[0], str.size()); | |
80 } | |
81 } | |
82 | |
83 void HttpOutput::StateMachine::SendBodyData(const void* buffer, size_t length) | |
84 { | |
85 if (state_ == State_WaitingHttpStatus) | |
86 { | |
87 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
88 } | |
89 | |
90 if (state_ == State_WritingHeader) | |
91 { | |
92 // Close the HTTP header before writing the body | |
911 | 93 stream_.Send(true, "\r\n", 2); |
910 | 94 state_ = State_WritingBody; |
95 } | |
96 | |
97 if (length > 0) | |
98 { | |
911 | 99 stream_.Send(false, buffer, length); |
910 | 100 } |
101 } | |
102 | |
103 void HttpOutput::StateMachine::SendBodyString(const std::string& str) | |
104 { | |
105 if (str.size() > 0) | |
106 { | |
107 SendBodyData(&str[0], str.size()); | |
108 } | |
109 } | |
110 | |
111 | |
330 | 112 void HttpOutput::PrepareOkHeader(Header& header, |
113 const char* contentType, | |
114 bool hasContentLength, | |
115 uint64_t contentLength, | |
116 const char* contentFilename) | |
117 { | |
118 header.clear(); | |
119 | |
120 if (contentType && contentType[0] != '\0') | |
121 { | |
122 header.push_back(std::make_pair("Content-Type", std::string(contentType))); | |
123 } | |
124 | |
125 if (hasContentLength) | |
126 { | |
127 header.push_back(std::make_pair("Content-Length", boost::lexical_cast<std::string>(contentLength))); | |
128 } | |
129 | |
130 if (contentFilename && contentFilename[0] != '\0') | |
131 { | |
132 std::string attachment = "attachment; filename=\"" + std::string(contentFilename) + "\""; | |
133 header.push_back(std::make_pair("Content-Disposition", attachment)); | |
134 } | |
135 } | |
136 | |
0 | 137 void HttpOutput::SendOkHeader(const char* contentType, |
138 bool hasContentLength, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
139 uint64_t contentLength, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
140 const char* contentFilename) |
0 | 141 { |
330 | 142 Header header; |
143 PrepareOkHeader(header, contentType, hasContentLength, contentLength, contentFilename); | |
144 SendOkHeader(header); | |
0 | 145 } |
146 | |
330 | 147 void HttpOutput::SendOkHeader(const Header& header) |
324 | 148 { |
911 | 149 stateMachine_.SendHttpStatus(HttpStatus_200_Ok); |
0 | 150 |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
151 std::string s; |
330 | 152 for (Header::const_iterator |
656 | 153 it = header.begin(); it != header.end(); ++it) |
324 | 154 { |
155 s += it->first + ": " + it->second + "\r\n"; | |
156 } | |
157 | |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
158 for (HttpHandler::Arguments::const_iterator |
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
159 it = cookies_.begin(); it != cookies_.end(); ++it) |
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
160 { |
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
161 s += "Set-Cookie: " + it->first + "=" + it->second + "\r\n"; |
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
162 } |
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
163 |
911 | 164 stateMachine_.SendHeaderString(s); |
207 | 165 } |
166 | |
167 | |
1042
8d1845feb277
set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
911
diff
changeset
|
168 void HttpOutput::SendMethodNotAllowed(const std::string& allowed) |
0 | 169 { |
911 | 170 stateMachine_.SendHttpStatus(HttpStatus_405_MethodNotAllowed); |
171 stateMachine_.SendHeaderString("Allow: " + allowed + "\r\n"); | |
0 | 172 } |
173 | |
174 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
175 void HttpOutput::SendHeader(HttpStatus status) |
0 | 176 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
177 if (status == HttpStatus_200_Ok || |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
178 status == HttpStatus_301_MovedPermanently || |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
179 status == HttpStatus_401_Unauthorized || |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
180 status == HttpStatus_405_MethodNotAllowed) |
0 | 181 { |
59 | 182 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); |
0 | 183 } |
184 | |
911 | 185 stateMachine_.SendHttpStatus(status); |
0 | 186 } |
187 | |
188 | |
189 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, | |
190 const std::string& contentType) | |
191 { | |
330 | 192 Header header; |
193 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL); | |
194 SendOkHeader(header); | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
195 SendBodyString(buffer); |
330 | 196 } |
197 | |
198 | |
0 | 199 void HttpOutput::AnswerBufferWithContentType(const void* buffer, |
200 size_t size, | |
201 const std::string& contentType) | |
202 { | |
339 | 203 Header header; |
204 PrepareOkHeader(header, contentType.c_str(), true, size, NULL); | |
205 SendOkHeader(header); | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
206 SendBodyData(buffer, size); |
339 | 207 } |
208 | |
209 | |
0 | 210 void HttpOutput::Redirect(const std::string& path) |
211 { | |
911 | 212 stateMachine_.SendHttpStatus(HttpStatus_301_MovedPermanently); |
213 stateMachine_.SendHeaderString("Location: " + path + "\r\n"); | |
0 | 214 } |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
215 |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
216 |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
217 void HttpOutput::SendUnauthorized(const std::string& realm) |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
218 { |
911 | 219 stateMachine_.SendHttpStatus(HttpStatus_401_Unauthorized); |
220 stateMachine_.SendHeaderString("WWW-Authenticate: Basic realm=\"" + realm + "\"\r\n"); | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
221 } |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
222 |
0 | 223 } |