summaryrefslogtreecommitdiff
path: root/fetch
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2019-10-23 22:25:36 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2019-10-23 22:25:36 -0500
commit8ef18e36a16625db16f2b39abfd4a19607bcf9fa (patch)
tree3789a7f48782c8a280df651a85485f91c1a1d734 /fetch
parent36d2505e1a4845cf2eeb4ce9bc2be5ce4d38ee80 (diff)
downloadhorizon-8ef18e36a16625db16f2b39abfd4a19607bcf9fa.tar.gz
horizon-8ef18e36a16625db16f2b39abfd4a19607bcf9fa.tar.bz2
horizon-8ef18e36a16625db16f2b39abfd4a19607bcf9fa.tar.xz
horizon-8ef18e36a16625db16f2b39abfd4a19607bcf9fa.zip
fetch: Finish implementing
Diffstat (limited to 'fetch')
-rw-r--r--fetch/CMakeLists.txt7
-rw-r--r--fetch/fetch.cc45
2 files changed, 49 insertions, 3 deletions
diff --git a/fetch/CMakeLists.txt b/fetch/CMakeLists.txt
index 7f83a36..e445dee 100644
--- a/fetch/CMakeLists.txt
+++ b/fetch/CMakeLists.txt
@@ -1,8 +1,15 @@
+pkg_check_modules(CURL libcurl)
+
set(FETCH_SRCS
fetch.cc
)
add_executable(hscript-fetch ${FETCH_SRCS})
+IF(CURL_FOUND)
+ add_definitions(-DHAVE_LIBCURL)
+ target_link_libraries(hscript-fetch ${CURL_LIBRARIES})
+ENDIF(CURL_FOUND)
+
install(TARGETS hscript-fetch DESTINATION bin)
IF(RSPEC_EXECUTABLE)
diff --git a/fetch/fetch.cc b/fetch/fetch.cc
index 941051f..5f696be 100644
--- a/fetch/fetch.cc
+++ b/fetch/fetch.cc
@@ -11,6 +11,11 @@
#include <algorithm> /* transform */
#include <cstdlib> /* EXIT_* */
+#ifdef HAVE_LIBCURL
+# include <cstring> /* strerror */
+# include <curl/curl.h> /* curl_* */
+# include <errno.h> /* errno */
+#endif /* HAVE_LIBCURL */
#include <fstream>
#include <map>
#include <string>
@@ -54,7 +59,7 @@ int process_local(const std::string &path) {
}
if(input.fail() && !input.eof()) {
- output_error("process_local", "line length error reading " + path);
+ output_error("process_local", "line too long while reading " + path);
return EXIT_FAILURE;
}
if(input.bad() && !input.eof()) {
@@ -66,12 +71,44 @@ int process_local(const std::string &path) {
}
+#ifdef HAVE_LIBCURL
/*! Download an installfile using cURL.
* @param path The remote path to download.
*/
int process_curl(const std::string &path) {
- return EXIT_FAILURE;
+ CURL *curl = curl_easy_init();
+ CURLcode result;
+ int return_code = EXIT_FAILURE;
+ char errbuf[CURL_ERROR_SIZE];
+ FILE *fp;
+
+ if(curl == nullptr) {
+ output_error("internal", "trouble initialising cURL library");
+ return EXIT_FAILURE;
+ }
+
+ fp = fopen(IFILE_PATH, "w");
+ if(fp == nullptr) {
+ output_error("internal", "couldn't open " + std::string(IFILE_PATH) +
+ " for writing", strerror(errno));
+ curl_easy_cleanup(curl);
+ return EXIT_FAILURE;
+ }
+
+ curl_easy_setopt(curl, CURLOPT_URL, path.c_str());
+ curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
+
+ result = curl_easy_perform(curl);
+ if(result == CURLE_OK) {
+ return_code = EXIT_SUCCESS;
+ } else {
+ output_error("curl", "couldn't download installfile", errbuf);
+ }
+ curl_easy_cleanup(curl);
+ return return_code;
}
+#endif /* HAVE_LIBCURL */
/*! The signature of a protocol dispatch function */
@@ -80,9 +117,11 @@ typedef int (*proto_dispatch_fn)(const std::string &);
/*! Protocol handlers */
static const std::map<std::string, proto_dispatch_fn> protos = {
+#ifdef HAVE_LIBCURL
{"http", process_curl},
{"https", process_curl},
- {"tftp", process_curl}
+ {"tftp", process_curl},
+#endif /* HAVE_LIBCURL */
};