Fix URL formatting issue
[viking/guyou.git] / src / download.c
blob1f10ed124a06dddd0217f70abf21b6b59a55ecfd
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <glib.h>
30 #include <glib/gstdio.h>
31 #include <glib/gi18n.h>
33 #include "download.h"
35 #include "curl_download.h"
37 static gboolean check_file_first_line(FILE* f, gchar *patterns[])
39 gchar **s;
40 gchar *bp;
41 fpos_t pos;
42 gchar buf[33];
43 size_t nr;
45 memset(buf, 0, sizeof(buf));
46 fgetpos(f, &pos);
47 rewind(f);
48 nr = fread(buf, 1, sizeof(buf) - 1, f);
49 fsetpos(f, &pos);
50 for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
51 if (!(isspace(*bp)))
52 break;
54 if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
55 return FALSE;
56 for (s = patterns; *s; s++) {
57 if (strncasecmp(*s, bp, strlen(*s)) == 0)
58 return TRUE;
60 return FALSE;
63 gboolean a_check_html_file(FILE* f)
65 gchar * html_str[] = {
66 "<html",
67 "<!DOCTYPE html",
68 "<head",
69 "<title",
70 NULL
73 return check_file_first_line(f, html_str);
76 gboolean a_check_map_file(FILE* f)
78 /* FIXME no more true since a_check_kml_file */
79 return !a_check_html_file(f);
82 gboolean a_check_kml_file(FILE* f)
84 gchar * kml_str[] = {
85 "<?xml",
86 NULL
89 return check_file_first_line(f, kml_str);
92 static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp)
94 FILE *f;
95 int ret;
96 gchar *tmpfilename;
97 gboolean failure = FALSE;
99 /* Check file */
100 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE )
102 /* File exists: return */
103 return -3;
104 } else {
105 gchar *dir = g_path_get_dirname ( fn );
106 g_mkdir_with_parents ( dir , 0777 );
107 g_free ( dir );
109 /* create placeholder file */
110 if ( ! (f = g_fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
111 return -4;
112 fclose ( f );
113 f = NULL;
116 tmpfilename = g_strdup_printf("%s.tmp", fn);
117 f = g_fopen ( tmpfilename, "w+b" );
118 if ( ! f ) {
119 g_free ( tmpfilename );
120 g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
121 return -4;
124 /* Call the backend function */
125 ret = curl_download_get_url ( hostname, uri, f, options, ftp );
126 if (ret == -1 || ret == 1 || ret == -2) {
127 g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
128 failure = TRUE;
131 if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) {
132 g_debug("%s: file content checking failed", __FUNCTION__);
133 failure = TRUE;
136 if (failure)
138 g_warning(_("Download error: %s"), fn);
139 fclose ( f );
140 f = NULL;
141 g_remove ( tmpfilename );
142 g_free ( tmpfilename );
143 g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
144 return -1;
147 fclose ( f );
148 f = NULL;
149 g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
150 g_free ( tmpfilename );
151 return ret;
154 /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
155 /* uri: like "/uri.html?whatever" */
156 /* only reason for the "wrapper" is so we can do redirects. */
157 int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
159 return download ( hostname, uri, fn, opt, FALSE );
162 int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
164 return download ( hostname, uri, fn, opt, TRUE );