comparison Resources/Orthanc/Core/HttpClient.h @ 16:ff1e935768e7

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