Repo-Init
 
Loading...
Searching...
No Matches
Http.cpp
Go to the documentation of this file.
1#include "connection/Http.hpp"
2
3#include <cstring>
4#include <stdexcept>
5
6void HTTP::setCommonFields(const std::string &fullURL, CURLoption method)
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}
13
14void HTTP::setCommonFields(const std::string &fullURL, CURLoption method, const std::string &payload)
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}
23
24CURLcode HTTP::performRequest(HttpStatus::Code &statusCode, std::string &receivedData)
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}
38
39size_t HTTP::writeDataCallback(const char *contents, size_t size, size_t nmemb, std::string *userp)
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}
51
52HTTP::HTTP(std::string addr, int timeoutInMs) : _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}
68
69CURLcode HTTP::sendGETRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
70{
71 // Prepare request specific options
72 setCommonFields(_hostAddr + index, CURLOPT_HTTPGET);
73 return performRequest(statusCode, receivedData);
74}
75
76CURLcode HTTP::sendHEADRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
77{
78 // Prepare request specific options
79 setCommonFields(_hostAddr + index, CURLOPT_NOBODY);
80 return performRequest(statusCode, receivedData);
81}
82
83CURLcode HTTP::sendPOSTRequest(const std::string &index, const std::string &payload, std::string &receivedData,
84 HttpStatus::Code &statusCode)
85{
86 // Prepare request specific options
87 setCommonFields(_hostAddr + index, CURLOPT_POST, payload);
88 return performRequest(statusCode, receivedData);
89}
90
91CURLcode HTTP::sendPUTRequest(const std::string &index, const std::string &payload, std::string &receivedData,
92 HttpStatus::Code &statusCode)
93{
94 // Prepare request specific options
95 setCommonFields(_hostAddr + index, CURLOPT_UPLOAD, payload);
96 return performRequest(statusCode, receivedData);
97}
98
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}
131
132HTTP::~HTTP() { curl_easy_cleanup(_curl); }
CURLcode sendPUTRequest(const std::string &index, const std::string &payload, std::string &receivedData, HttpStatus::Code &statusCode)
Definition Http.cpp:91
HTTP(std::string addr, int timeoutInMs=HTTP_TIMEOUT_MS)
Definition Http.cpp:52
CURLcode sendHEADRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
Definition Http.cpp:76
CURLcode sendPOSTRequest(const std::string &index, const std::string &payload, std::string &receivedData, HttpStatus::Code &statusCode)
Definition Http.cpp:83
CURLcode performRequest(HttpStatus::Code &statusCode, std::string &receivedData)
Definition Http.cpp:24
CURL * _curl
CURL handler.
Definition Http.hpp:56
std::unique_ptr< curlMemory > _data
Memory structure for CURL.
Definition Http.hpp:58
static size_t writeDataCallback(const char *contents, size_t size, size_t nmemb, std::string *userp)
Definition Http.cpp:39
~HTTP()
Definition Http.cpp:132
void setCommonFields(const std::string &fullURL, CURLoption method)
Definition Http.cpp:6
CURLcode sendGETRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
Definition Http.cpp:69
HTTPStats getStats()
Definition Http.cpp:99
std::string _hostAddr
Full path of server.
Definition Http.hpp:60