From 8357e9ed8299901e40358a5507ad68bd66562e14 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Sat, 5 May 2012 22:39:18 +0800 Subject: [PATCH] renamed HTTP agent class and namespace --- libgrive/src/drive/File.cc | 2 +- libgrive/src/protocol/HTTP.cc | 70 +++++++++------- libgrive/src/protocol/HTTP.hh | 146 ++++++++++++++++++---------------- 3 files changed, 119 insertions(+), 99 deletions(-) diff --git a/libgrive/src/drive/File.cc b/libgrive/src/drive/File.cc index 14cf37d..19a8140 100644 --- a/libgrive/src/drive/File.cc +++ b/libgrive/src/drive/File.cc @@ -139,7 +139,7 @@ void File::Upload( std::streambuf *file, const http::Headers& auth ) hdr.push_back( "If-Match: " + m_etag ) ; hdr.push_back( "Expect:" ) ; - Http http ; + http::Agent http ; http.Put( m_upload_link, meta, hdr ) ; // parse the header and find "Location" diff --git a/libgrive/src/protocol/HTTP.cc b/libgrive/src/protocol/HTTP.cc index 3c4af47..86062cd 100644 --- a/libgrive/src/protocol/HTTP.cc +++ b/libgrive/src/protocol/HTTP.cc @@ -21,6 +21,8 @@ #include "Download.hh" // #include "util/SignalHandler.hh" +#include "xml/Node.hh" +#include "xml/TreeBuilder.hh" // dependent libraries #include @@ -276,31 +278,33 @@ std::string Unescape( const std::string& str ) return result ; } -} } // end of namespace - -namespace gr { - -struct Http::Impl +struct Agent::Impl { CURL *curl ; std::string location ; + char error[CURL_ERROR_SIZE] ; } ; -Http::Http() : +Agent::Agent() : m_pimpl( new Impl ) { m_pimpl->curl = ::curl_easy_init(); - + ::curl_easy_setopt( m_pimpl->curl, CURLOPT_SSL_VERIFYPEER, 0L); + ::curl_easy_setopt( m_pimpl->curl, CURLOPT_SSL_VERIFYHOST, 0L); + ::curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADERFUNCTION, &Agent::HeaderCallback ) ; + ::curl_easy_setopt( m_pimpl->curl, CURLOPT_WRITEHEADER , this ) ; + ::curl_easy_setopt( m_pimpl->curl, CURLOPT_ERRORBUFFER, m_pimpl->error ) ; } -Http::~Http() +Agent::~Agent() { ::curl_easy_cleanup( m_pimpl->curl ); } -std::size_t Http::HeaderCallback( void *ptr, size_t size, size_t nmemb, Http *pthis ) +std::size_t Agent::HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent *pthis ) { - std::string line( (char*)ptr, (char*)ptr + size*nmemb ) ; + char *str = reinterpret_cast(ptr) ; + std::string line( str, str + size*nmemb ) ; static const std::string loc = "Location: " ; std::size_t pos = line.find( loc ) ; @@ -313,7 +317,13 @@ std::size_t Http::HeaderCallback( void *ptr, size_t size, size_t nmemb, Http *pt return size*nmemb ; } -std::string Http::Put( +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::string Agent::Put( const std::string& url, const std::string& data, const http::Headers& hdr ) @@ -326,47 +336,47 @@ std::string Http::Put( // 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, WriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp ) ; - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ; curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ; curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ; curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ; - curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &Http::HeaderCallback ) ; - curl_easy_setopt(curl, CURLOPT_WRITEHEADER , this ) ; - - // set headers - struct curl_slist *curl_hdr = 0 ; - for ( Headers::const_iterator i = hdr.begin() ; i != hdr.end() ; ++i ) - curl_hdr = curl_slist_append( curl_hdr, i->c_str() ) ; - curl_easy_setopt( curl, CURLOPT_HTTPHEADER, curl_hdr ) ; - - char error_buf[CURL_ERROR_SIZE] = {} ; - curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf ) ; + SetHeader( hdr ) ; + CURLcode curl_code = curl_easy_perform(curl); long http_code = 0; - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); if ( curl_code != CURLE_OK ) { - throw Exception( curl_code, http_code, error_buf ) ; + throw Exception( curl_code, http_code, m_pimpl->error ) ; } else if (http_code >= 400 ) { std::cout << "http error " << http_code << " " << resp << std::endl ; - throw Exception( curl_code, http_code, error_buf ) ; + throw Exception( curl_code, http_code, m_pimpl->error ) ; } return resp ; } -std::string Http::RedirLocation() const +void Agent::SetHeader( const http::Headers& hdr ) +{ + // set headers + struct curl_slist *curl_hdr = 0 ; + for ( Headers::const_iterator i = hdr.begin() ; i != hdr.end() ; ++i ) + curl_hdr = curl_slist_append( curl_hdr, i->c_str() ) ; + + ::curl_easy_setopt( m_pimpl->curl, CURLOPT_HTTPHEADER, curl_hdr ) ; +} + +std::string Agent::RedirLocation() const { return m_pimpl->location ; } -} // end of namespace + +} } // end of namespace diff --git a/libgrive/src/protocol/HTTP.hh b/libgrive/src/protocol/HTTP.hh index b549253..4c34237 100644 --- a/libgrive/src/protocol/HTTP.hh +++ b/libgrive/src/protocol/HTTP.hh @@ -24,74 +24,84 @@ #include #include -namespace gr { +namespace gr { namespace xml +{ + class Node ; + class TreeBuilder ; +} } - namespace http - { - typedef std::vector Headers ; - - std::string Get( const std::string& url, const Headers& hdr = Headers() ) ; - void GetFile( - const std::string& url, - const std::string& filename, - const Headers& hdr = Headers() ) ; - - void GetFile( - const std::string& url, - const std::string& filename, - std::string& md5sum, - const Headers& hdr = Headers() ) ; - - std::string PostData( - const std::string& url, - const std::string& data, - const Headers& hdr = Headers() ) ; - std::string PostDataWithHeader( - const std::string& url, - const std::string& data, - const Headers& hdr = Headers() ) ; - std::string PostFile( - const std::string& url, - const std::string& filename, - const Headers& hdr = Headers() ) ; - - std::string Put( - const std::string& url, - const std::string& data, - const Headers& hdr = Headers() ) ; - - std::string Escape( const std::string& str ) ; - std::string Unescape( const std::string& str ) ; - - class Exception : public std::runtime_error - { - public : - Exception( int curl_code, int http_code, const char *err_buf ) ; - - private : - static std::string Format( int curl_code, int http_code, const char *err_buf ) ; - } ; - } - - class Http - { - public : - Http() ; - ~Http() ; - - std::string Put( - const std::string& url, - const std::string& data, - const http::Headers& hdr = http::Headers() ) ; +namespace gr { namespace http { - std::string RedirLocation() const ; - - private : - static std::size_t HeaderCallback( void *ptr, size_t size, size_t nmemb, Http *pthis ) ; - - private : - struct Impl ; - std::auto_ptr m_pimpl ; - } ; +typedef std::vector Headers ; -} // end of namespace +std::string Get( const std::string& url, const Headers& hdr = Headers() ) ; +void GetFile( + const std::string& url, + const std::string& filename, + const Headers& hdr = Headers() ) ; + +void GetFile( + const std::string& url, + const std::string& filename, + std::string& md5sum, + const Headers& hdr = Headers() ) ; + +std::string PostData( + const std::string& url, + const std::string& data, + const Headers& hdr = Headers() ) ; +std::string PostDataWithHeader( + const std::string& url, + const std::string& data, + const Headers& hdr = Headers() ) ; +std::string PostFile( + const std::string& url, + const std::string& filename, + const Headers& hdr = Headers() ) ; + +std::string Put( + const std::string& url, + const std::string& data, + const Headers& hdr = Headers() ) ; + +std::string Escape( const std::string& str ) ; +std::string Unescape( const std::string& str ) ; + +class Exception : public std::runtime_error +{ +public : + Exception( int curl_code, int http_code, const char *err_buf ) ; + +private : + static std::string Format( int curl_code, int http_code, const char *err_buf ) ; +} ; + +class Agent +{ +public : + Agent() ; + ~Agent() ; + + std::string Put( + const std::string& url, + const std::string& data, + const http::Headers& hdr = http::Headers() ) ; + + xml::Node GetXml( + const std::string& url, + 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 ) ; + + void SetHeader( const http::Headers& hdr ) ; + +private : + struct Impl ; + std::auto_ptr m_pimpl ; +} ; + +} } // end of namespace