From bee5b676eeb24b19d789fbdc4094b8c8b7e12db0 Mon Sep 17 00:00:00 2001 From: Elias Norberg Date: Sun, 15 Jan 2012 20:06:03 +0100 Subject: Fix for FS#86 - Ability to control wheter 'Referer' will be sent or not The setting 'referer' can be one of four different values: 'always' - the default. always send referer 'never' - never send referer 'same-domain' - only send referer if it's the same domain Any other value, that is also a valid URL, will use this custom value as referer. (E.g. you could set it to http://no-referer.com) --- settings.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xxxterm.c | 42 ++++++++++++++++++++++++++++++++++++++++++ xxxterm.h | 8 ++++++++ 3 files changed, 107 insertions(+) diff --git a/settings.c b/settings.c index 298b7a5..61d7725 100644 --- a/settings.c +++ b/settings.c @@ -97,6 +97,8 @@ int enable_autoscroll = 0; int enable_favicon_entry = 1; int enable_favicon_tabs = 0; char *external_editor = NULL; +int referer_mode = XT_REFERER_ALWAYS; +char *referer_custom = NULL; char *cmd_font_name = NULL; char *oops_font_name = NULL; @@ -110,6 +112,7 @@ char *get_tab_style(struct settings *); char *get_edit_mode(struct settings *); char *get_download_mode(struct settings *); char *get_work_dir(struct settings *); +char *get_referer(struct settings *); int add_cookie_wl(struct settings *, char *); int add_js_wl(struct settings *, char *); @@ -131,6 +134,8 @@ int set_enable_favicon_entry(char *value); int set_enable_favicon_tabs(char *value); int set_download_mode(struct settings *, char *); int set_download_mode_rt(char *); +int set_referer(struct settings *, char *); +int set_referer_rt(char *); int set_external_editor(char *); void walk_mime_type(struct settings *, void (*)(struct settings *, @@ -277,6 +282,12 @@ struct special s_ua = { walk_ua }; +struct special s_referer = { + set_referer, + get_referer, + NULL +}; + struct settings rs[] = { { "allow_volatile_cookies", XT_S_INT, 0, &allow_volatile_cookies, NULL, NULL }, { "append_next", XT_S_INT, 0, &append_next, NULL, NULL }, @@ -338,6 +349,7 @@ struct settings rs[] = { { "enable_autoscroll", XT_S_INT, 0, &enable_autoscroll, NULL, NULL, NULL, set_enable_autoscroll }, { "enable_favicon_entry", XT_S_INT, 0, &enable_favicon_entry, NULL, NULL, NULL, set_enable_favicon_entry }, { "enable_favicon_tabs", XT_S_INT, 0, &enable_favicon_tabs, NULL, NULL, NULL, set_enable_favicon_tabs }, + { "referer", XT_S_STR, 0, NULL, NULL,&s_referer, NULL, set_referer_rt }, /* font settings */ { "cmd_font", XT_S_STR, 0, NULL, &cmd_font_name, NULL }, @@ -1076,6 +1088,51 @@ set_enable_favicon_tabs(char *value) return (0); } +char * +get_referer(struct settings *s) +{ + if (referer_mode == XT_REFERER_ALWAYS) + return (g_strdup("always")); + if (referer_mode == XT_REFERER_NEVER) + return (g_strdup("never")); + if (referer_mode == XT_REFERER_SAME_DOMAIN) + return (g_strdup("same-domain")); + if (referer_mode == XT_REFERER_CUSTOM) + return (g_strdup(referer_custom)); + return (NULL); +} + +int +set_referer(struct settings *s, char *value) +{ + if (referer_custom) + g_free(referer_custom); + + if (!strcmp(value, "always")) + referer_mode = XT_REFERER_ALWAYS; + else if (!strcmp(value, "never")) + referer_mode = XT_REFERER_NEVER; + else if (!strcmp(value, "same-domain")) + referer_mode = XT_REFERER_SAME_DOMAIN; + else if (!valid_url_type(value)) { + referer_mode = XT_REFERER_CUSTOM; + referer_custom = g_strdup(value); + } else { + /* we've already free'd the custom referer */ + if (referer_mode == XT_REFERER_CUSTOM) + referer_mode = XT_REFERER_NEVER; + return (1); + } + + return (0); +} + +int +set_referer_rt(char *value) +{ + return set_referer(NULL, value); +} + int set_external_editor(char *editor) { diff --git a/xxxterm.c b/xxxterm.c index 5b86427..e1e3383 100644 --- a/xxxterm.c +++ b/xxxterm.c @@ -4057,6 +4057,45 @@ webview_progress_changed_cb(WebKitWebView *wv, int progress, struct tab *t) update_statusbar_position(NULL, NULL); } +void +session_rq_cb(SoupSession *s, SoupMessage *msg, SoupSocket *socket, gpointer data) +{ + SoupURI *dest; + const char *ref; + + if (s == NULL || msg == NULL) + return; + + if (referer_mode == XT_REFERER_ALWAYS) + return; + + /* Check if referer is set - and what the user requested for referers */ + ref = soup_message_headers_get_one(msg->request_headers, "Referer"); + if (ref) { + DNPRINTF(XT_D_NAV, "session_rq_cb: Referer: %s\n", ref); + switch (referer_mode) { + case XT_REFERER_NEVER: + DNPRINTF(XT_D_NAV, "session_rq_cb: removing referer\n"); + soup_message_headers_remove(msg->request_headers, + "Referer"); + break; + case XT_REFERER_SAME_DOMAIN: + dest = soup_message_get_uri(msg); + + if (dest && !strstr(ref, dest->host)) { + soup_message_headers_remove(msg->request_headers, "Referer"); + DNPRINTF(XT_D_NAV, "session_rq_cb: removing referer (not same domain)\n"); + } + break; + case XT_REFERER_CUSTOM: + DNPRINTF(XT_D_NAV, "session_rq_cb: setting referer to %s\n", referer_custom); + soup_message_headers_replace(msg->request_headers, + "Referer", referer_custom); + break; + } + } +} + int webview_npd_cb(WebKitWebView *wv, WebKitWebFrame *wf, WebKitNetworkRequest *request, WebKitWebNavigationAction *na, @@ -7523,6 +7562,9 @@ main(int argc, char *argv[]) g_object_set(session, "max-conns-per-host", max_host_connections, (char *)NULL); + g_signal_connect(session, "request-queued", G_CALLBACK(session_rq_cb), + NULL); + /* see if there is already an xxxterm running */ if (single_instance && is_running()) { optn = 1; diff --git a/xxxterm.h b/xxxterm.h index a1d072f..af7c767 100644 --- a/xxxterm.h +++ b/xxxterm.h @@ -281,6 +281,7 @@ const gchar *get_uri(struct tab *); const gchar *get_title(struct tab *, bool); void load_uri(struct tab *t, gchar *uri); gboolean match_uri(const gchar *uri, const gchar *key); +int valid_url_type(char *); void load_webkit_string(struct tab *, const char *, gchar *); void button_set_stockid(GtkWidget *, char *); @@ -469,6 +470,11 @@ int command_mode(struct tab *, struct karg *); #define XT_DM_ASK (1) #define XT_DM_ADD (2) +#define XT_REFERER_ALWAYS (0) +#define XT_REFERER_NEVER (1) +#define XT_REFERER_SAME_DOMAIN (2) +#define XT_REFERER_CUSTOM (3) + #define CTRL GDK_CONTROL_MASK #define MOD1 GDK_MOD1_MASK #define SHFT GDK_SHIFT_MASK @@ -593,6 +599,8 @@ extern int auto_load_images; extern int enable_autoscroll; extern int enable_favicon_entry; extern int enable_favicon_tabs; +extern int referer_mode; +extern char *referer_custom; /* globals */ extern char *version; -- cgit 1.4.1-2-gfad0