added debug logs. the XML nodes need to be improved

pull/40/head
Matchman Green 2012-05-10 01:20:38 +08:00
parent 1ae7a18f63
commit d3b841ccfd
7 changed files with 150 additions and 80 deletions

View File

@ -23,6 +23,7 @@
#include "Entry.hh"
#include "http/Agent.hh"
#include "http/ResponseLog.hh"
#include "http/XmlResponse.hh"
#include "protocol/Json.hh"
#include "protocol/JsonResponse.hh"
@ -114,10 +115,12 @@ Drive::FolderListIterator Drive::FindFolder( const std::string& href )
void Drive::ConstructDirTree( http::Agent *http )
{
http::XmlResponse xml ;
http->Get( root_url + "/-/folder?showroot=true&max-results=10", &xml, m_http_hdr ) ;
http::ResponseLog log( "dir-", &xml ) ;
http->Get( root_url + "/-/folder?showroot=true&max-results=10", &log, m_http_hdr ) ;
std::ofstream abc( "abc.xml" ) ;
abc << xml.Response()["feed"] ;
std::ofstream abc( "abc.xml" ) ;
abc << xml.Response()["feed"]["entry"] ;
http::JsonResponse jrsp ;
http->Get( root_url + "/-/folder?alt=json", &jrsp, m_http_hdr ) ;

View File

@ -97,10 +97,6 @@ void Agent::SetLogFile( const std::string& prefix )
m_pimpl->log_prefix = prefix ;
}
std::string Agent::LogFilename() const
{
}
std::size_t Agent::HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent *pthis )
{
char *str = reinterpret_cast<char*>(ptr) ;
@ -127,24 +123,12 @@ std::size_t Agent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *re
return recv->OnData( ptr, size * nmemb ) ;
}
long Agent::Put(
const std::string& url,
const std::string& data,
long Agent::ExecCurl(
Receivable *dest,
const http::Headers& hdr )
{
CURL *curl = m_pimpl->curl ;
std::string put_data = data ;
// set common options
::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ;
::curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ;
::curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ;
assert( curl != 0 ) ;
SetHeader( hdr ) ;
@ -167,6 +151,28 @@ long Agent::Put(
return http_code ;
}
long Agent::Put(
const std::string& url,
const std::string& data,
Receivable *dest,
const http::Headers& hdr )
{
CURL *curl = m_pimpl->curl ;
std::string put_data = data ;
// set common options
::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ;
::curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ;
::curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ;
return ExecCurl( dest, hdr ) ;
}
long Agent::Get(
const std::string& url,
Receivable *dest,
@ -180,29 +186,7 @@ long Agent::Get(
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
::curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
SetHeader( hdr ) ;
/*
static int s_count = 0 ;
std::ostringstream logss ;
logss << "get" << s_count++ << ".txt" ;
g_log.open( logss.str().c_str() ) ;
*/
dest->Clear() ;
CURLcode curl_code = ::curl_easy_perform(curl);
// g_log.close() ;
long http_code = 0;
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if ( curl_code != CURLE_OK )
throw Exception( curl_code, http_code, m_pimpl->error ) ;
else if (http_code >= 400 )
{
std::cout << "http error " << http_code << std::endl ;
throw Exception( curl_code, http_code, m_pimpl->error ) ;
}
return http_code ;
return ExecCurl( dest, hdr ) ;
}
long Agent::Post(
@ -222,23 +206,7 @@ long Agent::Post(
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
SetHeader( hdr ) ;
dest->Clear() ;
CURLcode curl_code = ::curl_easy_perform(curl);
long http_code = 0;
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if ( curl_code != CURLE_OK )
throw Exception( curl_code, http_code, m_pimpl->error ) ;
else if (http_code >= 400 )
{
std::cout << "http error " << http_code << std::endl ;
throw Exception( curl_code, http_code, m_pimpl->error ) ;
}
return http_code ;
return ExecCurl( dest, hdr ) ;
}
long Agent::Custom(
@ -254,23 +222,7 @@ long Agent::Custom(
::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ;
::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ;
SetHeader( hdr ) ;
dest->Clear() ;
CURLcode curl_code = ::curl_easy_perform(curl);
long http_code = 0;
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if ( curl_code != CURLE_OK )
throw Exception( curl_code, http_code, m_pimpl->error ) ;
else if (http_code >= 400 )
{
std::cout << "http error " << http_code << std::endl ;
throw Exception( curl_code, http_code, m_pimpl->error ) ;
}
return http_code ;
return ExecCurl( dest, hdr ) ;
}
void Agent::SetHeader( const http::Headers& hdr )

View File

@ -76,7 +76,9 @@ private :
static std::size_t Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv ) ;
void SetHeader( const http::Headers& hdr ) ;
std::string LogFilename() const ;
long ExecCurl(
Receivable *dest,
const http::Headers& hdr) ;
private :
struct Impl ;

View File

@ -0,0 +1,51 @@
/*
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 "ResponseLog.hh"
#include "util/DateTime.hh"
#include <cassert>
namespace gr { namespace http {
ResponseLog::ResponseLog( const std::string& prefix, Receivable *next ) :
m_log( Filename(prefix).c_str() ),
m_next( next )
{
}
std::size_t ResponseLog::OnData( void *data, std::size_t count )
{
m_log.rdbuf()->sputn( reinterpret_cast<char*>(data), count ) ;
return m_next->OnData( data, count ) ;
}
void ResponseLog::Clear()
{
assert( m_next != 0 ) ;
m_next->Clear() ;
}
std::string ResponseLog::Filename( const std::string& prefix )
{
return prefix + DateTime::Now().Format( "%H%M%S" ) ;
}
}} // end of namespace

View File

@ -0,0 +1,45 @@
/*
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 "Receivable.hh"
#include <fstream>
#include <string>
namespace gr { namespace http {
class ResponseLog : public Receivable
{
public :
ResponseLog( const std::string& prefix, Receivable *next ) ;
std::size_t OnData( void *data, std::size_t count ) ;
void Clear() ;
private :
static std::string Filename( const std::string& prefix ) ;
private :
std::ofstream m_log ;
Receivable *m_next ;
} ;
} } // end of namespace

View File

@ -59,6 +59,20 @@ DateTime::DateTime( std::time_t sec, unsigned long nsec ) :
{
}
DateTime DateTime::Now()
{
return DateTime( std::time(0) ) ;
}
std::string DateTime::Format( const std::string& format ) const
{
struct tm tp = Tm() ;
char tmp[1024] ;
std::size_t count = ::strftime( tmp, sizeof(tmp), format.c_str(), &tp ) ;
return count > 0 ? std::string( tmp, count ) : "" ;
}
struct tm DateTime::Tm() const
{
struct tm tp ;

View File

@ -34,9 +34,12 @@ public :
explicit DateTime( const std::string& iso ) ;
explicit DateTime( std::time_t sec, unsigned long nsec = 0 ) ;
static DateTime Now() ;
std::time_t Sec( ) const ;
unsigned long NanoSec( ) const ;
std::string Format( const std::string& format ) const ;
tm Tm() const ;
timeval Tv() const ;