tv_rename/network.cpp

121 lines
3.6 KiB
C++
Raw Normal View History

2019-01-19 12:40:10 +00:00
#include "network.hpp"
2019-06-01 19:54:45 +00:00
#include <iostream>
2019-01-19 12:40:10 +00:00
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
// shamelessly stolen from http://www.cplusplus.com/forum/windows/109799/
Curl::Curl() {
connect = InternetOpen( L"WinInet/1.0", INTERNET_OPEN_TYPE_PRECONFIG,
nullptr, nullptr, 0 );
if ( !connect )
std::wcerr << "Something went wrong while connecting" << std::endl;
}
Curl::~Curl() {
if ( connect )
InternetCloseHandle( connect );
}
std::string Curl::execute( const std::wstring &url ) {
std::string source{};
source.reserve( 10000 );
HINTERNET openAddress = InternetOpenUrl(
connect, url.c_str(), nullptr, 0,
INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0 );
if ( !openAddress ) {
unsigned long errorNum = GetLastError();
std::wcout << "Failed to open URL" << std::endl
<< "Error No: " << errorNum << std::endl;
return "";
}
char dataReceived[4096];
unsigned long numberOfBytesRead = 0;
while ( InternetReadFile( openAddress, dataReceived, 4096,
&numberOfBytesRead ) &&
numberOfBytesRead ) {
source.append( dataReceived, numberOfBytesRead );
}
InternetCloseHandle( openAddress );
return source;
}
#else
size_t writeCallback( void *contents, size_t size, size_t nmemb,
void *target ) {
*static_cast< std::string * >( target ) +=
std::string( static_cast< char * >( contents ), size * nmemb );
2019-01-19 12:40:10 +00:00
return size * nmemb;
}
Curl::Curl() {
2019-02-04 16:39:48 +00:00
curl_global_init( CURL_GLOBAL_ALL );
2019-01-19 12:40:10 +00:00
curl_handle = curl_easy_init();
2019-02-04 16:39:48 +00:00
curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, writeCallback );
curl_easy_setopt( curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0" );
2019-01-19 12:40:10 +00:00
}
Curl::~Curl() {
2019-02-04 16:39:48 +00:00
curl_easy_cleanup( curl_handle );
curl_slist_free_all( chunk );
2019-01-19 12:40:10 +00:00
curl_global_cleanup();
}
void cleanUp( CURL *curl_handle ) {
curl_easy_setopt( curl_handle, CURLOPT_POST, 0 );
curl_easy_setopt( curl_handle, CURLOPT_POSTFIELDS, "" );
}
std::string Curl::get( const std::string &url ) {
// get rid of garbage
cleanUp( curl_handle );
2019-01-19 12:40:10 +00:00
std::string source;
2019-02-04 16:39:48 +00:00
source.reserve( 100000 );
curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA,
static_cast< void * >( &source ) );
curl_easy_setopt( curl_handle, CURLOPT_URL, url.c_str() );
curl_easy_setopt( curl_handle, CURLOPT_HTTPGET, 1 );
auto res = curl_easy_perform( curl_handle );
if ( res != CURLE_OK ) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror( res )
<< std::endl;
return "";
}
return source;
}
void Curl::addHeader( const std::string &header ) {
chunk = curl_slist_append( chunk, header.c_str() );
curl_easy_setopt( curl_handle, CURLOPT_HTTPHEADER, chunk );
}
void Curl::clearHeader() {
curl_slist_free_all( chunk );
chunk = NULL;
curl_easy_setopt( curl_handle, CURLOPT_HTTPHEADER, chunk );
}
std::string Curl::post( const std::string &url, const std::string &data ) {
std::string source;
source.reserve( 100000 );
curl_easy_setopt( curl_handle, CURLOPT_URL, url.c_str() );
curl_easy_setopt( curl_handle, CURLOPT_POST, 1 );
curl_easy_setopt( curl_handle, CURLOPT_POSTFIELDS, data.c_str() );
curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA,
static_cast< void * >( &source ) );
2019-02-04 16:39:48 +00:00
auto res = curl_easy_perform( curl_handle );
if ( res != CURLE_OK ) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror( res )
<< std::endl;
2019-01-19 12:40:10 +00:00
return "";
}
return source;
}
2019-02-04 16:39:48 +00:00
#endif