From 5426a708a9010d04cf8baed45ce9909a09088ef6 Mon Sep 17 00:00:00 2001 From: Rob Kendrick Date: Tue, 30 Jan 2007 19:51:54 +0000 Subject: [PATCH] Generates and use a User-Agent: string based on new netsurf_version_major/minor values, and results of uname(). svn path=/trunk/netsurf/; revision=3158 --- content/fetch.c | 6 +++++- desktop/netsurf.h | 2 ++ desktop/version.c | 3 ++- utils/utils.c | 35 +++++++++++++++++++++++++++++++++++ utils/utils.h | 1 + 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/content/fetch.c b/content/fetch.c index 337e4a5ed..cead364a2 100644 --- a/content/fetch.c +++ b/content/fetch.c @@ -98,7 +98,7 @@ struct cache_handle { struct cache_handle *r_next; /**< Next cached handle in ring. */ }; -static const char * const user_agent = "NetSurf"; +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; @@ -212,6 +212,10 @@ 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) diff --git a/desktop/netsurf.h b/desktop/netsurf.h index 71422d76e..a1865a604 100644 --- a/desktop/netsurf.h +++ b/desktop/netsurf.h @@ -12,6 +12,8 @@ extern bool netsurf_quit; extern const char * const netsurf_version; +extern const int netsurf_version_major; +extern const int netsurf_version_minor; #endif diff --git a/desktop/version.c b/desktop/version.c index 0ba2a66e9..5fc254cd0 100644 --- a/desktop/version.c +++ b/desktop/version.c @@ -1,2 +1,3 @@ const char * const netsurf_version = "Development Build"; - +const int netsurf_version_major = 0; +const int netsurf_version_minor = 0; diff --git a/utils/utils.c b/utils/utils.c index b0707b7f2..54a3020ae 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -2,6 +2,7 @@ * This file is part of NetSurf, http://netsurf-browser.org/ * Licensed under the GNU General Public License, * http://www.opensource.org/licenses/gpl-license + * Copyright 2007 Rob Kendrick * Copyright 2004-2007 James Bursa * Copyright 2003 Phil Mellor * Copyright 2003 John M Bell @@ -15,6 +16,8 @@ #include #include #include +#include +#include #include #include #include "netsurf/utils/config.h" @@ -23,6 +26,7 @@ #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) @@ -254,6 +258,37 @@ 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 67173d242..85e6ebede 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -57,6 +57,7 @@ char *human_friendly_bytesize(unsigned long bytesize); const char *rfc1123_date(time_t t); char *strcasestr(const char *haystack, const char *needle); unsigned int wallclock(void); +char *make_useragent(void); #ifdef __FreeBSD__ /* FreeBSD lacks strndup */ char *strndup(const char *s, size_t n);