Mercurial > hg > orthanc
comparison 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 |
comparison
equal
deleted
inserted
replaced
468:456b9d2e9af4 | 469:a6fe16a31615 |
---|---|
42 | 42 |
43 static CURLcode CheckCode(CURLcode code) | 43 static CURLcode CheckCode(CURLcode code) |
44 { | 44 { |
45 if (code != CURLE_OK) | 45 if (code != CURLE_OK) |
46 { | 46 { |
47 printf("ICI: %s\n", curl_easy_strerror(code)); | 47 //printf("ICI: %s\n", curl_easy_strerror(code)); |
48 throw HttpException("CURL: " + std::string(curl_easy_strerror(code))); | 48 throw HttpException("CURL: " + std::string(curl_easy_strerror(code))); |
49 } | 49 } |
50 | 50 |
51 return code; | 51 return code; |
52 } | 52 } |
67 | 67 |
68 return length; | 68 return length; |
69 } | 69 } |
70 | 70 |
71 | 71 |
72 HttpClient::HttpClient() : pimpl_(new PImpl) | 72 void HttpClient::Setup() |
73 { | 73 { |
74 pimpl_->postHeaders_ = NULL; | 74 pimpl_->postHeaders_ = NULL; |
75 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) | 75 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) |
76 { | 76 { |
77 throw HttpException("HttpClient: Not enough memory"); | 77 throw HttpException("HttpClient: Not enough memory"); |
102 lastStatus_ = Orthanc_HttpStatus_200_Ok; | 102 lastStatus_ = Orthanc_HttpStatus_200_Ok; |
103 isVerbose_ = false; | 103 isVerbose_ = false; |
104 } | 104 } |
105 | 105 |
106 | 106 |
107 HttpClient::HttpClient() : pimpl_(new PImpl) | |
108 { | |
109 Setup(); | |
110 } | |
111 | |
112 | |
113 HttpClient::HttpClient(const HttpClient& other) : pimpl_(new PImpl) | |
114 { | |
115 Setup(); | |
116 | |
117 if (other.IsVerbose()) | |
118 { | |
119 SetVerbose(true); | |
120 } | |
121 | |
122 if (other.credentials_.size() != 0) | |
123 { | |
124 credentials_ = other.credentials_; | |
125 } | |
126 } | |
127 | |
128 | |
107 HttpClient::~HttpClient() | 129 HttpClient::~HttpClient() |
108 { | 130 { |
109 curl_easy_cleanup(pimpl_->curl_); | 131 curl_easy_cleanup(pimpl_->curl_); |
110 curl_slist_free_all(pimpl_->postHeaders_); | 132 curl_slist_free_all(pimpl_->postHeaders_); |
111 } | 133 } |
130 { | 152 { |
131 answer.clear(); | 153 answer.clear(); |
132 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str())); | 154 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str())); |
133 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer)); | 155 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer)); |
134 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); | 156 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); |
157 | |
158 if (credentials_.size() != 0) | |
159 { | |
160 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_USERPWD, credentials_.c_str())); | |
161 } | |
135 | 162 |
136 switch (method_) | 163 switch (method_) |
137 { | 164 { |
138 case Orthanc_HttpMethod_Get: | 165 case Orthanc_HttpMethod_Get: |
139 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); | 166 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); |
205 | 232 |
206 | 233 |
207 void HttpClient::SetCredentials(const char* username, | 234 void HttpClient::SetCredentials(const char* username, |
208 const char* password) | 235 const char* password) |
209 { | 236 { |
210 std::string s = std::string(username) + ":" + std::string(password); | 237 credentials_ = std::string(username) + ":" + std::string(password); |
211 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_USERPWD, s.c_str())); | 238 } |
212 } | |
213 | |
214 | 239 |
215 | 240 |
216 void HttpClient::GlobalInitialize() | 241 void HttpClient::GlobalInitialize() |
217 { | 242 { |
218 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT)); | 243 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT)); |