summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2003-12-28 02:35:46 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2003-12-28 02:35:46 +0000
commitb0b2ec850f74d3dd8bd2fffa76fbd2af20b42705 (patch)
tree5edf5a114b68394c737318f1b04e6dfd4789fef6
parent7e7b70e1713590f39b0d3891bcdef6584901b2ab (diff)
downloadnetsurf-b0b2ec850f74d3dd8bd2fffa76fbd2af20b42705.tar.gz
netsurf-b0b2ec850f74d3dd8bd2fffa76fbd2af20b42705.tar.bz2
[project @ 2003-12-28 02:35:46 by jmb]
Remove expired cookies from the cookiejar. Cookies are removed every time a window is closed and when NetSurf is quit. /me slaps libcurl for not doing it itself. Make cookie_create() read the cookie jar location from the messages file. svn path=/import/netsurf/; revision=463
-rw-r--r--desktop/netsurf.c2
-rw-r--r--riscos/about.c2
-rw-r--r--riscos/gui.c4
-rw-r--r--riscos/window.c1
-rw-r--r--utils/utils.c71
-rw-r--r--utils/utils.h1
6 files changed, 79 insertions, 2 deletions
diff --git a/desktop/netsurf.c b/desktop/netsurf.c
index e64ac14bf..f8a632cde 100644
--- a/desktop/netsurf.c
+++ b/desktop/netsurf.c
@@ -16,6 +16,7 @@
#include "netsurf/desktop/browser.h"
#include "netsurf/desktop/gui.h"
#include "netsurf/utils/log.h"
+#include "netsurf/utils/utils.h"
bool netsurf_quit = false;
@@ -78,5 +79,6 @@ void netsurf_exit(void)
{
cache_quit();
fetch_quit();
+ clean_cookiejar();
gui_quit();
}
diff --git a/riscos/about.c b/riscos/about.c
index 27b154ca6..d99befffa 100644
--- a/riscos/about.c
+++ b/riscos/about.c
@@ -266,7 +266,7 @@ void cookie_create(void) {
exp[50], name[256], val[256];
unsigned int expiry;
- fp = fopen("Choices:WWW.NetSurf.Cookies", "r");
+ fp = fopen(messages_get("cookiejar"), "r");
if (!fp) {
LOG(("Failed to open cookie jar"));
return;
diff --git a/riscos/gui.c b/riscos/gui.c
index d2b77f0b2..696ac1abf 100644
--- a/riscos/gui.c
+++ b/riscos/gui.c
@@ -253,8 +253,10 @@ void gui_poll(bool active)
case wimp_CLOSE_WINDOW_REQUEST :
g = ro_lookup_gui_from_w(block.close.w);
- if (g != NULL)
+ if (g != NULL) {
browser_window_destroy(g->data.browser.bw, true);
+ clean_cookiejar();
+ }
else
ro_gui_dialog_close((wimp_w)(&(block.close.w)));
break;
diff --git a/riscos/window.c b/riscos/window.c
index 8774f31e3..01a8a5b96 100644
--- a/riscos/window.c
+++ b/riscos/window.c
@@ -703,6 +703,7 @@ bool ro_gui_window_keypress(gui_window *g, int key, bool toolbar)
case wimp_KEY_CONTROL + wimp_KEY_F2: /* Close window. */
browser_window_destroy(g->data.browser.bw, true);
+ clean_cookiejar();
return true;
case wimp_KEY_RETURN:
diff --git a/utils/utils.c b/utils/utils.c
index 260c1f7ea..ecc31f995 100644
--- a/utils/utils.c
+++ b/utils/utils.c
@@ -15,6 +15,7 @@
#include <uri.h>
#include <sys/types.h>
#include <regex.h>
+#include <time.h>
#include "libxml/encoding.h"
#include "libxml/uri.h"
#ifdef riscos
@@ -305,3 +306,73 @@ void regcomp_wrapper(regex_t *preg, const char *regex, int cflags)
}
}
+/**
+ * Remove expired cookies from the cookie jar.
+ * libcurl /really/ should do this for us.
+ * This gets called every time a window is closed or NetSurf is quit.
+ */
+
+void clean_cookiejar(void) {
+
+ FILE *fp;
+ int len;
+ char *cookies = 0, *pos;
+ char domain[256], flag[10], path[256], secure[10],
+ exp[50], name[256], val[256];
+ long int expiry;
+
+ fp = fopen(messages_get("cookiejar"), "r");
+ if (!fp) {
+ LOG(("Failed to open cookie jar"));
+ return;
+ }
+
+ /* read file length */
+ fseek(fp, 0, SEEK_END);
+ len = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ cookies = xcalloc((unsigned int)len, sizeof(char));
+ fread(cookies, (unsigned int)len, sizeof(char), fp);
+ fclose(fp);
+
+ if (remove(messages_get("cookiejar"))) {
+ LOG(("Failed to remove old jar"));
+ xfree(cookies);
+ return;
+ }
+
+ fp = fopen(messages_get("cookiejar"), "w+");
+ if (!fp) {
+ xfree(cookies);
+ LOG(("Failed to create new jar"));
+ return;
+ }
+ /* write header */
+ fputs("# Netscape HTTP Cookie File\n"
+ "# http://www.netscape.com/newsref/std/cookie_spec.html\n"
+ "# This file was generated by libcurl! Edit at your own risk.\n\n",
+ fp);
+
+ pos = cookies;
+ while (pos != (cookies+len-1)) {
+ if (*pos == '#') {
+ for (; *pos != '\n'; pos++);
+ pos += 1;
+ continue;
+ }
+ sscanf(pos, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", domain, flag,
+ path, secure, exp, name, val);
+ pos += (strlen(domain) + strlen(flag) + strlen(path) +
+ strlen(secure) + strlen(exp) + strlen(name) +
+ strlen(val) + 7);
+ sscanf(exp, "%ld", &expiry);
+ if (time(NULL) < expiry) { /* cookie hasn't expired */
+ fprintf(fp, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", domain,
+ flag, path, secure, exp, name, val);
+ }
+ }
+ fclose(fp);
+
+ xfree(cookies);
+}
diff --git a/utils/utils.h b/utils/utils.h
index e3a210352..4feeb34a3 100644
--- a/utils/utils.h
+++ b/utils/utils.h
@@ -30,5 +30,6 @@ char *url_join(char *rel_url, char *base_url);
char *get_host_from_url(char* url);
bool is_dir(const char *path);
void regcomp_wrapper(regex_t *preg, const char *regex, int cflags);
+void clean_cookiejar(void);
#endif