diff options
Diffstat (limited to 'indra/llmessage/llurlrequest.cpp')
-rw-r--r-- | indra/llmessage/llurlrequest.cpp | 256 |
1 files changed, 180 insertions, 76 deletions
diff --git a/indra/llmessage/llurlrequest.cpp b/indra/llmessage/llurlrequest.cpp index db67fbeea7..cb9d1c3731 100644 --- a/indra/llmessage/llurlrequest.cpp +++ b/indra/llmessage/llurlrequest.cpp @@ -4,57 +4,55 @@ * @date 2005-04-28 * @brief Implementation of the URLRequest class and related classes. * - * $LicenseInfo:firstyear=2005&license=viewergpl$ - * - * Copyright (c) 2005-2007, Linden Research, Inc. - * + * $LicenseInfo:firstyear=2005&license=viewerlgpl$ * Second Life Viewer Source Code - * The source code in this file ("Source Code") is provided by Linden Lab - * to you under the terms of the GNU General Public License, version 2.0 - * ("GPL"), unless you have obtained a separate licensing agreement - * ("Other License"), formally executed by you and Linden Lab. Terms of - * the GPL can be found in doc/GPL-license.txt in this distribution, or - * online at http://secondlife.com/developers/opensource/gplv2 + * Copyright (C) 2010, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. * - * There are special exceptions to the terms and conditions of the GPL as - * it is applied to this Source Code. View the full text of the exception - * in the file doc/FLOSS-exception.txt in this software distribution, or - * online at http://secondlife.com/developers/opensource/flossexception + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * - * By copying, modifying or distributing this software, you acknowledge - * that you have read and understood your obligations described above, - * and agree to abide by those obligations. + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO - * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, - * COMPLETENESS OR PERFORMANCE. + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA * $/LicenseInfo$ */ #include "linden_common.h" #include "llurlrequest.h" -#include <curl/curl.h> #include <algorithm> - +#include <openssl/x509_vfy.h> +#include <openssl/ssl.h> #include "llcurl.h" #include "llioutil.h" #include "llmemtype.h" #include "llpumpio.h" #include "llsd.h" #include "llstring.h" -#include "apr-1/apr_env.h" - +#include "apr_env.h" +#include "llapr.h" static const U32 HTTP_STATUS_PIPE_ERROR = 499; /** * String constants */ const std::string CONTEXT_DEST_URI_SD_LABEL("dest_uri"); +const std::string CONTEXT_TRANSFERED_BYTES("transfered_bytes"); static size_t headerCallback(void* data, size_t size, size_t nmemb, void* user); + + /** * class LLURLRequestDetail */ @@ -69,7 +67,9 @@ public: LLChannelDescriptors mChannels; U8* mLastRead; U32 mBodyLimit; + S32 mByteAccumulator; bool mIsBodyLimitSet; + LLURLRequest::SSLCertVerifyCallback mSSLVerifyCallback; }; LLURLRequestDetail::LLURLRequestDetail() : @@ -77,8 +77,9 @@ LLURLRequestDetail::LLURLRequestDetail() : mResponseBuffer(NULL), mLastRead(NULL), mBodyLimit(0), - mIsBodyLimitSet(false) - + mByteAccumulator(0), + mIsBodyLimitSet(false), + mSSLVerifyCallback(NULL) { LLMemType m1(LLMemType::MTYPE_IO_URL_REQUEST); mCurlRequest = new LLCurlEasyRequest(); @@ -92,11 +93,61 @@ LLURLRequestDetail::~LLURLRequestDetail() mLastRead = NULL; } +void LLURLRequest::setSSLVerifyCallback(SSLCertVerifyCallback callback, void *param) +{ + mDetail->mSSLVerifyCallback = callback; + mDetail->mCurlRequest->setSSLCtxCallback(LLURLRequest::_sslCtxCallback, (void *)this); + mDetail->mCurlRequest->setopt(CURLOPT_SSL_VERIFYPEER, true); + mDetail->mCurlRequest->setopt(CURLOPT_SSL_VERIFYHOST, 2); +} + + +// _sslCtxFunction +// Callback function called when an SSL Context is created via CURL +// used to configure the context for custom cert validation + +CURLcode LLURLRequest::_sslCtxCallback(CURL * curl, void *sslctx, void *param) +{ + LLURLRequest *req = (LLURLRequest *)param; + if(req == NULL || req->mDetail->mSSLVerifyCallback == NULL) + { + SSL_CTX_set_cert_verify_callback((SSL_CTX *)sslctx, NULL, NULL); + return CURLE_OK; + } + SSL_CTX * ctx = (SSL_CTX *) sslctx; + // disable any default verification for server certs + SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); + // set the verification callback. + SSL_CTX_set_cert_verify_callback(ctx, req->mDetail->mSSLVerifyCallback, (void *)req); + // the calls are void + return CURLE_OK; + +} /** * class LLURLRequest */ +// static +std::string LLURLRequest::actionAsVerb(LLURLRequest::ERequestAction action) +{ + static const std::string VERBS[] = + { + "(invalid)", + "HEAD", + "GET", + "PUT", + "POST", + "DELETE", + "MOVE" + }; + if(((S32)action <=0) || ((S32)action >= REQUEST_ACTION_COUNT)) + { + return VERBS[0]; + } + return VERBS[action]; +} + LLURLRequest::LLURLRequest(LLURLRequest::ERequestAction action) : mAction(action) { @@ -126,6 +177,11 @@ void LLURLRequest::setURL(const std::string& url) mDetail->mURL = url; } +std::string LLURLRequest::getURL() const +{ + return mDetail->mURL; +} + void LLURLRequest::addHeader(const char* header) { LLMemType m1(LLMemType::MTYPE_IO_URL_REQUEST); @@ -138,12 +194,6 @@ void LLURLRequest::setBodyLimit(U32 size) mDetail->mIsBodyLimitSet = true; } -void LLURLRequest::checkRootCertificate(bool check) -{ - mDetail->mCurlRequest->setopt(CURLOPT_SSL_VERIFYPEER, (check? TRUE : FALSE)); - mDetail->mCurlRequest->setoptString(CURLOPT_ENCODING, ""); -} - void LLURLRequest::setCallback(LLURLRequestComplete* callback) { LLMemType m1(LLMemType::MTYPE_IO_URL_REQUEST); @@ -165,18 +215,16 @@ void LLURLRequest::useProxy(bool use_proxy) if (use_proxy && (env_proxy == NULL)) { apr_status_t status; - apr_pool_t* pool; - apr_pool_create(&pool, NULL); - status = apr_env_get(&env_proxy, "ALL_PROXY", pool); + LLAPRPool pool; + status = apr_env_get(&env_proxy, "ALL_PROXY", pool.getAPRPool()); if (status != APR_SUCCESS) { - status = apr_env_get(&env_proxy, "http_proxy", pool); + status = apr_env_get(&env_proxy, "http_proxy", pool.getAPRPool()); } if (status != APR_SUCCESS) { use_proxy = FALSE; } - apr_pool_destroy(pool); } @@ -194,7 +242,12 @@ void LLURLRequest::useProxy(bool use_proxy) void LLURLRequest::useProxy(const std::string &proxy) { - curl_easy_setopt(mDetail->mCurlRequest, CURLOPT_PROXY, proxy.c_str()); + mDetail->mCurlRequest->setoptString(CURLOPT_PROXY, proxy); +} + +void LLURLRequest::allowCookies() +{ + mDetail->mCurlRequest->setoptString(CURLOPT_COOKIEFILE, ""); } // virtual @@ -228,7 +281,29 @@ LLIOPipe::EStatus LLURLRequest::process_impl( PUMP_DEBUG; LLMemType m1(LLMemType::MTYPE_IO_URL_REQUEST); //llinfos << "LLURLRequest::process_impl()" << llendl; - if(!buffer) return STATUS_ERROR; + if (!buffer) return STATUS_ERROR; + + // we're still waiting or prcessing, check how many + // bytes we have accumulated. + const S32 MIN_ACCUMULATION = 100000; + if(pump && (mDetail->mByteAccumulator > MIN_ACCUMULATION)) + { + // This is a pretty sloppy calculation, but this + // tries to make the gross assumption that if data + // is coming in at 56kb/s, then this transfer will + // probably succeed. So, if we're accumlated + // 100,000 bytes (MIN_ACCUMULATION) then let's + // give this client another 2s to complete. + const F32 TIMEOUT_ADJUSTMENT = 2.0f; + mDetail->mByteAccumulator = 0; + pump->adjustTimeoutSeconds(TIMEOUT_ADJUSTMENT); + lldebugs << "LLURLRequest adjustTimeoutSeconds for request: " << mDetail->mURL << llendl; + if (mState == STATE_INITIALIZED) + { + llinfos << "LLURLRequest adjustTimeoutSeconds called during upload" << llendl; + } + } + switch(mState) { case STATE_INITIALIZED: @@ -265,12 +340,16 @@ LLIOPipe::EStatus LLURLRequest::process_impl( { CURLcode result; bool newmsg = mDetail->mCurlRequest->getResult(&result); - if (!newmsg) + if(!newmsg) { + // keep processing break; } mState = STATE_HAVE_RESPONSE; + context[CONTEXT_REQUEST][CONTEXT_TRANSFERED_BYTES] = mRequestTransferedBytes; + context[CONTEXT_RESPONSE][CONTEXT_TRANSFERED_BYTES] = mResponseTransferedBytes; + lldebugs << this << "Setting context to " << context << llendl; switch(result) { case CURLE_OK: @@ -317,10 +396,16 @@ LLIOPipe::EStatus LLURLRequest::process_impl( // we already stuffed everything into channel in in the curl // callback, so we are done. eos = true; + context[CONTEXT_REQUEST][CONTEXT_TRANSFERED_BYTES] = mRequestTransferedBytes; + context[CONTEXT_RESPONSE][CONTEXT_TRANSFERED_BYTES] = mResponseTransferedBytes; + lldebugs << this << "Setting context to " << context << llendl; return STATUS_DONE; default: PUMP_DEBUG; + context[CONTEXT_REQUEST][CONTEXT_TRANSFERED_BYTES] = mRequestTransferedBytes; + context[CONTEXT_RESPONSE][CONTEXT_TRANSFERED_BYTES] = mResponseTransferedBytes; + lldebugs << this << "Setting context to " << context << llendl; return STATUS_ERROR; } } @@ -333,6 +418,8 @@ void LLURLRequest::initialize() mDetail->mCurlRequest->setopt(CURLOPT_NOSIGNAL, 1); mDetail->mCurlRequest->setWriteCallback(&downCallback, (void*)this); mDetail->mCurlRequest->setReadCallback(&upCallback, (void*)this); + mRequestTransferedBytes = 0; + mResponseTransferedBytes = 0; } bool LLURLRequest::configure() @@ -386,6 +473,13 @@ bool LLURLRequest::configure() rv = true; break; + case HTTP_MOVE: + // Set the handle for an http post + mDetail->mCurlRequest->setoptString(CURLOPT_CUSTOMREQUEST, "MOVE"); + // *NOTE: should we check for the Destination header? + rv = true; + break; + default: llwarns << "Unhandled URLRequest action: " << mAction << llendl; break; @@ -428,6 +522,8 @@ size_t LLURLRequest::downCallback( req->mDetail->mChannels.out(), (U8*)data, bytes); + req->mResponseTransferedBytes += bytes; + req->mDetail->mByteAccumulator += bytes; return bytes; } @@ -450,63 +546,76 @@ size_t LLURLRequest::upCallback( req->mDetail->mLastRead, (U8*)data, bytes); + req->mRequestTransferedBytes += bytes; return bytes; } static size_t headerCallback(void* data, size_t size, size_t nmemb, void* user) { - const char* headerLine = (const char*)data; - size_t headerLen = size * nmemb; + const char* header_line = (const char*)data; + size_t header_len = size * nmemb; LLURLRequestComplete* complete = (LLURLRequestComplete*)user; + if (!complete || !header_line) + { + return header_len; + } + // *TODO: This should be a utility in llstring.h: isascii() - for (size_t i = 0; i < headerLen; ++i) + for (size_t i = 0; i < header_len; ++i) { - if (headerLine[i] < 0) + if (header_line[i] < 0) { - return headerLen; + return header_len; } } - size_t sep; - for (sep = 0; sep < headerLen && headerLine[sep] != ':'; ++sep) { } - - if (sep < headerLen && complete) - { - std::string key(headerLine, sep); - std::string value(headerLine + sep + 1, headerLen - sep - 1); - - key = utf8str_tolower(utf8str_trim(key)); - value = utf8str_trim(value); + std::string header(header_line, header_len); - complete->header(key, value); - } - else + // Per HTTP spec the first header line must be the status line. + if (header.substr(0,5) == "HTTP/") { - std::string s(headerLine, headerLen); - - std::string::iterator end = s.end(); - std::string::iterator pos1 = std::find(s.begin(), end, ' '); + std::string::iterator end = header.end(); + std::string::iterator pos1 = std::find(header.begin(), end, ' '); if (pos1 != end) ++pos1; std::string::iterator pos2 = std::find(pos1, end, ' '); if (pos2 != end) ++pos2; std::string::iterator pos3 = std::find(pos2, end, '\r'); - std::string version(s.begin(), pos1); + std::string version(header.begin(), pos1); std::string status(pos1, pos2); std::string reason(pos2, pos3); - int statusCode = atoi(status.c_str()); - if (statusCode > 0) + S32 status_code = atoi(status.c_str()); + if (status_code > 0) { - if (complete) - { - complete->httpStatus((U32)statusCode, reason); - } + complete->httpStatus((U32)status_code, reason); + return header_len; } } - return headerLen; + std::string::iterator sep = std::find(header.begin(),header.end(),':'); + + if (sep != header.end()) + { + std::string key(header.begin(), sep); + std::string value(sep + 1, header.end()); + + key = utf8str_tolower(utf8str_trim(key)); + value = utf8str_trim(value); + + complete->header(key, value); + } + else + { + LLStringUtil::trim(header); + if (!header.empty()) + { + llwarns << "Unable to parse header: " << header << llendl; + } + } + + return header_len; } /** @@ -536,7 +645,7 @@ LLIOPipe::EStatus LLContextURLExtractor::process_impl( // find the context url if(context.has(CONTEXT_DEST_URI_SD_LABEL)) { - mRequest->setURL(context[CONTEXT_DEST_URI_SD_LABEL]); + mRequest->setURL(context[CONTEXT_DEST_URI_SD_LABEL].asString()); return STATUS_DONE; } return STATUS_ERROR; @@ -564,11 +673,6 @@ void LLURLRequestComplete::header(const std::string& header, const std::string& } //virtual -void LLURLRequestComplete::httpStatus(U32 status, const std::string& reason) -{ -} - -//virtual void LLURLRequestComplete::complete(const LLChannelDescriptors& channels, const buffer_ptr_t& buffer) { |