X-Git-Url: https://git.danieliu.xyz/?p=surf.git;a=blobdiff_plain;f=surf.c;fp=surf.c;h=2778beeb21ed36835b996b63e017822344a3dd1f;hp=fb277cc7dbe97eee40da52f2ec53b13a3d503ab2;hb=80d33d696d73c14c81da6e35ab06ee97517c9852;hpb=c20442a13864c870d7e5e24c31299284c6c45f97 diff --git a/surf.c b/surf.c index fb277cc..2778bee 100644 --- a/surf.c +++ b/surf.c @@ -38,6 +38,14 @@ typedef struct Client { struct Client *next; } Client; +typedef struct Cookie { + char *name; + char *value; + char *domain; + char *path; + struct Cookie *next; +} Cookie; + typedef struct { guint mod; guint keyval; @@ -59,7 +67,9 @@ typedef struct { } KeySet; SoupCookieJar *cookiejar; +SoupSession *session; Client *clients = NULL; +Cookie *cookies = NULL; gboolean embed = FALSE; gboolean showxid = FALSE; gboolean ignore_once = FALSE; @@ -67,6 +77,7 @@ extern char *optarg; extern int optind; static void cleanup(void); +static void proccookies(SoupMessage *m, Client *c); static void clipboard(Client *c, const Arg *arg); static void destroyclient(Client *c); static void destroywin(GtkWidget* w, Client *c); @@ -88,8 +99,12 @@ static WebKitWebView *newwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d); static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d); static void progresschange(WebKitWebView *view, gint p, Client *c); +static void request(SoupSession *s, SoupMessage *m, Client *c); +static void setcookie(char *name, char *val, char *dom, char *path, long exp); static void reload(Client *c, const Arg *arg); static void setup(void); +static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, + const gchar* title, Client *c); static void searchtext(Client *c, const Arg *arg); static void showsearch(Client *c, const Arg *arg); static void showurl(Client *c, const Arg *arg); @@ -107,6 +122,20 @@ cleanup(void) { destroyclient(clients); } +void +proccookies(SoupMessage *m, Client *c) { + GSList *l; + SoupCookie *co; + long t; + + for (l = soup_cookies_from_response(m); l; l = l->next){ + co = (SoupCookie *)l->data; + t = co->expires ? soup_date_to_time_t(co->expires) : 0; + setcookie(co->name, co->value, co->domain, co->value, t); + } + g_slist_free(l); +} + void clipboard(Client *c, const Arg *arg) { gboolean paste = *(gboolean *)arg; @@ -228,7 +257,6 @@ keypress(GtkWidget* w, GdkEventKey *ev, Client *c) { fprintf(stderr, "keypress(): Unknown Keypressmode\n"); break; } - if(n < LENGTH(keysets)) { matchkeys: for(m = 0; m < keysets[n].numkeys; m++) { @@ -309,7 +337,7 @@ loaduri(Client *c, const Arg *arg) { const gchar *uri = (gchar *)arg->v; if(!uri) uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar)); - u = g_strrstr(uri, ":") ? g_strdup(uri) + u = g_strrstr(uri, "://") ? g_strdup(uri) : g_strdup_printf("http://%s", uri); webkit_web_view_load_uri(c->view, u); c->progress = 0; @@ -360,6 +388,7 @@ newclient(void) { g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c); g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(newwindow), c); g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c); + g_signal_connect_after(session, "request-started", G_CALLBACK(request), c); /* urlbar */ c->urlbar = gtk_entry_new(); @@ -424,6 +453,7 @@ processx(GdkXEvent *e, GdkEvent *event, gpointer d) { unsigned long ldummy; unsigned char *buf = NULL; Arg arg; + if(((XEvent *)e)->type == PropertyNotify) { ev = &((XEvent *)e)->xproperty; if(ev->atom == urlprop && ev->state == PropertyNewValue) { @@ -448,6 +478,12 @@ progresschange(WebKitWebView* view, gint p, Client *c) { updatetitle(c, NULL); } +void +request(SoupSession *s, SoupMessage *m, Client *c) { + soup_message_add_header_handler(m, "got-headers", "Set-Cookie", + G_CALLBACK(proccookies), c); +} + void reload(Client *c, const Arg *arg) { gboolean nocache = *(gboolean *)arg; @@ -457,8 +493,15 @@ reload(Client *c, const Arg *arg) { webkit_web_view_reload(c->view); } -void setup(void) { +void +setcookie(char *name, char *val, char *dom, char *path, long exp) { + printf("%s %s %s %s %li\n", name, val, dom, path, exp); +} + +void +setup() { dpy = GDK_DISPLAY(); + session = webkit_get_default_session(); urlprop = XInternAtom(dpy, "_SURF_URL", False); } @@ -507,7 +550,7 @@ titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) { void usage() { fputs("surf - simple browser\n", stderr); - die("usage: surf [-e] [-x] [-u uri] [-f file]\n"); + die("usage: surf [-e] [-x] [uri]\n"); } void @@ -558,22 +601,22 @@ int main(int argc, char *argv[]) { showxid = TRUE; embed = TRUE; break; - case 'u': - c = newclient(); - arg.v = optarg; - loaduri(c, &arg); - break; - case 'f': - c = newclient(); - loadfile(c, optarg); - break; case 'v': die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n"); break; default: usage(); } - if(optind != argc) + if(optind + 1 == argc) { + c = newclient(); + arg.v = argv[optind]; + if(strchr("./", argv[optind][0]) || strcmp("-", argv[optind]) == 0) + loadfile(c, argv[optind]); + else + loaduri(c, &arg); + + } + else if(optind != argc) usage(); if(!clients) newclient();