Skip to content

Commit

Permalink
don't throw in CurlAgent for all HTTP errors (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
nestal committed Jul 25, 2012
1 parent 22fd859 commit 5a0c6ae
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
3 changes: 1 addition & 2 deletions libgrive/src/http/CurlAgent.cc
Expand Up @@ -156,12 +156,11 @@ long CurlAgent::ExecCurl(
Trace( "HTTP response %1%", http_code ) ;
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, 0 ) ;

if ( curl_code != CURLE_OK || (http_code >= 400 && http_code < 500) )
if ( curl_code != CURLE_OK )
{
BOOST_THROW_EXCEPTION(
Error()
<< CurlCode( curl_code )
<< HttpResponse( http_code )
<< Url( url )
<< expt::ErrMsg( error )
<< HttpHeader( hdr )
Expand Down
12 changes: 6 additions & 6 deletions libgrive/src/http/Error.hh
Expand Up @@ -29,16 +29,16 @@ struct Error : virtual Exception {} ;
// CURL error code
typedef boost::error_info<struct CurlCodeTag, int> CurlCode ;

// HTTP response code
typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ;

// HTTP response body
typedef boost::error_info<struct HttpResponseStrTag, std::string> HttpResponseText ;

// URL
typedef boost::error_info<struct UrlTag, std::string> Url ;

// HTTP headers
typedef boost::error_info<struct HeaderTag, Header> HttpHeader ;

// HTTP response code
typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ;

// HTTP response body
typedef boost::error_info<struct HttpResponseStrTag, std::string> HttpResponseText ;

} } // end of namespace
42 changes: 35 additions & 7 deletions libgrive/src/protocol/AuthAgent.cc
Expand Up @@ -19,6 +19,7 @@

#include "AuthAgent.hh"

#include "http/Error.hh"
#include "http/Header.hh"
#include "util/log/Log.hh"
#include "util/OS.hh"
Expand Down Expand Up @@ -50,11 +51,13 @@ long AuthAgent::Put(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;

long response ;
while ( CheckRetry(
response = m_agent->Put(url, data, dest, AppendHeader(hdr)) ) ) ;
response = m_agent->Put(url, data, dest, auth) ) ) ;

return response ;
return CheckHttpResponse(response, url, auth) ;
}

long AuthAgent::Put(
Expand All @@ -63,23 +66,27 @@ long AuthAgent::Put(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;

long response ;
while ( CheckRetry(
response = m_agent->Put( url, file, dest, AppendHeader(hdr) ) ) ) ;

return response ;
return CheckHttpResponse(response, url, auth) ;
}

long AuthAgent::Get(
const std::string& url,
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;

long response ;
while ( CheckRetry(
response = m_agent->Get( url, dest, AppendHeader(hdr) ) ) ) ;

return response ;
return CheckHttpResponse(response, url, auth) ;
}

long AuthAgent::Post(
Expand All @@ -88,11 +95,13 @@ long AuthAgent::Post(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;

long response ;
while ( CheckRetry(
response = m_agent->Post( url, data, dest, AppendHeader(hdr) ) ) ) ;

return response ;
return CheckHttpResponse(response, url, auth) ;
}

long AuthAgent::Custom(
Expand All @@ -101,11 +110,13 @@ long AuthAgent::Custom(
Receivable *dest,
const Header& hdr )
{
Header auth = AppendHeader(hdr) ;

long response ;
while ( CheckRetry(
response = m_agent->Custom( method, url, dest, AppendHeader(hdr) ) ) ) ;

return response ;
return CheckHttpResponse(response, url, auth) ;
}

std::string AuthAgent::RedirLocation() const
Expand Down Expand Up @@ -144,9 +155,26 @@ bool AuthAgent::CheckRetry( long response )
m_auth.Refresh() ;
return true ;
}

else
return false ;
}

long AuthAgent::CheckHttpResponse(
long response,
const std::string& url,
const http::Header& hdr )
{
// throw for other HTTP errors
if ( response >= 400 && response < 500 )
{
BOOST_THROW_EXCEPTION(
Error()
<< HttpResponse( response )
<< Url( url )
<< HttpHeader( hdr ) ) ;
}

return response ;
}

} // end of namespace
4 changes: 4 additions & 0 deletions libgrive/src/protocol/AuthAgent.hh
Expand Up @@ -73,6 +73,10 @@ public :
private :
http::Header AppendHeader( const http::Header& hdr ) const ;
bool CheckRetry( long response ) ;
long CheckHttpResponse(
long response,
const std::string& url,
const http::Header& hdr ) ;

private :
OAuth2 m_auth ;
Expand Down

0 comments on commit 5a0c6ae

Please sign in to comment.