ndmspc  v1.2.0-0.1.rc3
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. More...
 
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. More...
 
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. More...
 

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. More...
 
void throw_curl_error (CURLcode res) const
 Throws an exception if a CURL error occurs. More...
 

Static Private Member Functions

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

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.

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.

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.

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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.


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