diff --git a/lib/cppurl.hpp b/lib/cppurl.hpp index a81da5f..6717581 100644 --- a/lib/cppurl.hpp +++ b/lib/cppurl.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace marcelb { @@ -14,13 +15,43 @@ class Curl { CURL *curl; CURLcode res; string readBuffer; - - + struct curl_slist *headers = NULL; + string _useragent; public: - // Curl(); - string request(const string& req); + /** + * Postavi zaglavlje s ključem i vrijednošću + * Novi pozivi ne brišu stara zaglavlja, ponovljena se prepišu + */ + Curl& header(const string& key, const string& value); + + /** + * Postavi zaglavlja iz mape + * Ponovan poziv prepisat će ona zaglavlja koja postoje + */ + Curl& header(const map &_headers); + + /** + * Postavi u zaglavlje User-Agent + */ + Curl& useragent(const string& useragent_); + + /** + * Izvršiv HTTP GET zahtjev + * Vraća string HTTP tjela + */ + string get(const string& req); + + /** + * Obriši spremljeno zaglavlje + */ + Curl& clearheader(); + + /** + * Obrši trenutnog User-Agent -a + */ + Curl& clearuseragent(); }; diff --git a/src/cppurl.cpp b/src/cppurl.cpp index 7e784cc..a4b6c91 100644 --- a/src/cppurl.cpp +++ b/src/cppurl.cpp @@ -3,17 +3,42 @@ using namespace marcelb; -// marcelb::Curl::Curl() { -// } +static size_t marcelb::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; +} + +Curl& marcelb::Curl::header(const string& key, const string& value) { + headers = curl_slist_append(headers, string(key + ": " + value).c_str()); + return *this; +} + +Curl& marcelb::Curl::header(const map &_headers) { + for (auto h : _headers) { + header(h.first, h.second); + } + return *this; +} + +Curl& marcelb::Curl::useragent(const string& useragent_) { + _useragent = useragent_; + return *this; +} -string marcelb::Curl::request(const string& req){ +string marcelb::Curl::get(const string& req){ curl = curl_easy_init(); readBuffer.clear(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, req.c_str()); + if (headers != NULL) { + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + } + if (!_useragent.empty()) { + curl_easy_setopt(curl, CURLOPT_USERAGENT, _useragent.c_str()); + } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); res = curl_easy_perform(curl); @@ -23,8 +48,12 @@ string marcelb::Curl::request(const string& req){ return readBuffer; } +Curl& marcelb::Curl::clearheader() { + headers = NULL; + return *this; +} -static size_t marcelb::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { - ((std::string*)userp)->append((char*)contents, size * nmemb); - return size * nmemb; -} \ No newline at end of file +Curl& marcelb::Curl::clearuseragent() { + _useragent.clear(); + return *this; +} diff --git a/test/test b/test/test index d0b68c1..52392b6 100755 Binary files a/test/test and b/test/test differ diff --git a/test/test.cpp b/test/test.cpp index 41f058d..e48c09d 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -9,9 +9,9 @@ int main () { Curl rest; - cout << rest.request("https://reqres.in/api/unknown/2") << endl; - - cout << rest.request("https://reqres.in/api/users/2") << endl; + rest.header("Baba", "Janja").useragent("Dinio api client v1.1.0 - bitelex@bitelex.co"); + cout << rest.get("http://localhost:5000/?param1=tvt¶m2=2023") << endl; + // cout << rest.request("https://reqres.in/api/users/2") << endl; } \ No newline at end of file