From 5a0c6ae56e682022a2d8777ad51433322aaf20b0 Mon Sep 17 00:00:00 2001 From: Nestal Wan Date: Thu, 26 Jul 2012 01:23:20 +0800 Subject: [PATCH] don't throw in CurlAgent for all HTTP errors (#82) --- libgrive/src/http/CurlAgent.cc | 3 +-- libgrive/src/http/Error.hh | 12 ++++----- libgrive/src/protocol/AuthAgent.cc | 42 +++++++++++++++++++++++++----- libgrive/src/protocol/AuthAgent.hh | 4 +++ 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/libgrive/src/http/CurlAgent.cc b/libgrive/src/http/CurlAgent.cc index 85dfe38e..640e1eb5 100644 --- a/libgrive/src/http/CurlAgent.cc +++ b/libgrive/src/http/CurlAgent.cc @@ -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 ) diff --git a/libgrive/src/http/Error.hh b/libgrive/src/http/Error.hh index a513f5f3..9b41376e 100644 --- a/libgrive/src/http/Error.hh +++ b/libgrive/src/http/Error.hh @@ -29,16 +29,16 @@ struct Error : virtual Exception {} ; // CURL error code typedef boost::error_info CurlCode ; -// HTTP response code -typedef boost::error_info HttpResponse ; - -// HTTP response body -typedef boost::error_info HttpResponseText ; - // URL typedef boost::error_info Url ; // HTTP headers typedef boost::error_info HttpHeader ; +// HTTP response code +typedef boost::error_info HttpResponse ; + +// HTTP response body +typedef boost::error_info HttpResponseText ; + } } // end of namespace diff --git a/libgrive/src/protocol/AuthAgent.cc b/libgrive/src/protocol/AuthAgent.cc index 15732a4c..9d0239b8 100644 --- a/libgrive/src/protocol/AuthAgent.cc +++ b/libgrive/src/protocol/AuthAgent.cc @@ -19,6 +19,7 @@ #include "AuthAgent.hh" +#include "http/Error.hh" #include "http/Header.hh" #include "util/log/Log.hh" #include "util/OS.hh" @@ -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( @@ -63,11 +66,13 @@ 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( @@ -75,11 +80,13 @@ long AuthAgent::Get( 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( @@ -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( @@ -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 @@ -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 diff --git a/libgrive/src/protocol/AuthAgent.hh b/libgrive/src/protocol/AuthAgent.hh index 3b198c60..58900d18 100644 --- a/libgrive/src/protocol/AuthAgent.hh +++ b/libgrive/src/protocol/AuthAgent.hh @@ -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 ;