added HTTP receiver callback interface

pull/40/head
Matchman Green 2012-05-06 01:12:44 +08:00
parent ba737cba29
commit 1e6e6a4347
3 changed files with 84 additions and 14 deletions

View File

@ -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 <curl/curl.h>
@ -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<char*>(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<char*>(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

View File

@ -24,16 +24,12 @@
#include <stdexcept>
#include <vector>
namespace gr { namespace xml
{
class Node ;
class TreeBuilder ;
} }
namespace gr { namespace http {
typedef std::vector<std::string> 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 ) ;

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 <cstddef>
namespace gr { namespace http {
class Receivable
{
public :
virtual std::size_t OnData( void *data, std::size_t count ) = 0 ;
} ;
} } // end of namespace