mirror of https://github.com/vitalif/grive2
improved HTTP error handling to troubleshoot issue #31
parent
ade83c6d70
commit
97884cf25c
|
@ -25,6 +25,8 @@
|
||||||
|
|
||||||
#include "util/Log.hh"
|
#include "util/Log.hh"
|
||||||
|
|
||||||
|
#include <boost/throw_exception.hpp>
|
||||||
|
|
||||||
// dependent libraries
|
// dependent libraries
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
@ -119,14 +121,18 @@ std::size_t Agent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *re
|
||||||
}
|
}
|
||||||
|
|
||||||
long Agent::ExecCurl(
|
long Agent::ExecCurl(
|
||||||
|
const std::string& url,
|
||||||
Receivable *dest,
|
Receivable *dest,
|
||||||
const http::Headers& hdr )
|
const http::Headers& hdr )
|
||||||
{
|
{
|
||||||
CURL *curl = m_pimpl->curl ;
|
CURL *curl = m_pimpl->curl ;
|
||||||
assert( curl != 0 ) ;
|
assert( curl != 0 ) ;
|
||||||
|
|
||||||
char error[CURL_ERROR_SIZE] ;
|
char error[CURL_ERROR_SIZE] = {} ;
|
||||||
::curl_easy_setopt( m_pimpl->curl, CURLOPT_ERRORBUFFER, error ) ;
|
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error ) ;
|
||||||
|
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
|
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
|
||||||
|
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
|
||||||
|
|
||||||
SetHeader( hdr ) ;
|
SetHeader( hdr ) ;
|
||||||
|
|
||||||
|
@ -137,13 +143,17 @@ long Agent::ExecCurl(
|
||||||
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
||||||
|
|
||||||
Trace( "HTTP response %1%", http_code ) ;
|
Trace( "HTTP response %1%", http_code ) ;
|
||||||
|
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, 0 ) ;
|
||||||
|
|
||||||
if ( curl_code != CURLE_OK || http_code >= 400 )
|
if ( curl_code != CURLE_OK || http_code >= 400 )
|
||||||
{
|
{
|
||||||
throw Error()
|
BOOST_THROW_EXCEPTION(
|
||||||
<< CurlCode( curl_code )
|
Error()
|
||||||
<< HttpResponse( http_code )
|
<< CurlCode( curl_code )
|
||||||
<< expt::ErrMsg( error ) ;
|
<< HttpResponse( http_code )
|
||||||
|
<< Url( url )
|
||||||
|
<< expt::ErrMsg( error )
|
||||||
|
) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
return http_code ;
|
return http_code ;
|
||||||
|
@ -163,14 +173,11 @@ long Agent::Put(
|
||||||
|
|
||||||
// set common options
|
// set common options
|
||||||
::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
|
::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
|
||||||
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ;
|
::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ;
|
||||||
::curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ;
|
::curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ;
|
||||||
::curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ;
|
::curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ;
|
||||||
|
|
||||||
return ExecCurl( dest, hdr ) ;
|
return ExecCurl( url, dest, hdr ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
long Agent::Get(
|
long Agent::Get(
|
||||||
|
@ -182,13 +189,10 @@ long Agent::Get(
|
||||||
|
|
||||||
CURL *curl = m_pimpl->curl ;
|
CURL *curl = m_pimpl->curl ;
|
||||||
|
|
||||||
// set common options
|
// set get specific options
|
||||||
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
|
::curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
|
||||||
|
|
||||||
return ExecCurl( dest, hdr ) ;
|
return ExecCurl( url, dest, hdr ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
long Agent::Post(
|
long Agent::Post(
|
||||||
|
@ -201,16 +205,15 @@ long Agent::Post(
|
||||||
|
|
||||||
CURL *curl = m_pimpl->curl ;
|
CURL *curl = m_pimpl->curl ;
|
||||||
|
|
||||||
|
// make a copy because the parameter is const
|
||||||
std::string post_data = data ;
|
std::string post_data = data ;
|
||||||
|
|
||||||
|
// set post specific options
|
||||||
::curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
::curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||||
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &post_data[0] ) ;
|
::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &post_data[0] ) ;
|
||||||
::curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, post_data.size() ) ;
|
::curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, post_data.size() ) ;
|
||||||
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
|
|
||||||
|
|
||||||
return ExecCurl( dest, hdr ) ;
|
return ExecCurl( url, dest, hdr ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
long Agent::Custom(
|
long Agent::Custom(
|
||||||
|
@ -224,11 +227,8 @@ long Agent::Custom(
|
||||||
CURL *curl = m_pimpl->curl ;
|
CURL *curl = m_pimpl->curl ;
|
||||||
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str() );
|
::curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str() );
|
||||||
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
|
|
||||||
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
|
|
||||||
|
|
||||||
return ExecCurl( dest, hdr ) ;
|
return ExecCurl( url, dest, hdr ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Agent::SetHeader( const http::Headers& hdr )
|
void Agent::SetHeader( const http::Headers& hdr )
|
||||||
|
|
|
@ -77,6 +77,7 @@ private :
|
||||||
|
|
||||||
void SetHeader( const http::Headers& hdr ) ;
|
void SetHeader( const http::Headers& hdr ) ;
|
||||||
long ExecCurl(
|
long ExecCurl(
|
||||||
|
const std::string& url,
|
||||||
Receivable *dest,
|
Receivable *dest,
|
||||||
const http::Headers& hdr) ;
|
const http::Headers& hdr) ;
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,10 @@ typedef boost::error_info<struct CurlCodeTag, int> CurlCode ;
|
||||||
// HTTP response code
|
// HTTP response code
|
||||||
typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ;
|
typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ;
|
||||||
|
|
||||||
// HTTP response code
|
// HTTP response body
|
||||||
typedef boost::error_info<struct HttpResponseStrTag, std::string> HttpResponseText ;
|
typedef boost::error_info<struct HttpResponseStrTag, std::string> HttpResponseText ;
|
||||||
|
|
||||||
|
// URL
|
||||||
|
typedef boost::error_info<struct UrlTag, std::string> Url ;
|
||||||
|
|
||||||
} } // end of namespace
|
} } // end of namespace
|
||||||
|
|
Loading…
Reference in New Issue