diff --git a/libgrive/src/protocol/HTTP.cc b/libgrive/src/protocol/HTTP.cc index 86062cd..b55c263 100644 --- a/libgrive/src/protocol/HTTP.cc +++ b/libgrive/src/protocol/HTTP.cc @@ -20,9 +20,10 @@ #include "HTTP.hh" #include "Download.hh" -// #include "util/SignalHandler.hh" -#include "xml/Node.hh" -#include "xml/TreeBuilder.hh" +#include "Receivable.hh" + +// #include "xml/Node.hh" +// #include "xml/TreeBuilder.hh" // dependent libraries #include @@ -317,10 +318,16 @@ std::size_t Agent::HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent * return size*nmemb ; } -std::size_t Agent::XmlCallback( void *ptr, size_t size, size_t nmemb, xml::TreeBuilder *tb ) +// std::size_t Agent::XmlCallback( void *ptr, size_t size, size_t nmemb, xml::TreeBuilder *tb ) +// { +// tb->ParseData( reinterpret_cast(ptr), size*nmemb ) ; +// return size*nmemb ; +// } + +std::size_t Agent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv ) { - tb->ParseData( reinterpret_cast(ptr), size*nmemb ) ; - return size*nmemb ; + assert( recv != 0 ) ; + return recv->OnData( ptr, size * nmemb ) ; } std::string Agent::Put( @@ -363,6 +370,33 @@ std::string Agent::Put( return resp ; } +long Agent::Get( + const std::string& url, + Receivable *dest, + const http::Headers& hdr ) +{ + CURL *curl = m_pimpl->curl ; + + // set common options + ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + ::curl_easy_setopt(curl, CURLOPT_HEADER, 0); + ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive); + ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ; + ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ; + + SetHeader( hdr ) ; + + 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 ) ; + + return http_code ; +} + void Agent::SetHeader( const http::Headers& hdr ) { // set headers diff --git a/libgrive/src/protocol/HTTP.hh b/libgrive/src/protocol/HTTP.hh index 4c34237..7284803 100644 --- a/libgrive/src/protocol/HTTP.hh +++ b/libgrive/src/protocol/HTTP.hh @@ -24,16 +24,12 @@ #include #include -namespace gr { namespace xml -{ - class Node ; - class TreeBuilder ; -} } - namespace gr { namespace http { typedef std::vector Headers ; +class Receivable ; + std::string Get( const std::string& url, const Headers& hdr = Headers() ) ; void GetFile( const std::string& url, @@ -75,7 +71,13 @@ public : private : static std::string Format( int curl_code, int http_code, const char *err_buf ) ; } ; + +/*! \class Agent + \brief class to provide HTTP access + This class provides functions to send HTTP request in many methods (e.g. get, post and put). + Normally the HTTP response is returned in a Receivable. +*/ class Agent { public : @@ -87,15 +89,17 @@ public : const std::string& data, const http::Headers& hdr = http::Headers() ) ; - xml::Node GetXml( + long Get( const std::string& url, + Receivable *dest, const http::Headers& hdr = http::Headers() ) ; std::string RedirLocation() const ; private : static std::size_t HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent *pthis ) ; - static std::size_t XmlCallback( void* ptr, size_t size, size_t nmemb, gr::xml::TreeBuilder* tb ) ; +// static std::size_t XmlCallback( void* ptr, size_t size, size_t nmemb, gr::xml::TreeBuilder* tb ) ; + static std::size_t Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv ) ; void SetHeader( const http::Headers& hdr ) ; diff --git a/libgrive/src/protocol/Receivable.hh b/libgrive/src/protocol/Receivable.hh new file mode 100644 index 0000000..8db569b --- /dev/null +++ b/libgrive/src/protocol/Receivable.hh @@ -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 + +namespace gr { namespace http { + +class Receivable +{ +public : + virtual std::size_t OnData( void *data, std::size_t count ) = 0 ; +} ; + +} } // end of namespace