From df811feb3aa3d7f6dbf41008659096490c208666 Mon Sep 17 00:00:00 2001 From: Evgeny Grablyk Date: Mon, 4 May 2009 20:32:28 +0300 Subject: Some cookies. --- examples/scripts/cookies.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 examples/scripts/cookies.sh diff --git a/examples/scripts/cookies.sh b/examples/scripts/cookies.sh new file mode 100755 index 0000000..bbeed2e --- /dev/null +++ b/examples/scripts/cookies.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +echo $8 $9 >> ck \ No newline at end of file -- cgit v1.2.3 From 63a46f212c21697fbf8a73a80094d1db664a4b49 Mon Sep 17 00:00:00 2001 From: Evgeny Grablyk Date: Mon, 4 May 2009 22:07:02 +0300 Subject: Something's working --- Makefile | 2 +- examples/configs/sampleconfig-dev | 1 + uzbl.c | 100 ++++++++++++++++++++++++++++++++++---- uzbl.h | 15 +++++- 4 files changed, 107 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 52d96b6..35293f9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CPPFLAGS=$(shell pkg-config --cflags gtk+-2.0 webkit-1.0) -Wall -W +CPPFLAGS=$(shell pkg-config --cflags gtk+-2.0 webkit-1.0) -Wall -W -std=c99 -pedantic LDFLAGS=$(shell pkg-config --libs gtk+-2.0 webkit-1.0) all: uzbl uzblctrl diff --git a/examples/configs/sampleconfig-dev b/examples/configs/sampleconfig-dev index a1ba48d..aeb211b 100644 --- a/examples/configs/sampleconfig-dev +++ b/examples/configs/sampleconfig-dev @@ -19,6 +19,7 @@ always_insert_mode = 0 modkey = Mod1 show_status = 1 status_top = 0 +cookie_handler = ./examples/scripts/cookies.sh [bindings] ## scroll down/up/left/right diff --git a/uzbl.c b/uzbl.c index 74c9027..f3c1478 100644 --- a/uzbl.c +++ b/uzbl.c @@ -1,4 +1,4 @@ -/* -*- c-basic-offset: 4; */ +/* -*- c-basic-offset: 4; -*- */ // Original code taken from the example webkit-gtk+ application. see notice below. // Modified code is licensed under the GPL 3. See LICENSE file. @@ -90,7 +90,7 @@ static gboolean status_top = FALSE; static gchar* modkey = NULL; static guint modmask = 0; static guint http_debug = 0; - +static gchar* cookie_handler = NULL; /* settings from config: group bindings, key -> action */ static GHashTable* bindings; @@ -119,6 +119,8 @@ static char *proxy_url = NULL; static char *useragent = NULL; static gint max_conns; static gint max_conns_host; +static SoupCookieJar *ck; +static char* current_req = NULL; /* --- CALLBACKS --- */ @@ -156,7 +158,7 @@ download_cb (WebKitWebView *web_view, GObject *download, gpointer user_data) { if (download_handler) { const gchar* uri = webkit_download_get_uri ((WebKitDownload*)download); printf("Download -> %s\n",uri); - run_command(download_handler, uri); + run_command_async(download_handler, uri); } return (FALSE); } @@ -260,7 +262,7 @@ log_history_cb () { strftime (date, 80, "%Y-%m-%d %H:%M:%S", timeinfo); GString* args = g_string_new (""); g_string_printf (args, "'%s'", date); - run_command(history_handler, args->str); + run_command_async(history_handler, args->str); g_string_free (args, TRUE); } } @@ -355,7 +357,8 @@ load_uri (WebKitWebView * web_view, const gchar *param) { if (param) { GString* newuri = g_string_new (param); if (g_strrstr (param, "://") == NULL) - g_string_prepend (newuri, "http://"); + g_string_prepend (newuri, "http://"); + /* if we do handle cookies, ask our handler for them */ webkit_web_view_load_uri (web_view, newuri->str); g_string_free (newuri, TRUE); } @@ -388,7 +391,7 @@ close_uzbl (WebKitWebView *page, const char *param) { // make sure to put '' around args, so that if there is whitespace we can still keep arguments together. static gboolean -run_command(const char *command, const char *args) { +run_command_async(const char *command, const char *args) { //command [args] GString* to_execute = g_string_new (""); gboolean result; @@ -403,10 +406,26 @@ run_command(const char *command, const char *args) { return result; } +static gboolean +run_command_sync(const char *command, const char *args, char **stdout) { + //command [args] + GString* to_execute = g_string_new (""); + gboolean result; + g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s' '%s'", command, config_file, (int) getpid() , (int) xwin, fifo_path, socket_path); + g_string_append_printf (to_execute, " '%s' '%s'", uri, "TODO title here"); + if(args) { + g_string_append_printf (to_execute, " %s", args); + } + result = g_spawn_command_line_sync (to_execute->str, stdout, NULL, NULL, NULL); + printf("Called %s. Result: %s\n", to_execute->str, (result ? "TRUE" : "FALSE" )); + g_string_free (to_execute, TRUE); + return result; +} + static void spawn(WebKitWebView *web_view, const char *param) { (void)web_view; - run_command(param, NULL); + run_command_async(param, NULL); } static void @@ -826,7 +845,70 @@ settings_init () { printf("User-agent: %s\n", useragent? useragent : "default"); printf("Maximum connections: %d\n", max_conns ? max_conns : 0); printf("Maximum connections per host: %d\n", max_conns_host ? max_conns_host: 0); - + + /* om nom nom nom */ + cookie_handler = g_key_file_get_string(config, "behavior", "cookie_handler", NULL); + + if(cookie_handler){ + ck = soup_cookie_jar_new(); + soup_session_add_feature(soup_session, SOUP_SESSION_FEATURE(ck)); + g_signal_connect(ck, "changed", G_CALLBACK(cookie_recieved_action), NULL); + g_signal_connect(soup_session, "request-started", G_CALLBACK(ask_for_cookie), NULL); + printf("Cookie handler: %s\n", cookie_handler); + } + +} + +static void +ask_for_cookie (SoupSession *session, + SoupMessage *msg, + SoupSocket *socket, + gpointer user_data) +{ + char *cookie = NULL, *handler_req = NULL; + SoupURI *uri; + + uri = soup_message_get_uri(msg); + + if(current_req != NULL) + free(current_req); + current_req = soup_uri_to_string(uri, false); + + handler_req = malloc(strlen(current_req) + 7); + /* GET request_addr */ + sprintf(handler_req, "GET %s\n", current_req); + /* ask if there's a cookie for this url, if there is, we'll get header in *cookie */ + run_command_sync(cookie_handler, handler_req, &cookie); + /* if we got one, add it so it gets sent */ + if(cookie != NULL){ + soup_cookie_jar_add_cookie(ck, soup_cookie_parse(cookie, uri)); + /* free it from the box and eat it */ + free(cookie); + } + free(handler_req); +} + +static void +cookie_recieved_action (SoupCookieJar *jar, + SoupCookie *old_cookie, + SoupCookie *new_cookie, + gpointer user_data) +{ + char *ck, *rbuf; + if(new_cookie != NULL && old_cookie == NULL){ + ck = soup_cookie_to_cookie_header(new_cookie); + if(strcmp(ck, "=") == 0){ + free(ck); + return; + } + rbuf = malloc(strlen(ck) /*+ strlen(current_req)*/ + 7); + /* PUT request_addr cookie_header */ + sprintf(rbuf, "PUT %s\n", ck); + run_command_async(cookie_handler, rbuf); + free(rbuf); + free(ck); + soup_cookie_jar_delete_cookie(jar, new_cookie); + } } int @@ -854,7 +936,7 @@ main (int argc, char* argv[]) { settings_init (); commands_hash (); - + if (always_insert_mode) insert_mode = TRUE; diff --git a/uzbl.h b/uzbl.h index 7cd35c8..fafd92f 100644 --- a/uzbl.h +++ b/uzbl.h @@ -59,7 +59,10 @@ static void close_uzbl (WebKitWebView *page, const char *param); static gboolean -run_command(const char *command, const char *args); +run_command_async(const char *command, const char *args); + +static gboolean +run_command_sync(const char *command, const char *args, char **stdout); static void spawn(WebKitWebView *web_view, const char *param); @@ -103,4 +106,14 @@ add_binding (const gchar *key, const gchar *act); static void settings_init (); +static void +cookie_recieved_action (SoupCookieJar *jar, + SoupCookie *old_cookie, + SoupCookie *new_cookie, + gpointer user_data); +static void +ask_for_cookie (SoupSession *session, + SoupMessage *msg, + SoupSocket *socket, + gpointer user_data); /* vi: set et ts=4: */ -- cgit v1.2.3 From cc3a1419a12c01c2cabd87da99cc4464f715d196 Mon Sep 17 00:00:00 2001 From: Evgeny Grablyk Date: Tue, 5 May 2009 21:37:48 +0300 Subject: Run away from a terribly wrong approach. --- examples/scripts/cookies.sh | 61 ++++++++++++++++++++++++++++++++++++++++- uzbl.c | 67 +++++++++++---------------------------------- uzbl.h | 21 +++++++------- 3 files changed, 87 insertions(+), 62 deletions(-) diff --git a/examples/scripts/cookies.sh b/examples/scripts/cookies.sh index bbeed2e..69b786f 100755 --- a/examples/scripts/cookies.sh +++ b/examples/scripts/cookies.sh @@ -1,3 +1,62 @@ #!/bin/bash +# this is an example script of how you could manage your cookies.. +# you probably want your cookies config file in your $XDG_CONFIG_HOME ( eg $HOME/.config/uzbl/cookies) -echo $8 $9 >> ck \ No newline at end of file +# MAYBE TODO: allow user to edit cookie before saving. this cannot be done with zenity :( +# TODO: different cookie paths per config (eg per group of uzbl instances) + +if [ -f /usr/share/uzbl/examples/configs/cookies ] +then + file=/usr/share/uzbl/examples/configs/cookies +else + file=./examples/configs/cookies #useful when developing +fi + +if [ -d $XDG_DATA_HOME/uzbl/cookies ] +then + cookie_dir=$XDG_DATA_HOME/uzbl/cookies +else + cookie_dir=./examples/data +fi + +which zenity &>/dev/null || exit 2 + +uri=$6 +action=$8 # GET/PUT +host=${uri/\/*/} + + + +# $1 = section (TRUSTED or DENY) +# $2 =url +function match () { + sed -n "/$1/,/^\$/p" $file 2>/dev/null | grep -q "^$host" +} + +function readcookie () { + cookie= + while read + do + cookie="$REPLY +" + done +} + +function fetch_cookie () { + cookie=`cat $cookie_dir/$host.cookie` +} + +function store_cookie () { + echo $cookie > $cookie_dir/$host.cookie +} + +if match TRUSTED $host +then + [ $action == PUT ] && readcookie && store_cookie $host + [ $action == GET ] && fetch_cookie && echo "$cookie" +elif ! match DENY $host +then + [ $action == PUT ] && readcookie && zenity --question --title 'Uzbl Cookie handler' --text "Accept cookie from $host ? Contents:\n$cookie" && store_cookie $host + [ $action == GET ] && fetch_cookie && zenity --question --title 'Uzbl Cookie handler' --text "Submit cookie to $host ? Contents:\n$cookie" && echo $cookie +fi +exit 0 diff --git a/uzbl.c b/uzbl.c index f3c1478..99098f2 100644 --- a/uzbl.c +++ b/uzbl.c @@ -850,65 +850,30 @@ settings_init () { cookie_handler = g_key_file_get_string(config, "behavior", "cookie_handler", NULL); if(cookie_handler){ - ck = soup_cookie_jar_new(); - soup_session_add_feature(soup_session, SOUP_SESSION_FEATURE(ck)); - g_signal_connect(ck, "changed", G_CALLBACK(cookie_recieved_action), NULL); - g_signal_connect(soup_session, "request-started", G_CALLBACK(ask_for_cookie), NULL); + /* ck = soup_cookie_jar_new(); */ + /* soup_session_add_feature(soup_session, SOUP_SESSION_FEATURE(ck)); */ + /* g_signal_connect(ck, "changed", G_CALLBACK(cookie_recieved_action), NULL); */ + g_signal_connect(soup_session, "request-queued", G_CALLBACK(handle_cookies), NULL); printf("Cookie handler: %s\n", cookie_handler); } } -static void -ask_for_cookie (SoupSession *session, - SoupMessage *msg, - SoupSocket *socket, - gpointer user_data) -{ - char *cookie = NULL, *handler_req = NULL; - SoupURI *uri; - - uri = soup_message_get_uri(msg); - - if(current_req != NULL) - free(current_req); - current_req = soup_uri_to_string(uri, false); - - handler_req = malloc(strlen(current_req) + 7); - /* GET request_addr */ - sprintf(handler_req, "GET %s\n", current_req); - /* ask if there's a cookie for this url, if there is, we'll get header in *cookie */ - run_command_sync(cookie_handler, handler_req, &cookie); - /* if we got one, add it so it gets sent */ - if(cookie != NULL){ - soup_cookie_jar_add_cookie(ck, soup_cookie_parse(cookie, uri)); - /* free it from the box and eat it */ - free(cookie); - } - free(handler_req); +static void handle_cookies (SoupSession *session, + SoupMessage *msg, + gpointer user_data){ + soup_message_add_header_handler(msg, "got-headers", "Set-Cookie", G_CALLBACK(save_cookies)); + /* ask handler for cookies, if there are any, use + soup_message_headers_replace (msg->request_headers, + "Cookie", cookies); + to add them + */ } static void -cookie_recieved_action (SoupCookieJar *jar, - SoupCookie *old_cookie, - SoupCookie *new_cookie, - gpointer user_data) -{ - char *ck, *rbuf; - if(new_cookie != NULL && old_cookie == NULL){ - ck = soup_cookie_to_cookie_header(new_cookie); - if(strcmp(ck, "=") == 0){ - free(ck); - return; - } - rbuf = malloc(strlen(ck) /*+ strlen(current_req)*/ + 7); - /* PUT request_addr cookie_header */ - sprintf(rbuf, "PUT %s\n", ck); - run_command_async(cookie_handler, rbuf); - free(rbuf); - free(ck); - soup_cookie_jar_delete_cookie(jar, new_cookie); - } +save_cookies (SoupMessage *msg, + gpointer user_data){ + /* give them to handler */ } int diff --git a/uzbl.h b/uzbl.h index fafd92f..379aaa9 100644 --- a/uzbl.h +++ b/uzbl.h @@ -106,14 +106,15 @@ add_binding (const gchar *key, const gchar *act); static void settings_init (); -static void -cookie_recieved_action (SoupCookieJar *jar, - SoupCookie *old_cookie, - SoupCookie *new_cookie, - gpointer user_data); -static void -ask_for_cookie (SoupSession *session, - SoupMessage *msg, - SoupSocket *socket, - gpointer user_data); +/* static void */ +/* cookie_recieved_action (SoupCookieJar *jar, */ + /* SoupCookie *old_cookie, */ + /* SoupCookie *new_cookie, */ + /* gpointer user_data); */ +static void +catch_cookies (SoupSession *session, + SoupMessage *msg, + gpointer user_data); + /* SoupSocket *socket, */ + /* gpointer user_data); */ /* vi: set et ts=4: */ -- cgit v1.2.3 From 12b0591ed2b5c65574eda699cbf5396bf89e8940 Mon Sep 17 00:00:00 2001 From: Evgeny Grablyk Date: Tue, 5 May 2009 23:16:43 +0300 Subject: Implement saving cookies --- .gitignore | 3 ++- Makefile | 2 +- examples/scripts/cookies.sh | 62 +-------------------------------------------- uzbl.c | 15 +++++++++-- uzbl.h | 19 +++++++++----- 5 files changed, 30 insertions(+), 71 deletions(-) diff --git a/.gitignore b/.gitignore index 471b6f4..8a964fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ uzbl -uzblctrl \ No newline at end of file +uzblctrl +examples/data \ No newline at end of file diff --git a/Makefile b/Makefile index 35293f9..52d96b6 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CPPFLAGS=$(shell pkg-config --cflags gtk+-2.0 webkit-1.0) -Wall -W -std=c99 -pedantic +CPPFLAGS=$(shell pkg-config --cflags gtk+-2.0 webkit-1.0) -Wall -W LDFLAGS=$(shell pkg-config --libs gtk+-2.0 webkit-1.0) all: uzbl uzblctrl diff --git a/examples/scripts/cookies.sh b/examples/scripts/cookies.sh index 69b786f..d147930 100755 --- a/examples/scripts/cookies.sh +++ b/examples/scripts/cookies.sh @@ -1,62 +1,2 @@ #!/bin/bash -# this is an example script of how you could manage your cookies.. -# you probably want your cookies config file in your $XDG_CONFIG_HOME ( eg $HOME/.config/uzbl/cookies) - -# MAYBE TODO: allow user to edit cookie before saving. this cannot be done with zenity :( -# TODO: different cookie paths per config (eg per group of uzbl instances) - -if [ -f /usr/share/uzbl/examples/configs/cookies ] -then - file=/usr/share/uzbl/examples/configs/cookies -else - file=./examples/configs/cookies #useful when developing -fi - -if [ -d $XDG_DATA_HOME/uzbl/cookies ] -then - cookie_dir=$XDG_DATA_HOME/uzbl/cookies -else - cookie_dir=./examples/data -fi - -which zenity &>/dev/null || exit 2 - -uri=$6 -action=$8 # GET/PUT -host=${uri/\/*/} - - - -# $1 = section (TRUSTED or DENY) -# $2 =url -function match () { - sed -n "/$1/,/^\$/p" $file 2>/dev/null | grep -q "^$host" -} - -function readcookie () { - cookie= - while read - do - cookie="$REPLY -" - done -} - -function fetch_cookie () { - cookie=`cat $cookie_dir/$host.cookie` -} - -function store_cookie () { - echo $cookie > $cookie_dir/$host.cookie -} - -if match TRUSTED $host -then - [ $action == PUT ] && readcookie && store_cookie $host - [ $action == GET ] && fetch_cookie && echo "$cookie" -elif ! match DENY $host -then - [ $action == PUT ] && readcookie && zenity --question --title 'Uzbl Cookie handler' --text "Accept cookie from $host ? Contents:\n$cookie" && store_cookie $host - [ $action == GET ] && fetch_cookie && zenity --question --title 'Uzbl Cookie handler' --text "Submit cookie to $host ? Contents:\n$cookie" && echo $cookie -fi -exit 0 +echo $8 - $9 diff --git a/uzbl.c b/uzbl.c index 99098f2..161769b 100644 --- a/uzbl.c +++ b/uzbl.c @@ -862,7 +862,8 @@ settings_init () { static void handle_cookies (SoupSession *session, SoupMessage *msg, gpointer user_data){ - soup_message_add_header_handler(msg, "got-headers", "Set-Cookie", G_CALLBACK(save_cookies)); + soup_message_add_header_handler(msg, "got-headers", "Set-Cookie", G_CALLBACK(save_cookies), NULL); + /* ask handler for cookies, if there are any, use soup_message_headers_replace (msg->request_headers, "Cookie", cookies); @@ -873,7 +874,17 @@ static void handle_cookies (SoupSession *session, static void save_cookies (SoupMessage *msg, gpointer user_data){ - /* give them to handler */ + GSList *ck; + char *req, *cookie; + for (ck = soup_cookies_from_response(msg); ck; ck = ck->next){ + cookie = soup_cookie_to_set_cookie_header(ck->data); + req = malloc(strlen(cookie) + 10); + sprintf(req, "PUT \"%s\"", cookie); + run_command_async(cookie_handler, req); + free(req); + free(cookie); + } + g_slist_free(ck); } int diff --git a/uzbl.h b/uzbl.h index 379aaa9..e881fbe 100644 --- a/uzbl.h +++ b/uzbl.h @@ -111,10 +111,17 @@ settings_init (); /* SoupCookie *old_cookie, */ /* SoupCookie *new_cookie, */ /* gpointer user_data); */ -static void -catch_cookies (SoupSession *session, - SoupMessage *msg, - gpointer user_data); - /* SoupSocket *socket, */ - /* gpointer user_data); */ +/* static void */ +/* catch_cookies (SoupSession *session, */ +/* SoupMessage *msg, */ +/* gpointer user_data); */ +/* /\* SoupSocket *socket, *\/ */ +/* /\* gpointer user_data); *\/ */ + +static void handle_cookies (SoupSession *session, + SoupMessage *msg, + gpointer user_data); +static void +save_cookies (SoupMessage *msg, + gpointer user_data); /* vi: set et ts=4: */ -- cgit v1.2.3