X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/85611cd91c81b8218e954597a3aaf5e31bb22bfc..a31889935875a7d1d32c8dae2d6218d7938167f8:/src/http.c?ds=sidebyside diff --git a/src/http.c b/src/http.c index a610cd0d..6bbde741 100644 --- a/src/http.c +++ b/src/http.c @@ -20,41 +20,18 @@ */ #include +#include #include #include -#include -#include -#include - #ifdef WINDOWS - -#include #include -#define access(a,b) _access(a,b) -#define close(a) closesocket(a) - -char *dirname ( char * dir ) -{ - char *tmp = dir + strlen(dir) - 1; - while ( tmp != dir && *tmp != '\\' ) - tmp--; - *tmp = '\0'; - return dir; -} - #else - #include -#include #include #include #include - -/* dirname */ -#include - -#endif +#endif #include "http.h" @@ -111,36 +88,14 @@ int http_get_line(int sock, char *buf, int len) return 1; } -/* makes directory if neccessary */ -int http_download_get_url ( const char *hostname, const char *uri, const char *fn, int already_redirected, int sendhostname ) +int http_download_get_url ( const char *hostname, const char *uri, FILE *f, int already_redirected, DownloadOptions *options ) { static char input_buffer[1024]; int sock; int len; - FILE *f, *tmp_f; + FILE *tmp_f; /* int hnlen = strlen ( hostname ); */ - if ( access ( fn, F_OK ) == 0 ) - { - return -3; - } else { - if ( errno == ENOENT) - { - char *tmp = g_strdup ( fn ); -#ifdef WINDOWS - mkdir( dirname ( dirname ( tmp ) ) ); - g_free ( tmp ); tmp = g_strdup ( fn ); - mkdir( dirname ( tmp ) ); -#else - mkdir( dirname ( dirname ( tmp ) ), 0777 ); - g_free ( tmp ); tmp = g_strdup ( fn ); - mkdir( dirname ( tmp ), 0777 ); -#endif - g_free ( tmp ); - } - if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */ - return -4; - } #ifdef WINDOWS WSADATA usadata; WSAStartup ( MAKEWORD(2,2), &usadata ); @@ -149,13 +104,11 @@ int http_download_get_url ( const char *hostname, const char *uri, const char *f sock = http_connect ( hostname, 80 ); if (sock < 0) { - fclose ( f ); - remove ( fn ); return -1; } - if ( sendhostname ) { + if ( options != NULL && options->sendhostname ) { send ( sock, "GET http://", 11, 0); send ( sock, hostname, strlen(hostname), 0 ); send ( sock, uri, strlen ( uri ), 0 ); @@ -194,11 +147,9 @@ int http_download_get_url ( const char *hostname, const char *uri, const char *f { char *newhost = g_strndup ( input_buffer + 17, uri_start - input_buffer - 17 ); char *newuri = strdup ( uri_start ); - fclose ( f ); - remove ( fn ); close ( sock ); - rv = http_download_get_url ( newhost, newuri, fn, 1, sendhostname ); + rv = http_download_get_url ( newhost, newuri, f, 1, sendhostname ); free ( newhost ); free ( newuri ); @@ -207,8 +158,7 @@ int http_download_get_url ( const char *hostname, const char *uri, const char *f } } while (input_buffer[0] != '\r' ); - fclose ( f ); - remove ( fn ); + /* Something went wrong */ return 1; } @@ -216,8 +166,6 @@ int http_download_get_url ( const char *hostname, const char *uri, const char *f { if ( http_get_line ( sock, input_buffer, 1024 ) == 0 ) { - fclose ( f ); - remove ( fn ); close ( sock ); return -2; } @@ -239,7 +187,6 @@ int http_download_get_url ( const char *hostname, const char *uri, const char *f fwrite ( input_buffer, 1, len, f); } fclose ( tmp_f ); - fclose ( f ); close ( sock ); #ifdef WINDOWS