From 05474225342b64a751b1cc8bc61e81e8803b7079 Mon Sep 17 00:00:00 2001 From: James Bursa Date: Wed, 2 May 2007 02:38:38 +0000 Subject: Move user-agent generation into fetch.c and simplify. svn path=/trunk/netsurf/; revision=3260 --- content/fetch.c | 45 +++++++++++++++++++++++++++++++++++++-------- utils/utils.c | 32 -------------------------------- utils/utils.h | 1 - 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/content/fetch.c b/content/fetch.c index dda6e2923..032d0d48a 100644 --- a/content/fetch.c +++ b/content/fetch.c @@ -3,7 +3,7 @@ * Licensed under the GNU General Public License, * http://www.opensource.org/licenses/gpl-license * Copyright 2006 Daniel Silverstone - * Copyright 2004 James Bursa + * Copyright 2007 James Bursa * Copyright 2003 Phil Mellor */ @@ -20,12 +20,14 @@ */ #include +#include #include #include #include #include #include #include +#include #ifdef riscos #include #endif @@ -36,6 +38,7 @@ #endif #include "netsurf/content/fetch.h" #include "netsurf/content/urldb.h" +#include "netsurf/desktop/netsurf.h" #include "netsurf/desktop/options.h" #include "netsurf/render/form.h" #define NDEBUG @@ -100,7 +103,6 @@ struct cache_handle { struct cache_handle *r_next; /**< Next cached handle in ring. */ }; -static char *user_agent = "NetSurf"; CURLM *fetch_curl_multi; /**< Global cURL multi handle. */ /** Curl handle with default options set; not used for transfers. */ static CURL *fetch_blank_curl; @@ -111,7 +113,9 @@ static struct cache_handle *handle_ring = 0; /**< Ring of cached handles */ static char fetch_error_buffer[CURL_ERROR_SIZE]; /**< Error buffer for cURL. */ static char fetch_progress_buffer[256]; /**< Progress buffer for cURL */ static char fetch_proxy_userpwd[100]; /**< Proxy authentication details. */ +static char fetch_user_agent[100] = "NetSurf"; +static void fetch_init_user_agent(void); static CURLcode fetch_set_options(struct fetch *f); #ifdef WITH_SSL static CURLcode fetch_sslctxfun(CURL *curl_handle, SSL_CTX *sslctx, void *p); @@ -217,10 +221,6 @@ static void fetch_dispatch_jobs(void); void fetch_init(void) { CURLcode code; - char *ua = make_useragent(); - - if (ua != NULL) - user_agent = ua; code = curl_global_init(CURL_GLOBAL_ALL); if (code != CURLE_OK) @@ -232,6 +232,8 @@ void fetch_init(void) die("Failed to initialise the fetch module " "(curl_multi_init failed)."); + fetch_init_user_agent(); + /* Create a curl easy handle with the options that are common to all fetches. */ fetch_blank_curl = curl_easy_init(); @@ -250,7 +252,7 @@ void fetch_init(void) SETOPT(CURLOPT_HEADERFUNCTION, fetch_curl_header); SETOPT(CURLOPT_PROGRESSFUNCTION, fetch_curl_progress); SETOPT(CURLOPT_NOPROGRESS, 0); - SETOPT(CURLOPT_USERAGENT, user_agent); + SETOPT(CURLOPT_USERAGENT, fetch_user_agent); SETOPT(CURLOPT_ENCODING, "gzip"); SETOPT(CURLOPT_LOW_SPEED_LIMIT, 1L); SETOPT(CURLOPT_LOW_SPEED_TIME, 180L); @@ -268,6 +270,33 @@ curl_easy_setopt_failed: } +/** + * Fill fetch_user_agent with a string suitable for use as a user agent in + * HTTP requests. + */ + +void fetch_init_user_agent(void) +{ + struct utsname un; + + if (uname(&un) != 0) { + LOG(("uname: %i %s", errno, strerror(errno))); + die("Failed to initialise the fetch module " + "(uname failed)."); + } + + snprintf(fetch_user_agent, sizeof fetch_user_agent, + "NetSurf/%d.%d (%s; %s)", + netsurf_version_major, + netsurf_version_minor, + un.sysname, + un.machine); + fetch_user_agent[sizeof fetch_user_agent - 1] = 0; + + LOG(("fetch_user_agent \"%s\"", fetch_user_agent)); +} + + /** * Clean up for quit. * @@ -1098,7 +1127,7 @@ size_t fetch_curl_data(void *data, size_t size, size_t nmemb, return size * nmemb; } - LOG(("fetch %p, size %u", f, size * nmemb)); + LOG(("fetch %p, size %lu", f, size * nmemb)); if (f->abort || (!f->had_headers && fetch_process_headers(f))) { f->stopped = true; diff --git a/utils/utils.c b/utils/utils.c index 54a3020ae..cfd7d68aa 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -26,7 +25,6 @@ #include "netsurf/utils/messages.h" #include "netsurf/utils/utf8.h" #include "netsurf/utils/utils.h" -#include "netsurf/desktop/netsurf.h" char * strip(char * const s) @@ -258,36 +256,6 @@ unsigned int wallclock(void) return ((tv.tv_sec * 100) + (tv.tv_usec / 10000)); } -/** Generate a string suitable for use as a user agent in HTTP requests. - * - * \return heap-allocated result string, or NULL if the allocation failed. - */ -#define UA_BUF_SIZE 128 -char *make_useragent(void) -{ - struct utsname un; - char ua_name[UA_BUF_SIZE]; - char ua_machine[UA_BUF_SIZE]; - char *r; - - snprintf(ua_name, UA_BUF_SIZE, "NetSurf/%d.%d", - netsurf_version_major, - netsurf_version_minor); - - if (uname(&un) != 0) { - strncpy(ua_machine, "unknown machine", UA_BUF_SIZE); - } else { - snprintf(ua_machine, UA_BUF_SIZE, "(%s; %s)", un.sysname, - un.machine); - } - - if ((r = malloc(strlen(ua_name) + strlen(ua_machine) + 2)) == NULL) - return NULL; - - sprintf(r, "%s %s", ua_name, ua_machine); - - return r; -} #ifdef __FreeBSD__ diff --git a/utils/utils.h b/utils/utils.h index bd7ab166e..d080bd018 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -59,7 +59,6 @@ const char *rfc1123_date(time_t t); char *strcasestr(const char *haystack, const char *needle); #endif unsigned int wallclock(void); -char *make_useragent(void); #ifdef __FreeBSD__ /* FreeBSD lacks strndup */ char *strndup(const char *s, size_t n); -- cgit v1.2.3