Mercurial > hg > orthanc
annotate Core/HttpClient.cpp @ 1064:cd20e2568fc2
fix tests
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Jul 2014 16:13:36 +0200 |
parents | cc4ff680e2a0 |
children | b17b6bd59747 |
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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
33 #include "PrecompiledHeaders.h" |
0 | 34 #include "HttpClient.h" |
35 | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
36 #include "../Core/Toolbox.h" |
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
37 #include "../Core/OrthancException.h" |
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
38 |
0 | 39 #include <string.h> |
40 #include <curl/curl.h> | |
41 | |
42 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
43 namespace Orthanc |
0 | 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 { | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
56 throw OrthancException("libCURL error: " + std::string(curl_easy_strerror(code))); |
0 | 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 | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
80 void HttpClient::Setup() |
0 | 81 { |
82 pimpl_->postHeaders_ = NULL; | |
83 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) | |
84 { | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
85 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 86 } |
87 | |
88 pimpl_->curl_ = curl_easy_init(); | |
89 if (!pimpl_->curl_) | |
90 { | |
91 curl_slist_free_all(pimpl_->postHeaders_); | |
475
72cca077abf8
removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
473
diff
changeset
|
92 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 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 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
99 #if ORTHANC_SSL_ENABLED == 1 |
26 | 100 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 0)); |
101 #endif | |
102 | |
456 | 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 | |
0 | 108 url_ = ""; |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
109 method_ = HttpMethod_Get; |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
110 lastStatus_ = HttpStatus_200_Ok; |
0 | 111 isVerbose_ = false; |
112 } | |
113 | |
114 | |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
115 HttpClient::HttpClient() : pimpl_(new PImpl) |
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 Setup(); |
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 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
121 HttpClient::HttpClient(const HttpClient& other) : pimpl_(new PImpl) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
122 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
123 Setup(); |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
124 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
125 if (other.IsVerbose()) |
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 SetVerbose(true); |
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 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
130 if (other.credentials_.size() != 0) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
131 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
132 credentials_ = other.credentials_; |
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 |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
136 |
0 | 137 HttpClient::~HttpClient() |
138 { | |
139 curl_easy_cleanup(pimpl_->curl_); | |
140 curl_slist_free_all(pimpl_->postHeaders_); | |
141 } | |
142 | |
143 | |
144 void HttpClient::SetVerbose(bool isVerbose) | |
145 { | |
146 isVerbose_ = isVerbose; | |
147 | |
148 if (isVerbose_) | |
149 { | |
150 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1)); | |
151 } | |
152 else | |
153 { | |
154 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0)); | |
155 } | |
156 } | |
157 | |
158 | |
159 bool HttpClient::Apply(std::string& answer) | |
160 { | |
161 answer.clear(); | |
162 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str())); | |
163 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer)); | |
1052
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
164 |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
165 // Reset the parameters from previous calls to Apply() |
0 | 166 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); |
1052
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
167 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
168 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
169 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
170 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, NULL)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
171 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
172 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0)); |
0 | 173 |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
174 if (credentials_.size() != 0) |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
175 { |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
176 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
|
177 } |
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
178 |
0 | 179 switch (method_) |
180 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
181 case HttpMethod_Get: |
0 | 182 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); |
183 break; | |
184 | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
185 case HttpMethod_Post: |
0 | 186 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L)); |
187 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); | |
1052
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
188 break; |
0 | 189 |
1052
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
190 case HttpMethod_Delete: |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
191 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
192 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE")); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
193 break; |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
194 |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
195 case HttpMethod_Put: |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
196 // http://stackoverflow.com/a/7570281/881731: Don't use |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
197 // CURLOPT_PUT if there is a body |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
198 |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
199 // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
200 |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
201 curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */ |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
202 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
203 break; |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
204 |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
205 default: |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
206 throw OrthancException(ErrorCode_InternalError); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
207 } |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
208 |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
209 |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
210 if (method_ == HttpMethod_Post || |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
211 method_ == HttpMethod_Put) |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
212 { |
0 | 213 if (postData_.size() > 0) |
214 { | |
215 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str())); | |
216 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size())); | |
217 } | |
218 else | |
219 { | |
220 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); | |
221 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0)); | |
222 } | |
1052
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
223 } |
0 | 224 |
225 | |
226 // Do the actual request | |
227 CheckCode(curl_easy_perform(pimpl_->curl_)); | |
228 | |
229 long status; | |
230 CheckCode(curl_easy_getinfo(pimpl_->curl_, CURLINFO_RESPONSE_CODE, &status)); | |
231 | |
232 if (status == 0) | |
233 { | |
234 // 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
|
235 lastStatus_ = HttpStatus_500_InternalServerError; |
0 | 236 } |
237 else | |
238 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
469
diff
changeset
|
239 lastStatus_ = static_cast<HttpStatus>(status); |
0 | 240 } |
241 | |
242 return (status >= 200 && status < 300); | |
243 } | |
244 | |
245 | |
246 bool HttpClient::Apply(Json::Value& answer) | |
247 { | |
248 std::string s; | |
249 if (Apply(s)) | |
250 { | |
251 Json::Reader reader; | |
252 return reader.parse(s, answer); | |
253 } | |
254 else | |
255 { | |
256 return false; | |
257 } | |
258 } | |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
259 |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
260 |
468
456b9d2e9af4
rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
457
diff
changeset
|
261 void HttpClient::SetCredentials(const char* username, |
456b9d2e9af4
rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
457
diff
changeset
|
262 const char* password) |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
263 { |
469
a6fe16a31615
transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
468
diff
changeset
|
264 credentials_ = std::string(username) + ":" + std::string(password); |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
265 } |
457 | 266 |
267 | |
268 void HttpClient::GlobalInitialize() | |
269 { | |
270 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT)); | |
271 } | |
272 | |
273 void HttpClient::GlobalFinalize() | |
274 { | |
275 curl_global_cleanup(); | |
276 } | |
0 | 277 } |