mirror of https://github.com/vitalif/grive2
fixed state reuse in the receivables
parent
d621aa4296
commit
06158e6bf0
|
@ -113,6 +113,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 ) ;
|
||||
|
||||
std::ofstream abc( "abc.xml" ) ;
|
||||
abc << xml.Response()["feed"] ;
|
||||
|
||||
http::JsonResponse jrsp ;
|
||||
http->Get( root_url + "/-/folder?alt=json", &jrsp, m_http_hdr ) ;
|
||||
Json resp = jrsp.Response() ;
|
||||
|
@ -193,6 +199,7 @@ void Drive::UpdateFile( const Json& entry )
|
|||
if ( pit != m_coll.end() )
|
||||
path = pit->Dir() / file.Filename() ;
|
||||
}
|
||||
// std::cout << "2:" << path << std::endl;
|
||||
|
||||
// compare checksum first if file exists
|
||||
std::ifstream ifile( path.Str().c_str(), std::ios::binary | std::ios::in ) ;
|
||||
|
|
|
@ -58,6 +58,11 @@ Download::~Download( )
|
|||
::EVP_MD_CTX_destroy( m_mdctx ) ;
|
||||
}
|
||||
|
||||
void Download::Clear()
|
||||
{
|
||||
// no need to do anything
|
||||
}
|
||||
|
||||
std::string Download::Finish() const
|
||||
{
|
||||
// Unregister the signal
|
||||
|
|
|
@ -38,6 +38,7 @@ public :
|
|||
|
||||
std::string Finish() const ;
|
||||
|
||||
void Clear() ;
|
||||
std::size_t OnData( void *data, std::size_t count ) ;
|
||||
|
||||
static std::size_t Callback( char *data, std::size_t size, std::size_t nmemb, Download *pthis ) ;
|
||||
|
|
|
@ -106,8 +106,12 @@ std::size_t Agent::HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent *
|
|||
return size*nmemb ;
|
||||
}
|
||||
|
||||
// std::ofstream g_log ;
|
||||
|
||||
std::size_t Agent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv )
|
||||
{
|
||||
// g_log.rdbuf()->sputn( (char*)ptr, size*nmemb ) ;
|
||||
|
||||
assert( recv != 0 ) ;
|
||||
return recv->OnData( ptr, size * nmemb ) ;
|
||||
}
|
||||
|
@ -133,6 +137,7 @@ long Agent::Put(
|
|||
|
||||
SetHeader( hdr ) ;
|
||||
|
||||
dest->Clear() ;
|
||||
CURLcode curl_code = ::curl_easy_perform(curl);
|
||||
|
||||
long http_code = 0;
|
||||
|
@ -165,8 +170,15 @@ long Agent::Get(
|
|||
::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);
|
||||
|
@ -201,6 +213,7 @@ long Agent::Post(
|
|||
|
||||
SetHeader( hdr ) ;
|
||||
|
||||
dest->Clear() ;
|
||||
CURLcode curl_code = ::curl_easy_perform(curl);
|
||||
|
||||
long http_code = 0;
|
||||
|
@ -232,6 +245,7 @@ long Agent::Custom(
|
|||
|
||||
SetHeader( hdr ) ;
|
||||
|
||||
dest->Clear() ;
|
||||
CURLcode curl_code = ::curl_easy_perform(curl);
|
||||
|
||||
long http_code = 0;
|
||||
|
|
|
@ -27,6 +27,7 @@ class Receivable
|
|||
{
|
||||
public :
|
||||
virtual std::size_t OnData( void *data, std::size_t count ) = 0 ;
|
||||
virtual void Clear() = 0 ;
|
||||
} ;
|
||||
|
||||
} } // end of namespace
|
||||
|
|
|
@ -25,6 +25,11 @@ StringResponse::StringResponse()
|
|||
{
|
||||
}
|
||||
|
||||
void StringResponse::Clear()
|
||||
{
|
||||
m_resp.clear() ;
|
||||
}
|
||||
|
||||
std::size_t StringResponse::OnData( void *data, std::size_t count )
|
||||
{
|
||||
m_resp.append( reinterpret_cast<char*>(data), count ) ;
|
||||
|
|
|
@ -31,6 +31,7 @@ public :
|
|||
StringResponse() ;
|
||||
|
||||
std::size_t OnData( void *data, std::size_t count ) ;
|
||||
void Clear() ;
|
||||
|
||||
const std::string& Response() const ;
|
||||
|
||||
|
|
|
@ -20,27 +20,33 @@
|
|||
#include "XmlResponse.hh"
|
||||
|
||||
#include "xml/Node.hh"
|
||||
#include "xml/TreeBuilder.hh"
|
||||
|
||||
namespace gr { namespace http {
|
||||
|
||||
XmlResponse::XmlResponse()
|
||||
XmlResponse::XmlResponse() : m_tb( new xml::TreeBuilder )
|
||||
{
|
||||
}
|
||||
|
||||
std::size_t XmlResponse::OnData( void *data, std::size_t count )
|
||||
{
|
||||
m_tb.ParseData( reinterpret_cast<char*>(data), count ) ;
|
||||
m_tb->ParseData( reinterpret_cast<char*>(data), count ) ;
|
||||
return count ;
|
||||
}
|
||||
|
||||
void XmlResponse::Clear()
|
||||
{
|
||||
m_tb.reset( new xml::TreeBuilder ) ;
|
||||
}
|
||||
|
||||
void XmlResponse::Finish()
|
||||
{
|
||||
m_tb.ParseData( 0, 0, true ) ;
|
||||
m_tb->ParseData( 0, 0, true ) ;
|
||||
}
|
||||
|
||||
xml::Node XmlResponse::Response() const
|
||||
{
|
||||
return m_tb.Result() ;
|
||||
return m_tb->Result() ;
|
||||
}
|
||||
|
||||
} } // end of namespace
|
||||
|
|
|
@ -21,11 +21,12 @@
|
|||
|
||||
#include "Receivable.hh"
|
||||
|
||||
#include "xml/TreeBuilder.hh"
|
||||
#include <memory>
|
||||
|
||||
namespace gr { namespace xml
|
||||
{
|
||||
class Node ;
|
||||
class TreeBuilder ;
|
||||
} }
|
||||
|
||||
namespace gr { namespace http {
|
||||
|
@ -35,13 +36,14 @@ class XmlResponse : public Receivable
|
|||
public :
|
||||
XmlResponse() ;
|
||||
|
||||
void Clear() ;
|
||||
std::size_t OnData( void *data, std::size_t count ) ;
|
||||
void Finish() ;
|
||||
|
||||
xml::Node Response() const ;
|
||||
|
||||
private :
|
||||
xml::TreeBuilder m_tb ;
|
||||
std::auto_ptr<xml::TreeBuilder> m_tb ;
|
||||
} ;
|
||||
|
||||
} } // end of namespace
|
||||
|
|
|
@ -76,6 +76,7 @@ private :
|
|||
Json( struct json_object *json, NotOwned ) ;
|
||||
|
||||
private :
|
||||
public :
|
||||
struct json_object *m_json ;
|
||||
} ;
|
||||
|
||||
|
|
|
@ -27,6 +27,11 @@ JsonResponse::JsonResponse()
|
|||
{
|
||||
}
|
||||
|
||||
void JsonResponse::Clear()
|
||||
{
|
||||
m_resp.Clear() ;
|
||||
}
|
||||
|
||||
std::size_t JsonResponse::OnData( void *data, std::size_t count )
|
||||
{
|
||||
return m_resp.OnData( data, count ) ;
|
||||
|
|
|
@ -35,6 +35,7 @@ public :
|
|||
JsonResponse() ;
|
||||
|
||||
std::size_t OnData( void *data, std::size_t count ) ;
|
||||
void Clear() ;
|
||||
|
||||
Json Response() const ;
|
||||
|
||||
|
|
|
@ -365,16 +365,39 @@ std::ostream& operator<<( std::ostream& os, const Node& node )
|
|||
}
|
||||
else if ( node.GetType() == Node::attr )
|
||||
{
|
||||
os << node.Name() << "=\"" << node.Value() << "\"" ;
|
||||
os << node.Name() << "=\"" ;
|
||||
Node::PrintString( os, node.Value() ) ;
|
||||
os << "\"" ;
|
||||
}
|
||||
else
|
||||
{
|
||||
os << node.Value() ;
|
||||
Node::PrintString( os, node.Value() ) ;
|
||||
}
|
||||
|
||||
return os ;
|
||||
}
|
||||
|
||||
std::ostream& Node::PrintString( std::ostream& os, const std::string& s )
|
||||
{
|
||||
for ( std::string::const_iterator i = s.begin() ; i != s.end() ; ++i )
|
||||
Node::PrintChar( os, *i ) ;
|
||||
return os ;
|
||||
}
|
||||
|
||||
std::ostream& Node::PrintChar( std::ostream& os, char c )
|
||||
{
|
||||
switch ( c )
|
||||
{
|
||||
case '\"': os << """ ; break ;
|
||||
case '\'': os << "'" ; break ;
|
||||
case '&': os << "&" ; break ;
|
||||
case '<': os << "<" ; break ;
|
||||
case '>': os << ">" ; break ;
|
||||
default : os << c ; break ;
|
||||
}
|
||||
return os ;
|
||||
}
|
||||
|
||||
Node::iterator Node::begin() const
|
||||
{
|
||||
return iterator( m_ptr->Begin() ) ;
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
@ -58,6 +59,8 @@ public :
|
|||
Type GetType() const ;
|
||||
|
||||
static bool IsCompatible( Type parent, Type child ) ;
|
||||
static std::ostream& PrintChar( std::ostream& os, char c ) ;
|
||||
static std::ostream& PrintString( std::ostream& os, const std::string& s ) ;
|
||||
|
||||
iterator begin() const ;
|
||||
iterator end() const ;
|
||||
|
@ -92,8 +95,6 @@ public :
|
|||
ImplVec::iterator m_it ;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
private :
|
||||
explicit Node( Impl *impl ) ;
|
||||
|
||||
|
|
Loading…
Reference in New Issue