Mercurial > hg > orthanc-client
comparison Orthanc/Core/HttpClient.cpp @ 3:d5027f9f676a
fix build
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 01 Jun 2015 15:12:22 +0200 |
parents | |
children | e59bf2554e59 |
comparison
equal
deleted
inserted
replaced
2:8f22ed9d48d5 | 3:d5027f9f676a |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2015 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 #include "PrecompiledHeaders.h" | |
34 #include "HttpClient.h" | |
35 | |
36 #include "Toolbox.h" | |
37 #include "OrthancException.h" | |
38 | |
39 #include <string.h> | |
40 #include <curl/curl.h> | |
41 | |
42 | |
43 namespace Orthanc | |
44 { | |
45 struct HttpClient::PImpl | |
46 { | |
47 CURL* curl_; | |
48 struct curl_slist *postHeaders_; | |
49 }; | |
50 | |
51 | |
52 static CURLcode CheckCode(CURLcode code) | |
53 { | |
54 if (code != CURLE_OK) | |
55 { | |
56 throw OrthancException("libCURL error: " + std::string(curl_easy_strerror(code))); | |
57 } | |
58 | |
59 return code; | |
60 } | |
61 | |
62 | |
63 static size_t CurlCallback(void *buffer, size_t size, size_t nmemb, void *payload) | |
64 { | |
65 std::string& target = *(static_cast<std::string*>(payload)); | |
66 | |
67 size_t length = size * nmemb; | |
68 if (length == 0) | |
69 return 0; | |
70 | |
71 size_t pos = target.size(); | |
72 | |
73 target.resize(pos + length); | |
74 memcpy(&target.at(pos), buffer, length); | |
75 | |
76 return length; | |
77 } | |
78 | |
79 | |
80 void HttpClient::Setup() | |
81 { | |
82 pimpl_->postHeaders_ = NULL; | |
83 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) | |
84 { | |
85 throw OrthancException(ErrorCode_NotEnoughMemory); | |
86 } | |
87 | |
88 pimpl_->curl_ = curl_easy_init(); | |
89 if (!pimpl_->curl_) | |
90 { | |
91 curl_slist_free_all(pimpl_->postHeaders_); | |
92 throw OrthancException(ErrorCode_NotEnoughMemory); | |
93 } | |
94 | |
95 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback)); | |
96 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0)); | |
97 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1)); | |
98 | |
99 #if ORTHANC_SSL_ENABLED == 1 | |
100 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 0)); | |
101 #endif | |
102 | |
103 // This fixes the "longjmp causes uninitialized stack frame" crash | |
104 // that happens on modern Linux versions. | |
105 // http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame | |
106 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOSIGNAL, 1)); | |
107 | |
108 url_ = ""; | |
109 method_ = HttpMethod_Get; | |
110 lastStatus_ = HttpStatus_200_Ok; | |
111 isVerbose_ = false; | |
112 timeout_ = 0; | |
113 } | |
114 | |
115 | |
116 HttpClient::HttpClient() : pimpl_(new PImpl) | |
117 { | |
118 Setup(); | |
119 } | |
120 | |
121 | |
122 HttpClient::HttpClient(const HttpClient& other) : pimpl_(new PImpl) | |
123 { | |
124 Setup(); | |
125 | |
126 if (other.IsVerbose()) | |
127 { | |
128 SetVerbose(true); | |
129 } | |
130 | |
131 if (other.credentials_.size() != 0) | |
132 { | |
133 credentials_ = other.credentials_; | |
134 } | |
135 } | |
136 | |
137 | |
138 HttpClient::~HttpClient() | |
139 { | |
140 curl_easy_cleanup(pimpl_->curl_); | |
141 curl_slist_free_all(pimpl_->postHeaders_); | |
142 } | |
143 | |
144 | |
145 void HttpClient::SetVerbose(bool isVerbose) | |
146 { | |
147 isVerbose_ = isVerbose; | |
148 | |
149 if (isVerbose_) | |
150 { | |
151 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1)); | |
152 } | |
153 else | |
154 { | |
155 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0)); | |
156 } | |
157 } | |
158 | |
159 | |
160 bool HttpClient::Apply(std::string& answer) | |
161 { | |
162 answer.clear(); | |
163 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str())); | |
164 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer)); | |
165 | |
166 // Reset the parameters from previous calls to Apply() | |
167 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); | |
168 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L)); | |
169 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L)); | |
170 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L)); | |
171 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, NULL)); | |
172 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); | |
173 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0)); | |
174 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PROXY, NULL)); | |
175 | |
176 // Set timeouts | |
177 if (timeout_ <= 0) | |
178 { | |
179 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_TIMEOUT, 10)); /* default: 10 seconds */ | |
180 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CONNECTTIMEOUT, 10)); /* default: 10 seconds */ | |
181 } | |
182 else | |
183 { | |
184 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_TIMEOUT, timeout_)); | |
185 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CONNECTTIMEOUT, timeout_)); | |
186 } | |
187 | |
188 if (credentials_.size() != 0) | |
189 { | |
190 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_USERPWD, credentials_.c_str())); | |
191 } | |
192 | |
193 if (proxy_.size() != 0) | |
194 { | |
195 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PROXY, proxy_.c_str())); | |
196 } | |
197 | |
198 switch (method_) | |
199 { | |
200 case HttpMethod_Get: | |
201 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); | |
202 break; | |
203 | |
204 case HttpMethod_Post: | |
205 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L)); | |
206 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); | |
207 break; | |
208 | |
209 case HttpMethod_Delete: | |
210 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L)); | |
211 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE")); | |
212 break; | |
213 | |
214 case HttpMethod_Put: | |
215 // http://stackoverflow.com/a/7570281/881731: Don't use | |
216 // CURLOPT_PUT if there is a body | |
217 | |
218 // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); | |
219 | |
220 curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */ | |
221 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); | |
222 break; | |
223 | |
224 default: | |
225 throw OrthancException(ErrorCode_InternalError); | |
226 } | |
227 | |
228 | |
229 if (method_ == HttpMethod_Post || | |
230 method_ == HttpMethod_Put) | |
231 { | |
232 if (postData_.size() > 0) | |
233 { | |
234 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str())); | |
235 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size())); | |
236 } | |
237 else | |
238 { | |
239 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); | |
240 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0)); | |
241 } | |
242 } | |
243 | |
244 | |
245 // Do the actual request | |
246 CheckCode(curl_easy_perform(pimpl_->curl_)); | |
247 | |
248 long status; | |
249 CheckCode(curl_easy_getinfo(pimpl_->curl_, CURLINFO_RESPONSE_CODE, &status)); | |
250 | |
251 if (status == 0) | |
252 { | |
253 // This corresponds to a call to an inexistent host | |
254 lastStatus_ = HttpStatus_500_InternalServerError; | |
255 } | |
256 else | |
257 { | |
258 lastStatus_ = static_cast<HttpStatus>(status); | |
259 } | |
260 | |
261 return (status >= 200 && status < 300); | |
262 } | |
263 | |
264 | |
265 bool HttpClient::Apply(Json::Value& answer) | |
266 { | |
267 std::string s; | |
268 if (Apply(s)) | |
269 { | |
270 Json::Reader reader; | |
271 return reader.parse(s, answer); | |
272 } | |
273 else | |
274 { | |
275 return false; | |
276 } | |
277 } | |
278 | |
279 | |
280 void HttpClient::SetCredentials(const char* username, | |
281 const char* password) | |
282 { | |
283 credentials_ = std::string(username) + ":" + std::string(password); | |
284 } | |
285 | |
286 | |
287 void HttpClient::GlobalInitialize() | |
288 { | |
289 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT)); | |
290 } | |
291 | |
292 void HttpClient::GlobalFinalize() | |
293 { | |
294 curl_global_cleanup(); | |
295 } | |
296 } |