ndmspc v1.2.0-0.1.rc5
Loading...
Searching...
No Matches
Ndmspc::NHttpRequest Class Reference

Provides HTTP request functionality using libcurl. More...

#include <NHttpRequest.h>

Public Member Functions

 NHttpRequest ()
 Constructs a new NHttpRequest instance.
 
virtual ~NHttpRequest ()
 Destroys the NHttpRequest instance.
 
std::string get (const std::string &url, const std::string &cert_path="", const std::string &key_path="", const std::string &key_password_file="", bool insecure=false)
 Performs an HTTP GET request.
 
std::string post (const std::string &url, const std::string &post_data, const std::string &cert_path="", const std::string &key_path="", const std::string &key_password_file="", bool insecure=false)
 Performs an HTTP POST request.
 
int head (const std::string &url, const std::string &cert_path="", const std::string &key_path="", const std::string &key_password_file="", bool insecure=false)
 Performs an HTTP HEAD request.
 

Private Member Functions

CURLcode request (const std::string &method, const std::string &url, const std::string &data, std::ostringstream &response, const std::string &cert_path, const std::string &key_path, const std::string &key_password_file, bool insecure)
 Internal method to perform an HTTP request.
 
void throw_curl_error (CURLcode res) const
 Throws an exception if a CURL error occurs.
 

Static Private Member Functions

static size_t WriteCallback (void *contents, size_t size, size_t nmemb, void *userp)
 Callback for writing response data.
 
static size_t HeaderCallback (char *buffer, size_t size, size_t nitems, void *userdata)
 Callback for handling response headers.
 

Private Attributes

CURL * curl
 libcurl handle
 
struct curl_slist * headers
 List of custom headers.
 
std::vector< std::string > received_headers
 Received response headers.
 

Detailed Description

Provides HTTP request functionality using libcurl.

NHttpRequest wraps libcurl to perform HTTP GET, POST, and HEAD requests, supporting client certificate authentication and custom error handling. It offers methods for sending requests and retrieving responses, and handles headers and SSL options.

Author
Martin Vala mvala.nosp@m.@cer.nosp@m.n.ch

Definition at line 21 of file NHttpRequest.h.

Constructor & Destructor Documentation

◆ NHttpRequest()

Ndmspc::NHttpRequest::NHttpRequest ( )

Constructs a new NHttpRequest instance.

Definition at line 12 of file NHttpRequest.cxx.

References curl, and headers.

◆ ~NHttpRequest()

Ndmspc::NHttpRequest::~NHttpRequest ( )
virtual

Destroys the NHttpRequest instance.

Definition at line 21 of file NHttpRequest.cxx.

References curl, and headers.

Member Function Documentation

◆ get()

std::string Ndmspc::NHttpRequest::get ( const std::string & url,
const std::string & cert_path = "",
const std::string & key_path = "",
const std::string & key_password_file = "",
bool insecure = false )

Performs an HTTP GET request.

Parameters
urlThe target URL.
cert_pathPath to client certificate (optional).
key_pathPath to private key (optional).
key_password_filePath to key password file (optional).
insecureIf true, disables SSL verification.
Returns
Response body as a string.

Definition at line 32 of file NHttpRequest.cxx.

References request(), and throw_curl_error().

Referenced by Ndmspc::NUtils::OpenMacro().

◆ head()

int Ndmspc::NHttpRequest::head ( const std::string & url,
const std::string & cert_path = "",
const std::string & key_path = "",
const std::string & key_password_file = "",
bool insecure = false )

Performs an HTTP HEAD request.

Parameters
urlThe target URL.
cert_pathPath to client certificate (optional).
key_pathPath to private key (optional).
key_password_filePath to key password file (optional).
insecureIf true, disables SSL verification.
Returns
HTTP status code.

Definition at line 56 of file NHttpRequest.cxx.

References received_headers, request(), and throw_curl_error().

Referenced by Ndmspc::NUtils::AccessPathName().

◆ HeaderCallback()

size_t Ndmspc::NHttpRequest::HeaderCallback ( char * buffer,
size_t size,
size_t nitems,
void * userdata )
staticprivate

Callback for handling response headers.

Parameters
bufferPointer to header data.
sizeSize of a data unit.
nitemsNumber of data units.
userdataUser pointer (header storage).
Returns
Number of bytes handled.

Definition at line 186 of file NHttpRequest.cxx.

References headers.

Referenced by request().

◆ post()

std::string Ndmspc::NHttpRequest::post ( const std::string & url,
const std::string & post_data,
const std::string & cert_path = "",
const std::string & key_path = "",
const std::string & key_password_file = "",
bool insecure = false )

Performs an HTTP POST request.

Parameters
urlThe target URL.
post_dataData to post.
cert_pathPath to client certificate (optional).
key_pathPath to private key (optional).
key_password_filePath to key password file (optional).
insecureIf true, disables SSL verification.
Returns
Response body as a string.

Definition at line 44 of file NHttpRequest.cxx.

References request(), and throw_curl_error().

◆ request()

CURLcode Ndmspc::NHttpRequest::request ( const std::string & method,
const std::string & url,
const std::string & data,
std::ostringstream & response,
const std::string & cert_path,
const std::string & key_path,
const std::string & key_password_file,
bool insecure )
private

Internal method to perform an HTTP request.

Parameters
methodHTTP method ("GET", "POST", "HEAD").
urlThe target URL.
dataData to send (for POST).
responseOutput stream for response data.
cert_pathPath to client certificate.
key_pathPath to private key.
key_password_filePath to key password file.
insecureIf true, disables SSL verification.
Returns
CURLcode result.

Definition at line 76 of file NHttpRequest.cxx.

References curl, HeaderCallback(), headers, received_headers, and WriteCallback().

Referenced by get(), head(), and post().

◆ throw_curl_error()

void Ndmspc::NHttpRequest::throw_curl_error ( CURLcode res) const
private

Throws an exception if a CURL error occurs.

Parameters
resCURLcode result.

Definition at line 208 of file NHttpRequest.cxx.

Referenced by get(), head(), and post().

◆ WriteCallback()

size_t Ndmspc::NHttpRequest::WriteCallback ( void * contents,
size_t size,
size_t nmemb,
void * userp )
staticprivate

Callback for writing response data.

Parameters
contentsPointer to received data.
sizeSize of a data unit.
nmembNumber of data units.
userpUser pointer (output stream).
Returns
Number of bytes handled.

Definition at line 180 of file NHttpRequest.cxx.

Referenced by request().

Member Data Documentation

◆ curl

CURL* Ndmspc::NHttpRequest::curl
private

libcurl handle

Definition at line 108 of file NHttpRequest.h.

Referenced by NHttpRequest(), request(), and ~NHttpRequest().

◆ headers

struct curl_slist* Ndmspc::NHttpRequest::headers
private

List of custom headers.

Definition at line 109 of file NHttpRequest.h.

Referenced by HeaderCallback(), NHttpRequest(), request(), and ~NHttpRequest().

◆ received_headers

std::vector<std::string> Ndmspc::NHttpRequest::received_headers
private

Received response headers.

Definition at line 110 of file NHttpRequest.h.

Referenced by head(), and request().


The documentation for this class was generated from the following files: