- curl = curl_easy_init ();
- if ( curl )
- {
- curl_easy_setopt ( curl, CURLOPT_URL, uri );
- curl_easy_setopt ( curl, CURLOPT_FILE, f );
- if (strstr(uri, ".google.com"))
- curl_easy_setopt ( curl, CURLOPT_REFERER, "http://maps.google.com/");
- curl_easy_setopt ( curl, CURLOPT_USERAGENT, "viking/0.1.3 libcurl/7.15.4" );
- if (cookie_file = get_cookie_file(FALSE))
- curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
- res = curl_easy_perform ( curl );
- curl_easy_cleanup ( curl );
+ g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
+
+ curl = handle ? handle : curl_easy_init ();
+ if ( !curl ) {
+ return DOWNLOAD_ERROR;
+ }
+ if (vik_verbose)
+ curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
+ curl_easy_setopt ( curl, CURLOPT_URL, uri );
+ curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
+ curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
+ curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 );
+ curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL );
+ curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
+ if (options != NULL) {
+ if(options->referer != NULL)
+ curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
+ if(options->follow_location != 0) {
+ curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
+ curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
+ }
+ if (file_options != NULL) {
+ if(options->check_file_server_time && file_options->time_condition != 0) {
+ /* if file exists, check against server if file is recent enough */
+ curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
+ curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition);
+ }
+ if (options->use_etag) {
+ if (file_options->etag != NULL) {
+ /* add an header on the HTTP request */
+ char str[60];
+ g_snprintf(str, 60, "If-None-Match: %s", file_options->etag);
+ curl_send_headers = curl_slist_append(curl_send_headers, str);
+ curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers);
+ }
+ /* store the new etag from the server in an option value */
+ curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag));
+ curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func);
+ }
+ }
+ }
+ curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent );
+ if ((cookie_file = get_cookie_file(FALSE)) != NULL)
+ curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
+ res = curl_easy_perform ( curl );
+
+ if (res == 0) {
+ glong response;
+ curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
+ if (response == 304) { // 304 = Not Modified
+ res = DOWNLOAD_NO_NEWER_FILE;
+ } else if (response == 200 || // http: 200 = Ok
+ response == 226) { // ftp: 226 = sucess
+ gdouble size;
+ /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION
+ 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;
+ else
+ res = DOWNLOAD_NO_ERROR;
+ } else {
+ g_warning("%s: http response: %ld for uri %s (time_condition = %ld)\n", __FUNCTION__, response, uri, file_options->time_condition);
+ res = DOWNLOAD_ERROR;