From eae38c656b22b748eabe6f46e71bed94284fcfa1 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Wed, 13 Jun 2012 09:30:00 -0400 Subject: Change user_agent to rotate on a connection per tab (instead of globally for all connections), and implement http_accept which acts the same way but sets the HTTP Accept header. --- xombrero.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 8 deletions(-) (limited to 'xombrero.c') diff --git a/xombrero.c b/xombrero.c index 606ea2d..aacaa32 100644 --- a/xombrero.c +++ b/xombrero.c @@ -211,9 +211,9 @@ struct undo_tailq undos; struct keybinding_list kbl; struct sp_list spl; struct user_agent_list ua_list; +struct http_accept_list ha_list; struct cmd_alias_list cal; struct custom_uri_list cul; -int user_agent_count = 0; struct command_list chl; struct command_list shl; struct command_entry *history_at; @@ -695,6 +695,20 @@ sv_ignore_rb_cmp(struct sv_ignore *s1, struct sv_ignore *s2) } RB_GENERATE(sv_ignore_list, sv_ignore, entry, sv_ignore_rb_cmp); +int +user_agent_rb_cmp(struct user_agent *ua1, struct user_agent *ua2) +{ + return (ua1->id < ua2->id ? -1 : ua1->id > ua2->id); +} +RB_GENERATE(user_agent_list, user_agent, entry, user_agent_rb_cmp); + +int +http_accept_rb_cmp(struct http_accept *ha1, struct http_accept *ha2) +{ + return (ha1->id < ha2->id ? -1 : ha1->id > ha2->id); +} +RB_GENERATE(http_accept_list, http_accept, entry, http_accept_rb_cmp); + struct valid_url_types { char *type; } vut[] = { @@ -4699,9 +4713,10 @@ webview_npd_cb(WebKitWebView *wv, WebKitWebFrame *wf, WebKitNetworkRequest *request, WebKitWebNavigationAction *na, WebKitWebPolicyDecision *pd, struct tab *t) { - char *uri; WebKitWebNavigationReason reason; + struct user_agent ua_find, *ua; struct domain *d = NULL; + char *uri; if (t == NULL) { show_oops(NULL, "webview_npd_cb invalid parameters"); @@ -4740,13 +4755,17 @@ webview_npd_cb(WebKitWebView *wv, WebKitWebFrame *wf, } /* Change user agent if more than one has been given. */ - if (user_agent_count > 1) { - struct user_agent *ua; + if (!RB_EMPTY(&ua_list)) { + ua_find.id = t->user_agent_id; - if ((ua = TAILQ_NEXT(user_agent, entry)) == NULL) - user_agent = TAILQ_FIRST(&ua_list); - else + if ((ua = RB_FIND(user_agent_list, &ua_list, &ua_find)) == NULL) { + ua_find.id = 0; + t->user_agent_id = 1; + user_agent = RB_FIND(user_agent_list, &ua_list, &ua_find); + } else { + ++t->user_agent_id; user_agent = ua; + } free(t->user_agent); t->user_agent = g_strdup(user_agent->value); @@ -4784,6 +4803,8 @@ webview_rrs_cb(WebKitWebView *wv, WebKitWebFrame *wf, WebKitWebResource *res, { SoupMessage *msg; SoupURI *uri; + struct http_accept ha_find, *ha; + const char *accept; msg = webkit_network_request_get_message(request); if (!msg) return; @@ -4798,6 +4819,30 @@ webview_rrs_cb(WebKitWebView *wv, WebKitWebFrame *wf, WebKitWebResource *res, soup_uri_set_scheme(uri, SOUP_URI_SCHEME_HTTPS); } } + + /* Round-robin through HTTP Accept headers if any have been set */ + if (!RB_EMPTY(&ha_list)) { + accept = soup_message_headers_get_list(msg->request_headers, + "Accept"); + if (accept == NULL || + strncmp(accept, "text/html", strlen("text/html"))) + return; + + ha_find.id = t->http_accept_id; + ha = RB_FIND(http_accept_list, &ha_list, &ha_find); + if (ha == NULL) { + ha_find.id = 0; + t->http_accept_id = 1; + http_accept = RB_FIND(http_accept_list, &ha_list, + &ha_find); + } else { + ++t->http_accept_id; + http_accept = ha; + } + + soup_message_headers_replace(msg->request_headers, "Accept", + http_accept->value); + } } WebKitWebView * @@ -7254,6 +7299,9 @@ create_new_tab(char *title, struct undo *u, int focus, int position) b = gtk_hbox_new(FALSE, 0); t->tab_content = b; + t->user_agent_id = 0; + t->http_accept_id = 0; + #if GTK_CHECK_VERSION(2, 20, 0) t->spinner = gtk_spinner_new(); #endif @@ -8048,6 +8096,7 @@ main(int argc, char **argv) RB_INIT(&st_tree); RB_INIT(&svl); RB_INIT(&svil); + RB_INIT(&ua_list); TAILQ_INIT(&sessions); TAILQ_INIT(&tabs); @@ -8058,7 +8107,6 @@ main(int argc, char **argv) TAILQ_INIT(&spl); TAILQ_INIT(&chl); TAILQ_INIT(&shl); - TAILQ_INIT(&ua_list); TAILQ_INIT(&cul); #ifndef XT_RESOURCE_LIMITS_DISABLE -- cgit 1.4.1-2-gfad0