comparison PalantirCppClient/HttpClient.cpp @ 0:3959d33612cc

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Jul 2012 14:32:22 +0200
parents
children 6ba765ecf3db
comparison
equal deleted inserted replaced
-1:000000000000 0:3959d33612cc
1 /**
2 * Palantir - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
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
34 namespace Palantir
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 {
47 throw HttpException("CURL: " + std::string(curl_easy_strerror(code)));
48 }
49
50 return code;
51 }
52
53
54 static size_t CurlCallback(void *buffer, size_t size, size_t nmemb, void *payload)
55 {
56 std::string& target = *(static_cast<std::string*>(payload));
57
58 size_t length = size * nmemb;
59 if (length == 0)
60 return 0;
61
62 size_t pos = target.size();
63
64 target.resize(pos + length);
65 memcpy(&target.at(pos), buffer, length);
66
67 return length;
68 }
69
70
71 HttpClient::HttpClient() : pimpl_(new PImpl)
72 {
73 pimpl_->postHeaders_ = NULL;
74 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL)
75 {
76 throw HttpException("HttpClient: Not enough memory");
77 }
78
79 pimpl_->curl_ = curl_easy_init();
80 if (!pimpl_->curl_)
81 {
82 curl_slist_free_all(pimpl_->postHeaders_);
83 throw HttpException("HttpClient: Not enough memory");
84 }
85
86 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback));
87 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0));
88 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1));
89
90 url_ = "";
91 method_ = HttpMethod_Get;
92 lastStatus_ = HttpStatus_200_Ok;
93 isVerbose_ = false;
94 }
95
96
97 HttpClient::~HttpClient()
98 {
99 curl_easy_cleanup(pimpl_->curl_);
100 curl_slist_free_all(pimpl_->postHeaders_);
101 }
102
103
104 void HttpClient::SetVerbose(bool isVerbose)
105 {
106 isVerbose_ = isVerbose;
107
108 if (isVerbose_)
109 {
110 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1));
111 }
112 else
113 {
114 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0));
115 }
116 }
117
118
119 bool HttpClient::Apply(std::string& answer)
120 {
121 answer.clear();
122 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str()));
123 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer));
124 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL));
125
126 switch (method_)
127 {
128 case HttpMethod_Get:
129 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L));
130 break;
131
132 case HttpMethod_Post:
133 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L));
134 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_));
135
136 if (postData_.size() > 0)
137 {
138 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str()));
139 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size()));
140 }
141 else
142 {
143 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
144 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0));
145 }
146
147 break;
148
149 case HttpMethod_Delete:
150 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L));
151 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE"));
152 break;
153
154 case HttpMethod_Put:
155 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L));
156 break;
157
158 default:
159 throw HttpException("HttpClient: Internal error");
160 }
161
162 // Do the actual request
163 CheckCode(curl_easy_perform(pimpl_->curl_));
164
165 long status;
166 CheckCode(curl_easy_getinfo(pimpl_->curl_, CURLINFO_RESPONSE_CODE, &status));
167
168 if (status == 0)
169 {
170 // This corresponds to a call to an inexistent host
171 lastStatus_ = HttpStatus_500_InternalServerError;
172 }
173 else
174 {
175 lastStatus_ = static_cast<HttpStatus>(status);
176 }
177
178 return (status >= 200 && status < 300);
179 }
180
181
182 bool HttpClient::Apply(Json::Value& answer)
183 {
184 std::string s;
185 if (Apply(s))
186 {
187 Json::Reader reader;
188 return reader.parse(s, answer);
189 }
190 else
191 {
192 return false;
193 }
194 }
195 }