Skip to content

Commit

Permalink
refactoring. added namespaces for HTTP functions
Browse files Browse the repository at this point in the history
  • Loading branch information
match065 committed Apr 28, 2012
1 parent 9b0597e commit f4ee21f
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 72 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -16,6 +16,7 @@ add_executable( grive
src/protocol/HTTP.cc
src/protocol/Json.cc
src/protocol/OAuth2.cc
src/util/Crypt.cc
src/util/DateTime.cc
src/util/OS.cc
)
Expand Down
42 changes: 7 additions & 35 deletions src/drive/Drive.cc
Expand Up @@ -22,19 +22,15 @@
#include "protocol/HTTP.hh"
#include "protocol/Json.hh"
#include "protocol/OAuth2.hh"
#include "util/Crypt.hh"
#include "util/DateTime.hh"
#include "util/OS.hh"

// dependent libraries
#include <openssl/evp.h>

// standard C++ library
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iomanip>
#include <map>
#include <sstream>

// for debugging only
#include <iostream>
Expand All @@ -43,30 +39,6 @@ namespace gr {

const std::string root_url = "https://docs.google.com/feeds/default/private/full" ;

std::string MD5( std::streambuf *file )
{
char buf[64 * 1024] ;
EVP_MD_CTX md ;
EVP_MD_CTX_init( &md );
EVP_DigestInit_ex( &md, EVP_md5(), 0 ) ;

std::size_t count = 0 ;
while ( (count = file->sgetn( buf, sizeof(buf) )) > 0 )
{
EVP_DigestUpdate( &md, buf, count ) ;
}

unsigned int md5_size = EVP_MAX_MD_SIZE ;
unsigned char md5[EVP_MAX_MD_SIZE] ;
EVP_DigestFinal_ex( &md, md5, &md5_size ) ;

std::ostringstream ss ;
for ( unsigned int i = 0 ; i < md5_size ; i++ )
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(md5[i]) ;

return ss.str() ;
}

Drive::Drive( OAuth2& auth ) :
m_auth( auth ),
m_root( ".", "https://docs.google.com/feeds/default/private/full/folder%3Aroot" )
Expand All @@ -75,7 +47,7 @@ Drive::Drive( OAuth2& auth ) :
m_http_hdr.push_back( "GData-Version: 3.0" ) ;


Json resp = Json::Parse(HttpGet( root_url + "?alt=json&showfolders=true", m_http_hdr )) ;
Json resp = Json::Parse( http::Get( root_url + "?alt=json&showfolders=true", m_http_hdr )) ;
Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
ConstructDirTree( entries ) ;

Expand Down Expand Up @@ -182,8 +154,8 @@ void Drive::UpdateFile( const Json& entry )
if ( entry.Has( "docs$filename" ) )
{
// use title as the filename
std::string filename = entry["docs$filename"]["$t"].As() ;
std::string url = entry["content"]["src"].As() ;
std::string filename = entry["docs$filename"]["$t"].Get() ;
std::string url = entry["content"]["src"].Get() ;
std::string parent_href = Parent( entry ) ;

bool changed = true ;
Expand All @@ -204,7 +176,7 @@ void Drive::UpdateFile( const Json& entry )
if ( ifile && entry.Has("docs$md5Checksum") )
{
std::string remote_md5 = entry["docs$md5Checksum"]["$t"].As<std::string>() ;
std::string local_md5 = MD5( ifile.rdbuf() ) ;
std::string local_md5 = crypt::MD5( ifile.rdbuf() ) ;

std::transform( remote_md5.begin(), remote_md5.end(), remote_md5.begin(), tolower ) ;
if ( local_md5 == remote_md5 )
Expand All @@ -221,7 +193,7 @@ void Drive::UpdateFile( const Json& entry )
if ( remote > local )
{
std::cout << "downloading " << path << std::endl ;
HttpGetFile( url, path, m_http_hdr ) ;
http::GetFile( url, path, m_http_hdr ) ;
os::SetFileTime( path, remote ) ;
}
else
Expand All @@ -241,7 +213,7 @@ void Drive::UploadFile( const Json& entry )
"http://schemas.google.com/g/2005#resumable-edit-media" )["href"] ;
std::cout << resume_link.As<std::string>() << std::endl ;

std::string resp = HttpPut( resume_link.As(), "" ) ;
std::string resp = http::Put( resume_link.Get(), "", m_http_hdr ) ;

std::cout << "resp " << resp ;
}
Expand Down
48 changes: 27 additions & 21 deletions src/protocol/HTTP.cc
Expand Up @@ -31,19 +31,9 @@
#include <sstream>
#include <streambuf>

namespace gr {
namespace {

HttpException::HttpException( int curl_code, int http_code, const char *err_buf )
: runtime_error( Format( curl_code, http_code, err_buf ) )
{
}

std::string HttpException::Format( int curl_code, int http_code, const char *err_buf )
{
std::ostringstream ss ;
ss << "CURL code = " << curl_code << " HTTP code = " << http_code << " (" << err_buf << ")" ;
return ss.str() ;
}
using namespace gr::http ;

// libcurl callback to append to a string
std::size_t WriteCallback( char *data, size_t size, size_t nmemb, std::string *resp )
Expand Down Expand Up @@ -109,16 +99,32 @@ void DoCurl( CURL *curl )

if ( curl_code != CURLE_OK )
{
throw HttpException( curl_code, http_code, error_buf ) ;
throw Exception( curl_code, http_code, error_buf ) ;
}
else if (http_code >= 400 )
{
std::cout << "http error " << http_code << std::endl ;
throw HttpException( curl_code, http_code, error_buf ) ;
throw Exception( curl_code, http_code, error_buf ) ;
}
}

std::string HttpGet( const std::string& url, const Headers& hdr )
} // end of local namespace

namespace gr { namespace http {

Exception::Exception( int curl_code, int http_code, const char *err_buf )
: runtime_error( Format( curl_code, http_code, err_buf ) )
{
}

std::string Exception::Format( int curl_code, int http_code, const char *err_buf )
{
std::ostringstream ss ;
ss << "CURL code = " << curl_code << " HTTP code = " << http_code << " (" << err_buf << ")" ;
return ss.str() ;
}

std::string Get( const std::string& url, const Headers& hdr )
{
std::string resp ;
CURL *curl = InitCurl( url, &resp, hdr ) ;
Expand All @@ -127,7 +133,7 @@ std::string HttpGet( const std::string& url, const Headers& hdr )
return resp;
}

void HttpGetFile(
void GetFile(
const std::string& url,
const std::string& filename,
const Headers& hdr )
Expand All @@ -141,15 +147,15 @@ void HttpGetFile(
DoCurl( curl ) ;
}

void HttpGetFile(
void GetFile(
const std::string& url,
const std::string& filename,
std::string& md5sum,
const Headers& hdr )
{
}

std::string HttpPostData( const std::string& url, const std::string& data, const Headers& hdr )
std::string PostData( const std::string& url, const std::string& data, const Headers& hdr )
{
std::string resp ;
CURL *curl = InitCurl( url, &resp, hdr ) ;
Expand All @@ -164,13 +170,13 @@ std::string HttpPostData( const std::string& url, const std::string& data, const
return resp;
}

std::string HttpPostFile( const std::string& url, const std::string& filename, const Headers& hdr )
std::string PostFile( const std::string& url, const std::string& filename, const Headers& hdr )
{
std::string resp ;
return resp;
}

std::string HttpPut(
std::string Put(
const std::string& url,
const std::string& data,
const Headers& hdr )
Expand Down Expand Up @@ -216,4 +222,4 @@ std::string Unescape( const std::string& str )
return result ;
}

}
} } // end of namespace
22 changes: 12 additions & 10 deletions src/protocol/HTTP.hh
Expand Up @@ -23,44 +23,46 @@
#include <stdexcept>
#include <vector>

namespace gr
namespace gr { namespace http
{
typedef std::vector<std::string> Headers ;

std::string HttpGet( const std::string& url, const Headers& hdr = Headers() ) ;
void HttpGetFile(
std::string Get( const std::string& url, const Headers& hdr = Headers() ) ;
void GetFile(
const std::string& url,
const std::string& filename,
const Headers& hdr = Headers() ) ;

void HttpGetFile(
void GetFile(
const std::string& url,
const std::string& filename,
std::string& md5sum,
const Headers& hdr = Headers() ) ;

std::string HttpPostData(
std::string PostData(
const std::string& url,
const std::string& data,
const Headers& hdr = Headers() ) ;
std::string HttpPostFile(
std::string PostFile(
const std::string& url,
const std::string& filename,
const Headers& hdr = Headers() ) ;

std::string HttpPut(
std::string Put(
const std::string& url,
const std::string& data,
const Headers& hdr = Headers() ) ;

std::string Escape( const std::string& str ) ;
std::string Unescape( const std::string& str ) ;

class HttpException : public std::runtime_error
class Exception : public std::runtime_error
{
public :
HttpException( int curl_code, int http_code, const char *err_buf ) ;
Exception( int curl_code, int http_code, const char *err_buf ) ;

private :
static std::string Format( int curl_code, int http_code, const char *err_buf ) ;
} ;
}

} } // end of namespace
43 changes: 41 additions & 2 deletions src/protocol/Json.cc
Expand Up @@ -138,9 +138,18 @@ void Json::Add( const std::string& key, const Json& json )
::json_object_object_add( m_json, key.c_str(), json.m_json ) ;
}

Json::Proxy Json::As() const
template <>
bool Json::As<bool>() const
{
return Proxy( *this ) ;
assert( m_json != 0 ) ;
return ::json_object_get_boolean( m_json ) ;
}

template <>
bool Json::Is<bool>() const
{
assert( m_json != 0 ) ;
return ::json_object_is_type( m_json, json_type_boolean ) ;
}

template <>
Expand Down Expand Up @@ -248,4 +257,34 @@ bool Json::FindInArray( const std::string& key, const std::string& value, Json&
}
}

Json::Proxy Json::Get() const
{
return Proxy( *this ) ;
}

Json::Proxy::operator bool() const
{
return referring.As<bool>() ;
}

Json::Proxy::operator int() const
{
return referring.As<int>() ;
}

Json::Proxy::operator Object() const
{
return referring.As<Object>() ;
}

Json::Proxy::operator Array() const
{
return referring.As<Array>() ;
}

Json::Proxy::operator std::string() const
{
return referring.As<std::string>() ;
}

}
8 changes: 6 additions & 2 deletions src/protocol/Json.hh
Expand Up @@ -56,9 +56,13 @@ public :
{
const Json& referring ;
explicit Proxy( const Json& j ) : referring( j ) { }
template <typename T> operator T() const { return referring.As<T>() ; }
operator bool() const ;
operator int() const ;
operator Object() const ;
operator Array() const ;
operator std::string() const ;
} ;
Proxy As() const ;
Proxy Get() const ;

template <typename T>
bool Is() const ;
Expand Down
6 changes: 4 additions & 2 deletions src/protocol/OAuth2.cc
Expand Up @@ -57,7 +57,7 @@ void OAuth2::Auth( const std::string& auth_code )
"&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob" +
"&grant_type=authorization_code" ;

Json resp = Json::Parse( HttpPostData( token_url, post ) ) ;
Json resp = Json::Parse( http::PostData( token_url, post ) ) ;
m_access = resp["access_token"].As<std::string>() ;
m_refresh = resp["refresh_token"].As<std::string>() ;
}
Expand All @@ -66,6 +66,8 @@ std::string OAuth2::MakeAuthURL(
const std::string& client_id,
const std::string& state )
{
using gr::http::Escape ;

return "https://accounts.google.com/o/oauth2/auth"
"?scope=" +
Escape( "https://www.googleapis.com/auth/userinfo.email" ) + "+" +
Expand All @@ -86,7 +88,7 @@ void OAuth2::Refresh( )
"&client_secret=" + m_client_secret +
"&grant_type=refresh_token" ;

Json resp = Json::Parse( HttpPostData( token_url, post ) ) ;
Json resp = Json::Parse( http::PostData( token_url, post ) ) ;
m_access = resp["access_token"].As<std::string>() ;
}

Expand Down

0 comments on commit f4ee21f

Please sign in to comment.