comparison OrthancFramework/Sources/HttpServer/HttpOutput.h @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/HttpServer/HttpOutput.h@f9863630ec7f
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #pragma once
35
36 #include "../Enumerations.h"
37 #include "IHttpOutputStream.h"
38 #include "IHttpStreamAnswer.h"
39
40 #include <list>
41 #include <string>
42 #include <stdint.h>
43 #include <map>
44 #include <vector>
45
46 namespace Orthanc
47 {
48 class ORTHANC_PUBLIC HttpOutput : public boost::noncopyable
49 {
50 private:
51 typedef std::list< std::pair<std::string, std::string> > Header;
52
53 class StateMachine : public boost::noncopyable
54 {
55 public:
56 enum State
57 {
58 State_WritingHeader,
59 State_WritingBody,
60 State_WritingMultipart,
61 State_Done
62 };
63
64 private:
65 IHttpOutputStream& stream_;
66 State state_;
67
68 HttpStatus status_;
69 bool hasContentLength_;
70 uint64_t contentLength_;
71 uint64_t contentPosition_;
72 bool keepAlive_;
73 std::list<std::string> headers_;
74
75 std::string multipartBoundary_;
76 std::string multipartContentType_;
77
78 public:
79 StateMachine(IHttpOutputStream& stream,
80 bool isKeepAlive);
81
82 ~StateMachine();
83
84 void SetHttpStatus(HttpStatus status);
85
86 void SetContentLength(uint64_t length);
87
88 void SetContentType(const char* contentType);
89
90 void SetContentFilename(const char* filename);
91
92 void SetCookie(const std::string& cookie,
93 const std::string& value);
94
95 void AddHeader(const std::string& header,
96 const std::string& value);
97
98 void ClearHeaders();
99
100 void SendBody(const void* buffer, size_t length);
101
102 void StartMultipart(const std::string& subType,
103 const std::string& contentType);
104
105 void SendMultipartItem(const void* item,
106 size_t length,
107 const std::map<std::string, std::string>& headers);
108
109 void CloseMultipart();
110
111 void CloseBody();
112
113 State GetState() const
114 {
115 return state_;
116 }
117
118 void CheckHeadersCompatibilityWithMultipart() const;
119 };
120
121 StateMachine stateMachine_;
122 bool isDeflateAllowed_;
123 bool isGzipAllowed_;
124
125 HttpCompression GetPreferredCompression(size_t bodySize) const;
126
127 public:
128 HttpOutput(IHttpOutputStream& stream,
129 bool isKeepAlive) :
130 stateMachine_(stream, isKeepAlive),
131 isDeflateAllowed_(false),
132 isGzipAllowed_(false)
133 {
134 }
135
136 void SetDeflateAllowed(bool allowed)
137 {
138 isDeflateAllowed_ = allowed;
139 }
140
141 bool IsDeflateAllowed() const
142 {
143 return isDeflateAllowed_;
144 }
145
146 void SetGzipAllowed(bool allowed)
147 {
148 isGzipAllowed_ = allowed;
149 }
150
151 bool IsGzipAllowed() const
152 {
153 return isGzipAllowed_;
154 }
155
156 void SendStatus(HttpStatus status,
157 const char* message,
158 size_t messageSize);
159
160 void SendStatus(HttpStatus status)
161 {
162 SendStatus(status, NULL, 0);
163 }
164
165 void SendStatus(HttpStatus status,
166 const std::string& message)
167 {
168 SendStatus(status, message.c_str(), message.size());
169 }
170
171 void SetContentType(MimeType contentType)
172 {
173 stateMachine_.SetContentType(EnumerationToString(contentType));
174 }
175
176 void SetContentType(const std::string& contentType)
177 {
178 stateMachine_.SetContentType(contentType.c_str());
179 }
180
181 void SetContentFilename(const char* filename)
182 {
183 stateMachine_.SetContentFilename(filename);
184 }
185
186 void SetCookie(const std::string& cookie,
187 const std::string& value)
188 {
189 stateMachine_.SetCookie(cookie, value);
190 }
191
192 void AddHeader(const std::string& key,
193 const std::string& value)
194 {
195 stateMachine_.AddHeader(key, value);
196 }
197
198 void Answer(const void* buffer,
199 size_t length);
200
201 void Answer(const std::string& str);
202
203 void AnswerEmpty();
204
205 void SendMethodNotAllowed(const std::string& allowed);
206
207 void Redirect(const std::string& path);
208
209 void SendUnauthorized(const std::string& realm);
210
211 void StartMultipart(const std::string& subType,
212 const std::string& contentType)
213 {
214 stateMachine_.StartMultipart(subType, contentType);
215 }
216
217 void SendMultipartItem(const void* item,
218 size_t size,
219 const std::map<std::string, std::string>& headers)
220 {
221 stateMachine_.SendMultipartItem(item, size, headers);
222 }
223
224 void CloseMultipart()
225 {
226 stateMachine_.CloseMultipart();
227 }
228
229 bool IsWritingMultipart() const
230 {
231 return stateMachine_.GetState() == StateMachine::State_WritingMultipart;
232 }
233
234 void Answer(IHttpStreamAnswer& stream);
235
236 /**
237 * This method is a replacement to the combination
238 * "StartMultipart()" + "SendMultipartItem()". It generates the
239 * same answer, but it gives a chance to compress the body if
240 * "Accept-Encoding: gzip" is provided by the client, which is not
241 * possible in chunked transfers.
242 **/
243 void AnswerMultipartWithoutChunkedTransfer(
244 const std::string& subType,
245 const std::string& contentType,
246 const std::vector<const void*>& parts,
247 const std::vector<size_t>& sizes,
248 const std::vector<const std::map<std::string, std::string>*>& headers);
249 };
250 }