refactoring. added namespaces for HTTP functions

pull/40/head
Matchman Green 2012-04-29 00:01:45 +08:00
parent 9b0597eb08
commit f4ee21f5e4
9 changed files with 185 additions and 72 deletions

View File

@ -16,6 +16,7 @@ add_executable( grive
src/protocol/HTTP.cc src/protocol/HTTP.cc
src/protocol/Json.cc src/protocol/Json.cc
src/protocol/OAuth2.cc src/protocol/OAuth2.cc
src/util/Crypt.cc
src/util/DateTime.cc src/util/DateTime.cc
src/util/OS.cc src/util/OS.cc
) )

View File

@ -22,19 +22,15 @@
#include "protocol/HTTP.hh" #include "protocol/HTTP.hh"
#include "protocol/Json.hh" #include "protocol/Json.hh"
#include "protocol/OAuth2.hh" #include "protocol/OAuth2.hh"
#include "util/Crypt.hh"
#include "util/DateTime.hh" #include "util/DateTime.hh"
#include "util/OS.hh" #include "util/OS.hh"
// dependent libraries
#include <openssl/evp.h>
// standard C++ library // standard C++ library
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <fstream> #include <fstream>
#include <iomanip>
#include <map> #include <map>
#include <sstream>
// for debugging only // for debugging only
#include <iostream> #include <iostream>
@ -43,30 +39,6 @@ namespace gr {
const std::string root_url = "https://docs.google.com/feeds/default/private/full" ; 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 ) : Drive::Drive( OAuth2& auth ) :
m_auth( auth ), m_auth( auth ),
m_root( ".", "https://docs.google.com/feeds/default/private/full/folder%3Aroot" ) m_root( ".", "https://docs.google.com/feeds/default/private/full/folder%3Aroot" )
@ -75,7 +47,7 @@ Drive::Drive( OAuth2& auth ) :
m_http_hdr.push_back( "GData-Version: 3.0" ) ; 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>() ; Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
ConstructDirTree( entries ) ; ConstructDirTree( entries ) ;
@ -182,8 +154,8 @@ void Drive::UpdateFile( const Json& entry )
if ( entry.Has( "docs$filename" ) ) if ( entry.Has( "docs$filename" ) )
{ {
// use title as the filename // use title as the filename
std::string filename = entry["docs$filename"]["$t"].As() ; std::string filename = entry["docs$filename"]["$t"].Get() ;
std::string url = entry["content"]["src"].As() ; std::string url = entry["content"]["src"].Get() ;
std::string parent_href = Parent( entry ) ; std::string parent_href = Parent( entry ) ;
bool changed = true ; bool changed = true ;
@ -204,7 +176,7 @@ void Drive::UpdateFile( const Json& entry )
if ( ifile && entry.Has("docs$md5Checksum") ) if ( ifile && entry.Has("docs$md5Checksum") )
{ {
std::string remote_md5 = entry["docs$md5Checksum"]["$t"].As<std::string>() ; 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 ) ; std::transform( remote_md5.begin(), remote_md5.end(), remote_md5.begin(), tolower ) ;
if ( local_md5 == remote_md5 ) if ( local_md5 == remote_md5 )
@ -221,7 +193,7 @@ void Drive::UpdateFile( const Json& entry )
if ( remote > local ) if ( remote > local )
{ {
std::cout << "downloading " << path << std::endl ; std::cout << "downloading " << path << std::endl ;
HttpGetFile( url, path, m_http_hdr ) ; http::GetFile( url, path, m_http_hdr ) ;
os::SetFileTime( path, remote ) ; os::SetFileTime( path, remote ) ;
} }
else else
@ -241,7 +213,7 @@ void Drive::UploadFile( const Json& entry )
"http://schemas.google.com/g/2005#resumable-edit-media" )["href"] ; "http://schemas.google.com/g/2005#resumable-edit-media" )["href"] ;
std::cout << resume_link.As<std::string>() << std::endl ; 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 ; std::cout << "resp " << resp ;
} }

View File

@ -31,19 +31,9 @@
#include <sstream> #include <sstream>
#include <streambuf> #include <streambuf>
namespace gr { namespace {
HttpException::HttpException( int curl_code, int http_code, const char *err_buf ) using namespace gr::http ;
: 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() ;
}
// libcurl callback to append to a string // libcurl callback to append to a string
std::size_t WriteCallback( char *data, size_t size, size_t nmemb, std::string *resp ) std::size_t WriteCallback( char *data, size_t size, size_t nmemb, std::string *resp )
@ -109,16 +99,32 @@ void DoCurl( CURL *curl )
if ( curl_code != CURLE_OK ) 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 ) else if (http_code >= 400 )
{ {
std::cout << "http error " << http_code << std::endl ; 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 ; std::string resp ;
CURL *curl = InitCurl( url, &resp, hdr ) ; CURL *curl = InitCurl( url, &resp, hdr ) ;
@ -127,7 +133,7 @@ std::string HttpGet( const std::string& url, const Headers& hdr )
return resp; return resp;
} }
void HttpGetFile( void GetFile(
const std::string& url, const std::string& url,
const std::string& filename, const std::string& filename,
const Headers& hdr ) const Headers& hdr )
@ -141,7 +147,7 @@ void HttpGetFile(
DoCurl( curl ) ; DoCurl( curl ) ;
} }
void HttpGetFile( void GetFile(
const std::string& url, const std::string& url,
const std::string& filename, const std::string& filename,
std::string& md5sum, std::string& md5sum,
@ -149,7 +155,7 @@ void HttpGetFile(
{ {
} }
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 ; std::string resp ;
CURL *curl = InitCurl( url, &resp, hdr ) ; CURL *curl = InitCurl( url, &resp, hdr ) ;
@ -164,13 +170,13 @@ std::string HttpPostData( const std::string& url, const std::string& data, const
return resp; 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 ; std::string resp ;
return resp; return resp;
} }
std::string HttpPut( std::string Put(
const std::string& url, const std::string& url,
const std::string& data, const std::string& data,
const Headers& hdr ) const Headers& hdr )
@ -216,4 +222,4 @@ std::string Unescape( const std::string& str )
return result ; return result ;
} }
} } } // end of namespace

View File

@ -23,44 +23,46 @@
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
namespace gr namespace gr { namespace http
{ {
typedef std::vector<std::string> Headers ; typedef std::vector<std::string> Headers ;
std::string HttpGet( const std::string& url, const Headers& hdr = Headers() ) ; std::string Get( const std::string& url, const Headers& hdr = Headers() ) ;
void HttpGetFile( void GetFile(
const std::string& url, const std::string& url,
const std::string& filename, const std::string& filename,
const Headers& hdr = Headers() ) ; const Headers& hdr = Headers() ) ;
void HttpGetFile( void GetFile(
const std::string& url, const std::string& url,
const std::string& filename, const std::string& filename,
std::string& md5sum, std::string& md5sum,
const Headers& hdr = Headers() ) ; const Headers& hdr = Headers() ) ;
std::string HttpPostData( std::string PostData(
const std::string& url, const std::string& url,
const std::string& data, const std::string& data,
const Headers& hdr = Headers() ) ; const Headers& hdr = Headers() ) ;
std::string HttpPostFile( std::string PostFile(
const std::string& url, const std::string& url,
const std::string& filename, const std::string& filename,
const Headers& hdr = Headers() ) ; const Headers& hdr = Headers() ) ;
std::string HttpPut( std::string Put(
const std::string& url, const std::string& url,
const std::string& data,
const Headers& hdr = Headers() ) ; const Headers& hdr = Headers() ) ;
std::string Escape( const std::string& str ) ; std::string Escape( const std::string& str ) ;
std::string Unescape( const std::string& str ) ; std::string Unescape( const std::string& str ) ;
class HttpException : public std::runtime_error class Exception : public std::runtime_error
{ {
public : public :
HttpException( int curl_code, int http_code, const char *err_buf ) ; Exception( int curl_code, int http_code, const char *err_buf ) ;
private : private :
static std::string Format( int curl_code, int http_code, const char *err_buf ) ; static std::string Format( int curl_code, int http_code, const char *err_buf ) ;
} ; } ;
}
} } // end of namespace

View File

@ -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_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 <> template <>
@ -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>() ;
}
} }

View File

@ -56,9 +56,13 @@ public :
{ {
const Json& referring ; const Json& referring ;
explicit Proxy( const Json& j ) : referring( j ) { } 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> template <typename T>
bool Is() const ; bool Is() const ;

View File

@ -57,7 +57,7 @@ void OAuth2::Auth( const std::string& auth_code )
"&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob" + "&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob" +
"&grant_type=authorization_code" ; "&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_access = resp["access_token"].As<std::string>() ;
m_refresh = resp["refresh_token"].As<std::string>() ; m_refresh = resp["refresh_token"].As<std::string>() ;
} }
@ -66,6 +66,8 @@ std::string OAuth2::MakeAuthURL(
const std::string& client_id, const std::string& client_id,
const std::string& state ) const std::string& state )
{ {
using gr::http::Escape ;
return "https://accounts.google.com/o/oauth2/auth" return "https://accounts.google.com/o/oauth2/auth"
"?scope=" + "?scope=" +
Escape( "https://www.googleapis.com/auth/userinfo.email" ) + "+" + Escape( "https://www.googleapis.com/auth/userinfo.email" ) + "+" +
@ -86,7 +88,7 @@ void OAuth2::Refresh( )
"&client_secret=" + m_client_secret + "&client_secret=" + m_client_secret +
"&grant_type=refresh_token" ; "&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>() ; m_access = resp["access_token"].As<std::string>() ;
} }

55
src/util/Crypt.cc Normal file
View File

@ -0,0 +1,55 @@
/*
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 "Crypt.hh"
#include <iomanip>
#include <sstream>
// dependent libraries
#include <openssl/evp.h>
namespace gr { namespace crypt {
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 ) ;
// format the MD5 string
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() ;
}
} } // end of namespaces

32
src/util/Crypt.hh Normal file
View File

@ -0,0 +1,32 @@
/*
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.
*/
#pragma once
#include <string>
#include <iosfwd>
namespace gr {
namespace crypt
{
std::string MD5( std::streambuf *file ) ;
}
} // end of namespace gr