Generally improve handling of download hostname and uri parameters and report if not set.
curl_global_cleanup();
}
-int curl_download_uri ( const char *uri, FILE *f, DownloadFileOptions *options, CurlDownloadOptions *cdo, void *handle )
+/**
+ *
+ */
+CURL_download_t curl_download_uri ( const char *uri, FILE *f, DownloadFileOptions *options, CurlDownloadOptions *cdo, void *handle )
{
CURL *curl;
struct curl_slist *curl_send_headers = NULL;
return res;
}
-int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadFileOptions *options, gboolean ftp, CurlDownloadOptions *cdo, void *handle )
+/**
+ * curl_download_get_url:
+ * Either hostname or uri should be defined
+ *
+ */
+CURL_download_t curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadFileOptions *options, gboolean ftp, CurlDownloadOptions *cdo, void *handle )
{
- int ret;
gchar *full = NULL;
- if ( strstr ( hostname, "://" ) != NULL )
+ if ( hostname && strstr ( hostname, "://" ) != NULL )
/* Already full url */
full = (gchar *) hostname;
- else if ( strstr ( uri, "://" ) != NULL )
+ else if ( uri && strstr ( uri, "://" ) != NULL )
/* Already full url */
full = (gchar *) uri;
- else
+ else if ( hostname && uri )
/* Compose the full url */
full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
- ret = curl_download_uri ( full, f, options, cdo, handle );
- /* Free newly allocated memory, but do not free uri */
- if ( hostname != full && uri != full )
- g_free ( full );
- full = NULL;
+ else {
+ return CURL_DOWNLOAD_ERROR;
+ }
+
+ CURL_download_t ret = curl_download_uri ( full, f, options, cdo, handle );
+ g_free ( full );
return ret;
}
G_BEGIN_DECLS
/* Error messages returned by download functions */
-enum { CURL_DOWNLOAD_NO_ERROR = 0,
- CURL_DOWNLOAD_NO_NEWER_FILE,
- CURL_DOWNLOAD_ERROR };
+typedef enum {
+ CURL_DOWNLOAD_NO_ERROR = 0,
+ CURL_DOWNLOAD_NO_NEWER_FILE,
+ CURL_DOWNLOAD_ERROR
+} CURL_download_t;
typedef struct {
/**
void curl_download_init ();
void curl_download_uninit ();
-int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadFileOptions *options, gboolean ftp, CurlDownloadOptions *curl_options, void *handle );
-int curl_download_uri ( const char *uri, FILE *f, DownloadFileOptions *options, CurlDownloadOptions *curl_options, void *handle );
+CURL_download_t curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadFileOptions *options, gboolean ftp, CurlDownloadOptions *curl_options, void *handle );
+CURL_download_t curl_download_uri ( const char *uri, FILE *f, DownloadFileOptions *options, CurlDownloadOptions *curl_options, void *handle );
void * curl_download_handle_init ();
void curl_download_handle_cleanup ( void * handle );
static DownloadResult_t download( const char *hostname, const char *uri, const char *fn, DownloadFileOptions *options, gboolean ftp, void *handle)
{
FILE *f;
- int ret;
gchar *tmpfilename;
gboolean failure = FALSE;
CurlDownloadOptions cdo = {0, NULL, NULL};
g_free ( dir );
}
+ // Early test for valid hostname & uri to avoid unnecessary tmp file
+ if ( !hostname && !uri ) {
+ g_warning ( "%s: Parameter error - neither hostname nor uri defined", __FUNCTION__ );
+ return DOWNLOAD_PARAMETERS_ERROR;
+ }
+
tmpfilename = g_strdup_printf("%s.tmp", fn);
if (!lock_file ( tmpfilename ) )
{
}
/* Call the backend function */
- ret = curl_download_get_url ( hostname, uri, f, options, ftp, &cdo, handle );
+ CURL_download_t ret = curl_download_get_url ( hostname, uri, f, options, ftp, &cdo, handle );
DownloadResult_t result = DOWNLOAD_SUCCESS;
void a_download_uninit(void);
typedef enum {
+ DOWNLOAD_PARAMETERS_ERROR = -8, // Configuration issue
DOWNLOAD_FILE_WRITE_ERROR = -4, // Can't write downloaded file :(
DOWNLOAD_HTTP_ERROR = -2,
DOWNLOAD_CONTENT_ERROR = -1,
static DownloadFileOptions options = { FALSE, FALSE, NULL, 0, a_check_map_file, NULL, NULL };
DownloadResult_t result = a_http_download_get_url ( SRTM_HTTP_SITE, src_fn, p->dest, &options, NULL );
switch ( result ) {
+ case DOWNLOAD_PARAMETERS_ERROR:
case DOWNLOAD_CONTENT_ERROR:
case DOWNLOAD_HTTP_ERROR: {
gchar *msg = g_strdup_printf ( _("DEM download failure for %f, %f"), p->lat, p->lon );
if (need_download) {
DownloadResult_t dr = vik_map_source_download( MAPS_LAYER_NTH_TYPE(mdi->maptype), &(mdi->mapcoord), mdi->filename_buf, handle);
switch ( dr ) {
+ case DOWNLOAD_PARAMETERS_ERROR:
case DOWNLOAD_HTTP_ERROR:
case DOWNLOAD_CONTENT_ERROR: {
// TODO: ?? count up the number of download errors somehow...