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/ring.h | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/useragent.c | 60 ++++++++++++++++++++++++++++++++++ utils/useragent.h | 19 +++++++++++ 3 files changed, 177 insertions(+) create mode 100644 utils/ring.h create mode 100644 utils/useragent.c create mode 100644 utils/useragent.h (limited to 'utils') diff --git a/utils/ring.h b/utils/ring.h new file mode 100644 index 000000000..31698cd46 --- /dev/null +++ b/utils/ring.h @@ -0,0 +1,98 @@ +/* + * 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 2006, 2007 Daniel Silverstone + */ + +/** \file + * Ring list structure. + * + * Rings are structures which have an r_next pointer and an r_prev + * pointer which are always initialised and always point at the next + * or previous element respectively. + * + * The degenerate case of a single element in the ring simply points + * at itself in both directions. A zero element ring is NULL. + * + * Some of the ring functions are specific to the fetcher but may be + * of use to others and are thus included here. + */ + +#ifndef _NETSURF_UTILS_RING_H_ +#define _NETSURF_UTILS_RING_H_ + + +/** Insert the given item into the specified ring. + * Assumes that the element is zeroed as appropriate. + */ +#define RING_INSERT(ring,element) \ + /*LOG(("RING_INSERT(%s, %p(%s))", #ring, element, element->host));*/ \ + if (ring) { \ + element->r_next = ring; \ + element->r_prev = ring->r_prev; \ + ring->r_prev = element; \ + element->r_prev->r_next = element; \ + } else \ + ring = element->r_prev = element->r_next = element + +/** Remove the given element from the specified ring. + * Will zero the element as needed + */ +#define RING_REMOVE(ring, element) \ + /*LOG(("RING_REMOVE(%s, %p(%s)", #ring, element, element->host));*/ \ + if (element->r_next != element ) { \ + /* Not the only thing in the ring */ \ + element->r_next->r_prev = element->r_prev; \ + element->r_prev->r_next = element->r_next; \ + if (ring == element) ring = element->r_next; \ + } else { \ + /* Only thing in the ring */ \ + ring = 0; \ + } \ + element->r_next = element->r_prev = 0 + +/** Find the element (by hostname) in the given ring, leave it in the + * provided element variable + */ +#define RING_FINDBYHOST(ring, element, hostname) \ + /*LOG(("RING_FINDBYHOST(%s, %s)", #ring, hostname));*/ \ + if (ring) { \ + bool found = false; \ + element = ring; \ + do { \ + if (strcasecmp(element->host, hostname) == 0) { \ + found = true; \ + break; \ + } \ + element = element->r_next; \ + } while (element != ring); \ + if (!found) element = 0; \ + } else element = 0 + +/** Measure the size of a ring and put it in the supplied variable */ +#define RING_GETSIZE(ringtype, ring, sizevar) \ + /*LOG(("RING_GETSIZE(%s)", #ring));*/ \ + if (ring) { \ + ringtype *p = ring; \ + sizevar = 0; \ + do { \ + sizevar++; \ + p = p->r_next; \ + } while (p != ring); \ + } else sizevar = 0 + +/** Count the number of elements in the ring which match the provided hostname */ +#define RING_COUNTBYHOST(ringtype, ring, sizevar, hostname) \ + /*LOG(("RING_COUNTBYHOST(%s, %s)", #ring, hostname));*/ \ + if (ring) { \ + ringtype *p = ring; \ + sizevar = 0; \ + do { \ + if (strcasecmp(p->host, hostname) == 0) \ + sizevar++; \ + p = p->r_next; \ + } while (p != ring); \ + } else sizevar = 0 + +#endif 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; +} diff --git a/utils/useragent.h b/utils/useragent.h new file mode 100644 index 000000000..2cfa8b0c0 --- /dev/null +++ b/utils/useragent.h @@ -0,0 +1,19 @@ +/* + * 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 + */ + +#ifndef _NETSURF_UTILS_USERAGENT_H_ +#define _NETSURF_UTILS_USERAGENT_H_ + +/** Retrieve the core user agent for this release. + * + * The string returned can be relied upon to exist for the duration of + * the execution of the program. There is no need to copy it. + */ +const char * user_agent_string(void); + +#endif -- cgit v1.2.3