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, CURLoption method)
 
void setCommonFields (const std::string &fullURL, CURLoption method, const std::string &payload)
 
CURLcode performRequest (HttpStatus::Code &statusCode, std::string &receivedData)
 

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::unique_ptr< curlMemory_data = std::make_unique<curlMemory>()
 Memory structure for CURL.
 
std::string _hostAddr
 Full path of server.
 

Detailed Description

Represents an HTTP client connection

Definition at line 53 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 52 of file Http.cpp.

52 : _hostAddr(std::move(addr))
53{
54 if (_curl == nullptr)
55 {
56 throw std::invalid_argument("Can't init curl context");
57 }
58
59 curl_easy_setopt(_curl, CURLOPT_VERBOSE, 0L);
60 curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1L);
61 curl_easy_setopt(_curl, CURLOPT_TIMEOUT_MS, timeoutInMs);
62 curl_easy_setopt(_curl, CURLOPT_CONNECTTIMEOUT_MS, timeoutInMs);
63 curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, writeDataCallback);
64
65 curl_easy_setopt(_curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
66 curl_easy_setopt(_curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // At least TLSv1.2
67}
CURL * _curl
CURL handler.
Definition Http.hpp:56
static size_t writeDataCallback(const char *contents, size_t size, size_t nmemb, std::string *userp)
Definition Http.cpp:39
std::string _hostAddr
Full path of server.
Definition Http.hpp:60
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 132 of file Http.cpp.

132{ 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 130 of file Http.hpp.

130{ return _hostAddr; }

◆ getStats()

HTTPStats HTTP::getStats ( )

Gets the statistics of the HTTP object

Returns
The produced statistics

Definition at line 99 of file Http.cpp.

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

◆ 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,
std::string & receivedData )
private

Performs the request

Parameters
[out]statusCodeThe HTTP status code
[out]receivedDataThe received reply from the server
Returns
The status of the operation. CURLE_OK if successful.

Definition at line 24 of file Http.cpp.

25{
26 // Perform request
27 auto status = static_cast<long>(HttpStatus::Code::xxx_max);
28 const CURLcode retval = curl_easy_perform(_curl);
29 if (retval == CURLE_OK)
30 {
31 curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &status);
32 }
33 statusCode = static_cast<HttpStatus::Code>(status);
34 receivedData = _data->data;
35
36 return retval;
37}
std::unique_ptr< curlMemory > _data
Memory structure for CURL.
Definition Http.hpp:58
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 69 of file Http.cpp.

70{
71 // Prepare request specific options
72 setCommonFields(_hostAddr + index, CURLOPT_HTTPGET);
73 return performRequest(statusCode, receivedData);
74}
CURLcode performRequest(HttpStatus::Code &statusCode, std::string &receivedData)
Definition Http.cpp:24
void setCommonFields(const std::string &fullURL, 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 76 of file Http.cpp.

77{
78 // Prepare request specific options
79 setCommonFields(_hostAddr + index, CURLOPT_NOBODY);
80 return performRequest(statusCode, receivedData);
81}
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 83 of file Http.cpp.

85{
86 // Prepare request specific options
87 setCommonFields(_hostAddr + index, CURLOPT_POST, payload);
88 return performRequest(statusCode, receivedData);
89}
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 91 of file Http.cpp.

93{
94 // Prepare request specific options
95 setCommonFields(_hostAddr + index, CURLOPT_UPLOAD, payload);
96 return performRequest(statusCode, receivedData);
97}
Here is the call graph for this function:

◆ setCommonFields() [1/2]

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

Sets common fields for HTTP requests

Parameters
[in]fullURLThe full URL of the request
[in]methodThe HTTP method to use

Definition at line 6 of file Http.cpp.

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

◆ setCommonFields() [2/2]

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

Sets common fields for HTTP requests with payload

Parameters
[in]fullURLThe full URL of the request
[in]methodThe HTTP method to use
[in]payloadThe payload to send to the server

Definition at line 14 of file Http.cpp.

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

◆ 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 121 of file Http.hpp.

122 {
123 return curl_easy_setopt(_curl, option, value) == CURLE_OK;
124 }

◆ 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 39 of file Http.cpp.

40{
41 if (userp == nullptr)
42 {
43 return 0;
44 }
45
46 const size_t recvSize = size * nmemb;
47 userp->append(contents, recvSize);
48
49 return recvSize;
50}
Here is the caller graph for this function:

Member Data Documentation

◆ _curl

CURL* HTTP::_curl = curl_easy_init()
private

CURL handler.

Definition at line 56 of file Http.hpp.

◆ _data

std::unique_ptr<curlMemory> HTTP::_data = std::make_unique<curlMemory>()
private

Memory structure for CURL.

Definition at line 58 of file Http.hpp.

◆ _hostAddr

std::string HTTP::_hostAddr
private

Full path of server.

Definition at line 60 of file Http.hpp.


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