Created
April 22, 2019 22:17
-
-
Save Jacajack/af22073e723aeadb76fd5ab3814a0938 to your computer and use it in GitHub Desktop.
CURL-based, easy to use C++ URL request class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "url_request.hpp" | |
#include <algorithm> | |
#include <stdexcept> | |
// The write callback function | |
size_t url_request::write_callback( void *contents, size_t size, size_t nmemb, void *userp ) | |
{ | |
size_t realsize = size * nmemb; | |
url_request *rq = static_cast<url_request*>( userp ); | |
std::uint8_t *b = static_cast<std::uint8_t*>( contents ); | |
std::uint8_t *e = b + realsize; | |
std::copy( b, e, std::back_inserter( rq->m_data ) ); | |
return realsize; | |
} | |
url_request::url_request( const std::string &url ) : | |
m_curl( curl_easy_init( ), [](CURL *c){ curl_easy_cleanup( c ); } ) | |
{ | |
// Handle failed init | |
if ( !m_curl ) | |
throw std::runtime_error( "curl_easy_init() error!" ); | |
// Set options | |
curl_easy_setopt( m_curl.get( ), CURLOPT_URL, url.c_str( ) ); | |
curl_easy_setopt( m_curl.get( ), CURLOPT_USERAGENT, "libcurl-agent/1.0" ); | |
curl_easy_setopt( m_curl.get( ), CURLOPT_WRITEFUNCTION, url_request::write_callback ); | |
curl_easy_setopt( m_curl.get( ), CURLOPT_WRITEDATA, static_cast<void*>( this ) ); | |
// Perform the request | |
m_result = curl_easy_perform( m_curl.get( ) ); | |
} | |
bool url_request::get_success( ) const | |
{ | |
return m_result == CURLE_OK; | |
} | |
const std::vector<std::uint8_t> &url_request::get_data( ) const | |
{ | |
return m_data; | |
} | |
std::string url_request::get_string( ) const | |
{ | |
return std::string( m_data.begin( ), m_data.end( ) ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef URL_REQUEST_HPP | |
#define URL_REQUEST_HPP | |
#include <vector> | |
#include <memory> | |
#include <string> | |
#include <curl/curl.h> | |
// URL request RAII wrapper class | |
class url_request | |
{ | |
public: | |
url_request( const std::string &url ); | |
bool get_success( ) const; | |
const std::vector<std::uint8_t> &get_data( ) const; | |
std::string get_string( ) const; | |
private: | |
static size_t write_callback( void *contents, size_t size, size_t nmemb, void *userp ); | |
std::unique_ptr<CURL, void(*)(CURL*)> m_curl; | |
std::vector<std::uint8_t> m_data; | |
CURLcode m_result; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment