Enable chose http version, get response headers, http and curl status code
This commit is contained in:
parent
16440a9c71
commit
dc26ed563f
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@ -10,19 +11,28 @@ namespace marcelb {
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp);
|
static size_t bodyCallback(void *contents, size_t size, size_t nmemb, void *body_ptr);
|
||||||
|
static size_t headerCallback(char* buffer, size_t size, size_t nitems, void* header_ptr);
|
||||||
|
enum http_version { DEFAULT, HTTP1_0, HTTP1_1, HTTP2, HTTP2TLS, HTTP2PK, HTTP3 = 30};
|
||||||
|
|
||||||
class Curl {
|
class Curl {
|
||||||
|
// input
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
string readBuffer;
|
|
||||||
struct curl_slist *headers = NULL;
|
struct curl_slist *headers = NULL;
|
||||||
string _useragent;
|
string _useragent;
|
||||||
long _timeout = 0;
|
long _timeout = 0;
|
||||||
bool _sslvalidate = true;
|
bool _sslverifyoff = false;
|
||||||
|
http_version _protocol_v = DEFAULT;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
// output
|
||||||
|
CURLcode curlStatus;
|
||||||
|
long httpStatus;
|
||||||
|
map<string, string> responseHeader;
|
||||||
|
string body;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Postavi zaglavlje s ključem i vrijednošću
|
* Postavi zaglavlje s ključem i vrijednošću
|
||||||
* Novi pozivi ne brišu stara zaglavlja, ponovljena se prepišu
|
* Novi pozivi ne brišu stara zaglavlja, ponovljena se prepišu
|
||||||
@ -50,7 +60,14 @@ class Curl {
|
|||||||
* Omogući/onemogući validaciju certifikata kod SSL veza
|
* Omogući/onemogući validaciju certifikata kod SSL veza
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Curl& sslvalidate(const bool sslvalidate_);
|
Curl& sslverifyoff();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Postavi verziju HTTP protokola
|
||||||
|
* HTTP1_0 - HTTP1_1 - HTTP2 - HTTP2TLS - HTTP2PK - HTTP3
|
||||||
|
*/
|
||||||
|
|
||||||
|
Curl& httpv(const http_version protocol_v);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Izvršiv HTTP GET zahtjev
|
* Izvršiv HTTP GET zahtjev
|
||||||
|
@ -3,11 +3,26 @@
|
|||||||
|
|
||||||
using namespace marcelb;
|
using namespace marcelb;
|
||||||
|
|
||||||
static size_t marcelb::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
|
static size_t marcelb::bodyCallback(void *contents, size_t size, size_t nmemb, void *body_ptr) {
|
||||||
((std::string*)userp)->append((char*)contents, size * nmemb);
|
((string*)body_ptr)->append((char*)contents, size * nmemb);
|
||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t marcelb::headerCallback(char* buffer, size_t size, size_t nitems, void* header_ptr) {
|
||||||
|
string header_member = string(buffer);
|
||||||
|
string key, value;
|
||||||
|
|
||||||
|
auto doublepoint = header_member.find(": ");
|
||||||
|
if (doublepoint < header_member.length()){
|
||||||
|
key = header_member.substr(0, doublepoint);
|
||||||
|
value = header_member.substr(doublepoint+2, header_member.length()-2);
|
||||||
|
((map<string, string>*)header_ptr)->insert(make_pair(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
return nitems * size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Curl& marcelb::Curl::header(const string& key, const string& value) {
|
Curl& marcelb::Curl::header(const string& key, const string& value) {
|
||||||
headers = curl_slist_append(headers, string(key + ": " + value).c_str());
|
headers = curl_slist_append(headers, string(key + ": " + value).c_str());
|
||||||
return *this;
|
return *this;
|
||||||
@ -31,15 +46,20 @@ Curl& marcelb::Curl::timeout(const long timeout_) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Curl& marcelb::Curl::sslvalidate(const bool sslvalidate_) {
|
Curl& marcelb::Curl::sslverifyoff() {
|
||||||
_sslvalidate = sslvalidate_;
|
_sslverifyoff = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Curl& marcelb::Curl::httpv(const http_version protocol_v) {
|
||||||
|
_protocol_v = protocol_v;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
string marcelb::Curl::get(const string& req){
|
string marcelb::Curl::get(const string& req){
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
|
|
||||||
readBuffer.clear();
|
body.clear();
|
||||||
|
|
||||||
if(curl) {
|
if(curl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, req.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, req.c_str());
|
||||||
@ -52,17 +72,27 @@ string marcelb::Curl::get(const string& req){
|
|||||||
if (_timeout > 0) {
|
if (_timeout > 0) {
|
||||||
curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS , _timeout);
|
curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS , _timeout);
|
||||||
}
|
}
|
||||||
if (!_sslvalidate) {
|
if (_sslverifyoff) {
|
||||||
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, _sslvalidate ? 1 : 0);
|
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, _sslverifyoff ? 0 : 1);
|
||||||
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, _sslvalidate ? 1 : 0);
|
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, _sslverifyoff ? 0 : 1);
|
||||||
|
}
|
||||||
|
if (_protocol_v > DEFAULT) {
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)_protocol_v);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bodyCallback);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headerCallback);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &responseHeader);
|
||||||
|
|
||||||
|
curlStatus = curl_easy_perform(curl);
|
||||||
|
if(curlStatus == CURLE_OK) {
|
||||||
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpStatus);
|
||||||
}
|
}
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
|
||||||
res = curl_easy_perform(curl);
|
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
}
|
}
|
||||||
|
|
||||||
return readBuffer;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
Curl& marcelb::Curl::clearheader() {
|
Curl& marcelb::Curl::clearheader() {
|
||||||
|
@ -11,10 +11,17 @@ using namespace marcelb;
|
|||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
|
|
||||||
// Curl rest;
|
Curl rest;
|
||||||
// string header_value = "jebiga";
|
string header_value = "jebiga";
|
||||||
// rest.header("API", header_value);
|
rest.header("API", header_value);
|
||||||
// cout << rest.get("https://reqres.in/api/users/2") << endl;
|
cout << rest.get("https://reqres.in/api/users/2") << endl <<
|
||||||
|
"Curl status " << rest.curlStatus << endl <<
|
||||||
|
" http status " << rest.httpStatus << endl;
|
||||||
|
|
||||||
|
for (auto header : rest.responseHeader) {
|
||||||
|
cout << header.first << " " << header.second << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// vector<thread> thr;
|
// vector<thread> thr;
|
||||||
|
|
||||||
@ -47,20 +54,21 @@ int main () {
|
|||||||
// t1.join();
|
// t1.join();
|
||||||
// t2.join();
|
// t2.join();
|
||||||
|
|
||||||
vector<future<string>> debx_responses;
|
// vector<future<string>> debx_responses;
|
||||||
|
|
||||||
for (uint i=0; i<4; i++) {
|
// for (uint i=0; i<4; i++) {
|
||||||
debx_responses.push_back(async(launch::async, [&](){
|
// debx_responses.push_back(async(launch::async, [&](){
|
||||||
Curl rest;
|
// Curl rest;
|
||||||
// rest.timeout(6000);
|
// // rest.timeout(6000);
|
||||||
rest.sslvalidate(false);
|
// rest.sslverifyoff().httpv(HTTP2);
|
||||||
return rest.get("https://lab-it.ddns.net");
|
// return rest.get("https://lab-it.ddns.net");
|
||||||
}));
|
// }));
|
||||||
}
|
// }
|
||||||
|
|
||||||
for (uint i=0; i<4; i++) {
|
// for (uint i=0; i<4; i++) {
|
||||||
cout << debx_responses[i].get() << endl << endl;
|
// // cout << debx_responses[i].get() << endl << endl;
|
||||||
}
|
// debx_responses[i].get();
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user