merge in sm217s cookie code (which is work in progress)
authorDieter Plaetinck <dieter@plaetinck.be>
Wed, 6 May 2009 19:59:48 +0000 (21:59 +0200)
committerDieter Plaetinck <dieter@plaetinck.be>
Wed, 6 May 2009 19:59:48 +0000 (21:59 +0200)
1  2 
.gitignore
uzbl.c
uzbl.h

diff --cc .gitignore
@@@ -1,4 -1,3 +1,5 @@@
  uzbl
  uzblctrl
 -examples/data
 +*~
 +tags
++examples/data
diff --cc uzbl.c
--- 1/uzbl.c
--- 2/uzbl.c
+++ b/uzbl.c
@@@ -73,6 -83,6 +73,7 @@@ static gchar*   history_handler    = NU
  static gchar*   fifo_dir           = NULL;
  static gchar*   socket_dir         = NULL;
  static gchar*   download_handler   = NULL;
++static gchar*   cookie_handler     = NULL;
  static gboolean always_insert_mode = FALSE;
  static gboolean show_status        = FALSE;
  static gboolean insert_mode        = FALSE;
@@@ -410,150 -389,9 +412,150 @@@ close_uzbl (WebKitWebView *page, const 
      gtk_main_quit ();
  }
  
 +/* --Statusbar functions-- */
 +static char*
 +build_progressbar_ascii(int percent) {
 +   int width=10;
 +   int i;
 +   double l;
 +   GString *bar = g_string_new("");
 +
 +   l = (double)percent*((double)width/100.);
 +   l = (int)(l+.5)>=(int)l ? l+.5 : l;
 +
 +   g_string_append(bar, "[");
 +   for(i=0; i<(int)l; i++)
 +       g_string_append(bar, "=");
 +          
 +   for(; i<width; i++)
 +       g_string_append(bar, "ยท");
 +   g_string_append(bar, "]");
 +
 +   return g_string_free(bar, FALSE);
 +}
 +
 +static void
 +setup_scanner() {
 +     const GScannerConfig scan_config = {
 +             (
 +              "\t\r\n"
 +             )            /* cset_skip_characters */,
 +             (
 +              G_CSET_a_2_z
 +              "_"
 +              G_CSET_A_2_Z
 +             )            /* cset_identifier_first */,
 +             (
 +              G_CSET_a_2_z
 +              "_0123456789"
 +              G_CSET_A_2_Z
 +              G_CSET_LATINS
 +              G_CSET_LATINC
 +             )            /* cset_identifier_nth */,
 +             ( "#\n" )    /* cpair_comment_single */,
 +
 +             TRUE         /* case_sensitive */,
 +
 +             FALSE        /* skip_comment_multi */,
 +             FALSE        /* skip_comment_single */,
 +             FALSE        /* scan_comment_multi */,
 +             TRUE         /* scan_identifier */,
 +             TRUE         /* scan_identifier_1char */,
 +             FALSE        /* scan_identifier_NULL */,
 +             TRUE         /* scan_symbols */,
 +             FALSE        /* scan_binary */,
 +             FALSE        /* scan_octal */,
 +             FALSE        /* scan_float */,
 +             FALSE        /* scan_hex */,
 +             FALSE        /* scan_hex_dollar */,
 +             FALSE        /* scan_string_sq */,
 +             FALSE        /* scan_string_dq */,
 +             TRUE         /* numbers_2_int */,
 +             FALSE        /* int_2_float */,
 +             FALSE        /* identifier_2_string */,
 +             FALSE        /* char_2_token */,
 +             FALSE        /* symbol_2_token */,
 +             TRUE         /* scope_0_fallback */,
 +             FALSE,
 +             TRUE
 +     };
 +
 +     uzbl.scan = g_scanner_new(&scan_config);
 +     while(symp->symbol_name) {
 +         g_scanner_scope_add_symbol(uzbl.scan, 0,
 +                         symp->symbol_name,
 +                         GINT_TO_POINTER(symp->symbol_token));
 +         symp++;
 +     }
 +}
 +
 +static gchar *
 +parse_status_template(const char *template) {
 +     GTokenType token = G_TOKEN_NONE;
 +     GString *ret = g_string_new("");
 +     gchar *buf=NULL;
 +     int sym;
 +
 +     if(!template)
 +         return NULL;
 +
 +     g_scanner_input_text(uzbl.scan, template, strlen(template));
 +     while(!g_scanner_eof(uzbl.scan) && token != G_TOKEN_LAST) {
 +         token = g_scanner_get_next_token(uzbl.scan);
 +
 +         if(token == G_TOKEN_SYMBOL) {
 +             sym = (int)g_scanner_cur_value(uzbl.scan).v_symbol;
 +             switch(sym) {
 +                 case SYM_URI:
 +                     g_string_append(ret, uzbl.state.uri);
 +                     break;
 +                 case SYM_LOADPRGS:
 +                     g_string_append(ret, itos(uzbl.gui.sbar.load_progress));
 +                     break;
 +                 case SYM_LOADPRGSBAR:
 +                     buf = build_progressbar_ascii(uzbl.gui.sbar.load_progress);
 +                     g_string_append(ret, buf);
 +                     g_free(buf);
 +                     break;
 +                 case SYM_TITLE:
 +                     g_string_append(ret,
 +                         uzbl.gui.main_title?uzbl.gui.main_title:"");
 +                     break;
 +                 case SYM_NAME:
 +                     g_string_append(ret, 
 +                         uzbl.state.instance_name?uzbl.state.instance_name:"" );
 +                     break;
 +                 case SYM_KEYCMD:
 +                     g_string_append(ret, 
 +                         keycmd->str?keycmd->str:"" );
 +                     break;
 +                 case SYM_MODE:
 +                     g_string_append(ret, 
 +                         insert_mode?"[I]":"[C]" );
 +                     break;
 +                 default:
 +                     break;
 +             }
 +         }
 +         else if(token == G_TOKEN_INT) {
 +             g_string_append(ret, itos(g_scanner_cur_value(uzbl.scan).v_int));
 +         }
 +         else if(token == G_TOKEN_IDENTIFIER) {
 +             g_string_append(ret, (gchar *)g_scanner_cur_value(uzbl.scan).v_identifier);
 +         }
 +         else if(token == G_TOKEN_CHAR) {
 +             g_string_append_printf(ret, "%c", g_scanner_cur_value(uzbl.scan).v_char);
 +         }
 +     }
 +
 +     return g_string_free(ret, FALSE);
 +}
 +/* --End Statusbar functions-- */
 +
 +
  // 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 <uzbl conf> <uzbl pid> <uzbl win id> <uzbl fifo file> <uzbl socket file> [args]
      GString* to_execute = g_string_new ("");
      gboolean result;
      return result;
  }
  
+ static gboolean
+ run_command_sync(const char *command, const char *args, char **stdout) {
+       //command <uzbl conf> <uzbl pid> <uzbl win id> <uzbl fifo file> <uzbl socket file> [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");
++    g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s' '%s'", command, uzbl.state.config_file, (int) getpid() , (int) uzbl.xwin, uzbl.comm.fifo_path, uzbl.comm.socket_path);
++    g_string_append_printf (to_execute, " '%s' '%s'", uzbl.state.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;
@@@ -1030,6 -754,6 +1048,7 @@@ settings_init () 
      if (res) {
          history_handler    = g_key_file_get_value   (config, "behavior", "history_handler",    NULL);
          download_handler   = g_key_file_get_value   (config, "behavior", "download_handler",   NULL);
++      cookie_handler     = g_key_file_get_string  (config, "behavior", "cookie_handler",     NULL);
          always_insert_mode = g_key_file_get_boolean (config, "behavior", "always_insert_mode", NULL);
          show_status        = g_key_file_get_boolean (config, "behavior", "show_status",        NULL);
          modkey             = g_key_file_get_value   (config, "behavior", "modkey",             NULL);
              socket_dir     = g_key_file_get_value   (config, "behavior", "socket_dir",         NULL);
          keys               = g_key_file_get_keys    (config, "bindings", NULL,                 NULL);
      }
 -    
 +
      printf ("History handler: %s\n",    (history_handler    ? history_handler  : "disabled"));
      printf ("Download manager: %s\n",   (download_handler   ? download_handler : "disabled"));
 -    printf ("Fifo directory: %s\n",     (fifo_dir           ? fifo_dir         : "/tmp"));
 -    printf ("Socket directory: %s\n",   (socket_dir         ? socket_dir       : "/tmp"));
++    printf ("Cookie handler: %s\n",     (cookie_handler     ? cookie_handler   : "disabled"));
 +    printf ("Fifo directory: %s\n",     (fifo_dir           ? fifo_dir         : "disabled"));
 +    printf ("Socket directory: %s\n",   (socket_dir         ? socket_dir       : "disabled"));
      printf ("Always insert mode: %s\n", (always_insert_mode ? "TRUE"           : "FALSE"));
      printf ("Show status: %s\n",        (show_status        ? "TRUE"           : "FALSE"));
      printf ("Status top: %s\n",         (status_top         ? "TRUE"           : "FALSE"));
          http_debug = 0;
          fprintf(stderr, "Wrong http_debug level, ignoring.\n");
      } else if (http_debug > 0) {
 -        soup_logger = soup_logger_new(http_debug, -1);
 -        soup_session_add_feature(soup_session, SOUP_SESSION_FEATURE(soup_logger));
 +        n->soup_logger = soup_logger_new(http_debug, -1);
 +        soup_session_add_feature(n->soup_session, SOUP_SESSION_FEATURE(n->soup_logger));
      }
        
 -    if(useragent){
 -        g_object_set(G_OBJECT(soup_session), SOUP_SESSION_USER_AGENT, useragent, NULL);
 +    if(n->useragent){
 +        char* newagent  = malloc(1024);
 +
 +        strcpy(newagent, str_replace("%webkit-major%", itos(WEBKIT_MAJOR_VERSION), n->useragent));
 +        strcpy(newagent, str_replace("%webkit-minor%", itos(WEBKIT_MINOR_VERSION), newagent));
 +        strcpy(newagent, str_replace("%webkit-micro%", itos(WEBKIT_MICRO_VERSION), newagent));
 +
 +        if (uname (&unameinfo) == -1) {
 +            printf("Error getting uname info. Not replacing system-related user agent variables.\n");
 +        } else {
 +            strcpy(newagent, str_replace("%sysname%",     unameinfo.sysname, newagent));
 +            strcpy(newagent, str_replace("%nodename%",    unameinfo.nodename, newagent));
 +            strcpy(newagent, str_replace("%kernrel%",     unameinfo.release, newagent));
 +            strcpy(newagent, str_replace("%kernver%",     unameinfo.version, newagent));
 +            strcpy(newagent, str_replace("%arch-system%", unameinfo.machine, newagent));
 +
 +            #ifdef _GNU_SOURCE
 +                strcpy(newagent, str_replace("%domainname%", unameinfo.domainname, newagent));
 +            #endif
 +        }
 +
 +        strcpy(newagent, str_replace("%arch-uzbl%",    ARCH,                       newagent));
 +        strcpy(newagent, str_replace("%commit%",       COMMIT,                     newagent));
 +
 +        n->useragent = malloc(1024);
 +        strcpy(n->useragent, newagent);
 +        g_object_set(G_OBJECT(n->soup_session), SOUP_SESSION_USER_AGENT, n->useragent, NULL);
      }
  
 -    if(max_conns >= 1){
 -        g_object_set(G_OBJECT(soup_session), SOUP_SESSION_MAX_CONNS, max_conns, NULL);
 +    if(n->max_conns >= 1){
 +        g_object_set(G_OBJECT(n->soup_session), SOUP_SESSION_MAX_CONNS, n->max_conns, NULL);
      }
  
 -    if(max_conns_host >= 1){
 -        g_object_set(G_OBJECT(soup_session), SOUP_SESSION_MAX_CONNS_PER_HOST, max_conns_host, NULL);
 +    if(n->max_conns_host >= 1){
 +        g_object_set(G_OBJECT(n->soup_session), SOUP_SESSION_MAX_CONNS_PER_HOST, n->max_conns_host, NULL);
      }
  
 -    printf("Proxy configured: %s\n", proxy_url ? proxy_url : "none");
 +    printf("Proxy configured: %s\n", n->proxy_url ? n->proxy_url : "none");
      printf("HTTP logging level: %d\n", http_debug);
 -    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);
 +    printf("User-agent: %s\n", n->useragent? n->useragent : "default");
 +    printf("Maximum connections: %d\n", n->max_conns ? n->max_conns : 0);
 +    printf("Maximum connections per host: %d\n", n->max_conns_host ? n->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-queued", G_CALLBACK(handle_cookies), NULL);
 -              printf("Cookie handler: %s\n", cookie_handler);
++              g_signal_connect(n->soup_session, "request-queued", G_CALLBACK(handle_cookies), NULL);
+       }
+       
+ }
 -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), NULL);
++static void handle_cookies (SoupSession *session, SoupMessage *msg, gpointer user_data){
++    (void) session;
++    (void) user_data;
++    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
+       */
+ }
+ static void
 -save_cookies (SoupMessage *msg,
 -                        gpointer     user_data){
 -      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);
++save_cookies (SoupMessage *msg, gpointer user_data){
++    (void) user_data;
++    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 --cc uzbl.h
--- 1/uzbl.h
--- 2/uzbl.h
+++ b/uzbl.h
@@@ -187,13 -106,22 +190,19 @@@ add_binding (const gchar *key, const gc
  static void
  settings_init ();
  
 -/* 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); *\/ */
 +static void
 +search_text (WebKitWebView *page, const char *param);
 +
 +static void
 +run_js (WebKitWebView * web_view, const gchar *param);
 +
 +static char *
 +str_replace (const char* search, const char* replace, const char* string);
  
+ 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: */