From b0b2ec850f74d3dd8bd2fffa76fbd2af20b42705 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 28 Dec 2003 02:35:46 +0000 Subject: [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 --- utils/utils.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/utils.h | 1 + 2 files changed, 72 insertions(+) (limited to 'utils') 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 #include #include +#include #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 -- cgit v1.2.3