diff options
author | Rob Kendrick <rjek@netsurf-browser.org> | 2007-01-30 19:51:54 +0000 |
---|---|---|
committer | Rob Kendrick <rjek@netsurf-browser.org> | 2007-01-30 19:51:54 +0000 |
commit | 5426a708a9010d04cf8baed45ce9909a09088ef6 (patch) | |
tree | 9dc519c8edf0d3a186920fcf9f1e2e9e0df91ff6 /utils | |
parent | 7c88381a59771a3e0be35800af3496da1171e9a4 (diff) | |
download | netsurf-5426a708a9010d04cf8baed45ce9909a09088ef6.tar.gz netsurf-5426a708a9010d04cf8baed45ce9909a09088ef6.tar.bz2 |
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
Diffstat (limited to 'utils')
-rw-r--r-- | utils/utils.c | 35 | ||||
-rw-r--r-- | utils/utils.h | 1 |
2 files changed, 36 insertions, 0 deletions
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 <rjek@netsurf-browser.org> * Copyright 2004-2007 James Bursa <bursa@users.sourceforge.net> * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net> * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk> @@ -15,6 +16,8 @@ #include <string.h> #include <sys/stat.h> #include <sys/types.h> +#include <sys/utsname.h> +#include <sys/time.h> #include <regex.h> #include <time.h> #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); |