]> git.street.me.uk Git - andy/viking.git/blobdiff - src/download.c
Add referer to DownloadOptions
[andy/viking.git] / src / download.c
index e23e7863becb0d42be52f7dbda54f6af9bafa4b2..fcaef0b5db1da8bee67c2cec9bfba69d90a1f274 100644 (file)
@@ -25,6 +25,9 @@
 
 #include <stdio.h>
 #include <errno.h>
+#include <ctype.h>
+#include <string.h>
+#include <strings.h>
 #include <gtk/gtk.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -62,10 +65,46 @@ char *dirname ( char * dir )
 
 #endif 
 
-static int download( const char *hostname, const char *uri, const char *fn, int sendhostname)
+static int check_map_file(FILE* f)
+{
+  char **s;
+  char *bp;
+  int res = 0;  /* good */
+  fpos_t pos;
+  char buf[33];
+  size_t nr;
+  char * html_str[] = {
+    "<html",
+    "<!DOCTYPE html",
+    "<head",
+    "<title",
+    NULL
+  };
+
+
+  bzero(buf, sizeof(buf));
+  fgetpos(f, &pos);
+  rewind(f);
+  nr = fread(buf, 1, sizeof(buf) - 1, f);
+  fsetpos(f, &pos);
+  for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
+    if (!(isspace(*bp)))
+      break;
+  }
+  if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
+    return(res);
+  for (s = html_str; *s; s++) {
+    if (strncmp(*s, bp, strlen(*s)) == 0)
+      return(-1);
+  }
+  return(res);
+}
+
+static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options)
 {
   FILE *f;
   int ret;
+  char *tmpfilename;
 
   /* Check file */
   if ( access ( fn, F_OK ) == 0 )
@@ -87,37 +126,47 @@ static int download( const char *hostname, const char *uri, const char *fn, int
 #endif
       g_free ( tmp );
     }
+    /* create placeholder file */
     if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
       return -4;
+    fclose ( f );
+  }
+
+  tmpfilename = g_strdup_printf("%s.tmp", fn);
+  f = fopen ( tmpfilename, "w+b" );
+  if ( ! f ) {
+    g_free ( tmpfilename );
+    remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
+    return -4;
   }
 
   /* Call the backend function */
 #ifdef HAVE_LIBCURL
-  ret = curl_download_get_url ( hostname, uri, f );
+  ret = curl_download_get_url ( hostname, uri, f, options );
 #else
-  ret = http_download_get_url ( hostname, uri, f, 0, sendhostname );
+  ret = http_download_get_url ( hostname, uri, f, 0, options );
 #endif
 
-  if (ret == -1 || ret == 1 || ret == -2)
+  if (ret == -1 || ret == 1 || ret == -2 || check_map_file(f))
   {
+    fprintf(stderr, "Download error: %s\n", fn);
     fclose ( f );
-    remove ( fn );
+    remove ( tmpfilename );
+    g_free ( tmpfilename );
+    remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
     return -1;
   }
 
   fclose ( f );
+  rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
+  g_free ( tmpfilename );
   return ret;
 }
 
 /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
 /* uri: like "/uri.html?whatever" */
 /* only reason for the "wrapper" is so we can do redirects. */
-int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn )
-{
-  return download ( hostname, uri, fn, 1 );
-}
-
-int a_http_download_get_url_nohostname ( const char *hostname, const char *uri, const char *fn )
+int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
 {
-  return download ( hostname, uri, fn, 0 );
+  return download ( hostname, uri, fn, opt );
 }