grive2/libgrive/src/http/CurlAgent.cc

309 lines
7.3 KiB
C++
Raw Normal View History

2012-04-25 20:13:17 +04:00
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
2012-04-25 20:13:17 +04:00
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
2012-06-10 19:11:32 +04:00
#include "CurlAgent.hh"
2012-04-25 20:13:17 +04:00
#include "Download.hh"
2012-05-13 11:27:58 +04:00
#include "Error.hh"
2012-06-10 19:11:32 +04:00
#include "Header.hh"
2012-05-05 21:12:44 +04:00
#include "Receivable.hh"
2012-06-03 14:31:02 +04:00
#include "util/log/Log.hh"
2013-04-28 14:40:21 +04:00
#include "util/File.hh"
2012-05-13 12:10:18 +04:00
#include <boost/throw_exception.hpp>
// dependent libraries
2012-04-25 20:13:17 +04:00
#include <curl/curl.h>
#include <algorithm>
2012-04-25 20:13:17 +04:00
#include <cassert>
#include <cstring>
2012-07-16 21:58:16 +04:00
#include <limits>
2012-04-25 20:13:17 +04:00
#include <sstream>
#include <streambuf>
#include <iostream>
2012-04-25 20:13:17 +04:00
#include <signal.h>
namespace {
2012-04-25 20:13:17 +04:00
using namespace gr::http ;
2012-07-16 21:58:16 +04:00
using namespace gr ;
2012-04-25 20:13:17 +04:00
2012-07-16 21:58:16 +04:00
size_t ReadStringCallback( void *ptr, std::size_t size, std::size_t nmemb, std::string *data )
{
assert( ptr != 0 ) ;
assert( data != 0 ) ;
std::size_t count = std::min( size * nmemb, data->size() ) ;
if ( count > 0 )
{
2012-04-29 06:22:58 +04:00
std::memcpy( ptr, &(*data)[0], count ) ;
data->erase( 0, count ) ;
}
return count ;
}
2013-04-28 14:40:21 +04:00
size_t ReadFileCallback( void *ptr, std::size_t size, std::size_t nmemb, File *file )
2012-07-16 21:58:16 +04:00
{
assert( ptr != 0 ) ;
assert( file != 0 ) ;
u64_t count = std::min(
static_cast<u64_t>(size * nmemb),
static_cast<u64_t>(file->Size() - file->Tell()) ) ;
assert( count <= std::numeric_limits<std::size_t>::max() ) ;
if ( count > 0 )
2013-04-28 14:40:21 +04:00
file->Read( static_cast<char*>(ptr), static_cast<std::size_t>(count) ) ;
2012-07-16 21:58:16 +04:00
return count ;
}
} // end of local namespace
namespace gr { namespace http {
2012-06-10 19:11:32 +04:00
struct CurlAgent::Impl
2012-05-01 20:26:48 +04:00
{
CURL *curl ;
std::string location ;
} ;
2012-06-10 19:11:32 +04:00
CurlAgent::CurlAgent() :
2012-05-01 20:26:48 +04:00
m_pimpl( new Impl )
{
m_pimpl->curl = ::curl_easy_init();
2012-05-28 20:31:29 +04:00
}
2012-06-10 19:11:32 +04:00
void CurlAgent::Init()
2012-05-28 20:31:29 +04:00
{
::curl_easy_reset( m_pimpl->curl ) ;
::curl_easy_setopt( m_pimpl->curl, CURLOPT_SSL_VERIFYPEER, 0L ) ;
::curl_easy_setopt( m_pimpl->curl, CURLOPT_SSL_VERIFYHOST, 0L ) ;
2012-06-10 19:11:32 +04:00
::curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADERFUNCTION, &CurlAgent::HeaderCallback ) ;
2012-05-05 18:39:18 +04:00
::curl_easy_setopt( m_pimpl->curl, CURLOPT_WRITEHEADER , this ) ;
::curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADER, 0L ) ;
2012-05-01 20:26:48 +04:00
}
2012-06-10 19:11:32 +04:00
CurlAgent::~CurlAgent()
2012-05-01 20:26:48 +04:00
{
::curl_easy_cleanup( m_pimpl->curl );
}
2012-06-10 19:11:32 +04:00
std::size_t CurlAgent::HeaderCallback( void *ptr, size_t size, size_t nmemb, CurlAgent *pthis )
2012-05-01 20:26:48 +04:00
{
2012-05-05 18:39:18 +04:00
char *str = reinterpret_cast<char*>(ptr) ;
std::string line( str, str + size*nmemb ) ;
2012-05-01 20:26:48 +04:00
static const std::string loc = "Location: " ;
std::size_t pos = line.find( loc ) ;
if ( pos != line.npos )
{
std::size_t end_pos = line.find( "\r\n", pos ) ;
pthis->m_pimpl->location = line.substr( loc.size(), end_pos - loc.size() ) ;
}
return size*nmemb ;
}
2012-06-10 19:11:32 +04:00
std::size_t CurlAgent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv )
2012-05-05 18:39:18 +04:00
{
2012-05-05 21:12:44 +04:00
assert( recv != 0 ) ;
return recv->OnData( ptr, size * nmemb ) ;
2012-05-05 18:39:18 +04:00
}
2012-06-10 19:11:32 +04:00
long CurlAgent::ExecCurl(
const std::string& url,
Receivable *dest,
const http::Header& hdr )
2012-05-01 20:26:48 +04:00
{
CURL *curl = m_pimpl->curl ;
assert( curl != 0 ) ;
char error[CURL_ERROR_SIZE] = {} ;
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error ) ;
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
2012-06-10 19:11:32 +04:00
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlAgent::Receive ) ;
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
2012-05-01 20:26:48 +04:00
2012-05-05 18:39:18 +04:00
SetHeader( hdr ) ;
2012-05-20 20:10:29 +04:00
2012-05-09 18:25:42 +04:00
dest->Clear() ;
CURLcode curl_code = ::curl_easy_perform(curl);
2012-05-01 20:26:48 +04:00
// get the HTTTP response code
2012-05-01 20:26:48 +04:00
long http_code = 0;
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
2012-05-13 21:07:23 +04:00
Trace( "HTTP response %1%", http_code ) ;
2012-05-13 12:45:27 +04:00
// reset the curl buffer to prevent it from touch our "error" buffer
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, 0 ) ;
// only throw for libcurl errors
if ( curl_code != CURLE_OK )
2012-05-01 20:26:48 +04:00
{
BOOST_THROW_EXCEPTION(
Error()
<< CurlCode( curl_code )
<< Url( url )
<< expt::ErrMsg( error )
<< HttpHeader( hdr )
) ;
2012-05-01 20:26:48 +04:00
}
return http_code ;
2012-05-01 20:26:48 +04:00
}
2012-06-10 19:11:32 +04:00
long CurlAgent::Put(
const std::string& url,
const std::string& data,
Receivable *dest,
const Header& hdr )
{
2012-05-13 21:07:23 +04:00
Trace("HTTP PUT \"%1%\"", url ) ;
2012-05-28 20:31:29 +04:00
Init() ;
CURL *curl = m_pimpl->curl ;
std::string put_data = data ;
// set common options
::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
2012-07-16 21:58:16 +04:00
::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadStringCallback ) ;
::curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ;
::curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ;
return ExecCurl( url, dest, hdr ) ;
}
2012-07-16 21:58:16 +04:00
long CurlAgent::Put(
const std::string& url,
2013-04-28 14:40:21 +04:00
File& file,
2012-07-16 21:58:16 +04:00
Receivable *dest,
const Header& hdr )
{
Trace("HTTP PUT \"%1%\"", url ) ;
Init() ;
CURL *curl = m_pimpl->curl ;
// set common options
::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadFileCallback ) ;
::curl_easy_setopt(curl, CURLOPT_READDATA , &file ) ;
::curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, static_cast<curl_off_t>(file.Size()) ) ;
return ExecCurl( url, dest, hdr ) ;
}
2012-06-10 19:11:32 +04:00
long CurlAgent::Get(
2012-05-05 21:12:44 +04:00
const std::string& url,
Receivable *dest,
const Header& hdr )
2012-05-05 21:12:44 +04:00
{
2012-05-13 21:07:23 +04:00
Trace("HTTP GET \"%1%\"", url ) ;
2012-05-28 20:31:29 +04:00
Init() ;
2012-05-05 21:12:44 +04:00
// set get specific options
2012-06-11 21:24:22 +04:00
::curl_easy_setopt(m_pimpl->curl, CURLOPT_HTTPGET, 1L);
return ExecCurl( url, dest, hdr ) ;
}
2012-06-10 19:11:32 +04:00
long CurlAgent::Post(
const std::string& url,
const std::string& data,
Receivable *dest,
const Header& hdr )
{
2012-05-13 21:07:23 +04:00
Trace("HTTP POST \"%1%\" with \"%2%\"", url, data ) ;
2012-05-13 12:10:18 +04:00
2012-05-28 20:31:29 +04:00
Init() ;
CURL *curl = m_pimpl->curl ;
// make a copy because the parameter is const
std::string post_data = data ;
// set post specific options
::curl_easy_setopt(curl, CURLOPT_POST, 1L);
::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &post_data[0] ) ;
::curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, post_data.size() ) ;
2012-05-05 21:12:44 +04:00
return ExecCurl( url, dest, hdr ) ;
2012-05-05 21:12:44 +04:00
}
2012-06-10 19:11:32 +04:00
long CurlAgent::Custom(
2012-05-06 18:27:52 +04:00
const std::string& method,
const std::string& url,
Receivable *dest,
const Header& hdr )
2012-05-06 18:27:52 +04:00
{
2012-05-13 21:07:23 +04:00
Trace("HTTP %2% \"%1%\"", url, method ) ;
2012-05-13 12:10:18 +04:00
2012-05-06 18:27:52 +04:00
CURL *curl = m_pimpl->curl ;
::curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str() );
2012-06-03 14:34:03 +04:00
// ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1 );
2012-05-06 18:27:52 +04:00
return ExecCurl( url, dest, hdr ) ;
2012-05-06 18:27:52 +04:00
}
2012-06-10 19:11:32 +04:00
void CurlAgent::SetHeader( const Header& hdr )
2012-05-05 18:39:18 +04:00
{
// set headers
struct curl_slist *curl_hdr = 0 ;
for ( Header::iterator i = hdr.begin() ; i != hdr.end() ; ++i )
2012-05-05 18:39:18 +04:00
curl_hdr = curl_slist_append( curl_hdr, i->c_str() ) ;
::curl_easy_setopt( m_pimpl->curl, CURLOPT_HTTPHEADER, curl_hdr ) ;
}
2012-06-10 19:11:32 +04:00
std::string CurlAgent::RedirLocation() const
2012-05-01 20:26:48 +04:00
{
return m_pimpl->location ;
}
2012-06-10 19:11:32 +04:00
std::string CurlAgent::Escape( const std::string& str )
{
CURL *curl = m_pimpl->curl ;
char *tmp = curl_easy_escape( curl, str.c_str(), str.size() ) ;
std::string result = tmp ;
curl_free( tmp ) ;
return result ;
}
2012-06-10 19:11:32 +04:00
std::string CurlAgent::Unescape( const std::string& str )
{
CURL *curl = m_pimpl->curl ;
int r ;
char *tmp = curl_easy_unescape( curl, str.c_str(), str.size(), &r ) ;
std::string result = tmp ;
curl_free( tmp ) ;
return result ;
}
2012-05-05 18:39:18 +04:00
} } // end of namespace