From 437882ca3ba259911d02889621afa334ae1c76db Mon Sep 17 00:00:00 2001 From: Kelly Washington Date: Mon, 5 Mar 2007 19:08:01 +0000 Subject: merge r58577:58578 Branch_1-13-3 to release --- indra/llmessage/llhttpclient.cpp | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'indra/llmessage/llhttpclient.cpp') diff --git a/indra/llmessage/llhttpclient.cpp b/indra/llmessage/llhttpclient.cpp index 7d51b6c722..d83308d082 100644 --- a/indra/llmessage/llhttpclient.cpp +++ b/indra/llmessage/llhttpclient.cpp @@ -18,6 +18,8 @@ #include "llvfile.h" #include "llvfs.h" +#include + static const F32 HTTP_REQUEST_EXPIRY_SECS = 60.0f; static std::string gCABundle; @@ -224,6 +226,71 @@ void LLHTTPClient::get(const std::string& url, ResponderPtr responder) request(url, LLURLRequest::HTTP_GET, NULL, responder); } +// A simple class for managing data returned from a curl http request. +class LLHTTPBuffer +{ +public: + LLHTTPBuffer() { } + + static size_t curl_write( void *ptr, size_t size, size_t nmemb, void *user_data) + { + LLHTTPBuffer* self = (LLHTTPBuffer*)user_data; + + size_t bytes = (size * nmemb); + self->mBuffer.append((char*)ptr,bytes); + return nmemb; + } + + LLSD asLLSD() + { + LLSD content; + std::istringstream istr(mBuffer); + LLSDSerialize::fromXML(content, istr); + return content; + } + +private: + std::string mBuffer; +}; + +// This call is blocking! This is probably usually bad. :( +LLSD LLHTTPClient::blockingGet(const std::string& url) +{ + llinfos << "blockingGet of " << url << llendl; + + // Returns an LLSD map: {status: integer, body: map} + char curl_error_buffer[CURL_ERROR_SIZE]; + CURL* curlp = curl_easy_init(); + + LLHTTPBuffer http_buffer; + + curl_easy_setopt(curlp, CURLOPT_WRITEFUNCTION, LLHTTPBuffer::curl_write); + curl_easy_setopt(curlp, CURLOPT_WRITEDATA, &http_buffer); + curl_easy_setopt(curlp, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curlp, CURLOPT_ERRORBUFFER, curl_error_buffer); + curl_easy_setopt(curlp, CURLOPT_FAILONERROR, 1); + + LLSD response = LLSD::emptyMap(); + + S32 curl_success = curl_easy_perform(curlp); + + S32 http_status = 499; + curl_easy_getinfo(curlp,CURLINFO_RESPONSE_CODE, &http_status); + + if (curl_success != 0 + && http_status != 404) // We expect 404s, don't spam for them. + { + llwarns << "CURL ERROR: " << curl_error_buffer << llendl; + } + + response["status"] = http_status; + response["body"] = http_buffer.asLLSD(); + + curl_easy_cleanup(curlp); + + return response; +} + void LLHTTPClient::put(const std::string& url, const LLSD& body, ResponderPtr responder) { request(url, LLURLRequest::HTTP_PUT, new LLSDInjector(body), responder); -- cgit v1.2.3