Mercurial > hg > orthanc
annotate OrthancCppClient/HttpClient.cpp @ 469:a6fe16a31615
transmitting credentials by copy
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 12 Jul 2013 15:36:59 +0200 |
parents | 456b9d2e9af4 |
children | c9a5d72f8481 |
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 |
400 | 3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege, |
0 | 4 * Belgium |
5 * | |
6 * Permission is hereby granted, free of charge, to any person | |
7 * obtaining a copy of this software and associated documentation | |
8 * files (the "Software"), to deal in the Software without | |
9 * restriction, including without limitation the rights to use, copy, | |
10 * modify, merge, publish, distribute, sublicense, and/or sell copies | |
11 * of the Software, and to permit persons to whom the Software is | |
12 * furnished to do so, subject to the following conditions: | |
13 * | |
14 * The above copyright notice and this permission notice shall be | |
15 * included in all copies or substantial portions of the Software. | |
16 * | |
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 * SOFTWARE. | |
25 **/ | |
26 | |
27 | |
28 #include "HttpClient.h" | |
29 | |
30 #include <string.h> | |
31 #include <curl/curl.h> | |
32 | |
33 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
34 namespace Orthanc |
0 | 35 { |
36 struct HttpClient::PImpl | |
37 { | |
38 CURL* curl_; | |
39 struct curl_slist *postHeaders_; | |
40 }; | |
41 | |
42 | |
43 static CURLcode CheckCode(CURLcode code) | |
44 { | |
45 if (code != CURLE_OK) | |
46 { | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
47 //printf("ICI: %s\n", curl_easy_strerror(code)); |
0 | 48 throw HttpException("CURL: " + std::string(curl_easy_strerror(code))); |
49 } | |
50 | |
51 return code; | |
52 } | |
53 | |
54 | |
55 static size_t CurlCallback(void *buffer, size_t size, size_t nmemb, void *payload) | |
56 { | |
57 std::string& target = *(static_cast<std::string*>(payload)); | |
58 | |
59 size_t length = size * nmemb; | |
60 if (length == 0) | |
61 return 0; | |
62 | |
63 size_t pos = target.size(); | |
64 | |
65 target.resize(pos + length); | |
66 memcpy(&target.at(pos), buffer, length); | |
67 | |
68 return length; | |
69 } | |
70 | |
71 | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
72 void HttpClient::Setup() |
0 | 73 { |
74 pimpl_->postHeaders_ = NULL; | |
75 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) | |
76 { | |
77 throw HttpException("HttpClient: Not enough memory"); | |
78 } | |
79 | |
80 pimpl_->curl_ = curl_easy_init(); | |
81 if (!pimpl_->curl_) | |
82 { | |
83 curl_slist_free_all(pimpl_->postHeaders_); | |
84 throw HttpException("HttpClient: Not enough memory"); | |
85 } | |
86 | |
87 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback)); | |
88 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0)); | |
89 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1)); | |
90 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
91 #if ORTHANC_SSL_ENABLED == 1 |
26 | 92 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 0)); |
93 #endif | |
94 | |
456 | 95 // This fixes the "longjmp causes uninitialized stack frame" crash |
96 // that happens on modern Linux versions. | |
97 // http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame | |
98 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOSIGNAL, 1)); | |
99 | |
0 | 100 url_ = ""; |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
101 method_ = Orthanc_HttpMethod_Get; |
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
102 lastStatus_ = Orthanc_HttpStatus_200_Ok; |
0 | 103 isVerbose_ = false; |
104 } | |
105 | |
106 | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
107 HttpClient::HttpClient() : pimpl_(new PImpl) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
108 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
109 Setup(); |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
110 } |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
111 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
112 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
113 HttpClient::HttpClient(const HttpClient& other) : pimpl_(new PImpl) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
114 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
115 Setup(); |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
116 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
117 if (other.IsVerbose()) |
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 SetVerbose(true); |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
120 } |
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 if (other.credentials_.size() != 0) |
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 credentials_ = other.credentials_; |
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 } |
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 |
0 | 129 HttpClient::~HttpClient() |
130 { | |
131 curl_easy_cleanup(pimpl_->curl_); | |
132 curl_slist_free_all(pimpl_->postHeaders_); | |
133 } | |
134 | |
135 | |
136 void HttpClient::SetVerbose(bool isVerbose) | |
137 { | |
138 isVerbose_ = isVerbose; | |
139 | |
140 if (isVerbose_) | |
141 { | |
142 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1)); | |
143 } | |
144 else | |
145 { | |
146 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0)); | |
147 } | |
148 } | |
149 | |
150 | |
151 bool HttpClient::Apply(std::string& answer) | |
152 { | |
153 answer.clear(); | |
154 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str())); | |
155 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer)); | |
156 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); | |
157 | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
158 if (credentials_.size() != 0) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
159 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
160 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
|
161 } |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
162 |
0 | 163 switch (method_) |
164 { | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
165 case Orthanc_HttpMethod_Get: |
0 | 166 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); |
167 break; | |
168 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
169 case Orthanc_HttpMethod_Post: |
0 | 170 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L)); |
171 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); | |
172 | |
173 if (postData_.size() > 0) | |
174 { | |
175 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str())); | |
176 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size())); | |
177 } | |
178 else | |
179 { | |
180 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); | |
181 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0)); | |
182 } | |
183 | |
184 break; | |
185 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
186 case Orthanc_HttpMethod_Delete: |
0 | 187 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L)); |
188 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE")); | |
189 break; | |
190 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
191 case Orthanc_HttpMethod_Put: |
0 | 192 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); |
193 break; | |
194 | |
195 default: | |
196 throw HttpException("HttpClient: Internal error"); | |
197 } | |
198 | |
199 // Do the actual request | |
200 CheckCode(curl_easy_perform(pimpl_->curl_)); | |
201 | |
202 long status; | |
203 CheckCode(curl_easy_getinfo(pimpl_->curl_, CURLINFO_RESPONSE_CODE, &status)); | |
204 | |
205 if (status == 0) | |
206 { | |
207 // This corresponds to a call to an inexistent host | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
208 lastStatus_ = Orthanc_HttpStatus_500_InternalServerError; |
0 | 209 } |
210 else | |
211 { | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
212 lastStatus_ = static_cast<Orthanc_HttpStatus>(status); |
0 | 213 } |
214 | |
215 return (status >= 200 && status < 300); | |
216 } | |
217 | |
218 | |
219 bool HttpClient::Apply(Json::Value& answer) | |
220 { | |
221 std::string s; | |
222 if (Apply(s)) | |
223 { | |
224 Json::Reader reader; | |
225 return reader.parse(s, answer); | |
226 } | |
227 else | |
228 { | |
229 return false; | |
230 } | |
231 } | |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
232 |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
233 |
468
456b9d2e9af4
rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
457
diff
changeset
|
234 void HttpClient::SetCredentials(const char* username, |
456b9d2e9af4
rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
457
diff
changeset
|
235 const char* password) |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
236 { |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
237 credentials_ = std::string(username) + ":" + std::string(password); |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
238 } |
457 | 239 |
240 | |
241 void HttpClient::GlobalInitialize() | |
242 { | |
243 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT)); | |
244 } | |
245 | |
246 void HttpClient::GlobalFinalize() | |
247 { | |
248 curl_global_cleanup(); | |
249 } | |
0 | 250 } |