Mercurial > hg > orthanc
comparison Core/HttpClient.cpp @ 1986:99b249867052
HTTP headers in HttpClient
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 25 Apr 2016 17:23:07 +0200 |
parents | 7bd4cb780feb |
children | ce90d109bb64 |
comparison
equal
deleted
inserted
replaced
1985:3fcea6dc151d | 1986:99b249867052 |
---|---|
78 namespace Orthanc | 78 namespace Orthanc |
79 { | 79 { |
80 struct HttpClient::PImpl | 80 struct HttpClient::PImpl |
81 { | 81 { |
82 CURL* curl_; | 82 CURL* curl_; |
83 struct curl_slist *postHeaders_; | 83 struct curl_slist *defaultPostHeaders_; |
84 struct curl_slist *userHeaders_; | |
84 }; | 85 }; |
85 | 86 |
86 | 87 |
87 static void ThrowException(HttpStatus status) | 88 static void ThrowException(HttpStatus status) |
88 { | 89 { |
133 } | 134 } |
134 | 135 |
135 | 136 |
136 void HttpClient::Setup() | 137 void HttpClient::Setup() |
137 { | 138 { |
138 pimpl_->postHeaders_ = NULL; | 139 pimpl_->userHeaders_ = NULL; |
139 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) | 140 pimpl_->defaultPostHeaders_ = NULL; |
141 if ((pimpl_->defaultPostHeaders_ = curl_slist_append(pimpl_->defaultPostHeaders_, "Expect:")) == NULL) | |
140 { | 142 { |
141 throw OrthancException(ErrorCode_NotEnoughMemory); | 143 throw OrthancException(ErrorCode_NotEnoughMemory); |
142 } | 144 } |
143 | 145 |
144 pimpl_->curl_ = curl_easy_init(); | 146 pimpl_->curl_ = curl_easy_init(); |
145 if (!pimpl_->curl_) | 147 if (!pimpl_->curl_) |
146 { | 148 { |
147 curl_slist_free_all(pimpl_->postHeaders_); | 149 curl_slist_free_all(pimpl_->defaultPostHeaders_); |
148 throw OrthancException(ErrorCode_NotEnoughMemory); | 150 throw OrthancException(ErrorCode_NotEnoughMemory); |
149 } | 151 } |
150 | 152 |
151 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback)); | 153 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback)); |
152 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0)); | 154 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0)); |
189 | 191 |
190 | 192 |
191 HttpClient::~HttpClient() | 193 HttpClient::~HttpClient() |
192 { | 194 { |
193 curl_easy_cleanup(pimpl_->curl_); | 195 curl_easy_cleanup(pimpl_->curl_); |
194 curl_slist_free_all(pimpl_->postHeaders_); | 196 curl_slist_free_all(pimpl_->defaultPostHeaders_); |
197 ClearHeaders(); | |
195 } | 198 } |
196 | 199 |
197 | 200 |
198 void HttpClient::SetVerbose(bool isVerbose) | 201 void HttpClient::SetVerbose(bool isVerbose) |
199 { | 202 { |
204 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1)); | 207 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1)); |
205 } | 208 } |
206 else | 209 else |
207 { | 210 { |
208 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0)); | 211 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0)); |
212 } | |
213 } | |
214 | |
215 | |
216 void HttpClient::AddHeader(const std::string& key, | |
217 const std::string& value) | |
218 { | |
219 if (key.empty()) | |
220 { | |
221 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
222 } | |
223 | |
224 std::string s = key + ": " + value; | |
225 | |
226 if ((pimpl_->userHeaders_ = curl_slist_append(pimpl_->userHeaders_, s.c_str())) == NULL) | |
227 { | |
228 throw OrthancException(ErrorCode_NotEnoughMemory); | |
229 } | |
230 } | |
231 | |
232 | |
233 void HttpClient::ClearHeaders() | |
234 { | |
235 if (pimpl_->userHeaders_ != NULL) | |
236 { | |
237 curl_slist_free_all(pimpl_->userHeaders_); | |
238 pimpl_->userHeaders_ = NULL; | |
209 } | 239 } |
210 } | 240 } |
211 | 241 |
212 | 242 |
213 bool HttpClient::Apply(std::string& answer) | 243 bool HttpClient::Apply(std::string& answer) |
230 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 0)); | 260 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 0)); |
231 } | 261 } |
232 #endif | 262 #endif |
233 | 263 |
234 // Reset the parameters from previous calls to Apply() | 264 // Reset the parameters from previous calls to Apply() |
235 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); | 265 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->userHeaders_)); |
236 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L)); | 266 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L)); |
237 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L)); | 267 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L)); |
238 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L)); | 268 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L)); |
239 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, NULL)); | 269 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, NULL)); |
240 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); | 270 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); |
269 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); | 299 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); |
270 break; | 300 break; |
271 | 301 |
272 case HttpMethod_Post: | 302 case HttpMethod_Post: |
273 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L)); | 303 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L)); |
274 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); | 304 |
305 if (pimpl_->userHeaders_ == NULL) | |
306 { | |
307 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->defaultPostHeaders_)); | |
308 } | |
309 | |
275 break; | 310 break; |
276 | 311 |
277 case HttpMethod_Delete: | 312 case HttpMethod_Delete: |
278 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L)); | 313 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L)); |
279 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE")); | 314 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE")); |
284 // CURLOPT_PUT if there is a body | 319 // CURLOPT_PUT if there is a body |
285 | 320 |
286 // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); | 321 // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); |
287 | 322 |
288 curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */ | 323 curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */ |
289 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); | 324 |
325 if (pimpl_->userHeaders_ == NULL) | |
326 { | |
327 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->defaultPostHeaders_)); | |
328 } | |
329 | |
290 break; | 330 break; |
291 | 331 |
292 default: | 332 default: |
293 throw OrthancException(ErrorCode_InternalError); | 333 throw OrthancException(ErrorCode_InternalError); |
294 } | 334 } |