2012-04-25 20:13:17 +04:00
|
|
|
/*
|
|
|
|
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
|
2012-04-26 19:40:38 +04:00
|
|
|
as published by the Free Software Foundation version 2
|
|
|
|
of the License.
|
2012-04-25 20:13:17 +04:00
|
|
|
|
|
|
|
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 "HTTP.hh"
|
|
|
|
|
2012-04-26 08:03:30 +04:00
|
|
|
#include "Download.hh"
|
|
|
|
|
|
|
|
// dependent libraries
|
2012-04-25 20:13:17 +04:00
|
|
|
#include <curl/curl.h>
|
2012-04-26 08:03:30 +04:00
|
|
|
|
2012-04-28 14:33:08 +04:00
|
|
|
#include <algorithm>
|
2012-04-25 20:13:17 +04:00
|
|
|
#include <cassert>
|
2012-04-28 14:33:08 +04:00
|
|
|
#include <cstring>
|
2012-04-25 20:13:17 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <streambuf>
|
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
namespace {
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
using namespace gr::http ;
|
2012-04-25 20:13:17 +04:00
|
|
|
|
|
|
|
// libcurl callback to append to a string
|
|
|
|
std::size_t WriteCallback( char *data, size_t size, size_t nmemb, std::string *resp )
|
|
|
|
{
|
|
|
|
assert( resp != 0 ) ;
|
|
|
|
assert( data != 0 ) ;
|
|
|
|
|
|
|
|
std::size_t count = size * nmemb ;
|
|
|
|
resp->append( data, count ) ;
|
|
|
|
return count ;
|
|
|
|
}
|
|
|
|
|
2012-04-28 14:33:08 +04:00
|
|
|
size_t ReadCallback( void *ptr, std::size_t size, std::size_t nmemb, std::string *data )
|
|
|
|
{
|
|
|
|
assert( ptr != 0 ) ;
|
|
|
|
assert( data != 0 ) ;
|
|
|
|
|
|
|
|
std::size_t count = std::min( size * nmemb, data->size() ) ;
|
|
|
|
if ( count > 0 )
|
|
|
|
{
|
2012-04-29 06:22:58 +04:00
|
|
|
std::memcpy( ptr, &(*data)[0], count ) ;
|
2012-04-28 14:33:08 +04:00
|
|
|
data->erase( 0, count ) ;
|
|
|
|
}
|
2012-04-29 20:03:04 +04:00
|
|
|
|
2012-04-28 14:33:08 +04:00
|
|
|
return count ;
|
|
|
|
}
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
CURL* InitCurl( const std::string& url, std::string *resp, const Headers& hdr )
|
|
|
|
{
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
if ( curl == 0 )
|
|
|
|
throw std::bad_alloc() ;
|
|
|
|
|
|
|
|
// set common options
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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 ) ;
|
|
|
|
|
|
|
|
return curl ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoCurl( CURL *curl )
|
|
|
|
{
|
|
|
|
char error_buf[CURL_ERROR_SIZE] = {} ;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf ) ;
|
|
|
|
|
|
|
|
CURLcode curl_code = curl_easy_perform(curl);
|
|
|
|
|
|
|
|
int http_code = 0;
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
if ( curl_code != CURLE_OK )
|
|
|
|
{
|
2012-04-28 20:01:45 +04:00
|
|
|
throw Exception( curl_code, http_code, error_buf ) ;
|
2012-04-25 20:13:17 +04:00
|
|
|
}
|
|
|
|
else if (http_code >= 400 )
|
|
|
|
{
|
|
|
|
std::cout << "http error " << http_code << std::endl ;
|
2012-04-28 20:01:45 +04:00
|
|
|
throw Exception( curl_code, http_code, error_buf ) ;
|
2012-04-25 20:13:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
} // end of local namespace
|
|
|
|
|
|
|
|
namespace gr { namespace http {
|
|
|
|
|
|
|
|
Exception::Exception( int curl_code, int http_code, const char *err_buf )
|
|
|
|
: runtime_error( Format( curl_code, http_code, err_buf ) )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Exception::Format( int curl_code, int http_code, const char *err_buf )
|
|
|
|
{
|
|
|
|
std::ostringstream ss ;
|
|
|
|
ss << "CURL code = " << curl_code << " HTTP code = " << http_code << " (" << err_buf << ")" ;
|
|
|
|
return ss.str() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Get( const std::string& url, const Headers& hdr )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
|
|
|
std::string resp ;
|
|
|
|
CURL *curl = InitCurl( url, &resp, hdr ) ;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
|
|
|
|
DoCurl( curl ) ;
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
void GetFile(
|
2012-04-25 20:13:17 +04:00
|
|
|
const std::string& url,
|
|
|
|
const std::string& filename,
|
|
|
|
const Headers& hdr )
|
|
|
|
{
|
2012-04-26 08:03:30 +04:00
|
|
|
Download dl( filename, Download::NoChecksum() ) ;
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
CURL *curl = InitCurl( url, 0, hdr ) ;
|
2012-04-26 08:03:30 +04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Download::Callback);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dl ) ;
|
2012-04-25 20:13:17 +04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
|
|
|
|
DoCurl( curl ) ;
|
|
|
|
}
|
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
void GetFile(
|
2012-04-26 08:03:30 +04:00
|
|
|
const std::string& url,
|
|
|
|
const std::string& filename,
|
|
|
|
std::string& md5sum,
|
|
|
|
const Headers& hdr )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
std::string PostData( const std::string& url, const std::string& data, const Headers& hdr )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
|
|
|
std::string resp ;
|
|
|
|
CURL *curl = InitCurl( url, &resp, hdr ) ;
|
|
|
|
|
|
|
|
std::string post_data = data ;
|
|
|
|
|
2012-04-29 06:22:58 +04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
2012-04-25 20:13:17 +04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &post_data[0] ) ;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, post_data.size() ) ;
|
2012-04-30 21:14:41 +04:00
|
|
|
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1 ) ;
|
2012-04-29 13:15:03 +04:00
|
|
|
|
|
|
|
DoCurl( curl ) ;
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PostDataWithHeader( const std::string& url, const std::string& data, const Headers& hdr )
|
|
|
|
{
|
|
|
|
std::string resp ;
|
|
|
|
CURL *curl = InitCurl( url, &resp, hdr ) ;
|
|
|
|
|
|
|
|
std::string post_data = data ;
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &post_data[0] ) ;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, post_data.size() ) ;
|
2012-04-30 21:14:41 +04:00
|
|
|
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L ) ;
|
2012-04-29 13:15:03 +04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADER, 1L );
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-04-29 20:03:04 +04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
DoCurl( curl ) ;
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
std::cout << "response = " << resp << std::endl ;
|
|
|
|
throw ;
|
|
|
|
}
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
std::string PostFile( const std::string& url, const std::string& filename, const Headers& hdr )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
|
|
|
std::string resp ;
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
std::string Put(
|
2012-04-28 14:33:08 +04:00
|
|
|
const std::string& url,
|
|
|
|
const std::string& data,
|
|
|
|
const Headers& hdr )
|
|
|
|
{
|
|
|
|
std::string resp ;
|
|
|
|
CURL *curl = InitCurl( url, &resp, hdr ) ;
|
|
|
|
|
2012-04-29 06:22:58 +04:00
|
|
|
std::string put_data = data ;
|
|
|
|
|
|
|
|
// set common options
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADER, 1L );
|
|
|
|
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
|
2012-04-28 14:33:08 +04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ;
|
2012-04-30 21:14:41 +04:00
|
|
|
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L ) ;
|
2012-04-29 06:22:58 +04:00
|
|
|
|
2012-04-29 20:03:04 +04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
DoCurl( curl ) ;
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
std::cout << "response = " << resp << std::endl ;
|
|
|
|
throw ;
|
|
|
|
}
|
|
|
|
|
2012-04-28 14:33:08 +04:00
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
std::string Escape( const std::string& str )
|
|
|
|
{
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
|
|
|
|
char *tmp = curl_easy_escape( curl, str.c_str(), str.size() ) ;
|
|
|
|
std::string result = tmp ;
|
|
|
|
curl_free( tmp ) ;
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return result ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Unescape( const std::string& str )
|
|
|
|
{
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
|
|
|
|
int r ;
|
|
|
|
char *tmp = curl_easy_unescape( curl, str.c_str(), str.size(), &r ) ;
|
|
|
|
std::string result = tmp ;
|
|
|
|
curl_free( tmp ) ;
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return result ;
|
|
|
|
}
|
|
|
|
|
2012-04-28 20:01:45 +04:00
|
|
|
} } // end of namespace
|