Mercurial > hg > orthanc
annotate Core/HttpServer/HttpOutput.cpp @ 909:ef71057d8b26 plugins
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 20 Jun 2014 13:30:53 +0200 |
parents | e078ea944089 |
children | 28a52982196e |
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 { |
330 | 45 void HttpOutput::PrepareOkHeader(Header& header, |
46 const char* contentType, | |
47 bool hasContentLength, | |
48 uint64_t contentLength, | |
49 const char* contentFilename) | |
50 { | |
51 header.clear(); | |
52 | |
53 if (contentType && contentType[0] != '\0') | |
54 { | |
55 header.push_back(std::make_pair("Content-Type", std::string(contentType))); | |
56 } | |
57 | |
58 if (hasContentLength) | |
59 { | |
60 header.push_back(std::make_pair("Content-Length", boost::lexical_cast<std::string>(contentLength))); | |
61 } | |
62 | |
63 if (contentFilename && contentFilename[0] != '\0') | |
64 { | |
65 std::string attachment = "attachment; filename=\"" + std::string(contentFilename) + "\""; | |
66 header.push_back(std::make_pair("Content-Disposition", attachment)); | |
67 } | |
68 } | |
69 | |
0 | 70 void HttpOutput::SendOkHeader(const char* contentType, |
71 bool hasContentLength, | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
72 uint64_t contentLength, |
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
73 const char* contentFilename) |
0 | 74 { |
330 | 75 Header header; |
76 PrepareOkHeader(header, contentType, hasContentLength, contentLength, contentFilename); | |
77 SendOkHeader(header); | |
0 | 78 } |
79 | |
330 | 80 void HttpOutput::SendOkHeader(const Header& header) |
324 | 81 { |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
82 stream_.SendHttpStatus(HttpStatus_200_Ok); |
0 | 83 |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
84 std::string s; |
330 | 85 for (Header::const_iterator |
656 | 86 it = header.begin(); it != header.end(); ++it) |
324 | 87 { |
88 s += it->first + ": " + it->second + "\r\n"; | |
89 } | |
90 | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
91 stream_.SendHeaderString(s); |
207 | 92 } |
93 | |
94 | |
0 | 95 void HttpOutput::SendMethodNotAllowedError(const std::string& allowed) |
96 { | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
97 stream_.SendHttpStatus(HttpStatus_405_MethodNotAllowed); |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
98 stream_.SendHeaderString("Allow: " + allowed + "\r\n"); |
0 | 99 } |
100 | |
101 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
102 void HttpOutput::SendHeader(HttpStatus status) |
0 | 103 { |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
104 if (status == HttpStatus_200_Ok || |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
105 status == HttpStatus_301_MovedPermanently || |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
106 status == HttpStatus_401_Unauthorized || |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
107 status == HttpStatus_405_MethodNotAllowed) |
0 | 108 { |
59 | 109 throw OrthancException("Please use the dedicated methods to this HTTP status code in HttpOutput"); |
0 | 110 } |
111 | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
112 stream_.SendHttpStatus(status); |
0 | 113 } |
114 | |
115 | |
116 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, | |
117 const std::string& contentType) | |
118 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
119 SendOkHeader(contentType.c_str(), true, buffer.size(), NULL); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
120 SendBodyString(buffer); |
0 | 121 } |
122 | |
123 | |
339 | 124 void HttpOutput::PrepareCookies(Header& header, |
125 const HttpHandler::Arguments& cookies) | |
126 { | |
127 for (HttpHandler::Arguments::const_iterator it = cookies.begin(); | |
656 | 128 it != cookies.end(); ++it) |
339 | 129 { |
130 header.push_back(std::make_pair("Set-Cookie", it->first + "=" + it->second)); | |
131 } | |
132 } | |
133 | |
134 | |
330 | 135 void HttpOutput::AnswerBufferWithContentType(const std::string& buffer, |
136 const std::string& contentType, | |
137 const HttpHandler::Arguments& cookies) | |
138 { | |
139 Header header; | |
140 PrepareOkHeader(header, contentType.c_str(), true, buffer.size(), NULL); | |
339 | 141 PrepareCookies(header, cookies); |
330 | 142 SendOkHeader(header); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
143 SendBodyString(buffer); |
330 | 144 } |
145 | |
146 | |
0 | 147 void HttpOutput::AnswerBufferWithContentType(const void* buffer, |
148 size_t size, | |
149 const std::string& contentType) | |
150 { | |
155
93e1b0e3b83a
filenames when downloading json/dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
151 SendOkHeader(contentType.c_str(), true, size, NULL); |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
152 SendBodyData(buffer, size); |
0 | 153 } |
154 | |
339 | 155 |
156 void HttpOutput::AnswerBufferWithContentType(const void* buffer, | |
157 size_t size, | |
158 const std::string& contentType, | |
159 const HttpHandler::Arguments& cookies) | |
160 { | |
161 Header header; | |
162 PrepareOkHeader(header, contentType.c_str(), true, size, NULL); | |
163 PrepareCookies(header, cookies); | |
164 SendOkHeader(header); | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
165 SendBodyData(buffer, size); |
339 | 166 } |
167 | |
168 | |
0 | 169 void HttpOutput::Redirect(const std::string& path) |
170 { | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
171 stream_.SendHttpStatus(HttpStatus_301_MovedPermanently); |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
172 stream_.SendHeaderString("Location: " + path + "\r\n"); |
0 | 173 } |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
174 |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
175 |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
176 void HttpOutput::SendUnauthorized(const std::string& realm) |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
177 { |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
178 stream_.SendHttpStatus(HttpStatus_401_Unauthorized); |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
179 stream_.SendHeaderString("WWW-Authenticate: Basic realm=\"" + realm + "\"\r\n"); |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
180 } |
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
181 |
0 | 182 } |