NetSurf
useragent.c
Go to the documentation of this file.
1/*
2 * Copyright 2007 Daniel Silverstone <dsilvers@digital-scurf.org>
3 * Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org>
4 *
5 * This file is part of NetSurf, http://www.netsurf-browser.org/
6 *
7 * NetSurf is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * NetSurf is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "utils/config.h"
25#include "utils/utsname.h"
26#include "desktop/version.h"
27#include "utils/log.h"
28#include "utils/useragent.h"
29
30static const char *core_user_agent_string = NULL;
31
32#ifndef NETSURF_UA_FORMAT_STRING
33#define NETSURF_UA_FORMAT_STRING "Mozilla/5.0 (%s) NetSurf/%d.%d"
34#endif
35
36/**
37 * Prepare core_user_agent_string with a string suitable for use as a
38 * user agent in HTTP requests.
39 */
40static void
42{
43 struct utsname un;
44 const char *sysname = "Unknown";
45 char *ua_string;
46 int len;
47
48 if (uname(&un) >= 0) {
49 sysname = un.sysname;
50 if (strcmp(sysname, "Linux") == 0) {
51 /* Force desktop, not mobile */
52 sysname = "X11; Linux";
53 }
54 }
55
56 len = snprintf(NULL, 0, NETSURF_UA_FORMAT_STRING,
57 sysname,
60 ua_string = malloc(len + 1);
61 if (!ua_string) {
62 /** \todo this needs handling better */
63 return;
64 }
65 snprintf(ua_string, len + 1,
67 sysname,
70
71 core_user_agent_string = ua_string;
72
73 NSLOG(netsurf, INFO, "Built user agent \"%s\"",
75}
76
77/* This is a function so that later we can override it trivially */
78const char *
80{
81 if (core_user_agent_string == NULL)
84}
85
86/* Public API documented in useragent.h */
87void
89{
90 if (core_user_agent_string != NULL) {
91 /* Nasty cast because we need to de-const it to free it */
92 free((void *)core_user_agent_string);
94 }
95}
const int netsurf_version_minor
NetSuf browser minor version number.
Definition: version.c:30
const int netsurf_version_major
NetSuf browser major version number.
Definition: version.c:29
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Interface to utility string handling.
system information filled in by uname derived from posix spec.
Definition: utsname.h:31
char sysname[65]
Operating system name (e.g., "Linux")
Definition: utsname.h:32
void free_user_agent_string(void)
Free any memory allocated for the user_agent_string.
Definition: useragent.c:88
const char * user_agent_string(void)
Retrieve the core user agent for this release.
Definition: useragent.c:79
#define NETSURF_UA_FORMAT_STRING
Definition: useragent.c:33
static void user_agent_build_string(void)
Prepare core_user_agent_string with a string suitable for use as a user agent in HTTP requests.
Definition: useragent.c:41
static const char * core_user_agent_string
Definition: useragent.c:30
int uname(struct utsname *buf)
Get the system information.
Definition: utils.c:459
Interface to uts API to get name and information about current kernel.
Version information interface.