From 35a7379dda587add9d94dc4f710414a23976d6e7 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 7 Feb 2019 23:54:07 +0000 Subject: Support download of certificates from Qube (#1460). --- src/lib/internet.cc | 40 ++++++++++++++++++++++++++++++++++++++++ src/lib/internet.h | 1 + 2 files changed, 41 insertions(+) (limited to 'src/lib') diff --git a/src/lib/internet.cc b/src/lib/internet.cc index e0af49b66..b993117bb 100644 --- a/src/lib/internet.cc +++ b/src/lib/internet.cc @@ -39,6 +39,46 @@ using boost::optional; using boost::function; using boost::algorithm::trim; +static size_t +ls_url_data (void* buffer, size_t size, size_t nmemb, void* output) +{ + string* s = reinterpret_cast(output); + char* c = reinterpret_cast(buffer); + for (size_t i = 0; i < (size * nmemb); ++i) { + *s += c[i]; + } + return nmemb; +} + +list +ls_url (string url) +{ + CURL* curl = curl_easy_init (); + curl_easy_setopt (curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt (curl, CURLOPT_DIRLISTONLY, 1); + + string ls; + curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ls_url_data); + curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls); + CURLcode const cr = curl_easy_perform (curl); + + if (cr != CURLE_OK) { + return list(); + } + + list result; + result.push_back(""); + for (size_t i = 0; i < ls.size(); ++i) { + if (ls[i] == '\n') { + result.push_back(""); + } else { + result.back() += ls[i]; + } + } + + result.pop_back (); + return result; +} static size_t get_from_url_data (void* buffer, size_t size, size_t nmemb, void* stream) diff --git a/src/lib/internet.h b/src/lib/internet.h index 125533b5d..5f9a25e3b 100644 --- a/src/lib/internet.h +++ b/src/lib/internet.h @@ -27,3 +27,4 @@ class ScopedTemporary; boost::optional get_from_url (std::string url, bool pasv, bool skip_pasv_ip, ScopedTemporary& temp); boost::optional get_from_url (std::string url, bool pasv, bool skip_pasv_ip, boost::function load); boost::optional get_from_zip_url (std::string url, std::string file, bool pasv, bool skip_pasv_ip, boost::function load); +std::list ls_url (std::string url); -- cgit v1.2.3