Repo-Init
 
Loading...
Searching...
No Matches
Http.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <HttpStatusCodes_C++11.h>
4#include <curl/curl.h>
5
7constexpr int HTTP_TIMEOUT_MS = 1000;
8
38
43class HTTP {
44 private:
46 CURL *_curl = curl_easy_init();
48 std::string _hostAddr;
49
56 void setCommonFields(const std::string &fullURL, std::string &receivedData, CURLoption method);
57
65 void setCommonFields(const std::string &fullURL, std::string &receivedData, CURLoption method,
66 const std::string &payload);
67
76 static size_t writeDataCallback(const char *contents, size_t size, size_t nmemb, std::string *userp);
77
83 CURLcode performRequest(HttpStatus::Code &statusCode);
84
85 public:
91 explicit HTTP(std::string addr, int timeoutInMs = HTTP_TIMEOUT_MS);
92
94 HTTP(const HTTP & /*unused*/) = delete;
95
97 HTTP(HTTP && /*unused*/) = delete;
98
100 HTTP &operator=(HTTP /*unused*/) = delete;
101
103 HTTP &operator=(HTTP && /*unused*/) = delete;
104
111 template <typename T> bool setOption(CURLoption option, T value)
112 {
113 return curl_easy_setopt(_curl, option, value) == CURLE_OK;
114 }
115
120 [[nodiscard]] const std::string &getHostAddress() const { return _hostAddr; }
121
129 CURLcode sendGETRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode);
130
138 CURLcode sendHEADRequest(const std::string &index, std::string &receivedData, HttpStatus::Code &statusCode);
139
148 CURLcode sendPOSTRequest(const std::string &index, const std::string &payload, std::string &receivedData,
149 HttpStatus::Code &statusCode);
150
159 CURLcode sendPUTRequest(const std::string &index, const std::string &payload, std::string &receivedData,
160 HttpStatus::Code &statusCode);
161
167
171 ~HTTP();
172};
constexpr int HTTP_TIMEOUT_MS
HTTP connection timeout in milliseconds.
Definition Http.hpp:7
Definition Http.hpp:43
const std::string & getHostAddress() const
Definition Http.hpp:120
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
HTTP & operator=(HTTP)=delete
Copy assignment operator.
HTTP(const HTTP &)=delete
Copy constructor.
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
HTTP(HTTP &&)=delete
Move constructor.
HTTP & operator=(HTTP &&)=delete
Move assignment operator.
bool setOption(CURLoption option, T value)
Definition Http.hpp:111
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
long startTransferTime
Total processing time for starting transfer in microseconds.
Definition Http.hpp:34
size_t requestBytes
Total request size in bytes.
Definition Http.hpp:20
long uploadSpeed
Upload bandwidth in Bps.
Definition Http.hpp:22
long preTransferTime
Total processing time for pre-transfer period in microseconds.
Definition Http.hpp:30
size_t headerBytes
Total header size in bytes.
Definition Http.hpp:18
size_t uploadBytes
Total uploaded bytes.
Definition Http.hpp:14
long nameLookupTime
Total processing time for name lookup in microseconds.
Definition Http.hpp:28
long totalTime
Total processing time in microseconds.
Definition Http.hpp:36
long redirectTime
Total processing time for redirections in microseconds.
Definition Http.hpp:32
size_t downloadBytes
Total downloaded bytes.
Definition Http.hpp:16
long connectionTime
Total processing time for connection in microseconds.
Definition Http.hpp:26
long downloadSpeed
Download bandwidth in Bps.
Definition Http.hpp:24