X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/469856bf9dec2656433baaa976b9af84d79769ae..c29a31985e639878d65faacbdac2a87431875edd:/src/curl_download.c diff --git a/src/curl_download.c b/src/curl_download.c index 961959cd..be54741c 100644 --- a/src/curl_download.c +++ b/src/curl_download.c @@ -27,6 +27,7 @@ #endif #include +#include #ifdef HAVE_UNISTD_H #include @@ -41,6 +42,7 @@ #include #include "background.h" +#include "dir.h" #include "file.h" #include "globals.h" #include "curl_download.h" @@ -123,11 +125,15 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D curl = handle ? handle : curl_easy_init (); if ( !curl ) { - return DOWNLOAD_ERROR; + return CURL_DOWNLOAD_ERROR; } if (vik_verbose) curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); curl_easy_setopt ( curl, CURLOPT_NOSIGNAL, 1 ); // Yep, we're a multi-threaded program so don't let signals mess it up! + if ( options != NULL && options->user_pass ) { + curl_easy_setopt ( curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); + curl_easy_setopt ( curl, CURLOPT_USERPWD, options->user_pass ); + } curl_easy_setopt ( curl, CURLOPT_URL, uri ); curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f ); curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); @@ -170,7 +176,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D glong response; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response); if (response == 304) { // 304 = Not Modified - res = DOWNLOAD_NO_NEWER_FILE; + res = CURL_DOWNLOAD_NO_NEWER_FILE; } else if (response == 200 || // http: 200 = Ok response == 226) { // ftp: 226 = sucess gdouble size; @@ -178,15 +184,15 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D when the server has a (incorrect) time earlier than the time on the file we already have */ curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size); if (size == 0) - res = DOWNLOAD_ERROR; + res = CURL_DOWNLOAD_ERROR; else - res = DOWNLOAD_NO_ERROR; + res = CURL_DOWNLOAD_NO_ERROR; } else { g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri); - res = DOWNLOAD_ERROR; + res = CURL_DOWNLOAD_ERROR; } } else { - res = DOWNLOAD_ERROR; + res = CURL_DOWNLOAD_ERROR; } if (!handle) curl_easy_cleanup ( curl ); @@ -203,10 +209,19 @@ int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, Down int ret; gchar *full = NULL; - /* Compose the full url */ - full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); + if ( strstr ( hostname, "://" ) != NULL ) + /* Already full url */ + full = (gchar *) hostname; + else if ( strstr ( uri, "://" ) != NULL ) + /* Already full url */ + full = (gchar *) uri; + else + /* Compose the full url */ + full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); ret = curl_download_uri ( full, f, options, file_options, handle ); - g_free ( full ); + /* Free newly allocated memory, but do not free uri */ + if ( hostname != full && uri != full ) + g_free ( full ); full = NULL; return ret;