Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  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.  
  23. #include "utils/config.h"
  24. #include "utils/utsname.h"
  25. #include "desktop/netsurf.h"
  26. #include "utils/log.h"
  27. #include "utils/useragent.h"
  28.  
  29. static const char *core_user_agent_string = NULL;
  30.  
  31. #ifndef NETSURF_UA_FORMAT_STRING
  32. #define NETSURF_UA_FORMAT_STRING "NetSurf/%d.%d (%s)"
  33. #endif
  34.  
  35. /**
  36.  * Prepare core_user_agent_string with a string suitable for use as a
  37.  * user agent in HTTP requests.
  38.  */
  39. static void
  40. user_agent_build_string(void)
  41. {
  42.         struct utsname un;
  43.         const char *sysname = "Unknown";
  44.         char *ua_string;
  45.         int len;
  46.  
  47.         if (uname(&un) >= 0) {
  48.                 sysname = un.sysname;
  49.         }
  50.  
  51.         len = snprintf(NULL, 0, NETSURF_UA_FORMAT_STRING,
  52.                        netsurf_version_major,
  53.                        netsurf_version_minor,
  54.                        sysname);
  55.         ua_string = malloc(len + 1);
  56.         if (!ua_string) {
  57.                 /** \todo this needs handling better */
  58.                 return;
  59.         }
  60.         snprintf(ua_string, len + 1,
  61.                  NETSURF_UA_FORMAT_STRING,
  62.                  netsurf_version_major,
  63.                  netsurf_version_minor,
  64.                  sysname);
  65.  
  66.         core_user_agent_string = ua_string;
  67.  
  68.         LOG(("Built user agent \"%s\"", core_user_agent_string));
  69. }
  70.  
  71. /* This is a function so that later we can override it trivially */
  72. const char *
  73. user_agent_string(void)
  74. {
  75.         if (core_user_agent_string == NULL)
  76.                 user_agent_build_string();
  77.         return core_user_agent_string;
  78. }
  79.  
  80.