diff options
author | Jacek Antonelli | 2008-08-15 23:44:50 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:50 -0500 |
commit | 89fe5dab825a62a0e3fd8d248cbc91c65eb2a426 (patch) | |
tree | bcff14b7888d04a2fec799c59369f6095224bd08 /linden/indra/llmessage/llhttpclient.cpp | |
parent | Second Life viewer sources 1.13.3.2 (diff) | |
download | meta-impy-89fe5dab825a62a0e3fd8d248cbc91c65eb2a426.zip meta-impy-89fe5dab825a62a0e3fd8d248cbc91c65eb2a426.tar.gz meta-impy-89fe5dab825a62a0e3fd8d248cbc91c65eb2a426.tar.bz2 meta-impy-89fe5dab825a62a0e3fd8d248cbc91c65eb2a426.tar.xz |
Second Life viewer sources 1.14.0.0
Diffstat (limited to 'linden/indra/llmessage/llhttpclient.cpp')
-rw-r--r-- | linden/indra/llmessage/llhttpclient.cpp | 107 |
1 files changed, 106 insertions, 1 deletions
diff --git a/linden/indra/llmessage/llhttpclient.cpp b/linden/indra/llmessage/llhttpclient.cpp index d94918e..3755ae0 100644 --- a/linden/indra/llmessage/llhttpclient.cpp +++ b/linden/indra/llmessage/llhttpclient.cpp | |||
@@ -37,6 +37,8 @@ | |||
37 | #include "llvfile.h" | 37 | #include "llvfile.h" |
38 | #include "llvfs.h" | 38 | #include "llvfs.h" |
39 | 39 | ||
40 | #include <curl/curl.h> | ||
41 | |||
40 | static const F32 HTTP_REQUEST_EXPIRY_SECS = 60.0f; | 42 | static const F32 HTTP_REQUEST_EXPIRY_SECS = 60.0f; |
41 | 43 | ||
42 | static std::string gCABundle; | 44 | static std::string gCABundle; |
@@ -148,6 +150,27 @@ namespace | |||
148 | 150 | ||
149 | const LLSD mSD; | 151 | const LLSD mSD; |
150 | }; | 152 | }; |
153 | |||
154 | class RawInjector : public Injector | ||
155 | { | ||
156 | public: | ||
157 | RawInjector(const U8* data, S32 size) : mData(data), mSize(size) {} | ||
158 | virtual ~RawInjector() {} | ||
159 | |||
160 | const char* contentType() { return "application/octet-stream"; } | ||
161 | |||
162 | virtual EStatus process_impl(const LLChannelDescriptors& channels, | ||
163 | buffer_ptr_t& buffer, bool& eos, LLSD& context, LLPumpIO* pump) | ||
164 | { | ||
165 | LLBufferStream ostream(channels, buffer.get()); | ||
166 | ostream.write((const char *)mData, mSize); // hopefully chars are always U8s | ||
167 | eos = true; | ||
168 | return STATUS_DONE; | ||
169 | } | ||
170 | |||
171 | const U8* mData; | ||
172 | S32 mSize; | ||
173 | }; | ||
151 | 174 | ||
152 | class FileInjector : public Injector | 175 | class FileInjector : public Injector |
153 | { | 176 | { |
@@ -243,6 +266,84 @@ void LLHTTPClient::get(const std::string& url, ResponderPtr responder) | |||
243 | request(url, LLURLRequest::HTTP_GET, NULL, responder); | 266 | request(url, LLURLRequest::HTTP_GET, NULL, responder); |
244 | } | 267 | } |
245 | 268 | ||
269 | // A simple class for managing data returned from a curl http request. | ||
270 | class LLHTTPBuffer | ||
271 | { | ||
272 | public: | ||
273 | LLHTTPBuffer() { } | ||
274 | |||
275 | static size_t curl_write( void *ptr, size_t size, size_t nmemb, void *user_data) | ||
276 | { | ||
277 | LLHTTPBuffer* self = (LLHTTPBuffer*)user_data; | ||
278 | |||
279 | size_t bytes = (size * nmemb); | ||
280 | self->mBuffer.append((char*)ptr,bytes); | ||
281 | return nmemb; | ||
282 | } | ||
283 | |||
284 | LLSD asLLSD() | ||
285 | { | ||
286 | LLSD content; | ||
287 | |||
288 | if (mBuffer.empty()) return content; | ||
289 | |||
290 | std::istringstream istr(mBuffer); | ||
291 | LLSDSerialize::fromXML(content, istr); | ||
292 | return content; | ||
293 | } | ||
294 | |||
295 | std::string asString() | ||
296 | { | ||
297 | return mBuffer; | ||
298 | } | ||
299 | |||
300 | private: | ||
301 | std::string mBuffer; | ||
302 | }; | ||
303 | |||
304 | // This call is blocking! This is probably usually bad. :( | ||
305 | LLSD LLHTTPClient::blockingGet(const std::string& url) | ||
306 | { | ||
307 | llinfos << "blockingGet of " << url << llendl; | ||
308 | |||
309 | // Returns an LLSD map: {status: integer, body: map} | ||
310 | char curl_error_buffer[CURL_ERROR_SIZE]; | ||
311 | CURL* curlp = curl_easy_init(); | ||
312 | |||
313 | LLHTTPBuffer http_buffer; | ||
314 | |||
315 | curl_easy_setopt(curlp, CURLOPT_WRITEFUNCTION, LLHTTPBuffer::curl_write); | ||
316 | curl_easy_setopt(curlp, CURLOPT_WRITEDATA, &http_buffer); | ||
317 | curl_easy_setopt(curlp, CURLOPT_URL, url.c_str()); | ||
318 | curl_easy_setopt(curlp, CURLOPT_ERRORBUFFER, curl_error_buffer); | ||
319 | curl_easy_setopt(curlp, CURLOPT_FAILONERROR, 1); | ||
320 | |||
321 | LLSD response = LLSD::emptyMap(); | ||
322 | |||
323 | S32 curl_success = curl_easy_perform(curlp); | ||
324 | |||
325 | S32 http_status = 499; | ||
326 | curl_easy_getinfo(curlp,CURLINFO_RESPONSE_CODE, &http_status); | ||
327 | |||
328 | response["status"] = http_status; | ||
329 | |||
330 | if (curl_success != 0 | ||
331 | && http_status != 404) // We expect 404s, don't spam for them. | ||
332 | { | ||
333 | llwarns << "CURL ERROR: " << curl_error_buffer << llendl; | ||
334 | |||
335 | response["body"] = http_buffer.asString(); | ||
336 | } | ||
337 | else | ||
338 | { | ||
339 | response["body"] = http_buffer.asLLSD(); | ||
340 | } | ||
341 | |||
342 | curl_easy_cleanup(curlp); | ||
343 | |||
344 | return response; | ||
345 | } | ||
346 | |||
246 | void LLHTTPClient::put(const std::string& url, const LLSD& body, ResponderPtr responder) | 347 | void LLHTTPClient::put(const std::string& url, const LLSD& body, ResponderPtr responder) |
247 | { | 348 | { |
248 | request(url, LLURLRequest::HTTP_PUT, new LLSDInjector(body), responder); | 349 | request(url, LLURLRequest::HTTP_PUT, new LLSDInjector(body), responder); |
@@ -253,6 +354,11 @@ void LLHTTPClient::post(const std::string& url, const LLSD& body, ResponderPtr r | |||
253 | request(url, LLURLRequest::HTTP_POST, new LLSDInjector(body), responder); | 354 | request(url, LLURLRequest::HTTP_POST, new LLSDInjector(body), responder); |
254 | } | 355 | } |
255 | 356 | ||
357 | void LLHTTPClient::post(const std::string& url, const U8* data, S32 size, ResponderPtr responder) | ||
358 | { | ||
359 | request(url, LLURLRequest::HTTP_POST, new RawInjector(data, size), responder); | ||
360 | } | ||
361 | |||
256 | void LLHTTPClient::del(const std::string& url, ResponderPtr responder) | 362 | void LLHTTPClient::del(const std::string& url, ResponderPtr responder) |
257 | { | 363 | { |
258 | request(url, LLURLRequest::HTTP_DELETE, NULL, responder); | 364 | request(url, LLURLRequest::HTTP_DELETE, NULL, responder); |
@@ -301,4 +407,3 @@ namespace boost | |||
301 | } | 407 | } |
302 | } | 408 | } |
303 | }; | 409 | }; |
304 | |||