2012-07-19 20:39:36 +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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AuthAgent.hh"
|
2012-07-19 20:50:40 +04:00
|
|
|
|
2012-07-25 21:23:20 +04:00
|
|
|
#include "http/Error.hh"
|
2012-07-19 20:39:36 +04:00
|
|
|
#include "http/Header.hh"
|
2012-07-19 20:50:40 +04:00
|
|
|
#include "util/log/Log.hh"
|
|
|
|
#include "util/OS.hh"
|
2013-11-25 02:07:27 +04:00
|
|
|
#include "util/File.hh"
|
2012-07-19 20:39:36 +04:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace gr {
|
|
|
|
|
|
|
|
using namespace http ;
|
|
|
|
|
2015-09-30 15:01:50 +03:00
|
|
|
AuthAgent::AuthAgent( OAuth2& auth, std::auto_ptr<Agent> real_agent ) :
|
2012-07-19 20:39:36 +04:00
|
|
|
m_auth ( auth ),
|
|
|
|
m_agent ( real_agent )
|
|
|
|
{
|
|
|
|
assert( m_agent.get() != 0 ) ;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:01:50 +03:00
|
|
|
http::ResponseLog* AuthAgent::GetLog() const
|
|
|
|
{
|
|
|
|
return m_agent->GetLog();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthAgent::SetLog( http::ResponseLog *log )
|
|
|
|
{
|
|
|
|
return m_agent->SetLog( log );
|
|
|
|
}
|
|
|
|
|
2012-07-19 20:39:36 +04:00
|
|
|
Header AuthAgent::AppendHeader( const Header& hdr ) const
|
|
|
|
{
|
|
|
|
Header h(hdr) ;
|
|
|
|
h.Add( "Authorization: Bearer " + m_auth.AccessToken() ) ;
|
|
|
|
h.Add( "GData-Version: 3.0" ) ;
|
|
|
|
return h ;
|
|
|
|
}
|
|
|
|
|
|
|
|
long AuthAgent::Put(
|
|
|
|
const std::string& url,
|
|
|
|
const std::string& data,
|
2013-04-28 21:25:34 +04:00
|
|
|
DataStream *dest,
|
2012-07-19 20:39:36 +04:00
|
|
|
const Header& hdr )
|
|
|
|
{
|
2015-08-04 22:59:09 +03:00
|
|
|
long response;
|
|
|
|
Header auth;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auth = AppendHeader( hdr );
|
|
|
|
response = m_agent->Put( url, data, dest, auth );
|
|
|
|
} while ( CheckRetry( response ) );
|
|
|
|
return CheckHttpResponse( response, url, auth );
|
2012-07-19 20:39:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
long AuthAgent::Put(
|
|
|
|
const std::string& url,
|
2013-04-28 21:25:34 +04:00
|
|
|
File *file,
|
|
|
|
DataStream *dest,
|
2012-07-19 20:39:36 +04:00
|
|
|
const Header& hdr )
|
|
|
|
{
|
2015-08-04 22:59:09 +03:00
|
|
|
long response;
|
|
|
|
Header auth;
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
auth = AppendHeader( hdr );
|
2013-11-25 02:07:27 +04:00
|
|
|
response = m_agent->Put( url, file, dest, auth );
|
2015-08-04 22:59:09 +03:00
|
|
|
if ( !CheckRetry( response ) )
|
|
|
|
break;
|
|
|
|
file->Seek( 0, SEEK_SET );
|
2013-11-25 02:07:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// On 410 Gone or 412 Precondition failed, recovery may be possible so don't
|
|
|
|
// throw an exception
|
|
|
|
if ( response == 410 || response == 412 )
|
|
|
|
return response;
|
2012-07-19 20:50:40 +04:00
|
|
|
|
2015-08-04 22:59:09 +03:00
|
|
|
return CheckHttpResponse( response, url, auth );
|
2012-07-19 20:39:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
long AuthAgent::Get(
|
|
|
|
const std::string& url,
|
2013-04-28 21:25:34 +04:00
|
|
|
DataStream *dest,
|
2012-07-19 20:39:36 +04:00
|
|
|
const Header& hdr )
|
|
|
|
{
|
2015-08-04 22:59:09 +03:00
|
|
|
long response;
|
|
|
|
Header auth;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auth = AppendHeader( hdr );
|
|
|
|
response = m_agent->Get( url, dest, auth );
|
|
|
|
} while ( CheckRetry( response ) );
|
|
|
|
return CheckHttpResponse( response, url, auth );
|
2012-07-19 20:39:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
long AuthAgent::Post(
|
|
|
|
const std::string& url,
|
|
|
|
const std::string& data,
|
2013-04-28 21:25:34 +04:00
|
|
|
DataStream *dest,
|
2012-07-19 20:39:36 +04:00
|
|
|
const Header& hdr )
|
|
|
|
{
|
2015-08-04 22:59:09 +03:00
|
|
|
long response;
|
|
|
|
Header auth;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auth = AppendHeader( hdr );
|
|
|
|
response = m_agent->Post( url, data, dest, auth );
|
|
|
|
} while ( CheckRetry( response ) );
|
|
|
|
return CheckHttpResponse( response, url, auth );
|
2012-07-19 20:39:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
long AuthAgent::Custom(
|
|
|
|
const std::string& method,
|
|
|
|
const std::string& url,
|
2013-04-28 21:25:34 +04:00
|
|
|
DataStream *dest,
|
2012-07-19 20:39:36 +04:00
|
|
|
const Header& hdr )
|
|
|
|
{
|
2015-08-04 22:59:09 +03:00
|
|
|
long response;
|
|
|
|
Header auth;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auth = AppendHeader( hdr );
|
|
|
|
response = m_agent->Custom( method, url, dest, auth );
|
|
|
|
} while ( CheckRetry( response ) );
|
|
|
|
return CheckHttpResponse( response, url, auth );
|
2012-07-19 20:39:36 +04:00
|
|
|
}
|
|
|
|
|
2015-06-27 22:58:01 +03:00
|
|
|
std::string AuthAgent::LastError() const
|
|
|
|
{
|
|
|
|
return m_agent->LastError() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AuthAgent::LastErrorHeaders() const
|
|
|
|
{
|
|
|
|
return m_agent->LastErrorHeaders() ;
|
|
|
|
}
|
|
|
|
|
2012-07-19 20:39:36 +04:00
|
|
|
std::string AuthAgent::RedirLocation() const
|
|
|
|
{
|
|
|
|
return m_agent->RedirLocation() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AuthAgent::Escape( const std::string& str )
|
|
|
|
{
|
|
|
|
return m_agent->Escape( str ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AuthAgent::Unescape( const std::string& str )
|
|
|
|
{
|
|
|
|
return m_agent->Unescape( str ) ;
|
|
|
|
}
|
|
|
|
|
2012-07-19 20:50:40 +04:00
|
|
|
bool AuthAgent::CheckRetry( long response )
|
|
|
|
{
|
2015-06-27 22:58:01 +03:00
|
|
|
// HTTP 500 and 503 should be temporary. just wait a bit and retry
|
2012-07-19 20:50:40 +04:00
|
|
|
if ( response == 500 || response == 503 )
|
|
|
|
{
|
2015-07-04 00:25:13 +03:00
|
|
|
Log( "request failed due to temporary error: %1% (body: %2%). retrying in 5 seconds",
|
2015-06-27 22:58:01 +03:00
|
|
|
response, m_agent->LastError(), log::warning ) ;
|
2012-07-19 20:50:40 +04:00
|
|
|
|
|
|
|
os::Sleep( 5 ) ;
|
|
|
|
return true ;
|
|
|
|
}
|
2012-07-23 12:00:21 +04:00
|
|
|
|
|
|
|
// HTTP 401 Unauthorized. the auth token has been expired. refresh it
|
|
|
|
else if ( response == 401 )
|
|
|
|
{
|
2015-07-04 00:25:13 +03:00
|
|
|
Log( "request failed due to auth token expired: %1% (body: %2%). refreshing token",
|
|
|
|
response, m_agent->LastError(), log::warning ) ;
|
2012-07-23 12:00:21 +04:00
|
|
|
|
2013-11-27 01:00:42 +04:00
|
|
|
os::Sleep( 5 ) ;
|
2012-07-23 12:00:21 +04:00
|
|
|
m_auth.Refresh() ;
|
|
|
|
return true ;
|
|
|
|
}
|
2012-07-19 20:50:40 +04:00
|
|
|
else
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
2012-07-25 21:23:20 +04:00
|
|
|
long AuthAgent::CheckHttpResponse(
|
|
|
|
long response,
|
|
|
|
const std::string& url,
|
|
|
|
const http::Header& hdr )
|
|
|
|
{
|
|
|
|
// throw for other HTTP errors
|
2015-06-27 22:58:01 +03:00
|
|
|
if ( response >= 400 )
|
2012-07-25 21:23:20 +04:00
|
|
|
{
|
2015-06-27 22:58:01 +03:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
Error()
|
|
|
|
<< HttpResponseCode( response )
|
|
|
|
<< HttpResponseHeaders( m_agent->LastErrorHeaders() )
|
|
|
|
<< HttpResponseText( m_agent->LastError() )
|
|
|
|
<< Url( url )
|
|
|
|
<< HttpRequestHeaders( hdr ) ) ;
|
2012-07-25 21:23:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return response ;
|
|
|
|
}
|
|
|
|
|
2012-07-19 20:39:36 +04:00
|
|
|
} // end of namespace
|