cookie support: correct invocations of script, various fixes. almost a basic working...
authorDieter Plaetinck <dieter@plaetinck.be>
Thu, 7 May 2009 20:23:55 +0000 (22:23 +0200)
committerDieter Plaetinck <dieter@plaetinck.be>
Thu, 7 May 2009 20:23:55 +0000 (22:23 +0200)
README
examples/scripts/cookies.sh
uzbl.c

diff --git a/README b/README
index 8a2c48f..a16638d 100644 (file)
--- a/README
+++ b/README
@@ -118,7 +118,9 @@ The script specific arguments are this:
   $8 url of item to download
 * cookie handler
   $8 GET/PUT
-  $9 cookie (only with PUT requests)
+  $9 request address host (if current page url is www.foo.com/somepage, this could be something else then foo, eg advertising from another host)
+  $10 request address path
+  $11 cookie (only with PUT requests)
 
 KNOWN BUGS
 - Segfaults when using zoom commands (happens when max zoom already reached?).
index 02fb471..9f6d202 100755 (executable)
@@ -1,5 +1,9 @@
 #!/bin/bash
 # this is an example script of how you could manage your cookies..
+# we use the cookies.txt format (See http://kb.mozillazine.org/Cookies.txt)
+# This is one textfile with entries like this:
+# kb.mozillazine.org   FALSE   /       FALSE   1146030396      wikiUserID      16993
+# domain alow-read-other-subdomains path http-required expiration name value  
 # 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 :(
@@ -23,23 +27,68 @@ 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
+#cookie_file=$XDG_DATA_HOME/uzbl/cookies.txt
+cookie_file=./examples/data/cookies.txt
 
 which zenity &>/dev/null || exit 2
 
-uri=$6
-uri=${uri/http:\/\/} # strip 'http://' part
+# Example cookie:
+# test_cookie=CheckForPermission; expires=Thu, 07-May-2009 19:17:55 GMT; path=/; domain=.doubleclick.net
+
+# uri=$6
+# uri=${uri/http:\/\/} # strip 'http://' part
+# host=${uri/\/*/}
 action=$8 # GET/PUT
+host=$9
+shift
+path=$9
+shift
 cookie=$9
-host=${uri/\/*/}
 
+field_domain=$host
+field_path=$path
+field_name=
+field_value=
+field_exp='end_session'
+
+
+# FOR NOW LETS KEEP IT SIMPLE AND JUST ALWAYS PUT AND ALWAYS GET
+function parse_cookie () {
+       IFS=$';'
+       first_pair=1
+       for pair in $cookie
+       do
+               if [ "$first_pair" == 1 ]
+               then
+                       field_name=${i%%=*}
+                       field_value=${i#*=}
+                       first_pair=0
+               else
+                       read -r pair <<< "$pair" #strip leading/trailing wite space
+                       key=${i%%=*}
+                       val=${i#*=}
+                       [ "$key" == expires ] && field_exp=`date -u -d "$val" +'%s'`
+                       # TODO: domain
+                       [ "$key" == path ] && field_path=$val
+               fi
+       done
+       unset IFS
+}
+
+# match cookies in cookies.txt againsh hostname and path
+function get_cookie () {
+       path_esc=${path//\//\\/}
+       cookie=`awk "/^[^\t]*$host\t[^\t]*\t$path_esc/" cookie_file 2>/dev/null | tail -n 1`
+       [ -n "$cookie" ]
+}
+
+[ $action == PUT ] && parse_cookie && echo -e "$field_domain\tFALSE\t$field_path\tFALSE\t$field_exp\t$field_name\t$field_value" >> $cookie_file
+[ $action == GET ] && get_cookie && echo "$cookie"
+
+exit
 
 
+# TODO: implement this later.
 # $1 = section (TRUSTED or DENY)
 # $2 =url
 function match () {
@@ -47,11 +96,11 @@ function match () {
 }
 
 function fetch_cookie () {
-       cookie=`cat $cookie_dir/$host.cookie`
+       cookie=`cat $cookie_file/$host.cookie`
 }
 
 function store_cookie () {
-       echo $cookie > $cookie_dir/$host.cookie
+       echo $cookie > $cookie_file/$host.cookie
 }
 
 if match TRUSTED $host
diff --git a/uzbl.c b/uzbl.c
index 66498f6..db23821 100644 (file)
--- a/uzbl.c
+++ b/uzbl.c
@@ -1058,17 +1058,17 @@ settings_init () {
     if (res) {
         b->history_handler    = g_key_file_get_value   (config, "behavior", "history_handler",    NULL);
         b->download_handler   = g_key_file_get_value   (config, "behavior", "download_handler",   NULL);
-           b->cookie_handler     = g_key_file_get_string  (config, "behavior", "cookie_handler",     NULL);
+        b->cookie_handler     = g_key_file_get_string  (config, "behavior", "cookie_handler",     NULL);
         b->always_insert_mode = g_key_file_get_boolean (config, "behavior", "always_insert_mode", NULL);
         b->show_status        = g_key_file_get_boolean (config, "behavior", "show_status",        NULL);
         b->modkey             = g_key_file_get_value   (config, "behavior", "modkey",             NULL);
         b->status_top         = g_key_file_get_boolean (config, "behavior", "status_top",         NULL);
-        b->status_format       = g_key_file_get_string (config, "behavior", "status_format",         NULL);
+        b->status_format      = g_key_file_get_string (config, "behavior", "status_format",         NULL);
         if (! b->fifo_dir)
             b->fifo_dir       = g_key_file_get_value  (config, "behavior", "fifo_dir",           NULL);
         if (! b->socket_dir)
             b->socket_dir     = g_key_file_get_value   (config, "behavior", "socket_dir",         NULL);
-        keys               = g_key_file_get_keys    (config, "bindings", NULL,                 NULL);
+        keys                  = g_key_file_get_keys    (config, "bindings", NULL,                 NULL);
     }
 
     printf ("History handler: %s\n",    (b->history_handler    ? b->history_handler  : "disabled"));
@@ -1195,26 +1195,30 @@ settings_init () {
 static void handle_cookies (SoupSession *session, SoupMessage *msg, gpointer user_data){
     (void) session;
     (void) user_data;
+    gchar * stdout = NULL;
     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);
-          to add them
-       */
+    GString* args = g_string_new ("");
+    SoupURI * soup_uri = soup_message_get_uri(msg);
+    g_string_printf (args, "GET %s %s", soup_uri->host, soup_uri->path);
+    run_command_sync(uzbl.behave.cookie_handler, args->str, &stdout);
+    if(stdout) {
+        soup_message_headers_replace (msg->request_headers, "Cookie", stdout);
+    }
+    g_string_free(args, TRUE);
 }
 
 static void
 save_cookies (SoupMessage *msg, gpointer user_data){
     (void) user_data;
     GSList *ck;
-    char *req, *cookie;
+    char *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(uzbl.behave.cookie_handler, req);
-        free(req);
+        GString* args = g_string_new ("");
+        SoupURI * soup_uri = soup_message_get_uri(msg);
+        g_string_printf (args, "PUT %s %s \"%s\"", soup_uri->host, soup_uri->path, cookie);
+        run_command_async(uzbl.behave.cookie_handler, args->str);
+        g_string_free(args, TRUE);
         free(cookie);
     }
     g_slist_free(ck);