reSIProcate/DialogUsageManager  9680
CurlHttpProvider.cxx
Go to the documentation of this file.
00001 #include "CurlHttpProvider.hxx"
00002 #include "resip/stack/external/HttpGetMessage.hxx"
00003 #include "resip/stack/TransactionUser.hxx"
00004 #include "rutil/Logger.hxx"
00005 
00006 #include <curl/curl.h>
00007 
00008 #define RESIPROCATE_SUBSYSTEM Subsystem::TRANSPORT
00009 
00010 using namespace resip;
00011 
00012 CurlHttpProvider::CurlHttpProvider()
00013 {
00014    curl_global_init(CURL_GLOBAL_DEFAULT);
00015 }
00016 
00017 CurlHttpProvider::~CurlHttpProvider()
00018 {
00019 }
00020 
00021 void 
00022 CurlHttpProvider::get(const GenericUri& target, const Data& tid, TransactionUser& tu)
00023 {
00024    //fix cleanup logic, self deletes for now
00025    RequestThread* rt = new RequestThread(target, tid, tu);
00026    rt->run();   
00027 }
00028 
00029 CurlHttpProvider* 
00030 CurlHttpProviderFactory::createHttpProvider()
00031 {
00032    return new CurlHttpProvider();
00033 }
00034 
00035 CurlHttpProvider::RequestThread::RequestThread(const GenericUri& target, 
00036                              const Data& tid, 
00037                              TransactionUser& tu) :
00038    mTarget(target),
00039    mTid(tid),
00040    mTransactionUser(tu),
00041    mX509Blob(),
00042    mStream(mX509Blob)
00043 {}
00044 
00045 CurlHttpProvider::RequestThread::~RequestThread()
00046 {
00047 }
00048 
00049 void 
00050 CurlHttpProvider::RequestThread::thread()
00051 {
00052    CURL *curl = curl_easy_init();
00053    if (!curl)
00054    {
00055        HttpGetMessage* res = new HttpGetMessage(mTid, false, Data::Empty, Mime());
00056        mTransactionUser.post(res);
00057    }
00058 
00059    curl_easy_setopt(curl, CURLOPT_URL, mTarget.uri().c_str());
00060    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, RequestThread::curlCallback);
00061    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mStream);
00062    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
00063    
00064    int code = 404;
00065    curl_easy_perform(curl);
00066 #if ( LIBCURL_VERSION_NUM > 0x070a07 )
00067    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
00068 #else
00069    curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &code);
00070 #endif
00071    InfoLog ( << "Curl returned: " << code);   
00072 
00073     if (code / 100 == 2) 
00074     {
00075         //.dcm.  vodoo to trick the lazy parsers, should add convenience methods to
00076         //clean this up
00077         Mime contentType; 
00078         {
00079            char* contentTypeString;
00080            curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &contentTypeString);
00081 
00082            InfoLog ( << "Content type from curl: " << contentTypeString);
00083            
00084            if (contentTypeString)
00085            {
00086               HeaderFieldValue tmp(contentTypeString, strlen(contentTypeString));
00087               Mime tempMime(&tmp, Headers::ContentType);
00088               contentType = tempMime;
00089               delete contentTypeString;          
00090            }
00091         }
00092         mStream.flush();        
00093         HttpGetMessage* res = new HttpGetMessage(mTid, true, mX509Blob, contentType);
00094         mTransactionUser.post(res);
00095     }
00096     else
00097     {
00098        HttpGetMessage* res = new HttpGetMessage(mTid, false, Data::Empty, Mime());
00099        mTransactionUser.post(res);
00100     }
00101     curl_easy_cleanup(curl);
00102 
00103     delete this;
00104 }
00105 
00106 int 
00107 CurlHttpProvider::RequestThread::curlCallback(void *ptr, size_t size, size_t nmemb, void *stream)
00108 {
00109    DataStream* ds = reinterpret_cast<DataStream*>(stream);
00110    assert(size == 1);
00111    ds->write(reinterpret_cast<const char*>(ptr), nmemb);
00112    InfoLog ( << "Wrote: " << nmemb << " to stream" );   
00113    return nmemb;
00114 }
00115 
00116 
00117 /* ====================================================================
00118  * The Vovida Software License, Version 1.0 
00119  * 
00120  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00121  * 
00122  * Redistribution and use in source and binary forms, with or without
00123  * modification, are permitted provided that the following conditions
00124  * are met:
00125  * 
00126  * 1. Redistributions of source code must retain the above copyright
00127  *    notice, this list of conditions and the following disclaimer.
00128  * 
00129  * 2. Redistributions in binary form must reproduce the above copyright
00130  *    notice, this list of conditions and the following disclaimer in
00131  *    the documentation and/or other materials provided with the
00132  *    distribution.
00133  * 
00134  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00135  *    and "Vovida Open Communication Application Library (VOCAL)" must
00136  *    not be used to endorse or promote products derived from this
00137  *    software without prior written permission. For written
00138  *    permission, please contact vocal@vovida.org.
00139  *
00140  * 4. Products derived from this software may not be called "VOCAL", nor
00141  *    may "VOCAL" appear in their name, without prior written
00142  *    permission of Vovida Networks, Inc.
00143  * 
00144  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00145  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00146  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00147  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00148  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00149  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00150  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00151  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00152  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00153  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00154  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00155  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00156  * DAMAGE.
00157  * 
00158  * ====================================================================
00159  * 
00160  * This software consists of voluntary contributions made by Vovida
00161  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00162  * Inc.  For more information on Vovida Networks, Inc., please see
00163  * <http://www.vovida.org/>.
00164  *
00165  */