Move user-agent generation into fetch.c and simplify.

svn path=/trunk/netsurf/; revision=3260
This commit is contained in:
James Bursa 2007-05-02 02:38:38 +00:00
parent 6e6d397e9d
commit 0547422534
3 changed files with 37 additions and 41 deletions

View File

@ -3,7 +3,7 @@
* Licensed under the GNU General Public License, * Licensed under the GNU General Public License,
* http://www.opensource.org/licenses/gpl-license * http://www.opensource.org/licenses/gpl-license
* Copyright 2006 Daniel Silverstone <dsilvers@digital-scurf.org> * Copyright 2006 Daniel Silverstone <dsilvers@digital-scurf.org>
* Copyright 2004 James Bursa <bursa@users.sourceforge.net> * Copyright 2007 James Bursa <bursa@users.sourceforge.net>
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net> * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
*/ */
@ -20,12 +20,14 @@
*/ */
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <time.h> #include <time.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/utsname.h>
#ifdef riscos #ifdef riscos
#include <unixlib/local.h> #include <unixlib/local.h>
#endif #endif
@ -36,6 +38,7 @@
#endif #endif
#include "netsurf/content/fetch.h" #include "netsurf/content/fetch.h"
#include "netsurf/content/urldb.h" #include "netsurf/content/urldb.h"
#include "netsurf/desktop/netsurf.h"
#include "netsurf/desktop/options.h" #include "netsurf/desktop/options.h"
#include "netsurf/render/form.h" #include "netsurf/render/form.h"
#define NDEBUG #define NDEBUG
@ -100,7 +103,6 @@ struct cache_handle {
struct cache_handle *r_next; /**< Next cached handle in ring. */ struct cache_handle *r_next; /**< Next cached handle in ring. */
}; };
static char *user_agent = "NetSurf";
CURLM *fetch_curl_multi; /**< Global cURL multi handle. */ CURLM *fetch_curl_multi; /**< Global cURL multi handle. */
/** Curl handle with default options set; not used for transfers. */ /** Curl handle with default options set; not used for transfers. */
static CURL *fetch_blank_curl; static CURL *fetch_blank_curl;
@ -111,7 +113,9 @@ static struct cache_handle *handle_ring = 0; /**< Ring of cached handles */
static char fetch_error_buffer[CURL_ERROR_SIZE]; /**< Error buffer for cURL. */ static char fetch_error_buffer[CURL_ERROR_SIZE]; /**< Error buffer for cURL. */
static char fetch_progress_buffer[256]; /**< Progress buffer for cURL */ static char fetch_progress_buffer[256]; /**< Progress buffer for cURL */
static char fetch_proxy_userpwd[100]; /**< Proxy authentication details. */ static char fetch_proxy_userpwd[100]; /**< Proxy authentication details. */
static char fetch_user_agent[100] = "NetSurf";
static void fetch_init_user_agent(void);
static CURLcode fetch_set_options(struct fetch *f); static CURLcode fetch_set_options(struct fetch *f);
#ifdef WITH_SSL #ifdef WITH_SSL
static CURLcode fetch_sslctxfun(CURL *curl_handle, SSL_CTX *sslctx, void *p); static CURLcode fetch_sslctxfun(CURL *curl_handle, SSL_CTX *sslctx, void *p);
@ -217,10 +221,6 @@ static void fetch_dispatch_jobs(void);
void fetch_init(void) void fetch_init(void)
{ {
CURLcode code; CURLcode code;
char *ua = make_useragent();
if (ua != NULL)
user_agent = ua;
code = curl_global_init(CURL_GLOBAL_ALL); code = curl_global_init(CURL_GLOBAL_ALL);
if (code != CURLE_OK) if (code != CURLE_OK)
@ -232,6 +232,8 @@ void fetch_init(void)
die("Failed to initialise the fetch module " die("Failed to initialise the fetch module "
"(curl_multi_init failed)."); "(curl_multi_init failed).");
fetch_init_user_agent();
/* Create a curl easy handle with the options that are common to all /* Create a curl easy handle with the options that are common to all
fetches. */ fetches. */
fetch_blank_curl = curl_easy_init(); fetch_blank_curl = curl_easy_init();
@ -250,7 +252,7 @@ void fetch_init(void)
SETOPT(CURLOPT_HEADERFUNCTION, fetch_curl_header); SETOPT(CURLOPT_HEADERFUNCTION, fetch_curl_header);
SETOPT(CURLOPT_PROGRESSFUNCTION, fetch_curl_progress); SETOPT(CURLOPT_PROGRESSFUNCTION, fetch_curl_progress);
SETOPT(CURLOPT_NOPROGRESS, 0); SETOPT(CURLOPT_NOPROGRESS, 0);
SETOPT(CURLOPT_USERAGENT, user_agent); SETOPT(CURLOPT_USERAGENT, fetch_user_agent);
SETOPT(CURLOPT_ENCODING, "gzip"); SETOPT(CURLOPT_ENCODING, "gzip");
SETOPT(CURLOPT_LOW_SPEED_LIMIT, 1L); SETOPT(CURLOPT_LOW_SPEED_LIMIT, 1L);
SETOPT(CURLOPT_LOW_SPEED_TIME, 180L); SETOPT(CURLOPT_LOW_SPEED_TIME, 180L);
@ -268,6 +270,33 @@ curl_easy_setopt_failed:
} }
/**
* Fill fetch_user_agent with a string suitable for use as a user agent in
* HTTP requests.
*/
void fetch_init_user_agent(void)
{
struct utsname un;
if (uname(&un) != 0) {
LOG(("uname: %i %s", errno, strerror(errno)));
die("Failed to initialise the fetch module "
"(uname failed).");
}
snprintf(fetch_user_agent, sizeof fetch_user_agent,
"NetSurf/%d.%d (%s; %s)",
netsurf_version_major,
netsurf_version_minor,
un.sysname,
un.machine);
fetch_user_agent[sizeof fetch_user_agent - 1] = 0;
LOG(("fetch_user_agent \"%s\"", fetch_user_agent));
}
/** /**
* Clean up for quit. * Clean up for quit.
* *
@ -1098,7 +1127,7 @@ size_t fetch_curl_data(void *data, size_t size, size_t nmemb,
return size * nmemb; return size * nmemb;
} }
LOG(("fetch %p, size %u", f, size * nmemb)); LOG(("fetch %p, size %lu", f, size * nmemb));
if (f->abort || (!f->had_headers && fetch_process_headers(f))) { if (f->abort || (!f->had_headers && fetch_process_headers(f))) {
f->stopped = true; f->stopped = true;

View File

@ -16,7 +16,6 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/utsname.h>
#include <sys/time.h> #include <sys/time.h>
#include <regex.h> #include <regex.h>
#include <time.h> #include <time.h>
@ -26,7 +25,6 @@
#include "netsurf/utils/messages.h" #include "netsurf/utils/messages.h"
#include "netsurf/utils/utf8.h" #include "netsurf/utils/utf8.h"
#include "netsurf/utils/utils.h" #include "netsurf/utils/utils.h"
#include "netsurf/desktop/netsurf.h"
char * strip(char * const s) char * strip(char * const s)
@ -258,36 +256,6 @@ unsigned int wallclock(void)
return ((tv.tv_sec * 100) + (tv.tv_usec / 10000)); 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__ #ifdef __FreeBSD__

View File

@ -59,7 +59,6 @@ const char *rfc1123_date(time_t t);
char *strcasestr(const char *haystack, const char *needle); char *strcasestr(const char *haystack, const char *needle);
#endif #endif
unsigned int wallclock(void); unsigned int wallclock(void);
char *make_useragent(void);
#ifdef __FreeBSD__ #ifdef __FreeBSD__
/* FreeBSD lacks strndup */ /* FreeBSD lacks strndup */
char *strndup(const char *s, size_t n); char *strndup(const char *s, size_t n);