Add timeout option

dev v0.2
marcelb 12 months ago
parent 2a7dc27323
commit da19d0a45c
  1. 7
      lib/cppurl.hpp
  2. 8
      src/cppurl.cpp
  3. BIN
      test/test
  4. 56
      test/test.cpp

@ -17,6 +17,7 @@ class Curl {
string readBuffer; string readBuffer;
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
string _useragent; string _useragent;
long _timeout = 0;
public: public:
@ -37,6 +38,12 @@ class Curl {
*/ */
Curl& useragent(const string& useragent_); Curl& useragent(const string& useragent_);
/**
* Postavi vrijeme isteka zahtjeva
*/
Curl& timeout(const long _timeout);
/** /**
* Izvršiv HTTP GET zahtjev * Izvršiv HTTP GET zahtjev
* Vraća string HTTP tjela * Vraća string HTTP tjela

@ -26,6 +26,11 @@ Curl& marcelb::Curl::useragent(const string& useragent_) {
return *this; return *this;
} }
Curl& marcelb::Curl::timeout(const long timeout_) {
_timeout = timeout_;
}
string marcelb::Curl::get(const string& req){ string marcelb::Curl::get(const string& req){
curl = curl_easy_init(); curl = curl_easy_init();
@ -39,6 +44,9 @@ string marcelb::Curl::get(const string& req){
if (!_useragent.empty()) { if (!_useragent.empty()) {
curl_easy_setopt(curl, CURLOPT_USERAGENT, _useragent.c_str()); curl_easy_setopt(curl, CURLOPT_USERAGENT, _useragent.c_str());
} }
if (_timeout > 0) {
curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS , _timeout);
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);

Binary file not shown.

@ -1,4 +1,8 @@
#include <iostream> #include <iostream>
#include <vector>
#include <thread>
#include <future>
#include <string>
#include "../lib/cppurl.hpp" #include "../lib/cppurl.hpp"
@ -7,11 +11,55 @@ using namespace marcelb;
int main () { int main () {
Curl rest; // Curl rest;
// string header_value = "jebiga";
// rest.header("API", header_value);
// cout << rest.get("https://reqres.in/api/users/2") << endl;
rest.header("Baba", "Janja").useragent("Dinio api client v1.1.0 - bitelex@bitelex.co"); // vector<thread> thr;
cout << rest.get("http://localhost:5000/?param1=tvt&param2=2023") << endl;
// cout << rest.request("https://reqres.in/api/users/2") << endl;
// for (uint i=0; i<4; i++) {
// thr.push_back(thread([](){
// Curl rest;
// string header_value = "jebiga";
// rest.header("API", header_value);
// cout << rest.get("https://reqres.in/api/users/2") << endl;
// }));
// }
// for (uint i=0; i<thr.size(); i++) {
// thr[i].join();
// }
// thread t1([](){
// Curl rest;
// cout << rest.get("https://reqres.in/api/users/2") << endl;
// });
// // t1.join();
// thread t2([](){
// Curl rest;
// cout << rest.get("https://reqres.in/api/users/2") << endl;
// });
// t1.join();
// t2.join();
vector<future<string>> debx_responses;
for (uint i=0; i<4; i++) {
debx_responses.push_back(async(launch::async, [&](){
Curl rest;
rest.timeout(600);
return rest.get("https://reqres.iin/api/users/2");
}));
}
for (uint i=0; i<4; i++) {
cout << debx_responses[i].get() << endl << endl;
}
} }
Loading…
Cancel
Save