Mercurial > hg > orthanc
annotate Core/HttpClient.cpp @ 755:584b59a80ffb Orthanc-0.7.4
set version
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 16 Apr 2014 11:42:56 +0200 |
parents | 203157cb4fde |
children | a811bdf8b8eb |
rev | line source |
---|---|
0 | 1 /** |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 4 * Belgium |
5 * | |
493
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
6 * This program is free software: you can redistribute it and/or |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
7 * modify it under the terms of the GNU General Public License as |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
9 * License, or (at your option) any later version. |
0 | 10 * |
493
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
11 * In addition, as a special exception, the copyright holders of this |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
12 * program give permission to link the code of its release with the |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
14 * that use the same license as the "OpenSSL" library), and distribute |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
15 * the linked executables. You must obey the GNU General Public License |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
16 * in all respects for all of the code used other than "OpenSSL". If you |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
17 * modify file(s) with this exception, you may extend this exception to |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
18 * your version of the file(s), but you are not obligated to do so. If |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
19 * you do not wish to do so, delete this exception statement from your |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
20 * version. If you delete this exception statement from all source files |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
21 * in the program, then also delete it here. |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
22 * |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
23 * This program is distributed in the hope that it will be useful, but |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
26 * General Public License for more details. |
0 | 27 * |
493
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
28 * You should have received a copy of the GNU General Public License |
ff34c51cd3dd
fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 30 **/ |
31 | |
32 | |
33 #include "HttpClient.h" | |
34 | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
35 #include "../Core/Toolbox.h" |
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
36 #include "../Core/OrthancException.h" |
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
37 |
0 | 38 #include <string.h> |
39 #include <curl/curl.h> | |
40 | |
41 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
42 namespace Orthanc |
0 | 43 { |
44 struct HttpClient::PImpl | |
45 { | |
46 CURL* curl_; | |
47 struct curl_slist *postHeaders_; | |
48 }; | |
49 | |
50 | |
51 static CURLcode CheckCode(CURLcode code) | |
52 { | |
53 if (code != CURLE_OK) | |
54 { | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
55 throw OrthancException("libCURL error: " + std::string(curl_easy_strerror(code))); |
0 | 56 } |
57 | |
58 return code; | |
59 } | |
60 | |
61 | |
62 static size_t CurlCallback(void *buffer, size_t size, size_t nmemb, void *payload) | |
63 { | |
64 std::string& target = *(static_cast<std::string*>(payload)); | |
65 | |
66 size_t length = size * nmemb; | |
67 if (length == 0) | |
68 return 0; | |
69 | |
70 size_t pos = target.size(); | |
71 | |
72 target.resize(pos + length); | |
73 memcpy(&target.at(pos), buffer, length); | |
74 | |
75 return length; | |
76 } | |
77 | |
78 | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
79 void HttpClient::Setup() |
0 | 80 { |
81 pimpl_->postHeaders_ = NULL; | |
82 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) | |
83 { | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
84 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 85 } |
86 | |
87 pimpl_->curl_ = curl_easy_init(); | |
88 if (!pimpl_->curl_) | |
89 { | |
90 curl_slist_free_all(pimpl_->postHeaders_); | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
91 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 92 } |
93 | |
94 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback)); | |
95 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0)); | |
96 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1)); | |
97 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
98 #if ORTHANC_SSL_ENABLED == 1 |
26 | 99 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 0)); |
100 #endif | |
101 | |
456 | 102 // This fixes the "longjmp causes uninitialized stack frame" crash |
103 // that happens on modern Linux versions. | |
104 // http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame | |
105 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOSIGNAL, 1)); | |
106 | |
0 | 107 url_ = ""; |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
108 method_ = HttpMethod_Get; |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
109 lastStatus_ = HttpStatus_200_Ok; |
0 | 110 isVerbose_ = false; |
111 } | |
112 | |
113 | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
114 HttpClient::HttpClient() : pimpl_(new PImpl) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
115 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
116 Setup(); |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
117 } |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
118 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
119 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
120 HttpClient::HttpClient(const HttpClient& other) : pimpl_(new PImpl) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
121 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
122 Setup(); |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
123 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
124 if (other.IsVerbose()) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
125 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
126 SetVerbose(true); |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
127 } |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
128 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
129 if (other.credentials_.size() != 0) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
130 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
131 credentials_ = other.credentials_; |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
132 } |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
133 } |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
134 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
135 |
0 | 136 HttpClient::~HttpClient() |
137 { | |
138 curl_easy_cleanup(pimpl_->curl_); | |
139 curl_slist_free_all(pimpl_->postHeaders_); | |
140 } | |
141 | |
142 | |
143 void HttpClient::SetVerbose(bool isVerbose) | |
144 { | |
145 isVerbose_ = isVerbose; | |
146 | |
147 if (isVerbose_) | |
148 { | |
149 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1)); | |
150 } | |
151 else | |
152 { | |
153 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0)); | |
154 } | |
155 } | |
156 | |
157 | |
158 bool HttpClient::Apply(std::string& answer) | |
159 { | |
160 answer.clear(); | |
161 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str())); | |
162 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer)); | |
163 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); | |
164 | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
165 if (credentials_.size() != 0) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
166 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
167 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_USERPWD, credentials_.c_str())); |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
168 } |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
169 |
0 | 170 switch (method_) |
171 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
172 case HttpMethod_Get: |
0 | 173 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); |
174 break; | |
175 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
176 case HttpMethod_Post: |
0 | 177 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L)); |
178 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); | |
179 | |
180 if (postData_.size() > 0) | |
181 { | |
182 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str())); | |
183 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size())); | |
184 } | |
185 else | |
186 { | |
187 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); | |
188 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0)); | |
189 } | |
190 | |
191 break; | |
192 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
193 case HttpMethod_Delete: |
0 | 194 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L)); |
195 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE")); | |
196 break; | |
197 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
198 case HttpMethod_Put: |
0 | 199 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); |
200 break; | |
201 | |
202 default: | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
203 throw OrthancException(ErrorCode_InternalError); |
0 | 204 } |
205 | |
206 // Do the actual request | |
207 CheckCode(curl_easy_perform(pimpl_->curl_)); | |
208 | |
209 long status; | |
210 CheckCode(curl_easy_getinfo(pimpl_->curl_, CURLINFO_RESPONSE_CODE, &status)); | |
211 | |
212 if (status == 0) | |
213 { | |
214 // This corresponds to a call to an inexistent host | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
215 lastStatus_ = HttpStatus_500_InternalServerError; |
0 | 216 } |
217 else | |
218 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
219 lastStatus_ = static_cast<HttpStatus>(status); |
0 | 220 } |
221 | |
222 return (status >= 200 && status < 300); | |
223 } | |
224 | |
225 | |
226 bool HttpClient::Apply(Json::Value& answer) | |
227 { | |
228 std::string s; | |
229 if (Apply(s)) | |
230 { | |
231 Json::Reader reader; | |
232 return reader.parse(s, answer); | |
233 } | |
234 else | |
235 { | |
236 return false; | |
237 } | |
238 } | |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
239 |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
240 |
468
456b9d2e9af4
rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
457
diff
changeset
|
241 void HttpClient::SetCredentials(const char* username, |
456b9d2e9af4
rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
457
diff
changeset
|
242 const char* password) |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
243 { |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
244 credentials_ = std::string(username) + ":" + std::string(password); |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
245 } |
457 | 246 |
247 | |
248 void HttpClient::GlobalInitialize() | |
249 { | |
250 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT)); | |
251 } | |
252 | |
253 void HttpClient::GlobalFinalize() | |
254 { | |
255 curl_global_cleanup(); | |
256 } | |
0 | 257 } |