comparison Resources/Orthanc/Core/HttpClient.h @ 200:03afbee0cc7b

integration of Orthanc core into Stone
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Mar 2018 11:04:03 +0100
parents
children
comparison
equal deleted inserted replaced
199:dabe9982fca3 200:03afbee0cc7b
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-2018 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 "WebServiceParameters.h"
38
39 #include <string>
40 #include <boost/shared_ptr.hpp>
41 #include <json/json.h>
42
43 #if !defined(ORTHANC_ENABLE_SSL)
44 # error The macro ORTHANC_ENABLE_SSL must be defined
45 #endif
46
47 #if !defined(ORTHANC_ENABLE_PKCS11)
48 # error The macro ORTHANC_ENABLE_PKCS11 must be defined
49 #endif
50
51
52 namespace Orthanc
53 {
54 class HttpClient
55 {
56 public:
57 typedef std::map<std::string, std::string> HttpHeaders;
58
59 private:
60 class GlobalParameters;
61
62 struct PImpl;
63 boost::shared_ptr<PImpl> pimpl_;
64
65 std::string url_;
66 std::string credentials_;
67 HttpMethod method_;
68 HttpStatus lastStatus_;
69 std::string body_; // This only makes sense for POST and PUT requests
70 bool isVerbose_;
71 long timeout_;
72 std::string proxy_;
73 bool verifyPeers_;
74 std::string caCertificates_;
75 std::string clientCertificateFile_;
76 std::string clientCertificateKeyFile_;
77 std::string clientCertificateKeyPassword_;
78 bool pkcs11Enabled_;
79 bool headersToLowerCase_;
80 bool redirectionFollowed_;
81
82 void Setup();
83
84 void operator= (const HttpClient&); // Assignment forbidden
85 HttpClient(const HttpClient& base); // Copy forbidden
86
87 bool ApplyInternal(std::string& answerBody,
88 HttpHeaders* answerHeaders);
89
90 bool ApplyInternal(Json::Value& answerBody,
91 HttpHeaders* answerHeaders);
92
93 public:
94 HttpClient();
95
96 HttpClient(const WebServiceParameters& service,
97 const std::string& uri);
98
99 ~HttpClient();
100
101 void SetUrl(const char* url)
102 {
103 url_ = std::string(url);
104 }
105
106 void SetUrl(const std::string& url)
107 {
108 url_ = url;
109 }
110
111 const std::string& GetUrl() const
112 {
113 return url_;
114 }
115
116 void SetMethod(HttpMethod method)
117 {
118 method_ = method;
119 }
120
121 HttpMethod GetMethod() const
122 {
123 return method_;
124 }
125
126 void SetTimeout(long seconds)
127 {
128 timeout_ = seconds;
129 }
130
131 long GetTimeout() const
132 {
133 return timeout_;
134 }
135
136 void SetBody(const std::string& data)
137 {
138 body_ = data;
139 }
140
141 std::string& GetBody()
142 {
143 return body_;
144 }
145
146 const std::string& GetBody() const
147 {
148 return body_;
149 }
150
151 void SetVerbose(bool isVerbose);
152
153 bool IsVerbose() const
154 {
155 return isVerbose_;
156 }
157
158 void AddHeader(const std::string& key,
159 const std::string& value);
160
161 void ClearHeaders();
162
163 bool Apply(std::string& answerBody)
164 {
165 return ApplyInternal(answerBody, NULL);
166 }
167
168 bool Apply(Json::Value& answerBody)
169 {
170 return ApplyInternal(answerBody, NULL);
171 }
172
173 bool Apply(std::string& answerBody,
174 HttpHeaders& answerHeaders)
175 {
176 return ApplyInternal(answerBody, &answerHeaders);
177 }
178
179 bool Apply(Json::Value& answerBody,
180 HttpHeaders& answerHeaders)
181 {
182 return ApplyInternal(answerBody, &answerHeaders);
183 }
184
185 HttpStatus GetLastStatus() const
186 {
187 return lastStatus_;
188 }
189
190 void SetCredentials(const char* username,
191 const char* password);
192
193 void SetProxy(const std::string& proxy)
194 {
195 proxy_ = proxy;
196 }
197
198 void SetHttpsVerifyPeers(bool verify)
199 {
200 verifyPeers_ = verify;
201 }
202
203 bool IsHttpsVerifyPeers() const
204 {
205 return verifyPeers_;
206 }
207
208 void SetHttpsCACertificates(const std::string& certificates)
209 {
210 caCertificates_ = certificates;
211 }
212
213 const std::string& GetHttpsCACertificates() const
214 {
215 return caCertificates_;
216 }
217
218 void SetClientCertificate(const std::string& certificateFile,
219 const std::string& certificateKeyFile,
220 const std::string& certificateKeyPassword);
221
222 void SetPkcs11Enabled(bool enabled)
223 {
224 pkcs11Enabled_ = enabled;
225 }
226
227 bool IsPkcs11Enabled() const
228 {
229 return pkcs11Enabled_;
230 }
231
232 const std::string& GetClientCertificateFile() const
233 {
234 return clientCertificateFile_;
235 }
236
237 const std::string& GetClientCertificateKeyFile() const
238 {
239 return clientCertificateKeyFile_;
240 }
241
242 const std::string& GetClientCertificateKeyPassword() const
243 {
244 return clientCertificateKeyPassword_;
245 }
246
247 void SetConvertHeadersToLowerCase(bool lowerCase)
248 {
249 headersToLowerCase_ = lowerCase;
250 }
251
252 bool IsConvertHeadersToLowerCase() const
253 {
254 return headersToLowerCase_;
255 }
256
257 void SetRedirectionFollowed(bool follow)
258 {
259 redirectionFollowed_ = follow;
260 }
261
262 bool IsRedirectionFollowed() const
263 {
264 return redirectionFollowed_;
265 }
266
267 static void GlobalInitialize();
268
269 static void GlobalFinalize();
270
271 static void InitializeOpenSsl();
272
273 static void FinalizeOpenSsl();
274
275 static void InitializePkcs11(const std::string& module,
276 const std::string& pin,
277 bool verbose);
278
279 static void ConfigureSsl(bool httpsVerifyPeers,
280 const std::string& httpsCACertificates);
281
282 static void SetDefaultProxy(const std::string& proxy);
283
284 static void SetDefaultTimeout(long timeout);
285
286 void ApplyAndThrowException(std::string& answerBody);
287
288 void ApplyAndThrowException(Json::Value& answerBody);
289
290 void ApplyAndThrowException(std::string& answerBody,
291 HttpHeaders& answerHeaders);
292
293 void ApplyAndThrowException(Json::Value& answerBody,
294 HttpHeaders& answerHeaders);
295 };
296 }