comparison Framework/Orthanc/Core/HttpClient.h @ 1:2dbe613f6c93

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