refactored curl code into new helper class
This commit is contained in:
35
main.cpp
35
main.cpp
@ -13,8 +13,8 @@
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <getopt.h>
|
||||
#include <curl/curl.h>
|
||||
#include <libsclogging.hpp>
|
||||
#include "requester.hpp"
|
||||
|
||||
sc::logger logger {"curly", sc::loglevel::info};
|
||||
|
||||
@ -28,16 +28,6 @@ void print_version() {
|
||||
std::cout << "curly version 1.0\n";
|
||||
}
|
||||
|
||||
size_t write_data(void* buf, size_t sz, size_t nmemb, void* userp) {
|
||||
size_t realsize = sz * nmemb;
|
||||
SCInfo(logger, "received ", realsize, " bytes");
|
||||
std::vector<char>* dest {reinterpret_cast<std::vector<char>*>(userp)};
|
||||
size_t oldsize = dest->size();
|
||||
dest->resize(oldsize + realsize);
|
||||
std::memcpy(dest->data() + oldsize, buf, realsize);
|
||||
return realsize;
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
try {
|
||||
int opt_char, opt_val;
|
||||
@ -76,27 +66,10 @@ int main(int argc, const char * argv[]) {
|
||||
}
|
||||
}
|
||||
std::cout << "hello, curly\n";
|
||||
requester req;
|
||||
std::cout << req.get("https://www.swiftcoder.nl/") << '\n';
|
||||
std::cout << req.get("https://www.swiftcoder.nl/moneydance/") << '\n';
|
||||
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
auto easy_handle = curl_easy_init();
|
||||
curl_easy_setopt(easy_handle, CURLOPT_URL, "https://www.swiftcoder.nl/");
|
||||
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
|
||||
std::vector<char> buf;
|
||||
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &buf);
|
||||
auto success = curl_easy_perform(easy_handle);
|
||||
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
|
||||
|
||||
std::string text {buf.data(), buf.size()};
|
||||
std::cout << text << '\n';
|
||||
|
||||
curl_easy_setopt(easy_handle, CURLOPT_URL, "https://www.swiftcoder.nl/moneydance/");
|
||||
buf.clear();
|
||||
success = curl_easy_perform(easy_handle);
|
||||
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
|
||||
|
||||
std::string page2 {buf.data(), buf.size()};
|
||||
std::cout << page2 << '\n';
|
||||
|
||||
} catch (const std::exception& ex) {
|
||||
std::cerr << "curly: " << ex.what() << '\n';
|
||||
return EXIT_FAILURE;
|
||||
|
Reference in New Issue
Block a user