Permalink
Browse files

don't throw in CurlAgent for all HTTP errors (#82)

  • Loading branch information...
1 parent 22fd859 commit 5a0c6ae56e682022a2d8777ad51433322aaf20b0 @nestal nestal committed Jul 25, 2012
View
3 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 )
View
12 libgrive/src/http/Error.hh
@@ -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
View
42 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,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(
@@ -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
View
4 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 ;

0 comments on commit 5a0c6ae

Please sign in to comment.