Repo-Init
 
Loading...
Searching...
No Matches
HTTP Class Reference

#include <Http.hpp>

Public Member Functions

 HTTP (std::string addr, int timeoutInMs=HTTP_TIMEOUT_MS)
 
 HTTP (const HTTP &)=delete
 Copy constructor.
 
 HTTP (HTTP &&)=delete
 Move constructor.
 
HTTPoperator= (HTTP)=delete
 Copy assignment operator.
 
HTTPoperator= (HTTP &&)=delete
 Move assignment operator.
 
template<typename T >
bool setOption (CURLoption option, T value)
 
const std::string & getHostAddress () const
 
CURLcode sendGETRequest (const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
 
CURLcode sendHEADRequest (const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
 
CURLcode sendPOSTRequest (const std::string &index, const std::string &payload, std::string &receivedData, HttpStatus::Code &statusCode)
 
CURLcode sendPUTRequest (const std::string &index, const std::string &payload, std::string &receivedData, HttpStatus::Code &statusCode)
 
HTTPStats getStats ()
 
 ~HTTP ()
 

Private Member Functions

void setCommonFields (const std::string &fullURL, std::string &receivedData, CURLoption method)
 
void setCommonFields (const std::string &fullURL, std::string &receivedData, CURLoption method, const std::string &payload)
 
CURLcode performRequest (HttpStatus::Code &statusCode)
 

Static Private Member Functions

static size_t writeDataCallback (const char *contents, size_t size, size_t nmemb, std::string *userp)
 

Private Attributes

CURL * _curl = curl_easy_init()
 CURL handler.
 
std::string _hostAddr
 Full path of server.
 

Detailed Description

Represents an HTTP client connection

Definition at line 43 of file Http.hpp.

Constructor & Destructor Documentation

◆ HTTP() [1/3]

HTTP::HTTP ( std::string addr,
int timeoutInMs = HTTP_TIMEOUT_MS )
explicit

Constructs a new HTTP object

Parameters
[in]addrThe full path to the server
[in]timeoutInMsThe connection timeout in milliseconds

Definition at line 46 of file Http.cpp.

46 : _hostAddr(std::move(addr))
47{
48 if (_curl == nullptr)
49 {
50 throw std::invalid_argument("Can't init curl context");
51 }
52
53 curl_easy_setopt(_curl, CURLOPT_VERBOSE, 0L);
54 curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1L);
55 curl_easy_setopt(_curl, CURLOPT_TIMEOUT_MS, timeoutInMs);
56 curl_easy_setopt(_curl, CURLOPT_CONNECTTIMEOUT_MS, timeoutInMs);
57 curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, writeDataCallback);
58
59 curl_easy_setopt(_curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
60 curl_easy_setopt(_curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // At least TLSv1.2
61}
CURL * _curl
CURL handler.
Definition Http.hpp:46
static size_t writeDataCallback(const char *contents, size_t size, size_t nmemb, std::string *userp)
Definition Http.cpp:37
std::string _hostAddr
Full path of server.
Definition Http.hpp:48
Here is the call graph for this function:

◆ HTTP() [2/3]

HTTP::HTTP ( const HTTP & )
delete

Copy constructor.

◆ HTTP() [3/3]

HTTP::HTTP ( HTTP && )
delete

Move constructor.

◆ ~HTTP()

HTTP::~HTTP ( )

Destroys the HTTP object

Definition at line 126 of file Http.cpp.

126{ curl_easy_cleanup(_curl); }

Member Function Documentation

◆ getHostAddress()

const std::string & HTTP::getHostAddress ( ) const
inlinenodiscard

Gets the host address of the object

Returns
The host address

Definition at line 120 of file Http.hpp.

120{ return _hostAddr; }

◆ getStats()

HTTPStats HTTP::getStats ( )

Gets the statistics of the HTTP object

Returns
The produced statistics

Definition at line 93 of file Http.cpp.

94{
95 HTTPStats stats{};
96
97 curl_off_t value = 0;
98 curl_easy_getinfo(_curl, CURLINFO_SIZE_UPLOAD_T, &value);
99 stats.uploadBytes = static_cast<size_t>(value);
100 curl_easy_getinfo(_curl, CURLINFO_SIZE_DOWNLOAD_T, &value);
101 stats.downloadBytes = static_cast<size_t>(value);
102 curl_easy_getinfo(_curl, CURLINFO_HEADER_SIZE, &value);
103 stats.headerBytes = static_cast<size_t>(value);
104 curl_easy_getinfo(_curl, CURLINFO_REQUEST_SIZE, &value);
105 stats.requestBytes = static_cast<size_t>(value);
106 curl_easy_getinfo(_curl, CURLINFO_SPEED_UPLOAD_T, &value);
107 stats.uploadSpeed = value;
108 curl_easy_getinfo(_curl, CURLINFO_SPEED_DOWNLOAD_T, &value);
109 stats.downloadSpeed = value;
110 curl_easy_getinfo(_curl, CURLINFO_CONNECT_TIME_T, &value);
111 stats.connectionTime = value;
112 curl_easy_getinfo(_curl, CURLINFO_NAMELOOKUP_TIME_T, &value);
113 stats.nameLookupTime = value;
114 curl_easy_getinfo(_curl, CURLINFO_PRETRANSFER_TIME_T, &value);
115 stats.preTransferTime = value;
116 curl_easy_getinfo(_curl, CURLINFO_REDIRECT_TIME_T, &value);
117 stats.redirectTime = value;
118 curl_easy_getinfo(_curl, CURLINFO_STARTTRANSFER_TIME_T, &value);
119 stats.startTransferTime = value;
120 curl_easy_getinfo(_curl, CURLINFO_TOTAL_TIME_T, &value);
121 stats.totalTime = value;
122
123 return stats;
124}

◆ operator=() [1/2]

HTTP & HTTP::operator= ( HTTP && )
delete

Move assignment operator.

◆ operator=() [2/2]

HTTP & HTTP::operator= ( HTTP )
delete

Copy assignment operator.

◆ performRequest()

CURLcode HTTP::performRequest ( HttpStatus::Code & statusCode)
private

Performs the request

Parameters
[out]statusCodeThe HTTP status code (set if CURLE_OK, otherwise unchanged)
Returns
The status of the operation. CURLE_OK if successful.

Definition at line 23 of file Http.cpp.

24{
25 // Perform request
26 auto status = static_cast<long>(HttpStatus::Code::xxx_max);
27 const CURLcode retval = curl_easy_perform(_curl);
28 if (retval == CURLE_OK)
29 {
30 curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &status);
31 }
32 statusCode = static_cast<HttpStatus::Code>(status);
33
34 return retval;
35}
Here is the caller graph for this function:

◆ sendGETRequest()

CURLcode HTTP::sendGETRequest ( const std::string & index,
std::string & receivedData,
HttpStatus::Code & statusCode )

Sends a GET request

Parameters
[in]indexThe value to append to the server address
[out]receivedDataThe received reply from the server
[out]statusCodeThe HTTP status code (set if CURLE_OK, otherwise unchanged)
Returns
The status of the operation. CURLE_OK if successful.

Definition at line 63 of file Http.cpp.

64{
65 // Prepare request specific options
66 setCommonFields(_hostAddr + index, receivedData, CURLOPT_HTTPGET);
67 return performRequest(statusCode);
68}
CURLcode performRequest(HttpStatus::Code &statusCode)
Definition Http.cpp:23
void setCommonFields(const std::string &fullURL, std::string &receivedData, CURLoption method)
Definition Http.cpp:6
Here is the call graph for this function:

◆ sendHEADRequest()

CURLcode HTTP::sendHEADRequest ( const std::string & index,
std::string & receivedData,
HttpStatus::Code & statusCode )

Sends a HEAD request

Parameters
[in]indexThe value to append to the server address
[out]receivedDataThe received reply from the server
[out]statusCodeThe HTTP status code (set if CURLE_OK, otherwise unchanged)
Returns
The status of the operation. CURLE_OK if successful.

Definition at line 70 of file Http.cpp.

71{
72 // Prepare request specific options
73 setCommonFields(_hostAddr + index, receivedData, CURLOPT_NOBODY);
74 return performRequest(statusCode);
75}
Here is the call graph for this function:

◆ sendPOSTRequest()

CURLcode HTTP::sendPOSTRequest ( const std::string & index,
const std::string & payload,
std::string & receivedData,
HttpStatus::Code & statusCode )

Sends a POST request

Parameters
[in]indexThe value to append to the server address
[in]payloadThe payload to send to the server
[out]receivedDataThe received reply from the server
[out]statusCodeThe HTTP status code (set if CURLE_OK, otherwise unchanged)
Returns
The status of the operation. CURLE_OK if successful.

Definition at line 77 of file Http.cpp.

79{
80 // Prepare request specific options
81 setCommonFields(_hostAddr + index, receivedData, CURLOPT_POST, payload);
82 return performRequest(statusCode);
83}
Here is the call graph for this function:

◆ sendPUTRequest()

CURLcode HTTP::sendPUTRequest ( const std::string & index,
const std::string & payload,
std::string & receivedData,
HttpStatus::Code & statusCode )

Sends a PUT request

Parameters
[in]indexThe value to append to the server address
[in]payloadThe payload to send to the server
[out]receivedDataThe received reply from the server
[out]statusCodeThe HTTP status code (set if CURLE_OK, otherwise unchanged)
Returns
The status of the operation. CURLE_OK if successful.

Definition at line 85 of file Http.cpp.

87{
88 // Prepare request specific options
89 setCommonFields(_hostAddr + index, receivedData, CURLOPT_UPLOAD, payload);
90 return performRequest(statusCode);
91}
Here is the call graph for this function:

◆ setCommonFields() [1/2]

void HTTP::setCommonFields ( const std::string & fullURL,
std::string & receivedData,
CURLoption method )
private

Sets common fields for HTTP requests

Parameters
[in]fullURLThe full URL of the request
[out]receivedDataThe received data from the server
[in]methodThe HTTP method to use

Definition at line 6 of file Http.cpp.

7{
8 curl_easy_setopt(_curl, CURLOPT_URL, fullURL.c_str());
9 curl_easy_setopt(_curl, CURLOPT_WRITEDATA, static_cast<void *>(&receivedData)); // Register user-supplied memory
10 curl_easy_setopt(_curl, method, 1L);
11}
Here is the caller graph for this function:

◆ setCommonFields() [2/2]

void HTTP::setCommonFields ( const std::string & fullURL,
std::string & receivedData,
CURLoption method,
const std::string & payload )
private

Sets common fields for HTTP requests with payload

Parameters
[in]fullURLThe full URL of the request
[out]receivedDataThe received data from the server
[in]methodThe HTTP method to use
[in]payloadThe payload to send to the server

Definition at line 13 of file Http.cpp.

15{
16 curl_easy_setopt(_curl, CURLOPT_POSTFIELDS, payload.c_str());
17 curl_easy_setopt(_curl, CURLOPT_POSTFIELDSIZE_LARGE, payload.size());
18 curl_easy_setopt(_curl, CURLOPT_URL, fullURL.c_str());
19 curl_easy_setopt(_curl, CURLOPT_WRITEDATA, static_cast<void *>(&receivedData)); // Register user-supplied memory
20 curl_easy_setopt(_curl, method, 1L);
21}

◆ setOption()

template<typename T >
bool HTTP::setOption ( CURLoption option,
T value )
inline

Sets an option for the HTTP object

Parameters
[in]optionThe CURL option to set
[in]valueThe corresponding value
Returns
true if the option was set successfully, false otherwise

Definition at line 111 of file Http.hpp.

112 {
113 return curl_easy_setopt(_curl, option, value) == CURLE_OK;
114 }

◆ writeDataCallback()

size_t HTTP::writeDataCallback ( const char * contents,
size_t size,
size_t nmemb,
std::string * userp )
staticprivate

Callback function for writing received data

Parameters
[in]contentsThe received data
[in]sizeThe size of each element
[in]nmembThe number of elements
[in]userpUser pointer to a string
Returns
The total size of the received data

Definition at line 37 of file Http.cpp.

38{
39 const size_t recvSize = size * nmemb;
40 // Userp always referenced, there is no possibility of userp is nullptr. Directly use without checking
41 userp->assign(contents, contents + recvSize);
42
43 return recvSize;
44}
Here is the caller graph for this function:

Member Data Documentation

◆ _curl

CURL* HTTP::_curl = curl_easy_init()
private

CURL handler.

Definition at line 46 of file Http.hpp.

◆ _hostAddr

std::string HTTP::_hostAddr
private

Full path of server.

Definition at line 48 of file Http.hpp.


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