Mercurial > hg > orthanc
annotate OrthancCppClient/HttpClient.cpp @ 460:339067c6bdb1
thanks ryan
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jul 2013 15:09:05 +0200 |
parents | 997282a61ff8 |
children | 456b9d2e9af4 |
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 { | |
29
042ac60f5bf9
simplified build of curl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
26
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 | |
72 HttpClient::HttpClient() : pimpl_(new PImpl) | |
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 | |
107 HttpClient::~HttpClient() | |
108 { | |
109 curl_easy_cleanup(pimpl_->curl_); | |
110 curl_slist_free_all(pimpl_->postHeaders_); | |
111 } | |
112 | |
113 | |
114 void HttpClient::SetVerbose(bool isVerbose) | |
115 { | |
116 isVerbose_ = isVerbose; | |
117 | |
118 if (isVerbose_) | |
119 { | |
120 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1)); | |
121 } | |
122 else | |
123 { | |
124 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0)); | |
125 } | |
126 } | |
127 | |
128 | |
129 bool HttpClient::Apply(std::string& answer) | |
130 { | |
131 answer.clear(); | |
132 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str())); | |
133 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer)); | |
134 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); | |
135 | |
136 switch (method_) | |
137 { | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
138 case Orthanc_HttpMethod_Get: |
0 | 139 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L)); |
140 break; | |
141 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
142 case Orthanc_HttpMethod_Post: |
0 | 143 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L)); |
144 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); | |
145 | |
146 if (postData_.size() > 0) | |
147 { | |
148 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str())); | |
149 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size())); | |
150 } | |
151 else | |
152 { | |
153 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); | |
154 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0)); | |
155 } | |
156 | |
157 break; | |
158 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
159 case Orthanc_HttpMethod_Delete: |
0 | 160 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L)); |
161 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE")); | |
162 break; | |
163 | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
164 case Orthanc_HttpMethod_Put: |
0 | 165 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); |
166 break; | |
167 | |
168 default: | |
169 throw HttpException("HttpClient: Internal error"); | |
170 } | |
171 | |
172 // Do the actual request | |
173 CheckCode(curl_easy_perform(pimpl_->curl_)); | |
174 | |
175 long status; | |
176 CheckCode(curl_easy_getinfo(pimpl_->curl_, CURLINFO_RESPONSE_CODE, &status)); | |
177 | |
178 if (status == 0) | |
179 { | |
180 // This corresponds to a call to an inexistent host | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
181 lastStatus_ = Orthanc_HttpStatus_500_InternalServerError; |
0 | 182 } |
183 else | |
184 { | |
60
77aec9be0a51
renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
185 lastStatus_ = static_cast<Orthanc_HttpStatus>(status); |
0 | 186 } |
187 | |
188 return (status >= 200 && status < 300); | |
189 } | |
190 | |
191 | |
192 bool HttpClient::Apply(Json::Value& answer) | |
193 { | |
194 std::string s; | |
195 if (Apply(s)) | |
196 { | |
197 Json::Reader reader; | |
198 return reader.parse(s, answer); | |
199 } | |
200 else | |
201 { | |
202 return false; | |
203 } | |
204 } | |
144
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
205 |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
206 |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
207 void HttpClient::SetPassword(const char* username, |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
208 const char* password) |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
209 { |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
210 std::string s = std::string(username) + ":" + std::string(password); |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
211 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_USERPWD, s.c_str())); |
aa6c8a942952
http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
60
diff
changeset
|
212 } |
457 | 213 |
214 | |
215 | |
216 void HttpClient::GlobalInitialize() | |
217 { | |
218 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT)); | |
219 } | |
220 | |
221 void HttpClient::GlobalFinalize() | |
222 { | |
223 curl_global_cleanup(); | |
224 } | |
0 | 225 } |