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, std::string &receivedData, CURLoption method)
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}
12
13void HTTP::setCommonFields(const std::string &fullURL, std::string &receivedData, CURLoption method,
14 const std::string &payload)
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}
22
23CURLcode HTTP::performRequest(HttpStatus::Code &statusCode)
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}
36
37size_t HTTP::writeDataCallback(const char *contents, size_t size, size_t nmemb, std::string *userp)
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}
45
46HTTP::HTTP(std::string addr, int timeoutInMs) : _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}
62
63CURLcode HTTP::sendGETRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
64{
65 // Prepare request specific options
66 setCommonFields(_hostAddr + index, receivedData, CURLOPT_HTTPGET);
67 return performRequest(statusCode);
68}
69
70CURLcode HTTP::sendHEADRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
71{
72 // Prepare request specific options
73 setCommonFields(_hostAddr + index, receivedData, CURLOPT_NOBODY);
74 return performRequest(statusCode);
75}
76
77CURLcode HTTP::sendPOSTRequest(const std::string &index, const std::string &payload, std::string &receivedData,
78 HttpStatus::Code &statusCode)
79{
80 // Prepare request specific options
81 setCommonFields(_hostAddr + index, receivedData, CURLOPT_POST, payload);
82 return performRequest(statusCode);
83}
84
85CURLcode HTTP::sendPUTRequest(const std::string &index, const std::string &payload, std::string &receivedData,
86 HttpStatus::Code &statusCode)
87{
88 // Prepare request specific options
89 setCommonFields(_hostAddr + index, receivedData, CURLOPT_UPLOAD, payload);
90 return performRequest(statusCode);
91}
92
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}
125
126HTTP::~HTTP() { curl_easy_cleanup(_curl); }
CURLcode sendPUTRequest(const std::string &index, const std::string &payload, std::string &receivedData, HttpStatus::Code &statusCode)
Definition Http.cpp:85
HTTP(std::string addr, int timeoutInMs=HTTP_TIMEOUT_MS)
Definition Http.cpp:46
CURLcode sendHEADRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
Definition Http.cpp:70
CURLcode performRequest(HttpStatus::Code &statusCode)
Definition Http.cpp:23
CURLcode sendPOSTRequest(const std::string &index, const std::string &payload, std::string &receivedData, HttpStatus::Code &statusCode)
Definition Http.cpp:77
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
~HTTP()
Definition Http.cpp:126
void setCommonFields(const std::string &fullURL, std::string &receivedData, CURLoption method)
Definition Http.cpp:6
CURLcode sendGETRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode)
Definition Http.cpp:63
HTTPStats getStats()
Definition Http.cpp:93
std::string _hostAddr
Full path of server.
Definition Http.hpp:48