comparison OrthancCppClient/HttpClient.cpp @ 475:72cca077abf8

removal of HttpException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 15 Jul 2013 17:32:47 +0200
parents c9a5d72f8481
children
comparison
equal deleted inserted replaced
474:a9693dc7089c 475:72cca077abf8
25 **/ 25 **/
26 26
27 27
28 #include "HttpClient.h" 28 #include "HttpClient.h"
29 29
30 #include "../Core/Toolbox.h"
31 #include "../Core/OrthancException.h"
32
30 #include <string.h> 33 #include <string.h>
31 #include <curl/curl.h> 34 #include <curl/curl.h>
32 35
33 36
34 namespace Orthanc 37 namespace Orthanc
42 45
43 static CURLcode CheckCode(CURLcode code) 46 static CURLcode CheckCode(CURLcode code)
44 { 47 {
45 if (code != CURLE_OK) 48 if (code != CURLE_OK)
46 { 49 {
47 //printf("ICI: %s\n", curl_easy_strerror(code)); 50 throw OrthancException("libCURL error: " + std::string(curl_easy_strerror(code)));
48 throw HttpException("CURL: " + std::string(curl_easy_strerror(code)));
49 } 51 }
50 52
51 return code; 53 return code;
52 } 54 }
53 55
72 void HttpClient::Setup() 74 void HttpClient::Setup()
73 { 75 {
74 pimpl_->postHeaders_ = NULL; 76 pimpl_->postHeaders_ = NULL;
75 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) 77 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL)
76 { 78 {
77 throw HttpException("HttpClient: Not enough memory"); 79 throw OrthancException(ErrorCode_NotEnoughMemory);
78 } 80 }
79 81
80 pimpl_->curl_ = curl_easy_init(); 82 pimpl_->curl_ = curl_easy_init();
81 if (!pimpl_->curl_) 83 if (!pimpl_->curl_)
82 { 84 {
83 curl_slist_free_all(pimpl_->postHeaders_); 85 curl_slist_free_all(pimpl_->postHeaders_);
84 throw HttpException("HttpClient: Not enough memory"); 86 throw OrthancException(ErrorCode_NotEnoughMemory);
85 } 87 }
86 88
87 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback)); 89 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback));
88 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0)); 90 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0));
89 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1)); 91 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1));
191 case HttpMethod_Put: 193 case HttpMethod_Put:
192 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); 194 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L));
193 break; 195 break;
194 196
195 default: 197 default:
196 throw HttpException("HttpClient: Internal error"); 198 throw OrthancException(ErrorCode_InternalError);
197 } 199 }
198 200
199 // Do the actual request 201 // Do the actual request
200 CheckCode(curl_easy_perform(pimpl_->curl_)); 202 CheckCode(curl_easy_perform(pimpl_->curl_));
201 203
245 247
246 void HttpClient::GlobalFinalize() 248 void HttpClient::GlobalFinalize()
247 { 249 {
248 curl_global_cleanup(); 250 curl_global_cleanup();
249 } 251 }
252
253 const char* HttpClient::GetLastStatusText() const
254 {
255 return Toolbox::ToString(lastStatus_);
256 }
250 } 257 }