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