comparison OrthancFramework/Sources/HttpClient.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/HttpClient.h@05a363186da6
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 "OrthancFramework.h"
38 #include "WebServiceParameters.h"
39
40 #include <string>
41 #include <boost/noncopyable.hpp>
42 #include <boost/shared_ptr.hpp>
43 #include <json/json.h>
44
45 #if !defined(ORTHANC_ENABLE_CURL)
46 # error The macro ORTHANC_ENABLE_CURL must be defined
47 #endif
48
49 #if ORTHANC_ENABLE_CURL != 1
50 # error Support for curl is disabled, cannot use this file
51 #endif
52
53 #if !defined(ORTHANC_ENABLE_SSL)
54 # error The macro ORTHANC_ENABLE_SSL must be defined
55 #endif
56
57 #if !defined(ORTHANC_ENABLE_PKCS11)
58 # error The macro ORTHANC_ENABLE_PKCS11 must be defined
59 #endif
60
61
62 namespace Orthanc
63 {
64 class ORTHANC_PUBLIC HttpClient : public boost::noncopyable
65 {
66 public:
67 typedef std::map<std::string, std::string> HttpHeaders;
68
69 class IRequestBody : public boost::noncopyable
70 {
71 public:
72 virtual ~IRequestBody()
73 {
74 }
75
76 virtual bool ReadNextChunk(std::string& chunk) = 0;
77 };
78
79 class IAnswer : public boost::noncopyable
80 {
81 public:
82 virtual ~IAnswer()
83 {
84 }
85
86 virtual void AddHeader(const std::string& key,
87 const std::string& value) = 0;
88
89 virtual void AddChunk(const void* data,
90 size_t size) = 0;
91 };
92
93 private:
94 class CurlHeaders;
95 class CurlRequestBody;
96 class CurlAnswer;
97 class DefaultAnswer;
98 class GlobalParameters;
99
100 struct PImpl;
101 boost::shared_ptr<PImpl> pimpl_;
102
103 std::string url_;
104 std::string credentials_;
105 HttpMethod method_;
106 HttpStatus lastStatus_;
107 std::string body_; // This only makes sense for POST and PUT requests
108 bool isVerbose_;
109 long timeout_;
110 std::string proxy_;
111 bool verifyPeers_;
112 std::string caCertificates_;
113 std::string clientCertificateFile_;
114 std::string clientCertificateKeyFile_;
115 std::string clientCertificateKeyPassword_;
116 bool pkcs11Enabled_;
117 bool headersToLowerCase_;
118 bool redirectionFollowed_;
119
120 void Setup();
121
122 void operator= (const HttpClient&); // Assignment forbidden
123 HttpClient(const HttpClient& base); // Copy forbidden
124
125 bool ApplyInternal(CurlAnswer& answer);
126
127 bool ApplyInternal(std::string& answerBody,
128 HttpHeaders* answerHeaders);
129
130 bool ApplyInternal(Json::Value& answerBody,
131 HttpHeaders* answerHeaders);
132
133 public:
134 HttpClient();
135
136 HttpClient(const WebServiceParameters& service,
137 const std::string& uri);
138
139 ~HttpClient();
140
141 void SetUrl(const char* url)
142 {
143 url_ = std::string(url);
144 }
145
146 void SetUrl(const std::string& url)
147 {
148 url_ = url;
149 }
150
151 const std::string& GetUrl() const
152 {
153 return url_;
154 }
155
156 void SetMethod(HttpMethod method)
157 {
158 method_ = method;
159 }
160
161 HttpMethod GetMethod() const
162 {
163 return method_;
164 }
165
166 void SetTimeout(long seconds)
167 {
168 timeout_ = seconds;
169 }
170
171 long GetTimeout() const
172 {
173 return timeout_;
174 }
175
176 void SetBody(const std::string& data);
177
178 std::string& GetBody()
179 {
180 return body_;
181 }
182
183 const std::string& GetBody() const
184 {
185 return body_;
186 }
187
188 void SetBody(IRequestBody& body);
189
190 void ClearBody();
191
192 void SetVerbose(bool isVerbose);
193
194 bool IsVerbose() const
195 {
196 return isVerbose_;
197 }
198
199 void AddHeader(const std::string& key,
200 const std::string& value);
201
202 void ClearHeaders();
203
204 bool Apply(IAnswer& answer);
205
206 bool Apply(std::string& answerBody)
207 {
208 return ApplyInternal(answerBody, NULL);
209 }
210
211 bool Apply(Json::Value& answerBody)
212 {
213 return ApplyInternal(answerBody, NULL);
214 }
215
216 bool Apply(std::string& answerBody,
217 HttpHeaders& answerHeaders)
218 {
219 return ApplyInternal(answerBody, &answerHeaders);
220 }
221
222 bool Apply(Json::Value& answerBody,
223 HttpHeaders& answerHeaders)
224 {
225 return ApplyInternal(answerBody, &answerHeaders);
226 }
227
228 HttpStatus GetLastStatus() const
229 {
230 return lastStatus_;
231 }
232
233 void SetCredentials(const char* username,
234 const char* password);
235
236 void SetProxy(const std::string& proxy)
237 {
238 proxy_ = proxy;
239 }
240
241 void SetHttpsVerifyPeers(bool verify)
242 {
243 verifyPeers_ = verify;
244 }
245
246 bool IsHttpsVerifyPeers() const
247 {
248 return verifyPeers_;
249 }
250
251 void SetHttpsCACertificates(const std::string& certificates)
252 {
253 caCertificates_ = certificates;
254 }
255
256 const std::string& GetHttpsCACertificates() const
257 {
258 return caCertificates_;
259 }
260
261 void SetClientCertificate(const std::string& certificateFile,
262 const std::string& certificateKeyFile,
263 const std::string& certificateKeyPassword);
264
265 void SetPkcs11Enabled(bool enabled)
266 {
267 pkcs11Enabled_ = enabled;
268 }
269
270 bool IsPkcs11Enabled() const
271 {
272 return pkcs11Enabled_;
273 }
274
275 const std::string& GetClientCertificateFile() const
276 {
277 return clientCertificateFile_;
278 }
279
280 const std::string& GetClientCertificateKeyFile() const
281 {
282 return clientCertificateKeyFile_;
283 }
284
285 const std::string& GetClientCertificateKeyPassword() const
286 {
287 return clientCertificateKeyPassword_;
288 }
289
290 void SetConvertHeadersToLowerCase(bool lowerCase)
291 {
292 headersToLowerCase_ = lowerCase;
293 }
294
295 bool IsConvertHeadersToLowerCase() const
296 {
297 return headersToLowerCase_;
298 }
299
300 void SetRedirectionFollowed(bool follow)
301 {
302 redirectionFollowed_ = follow;
303 }
304
305 bool IsRedirectionFollowed() const
306 {
307 return redirectionFollowed_;
308 }
309
310 static void GlobalInitialize();
311
312 static void GlobalFinalize();
313
314 static void InitializePkcs11(const std::string& module,
315 const std::string& pin,
316 bool verbose);
317
318 static void ConfigureSsl(bool httpsVerifyPeers,
319 const std::string& httpsCACertificates);
320
321 static void SetDefaultVerbose(bool verbose);
322
323 static void SetDefaultProxy(const std::string& proxy);
324
325 static void SetDefaultTimeout(long timeout);
326
327 void ApplyAndThrowException(IAnswer& answer);
328
329 void ApplyAndThrowException(std::string& answerBody);
330
331 void ApplyAndThrowException(Json::Value& answerBody);
332
333 void ApplyAndThrowException(std::string& answerBody,
334 HttpHeaders& answerHeaders);
335
336 void ApplyAndThrowException(Json::Value& answerBody,
337 HttpHeaders& answerHeaders);
338
339 static void ThrowException(HttpStatus status);
340 };
341 }