From: Guilhem Bonnefille Date: Sat, 23 Jun 2007 14:44:34 +0000 (+0000) Subject: Add referer to DownloadOptions X-Git-Url: https://git.street.me.uk/andy/viking.git/commitdiff_plain/a31889935875a7d1d32c8dae2d6218d7938167f8?hp=108889308bdc1f0a1e3329f68ce89fbe14112cbd Add referer to DownloadOptions This allows to specify a referer per map source --- diff --git a/ChangeLog b/ChangeLog index f253a082..15ccfb9c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 2007-06-23 Guilhem Bonnefille : * Minor change to avoid SEGFAULT on x86_64 + * Add DownloadOptions type to specify referer per map source 2007-06-19 Quy Tonthat : diff --git a/src/curl_download.c b/src/curl_download.c index 3435e521..210d64c5 100644 --- a/src/curl_download.c +++ b/src/curl_download.c @@ -77,7 +77,7 @@ void curl_download_init() get_cookie_file(TRUE); } -int curl_download_uri ( const char *uri, FILE *f ) +int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options ) { CURL *curl; CURLcode res = CURLE_FAILED_INIT; @@ -88,8 +88,8 @@ int curl_download_uri ( const char *uri, FILE *f ) { 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/"); + if (options != NULL && options->referer != NULL) + curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer); 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); @@ -99,14 +99,14 @@ int curl_download_uri ( const char *uri, FILE *f ) return(res); } -int curl_download_get_url ( const char *hostname, const char *uri, FILE *f ) +int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options ) { int ret; gchar *full = NULL; /* Compose the full url */ full = g_strdup_printf ( "http://%s%s", hostname, uri ); - ret = curl_download_uri ( full, f ); + ret = curl_download_uri ( full, f, options ); g_free ( full ); full = NULL; diff --git a/src/curl_download.h b/src/curl_download.h index 8043b1d9..8ad40038 100644 --- a/src/curl_download.h +++ b/src/curl_download.h @@ -24,8 +24,10 @@ #include +#include "download.h" + void curl_download_init (); -int curl_download_get_url ( const char *hostname, const char *uri, FILE *f ); -int curl_download_uri ( const char *uri, FILE *f ); +int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options ); +int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options ); #endif diff --git a/src/download.c b/src/download.c index dea5b823..fcaef0b5 100644 --- a/src/download.c +++ b/src/download.c @@ -100,7 +100,7 @@ static int check_map_file(FILE* f) return(res); } -static int download( const char *hostname, const char *uri, const char *fn, int sendhostname) +static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options) { FILE *f; int ret; @@ -142,9 +142,9 @@ static int download( const char *hostname, const char *uri, const char *fn, int /* 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 || check_map_file(f)) @@ -168,7 +168,5 @@ static int download( const char *hostname, const char *uri, const char *fn, int /* 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, DownloadOptions *opt ) { - int sendhostname = 0; - if (opt != NULL) sendhostname = opt->sendhostname; - return download ( hostname, uri, fn, sendhostname ); + return download ( hostname, uri, fn, opt ); } diff --git a/src/download.h b/src/download.h index 5439a4ce..d31776ae 100644 --- a/src/download.h +++ b/src/download.h @@ -23,7 +23,16 @@ #define _VIKING_DOWNLOAD_H typedef struct { + /** + * Indicates if we should send hostname on the GET request. + * (see http.c) + */ int sendhostname; + /** + * The REFERER string to use. + * Could be NULL. + */ + gchar *referer; } DownloadOptions; /* TODO: convert to Glib */ diff --git a/src/expedia.c b/src/expedia.c index bc7b6086..cd0dfa9d 100644 --- a/src/expedia.c +++ b/src/expedia.c @@ -37,7 +37,7 @@ static gboolean expedia_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, static void expedia_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ); static int expedia_download ( MapCoord *src, const gchar *dest_fn ); -static DownloadOptions expedia_options = { 0 }; +static DownloadOptions expedia_options = { 0, NULL }; void expedia_init() { VikMapsLayer_MapType map_type = { 5, 0, 0, VIK_VIEWPORT_DRAWMODE_EXPEDIA, expedia_coord_to_mapcoord, expedia_mapcoord_to_center_coord, expedia_download }; diff --git a/src/google.c b/src/google.c index 62a480b6..4034eccc 100644 --- a/src/google.c +++ b/src/google.c @@ -40,7 +40,7 @@ static int google_kh_download ( MapCoord *src, const gchar *dest_fn ); static void google_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ); static gboolean google_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ); -static DownloadOptions google_options = { 1 }; +static DownloadOptions google_options = { 1, "http://maps.google.com/" }; void google_init () { VikMapsLayer_MapType google_1 = { 7, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_download }; diff --git a/src/googlemaps.c b/src/googlemaps.c index df8ff629..87795fdf 100644 --- a/src/googlemaps.c +++ b/src/googlemaps.c @@ -30,7 +30,7 @@ #include "googlemaps.h" -static DownloadOptions googlemaps_options = { 1 }; +static DownloadOptions googlemaps_options = { 1, "http://maps.google.com/" }; /* initialisation */ void googlemaps_init () { diff --git a/src/googlesearch.c b/src/googlesearch.c index a2c45be3..8037663c 100644 --- a/src/googlesearch.c +++ b/src/googlesearch.c @@ -34,6 +34,8 @@ static gchar *last_search_str = NULL; static VikCoord *last_coord = NULL; static gchar *last_successful_search_str = NULL; +static DownloadOptions googlesearch_options = { 0, "http://maps.google.com/" }; + gchar * a_googlesearch_get_search_string_for_this_place(VikWindow *vw) { if (!last_coord) @@ -206,7 +208,7 @@ static int google_search_get_coord(VikWindow *vw, VikViewport *vvp, gchar *srch_ uri = g_strdup_printf(GOOGLE_SEARCH_URL_FMT, escaped_srch_str); /* TODO: curl may not be available */ - if (curl_download_uri(uri, tmp_file)) { /* error */ + if (curl_download_uri(uri, tmp_file, &googlesearch_options)) { /* error */ fprintf(stderr, "DEBUG: %s() download error\n", __PRETTY_FUNCTION__); fclose(tmp_file); ret = -1; diff --git a/src/http.c b/src/http.c index b8e0dab5..6bbde741 100644 --- a/src/http.c +++ b/src/http.c @@ -88,7 +88,7 @@ int http_get_line(int sock, char *buf, int len) return 1; } -int http_download_get_url ( const char *hostname, const char *uri, FILE *f, 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; @@ -108,7 +108,7 @@ int http_download_get_url ( const char *hostname, const char *uri, FILE *f, int } - 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 ); diff --git a/src/http.h b/src/http.h index 04f5f743..d9aac4ab 100644 --- a/src/http.h +++ b/src/http.h @@ -24,6 +24,8 @@ #include -int http_download_get_url ( const char *hostname, const char *uri, FILE *f, int already_redirected, int sendhostname ); +#include "download.h" + +int http_download_get_url ( const char *hostname, const char *uri, FILE *f, int already_redirected, DownloadOptions *options ); #endif diff --git a/src/khmaps.c b/src/khmaps.c index bed9da85..5921c987 100644 --- a/src/khmaps.c +++ b/src/khmaps.c @@ -31,7 +31,7 @@ #include "khmaps.h" -static DownloadOptions khmaps_options = { 1 }; +static DownloadOptions khmaps_options = { 1, NULL }; void khmaps_init () { VikMapsLayer_MapType map_type = { 8, 256, 256, VIK_VIEWPORT_DRAWMODE_KH, khmaps_coord_to_mapcoord, khmaps_mapcoord_to_center_coord, khmaps_download }; diff --git a/src/osm.c b/src/osm.c index b49ec948..5fd04697 100644 --- a/src/osm.c +++ b/src/osm.c @@ -38,7 +38,7 @@ static int osm_maplint_download ( MapCoord *src, const gchar *dest_fn ); static int osm_mapnik_download ( MapCoord *src, const gchar *dest_fn ); static int osm_osmarender_download ( MapCoord *src, const gchar *dest_fn ); -static DownloadOptions osm_options = { 1 }; +static DownloadOptions osm_options = { 1, NULL }; /* initialisation */ void osm_init () { diff --git a/src/terraserver.c b/src/terraserver.c index 4499dbfd..68c77b8f 100644 --- a/src/terraserver.c +++ b/src/terraserver.c @@ -41,7 +41,7 @@ static int terraserver_urban_download ( MapCoord *src, const gchar *dest_fn ); static void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ); -static DownloadOptions terraserver_options = { 0 }; +static DownloadOptions terraserver_options = { 0, NULL }; void terraserver_init () { VikMapsLayer_MapType map_type_1 = { 2, 200, 200, VIK_VIEWPORT_DRAWMODE_UTM, terraserver_topo_coord_to_mapcoord, terraserver_mapcoord_to_center_coord, terraserver_topo_download };