From 1dd7e97eb3f2cd317ef64fa9c054aacba6499215 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sun, 10 Jun 2007 17:46:44 +0000 Subject: Merge scheme switcher branch in. svn path=/trunk/netsurf/; revision=3330 --- utils/useragent.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 utils/useragent.c (limited to 'utils/useragent.c') diff --git a/utils/useragent.c b/utils/useragent.c new file mode 100644 index 000000000..b76255f2c --- /dev/null +++ b/utils/useragent.c @@ -0,0 +1,60 @@ +/* + * 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 Daniel Silverstone + * Copyright 2007 Rob Kendrick + */ + +#include +#include + +#include "useragent.h" +#include "desktop/netsurf.h" +#include "utils/log.h" + +static const char *core_user_agent_string = NULL; + +#define NETSURF_UA_FORMAT_STRING "NetSurf/%d.%d (%s; %s)" + +/** + * Prepare core_user_agent_string with a string suitable for use as a + * user agent in HTTP requests. + */ +static void +build_user_agent(void) +{ + struct utsname un; + const char *sysname = "Unknown"; + const char *machine = "Unknown"; + int len; + + if (uname(&un) == 0) { + sysname = un.sysname; + machine = un.machine; + } + + len = snprintf(NULL, 0, NETSURF_UA_FORMAT_STRING, + netsurf_version_major, + netsurf_version_minor, + un.sysname, + un.machine); + core_user_agent_string = malloc(len + 1); + snprintf(core_user_agent_string, len + 1, + NETSURF_UA_FORMAT_STRING, + netsurf_version_major, + netsurf_version_minor, + un.sysname, + un.machine); + + LOG(("Built user agent \"%s\"", core_user_agent_string)); +} + +/* This is a function so that later we can override it trivially */ +const char * +user_agent_string(void) +{ + if (core_user_agent_string == NULL) + build_user_agent(); + return core_user_agent_string; +} -- cgit v1.2.3