diff options
Diffstat (limited to 'dpi')
-rw-r--r-- | dpi/cookies.c | 117 |
1 files changed, 61 insertions, 56 deletions
diff --git a/dpi/cookies.c b/dpi/cookies.c index a6009d4c..a915dca6 100644 --- a/dpi/cookies.c +++ b/dpi/cookies.c @@ -246,69 +246,19 @@ static void Cookies_tm_init(struct tm *tm) } /* - * Initialize the cookies module - * (The 'disabled' variable is writeable only within Cookies_init) + * Read in cookies from 'stream' (cookies.txt) */ -static void Cookies_init() +static void Cookies_load_cookies(FILE *stream) { - CookieData_t *cookie; - char *filename, *rc = NULL; char line[LINE_MAXLEN]; -#ifndef HAVE_LOCKF - struct flock lck; -#endif - struct tm future_tm = {7, 14, 3, 19, 0, 138, 0, 0, 0, 0, 0}; - - /* Default setting */ - disabled = TRUE; - - cookies_epoch_time = mktime(&cookies_epoch_tm); - cookies_future_time = mktime(&future_tm); - - /* Read and parse the cookie control file (cookiesrc) */ - if (Cookie_control_init() != 0) { - MSG("Disabling cookies.\n"); - return; - } - - /* Get a stream for the cookies file */ - filename = dStrconcat(dGethomedir(), "/.dillo/cookies.txt", NULL); - file_stream = Cookies_fopen(filename, "r+", cookies_txt_header_str); - - dFree(filename); - - if (!file_stream) { - MSG("ERROR: Can't open ~/.dillo/cookies.txt; disabling cookies\n"); - return; - } - - /* Try to get a lock from the file descriptor */ -#ifdef HAVE_LOCKF - disabled = (lockf(fileno(file_stream), F_TLOCK, 0) == -1); -#else /* POSIX lock */ - lck.l_start = 0; /* start at beginning of file */ - lck.l_len = 0; /* lock entire file */ - lck.l_type = F_WRLCK; - lck.l_whence = SEEK_SET; /* absolute offset */ - - disabled = (fcntl(fileno(file_stream), F_SETLK, &lck) == -1); -#endif - if (disabled) { - MSG("The cookies file has a file lock; disabling cookies!\n"); - fclose(file_stream); - return; - } - - MSG("Enabling cookies as per cookiesrc...\n"); all_cookies = dList_new(32); domains = dList_new(32); /* Get all lines in the file */ - while (!feof(file_stream)) { + while (!feof(stream)) { line[0] = '\0'; - rc = fgets(line, LINE_MAXLEN, file_stream); - if (!rc && ferror(file_stream)) { + if ((fgets(line, LINE_MAXLEN, stream) == NULL) && ferror(stream)) { MSG("Error while reading from cookies.txt: %s\n", dStrerror(errno)); break; /* bail out */ } @@ -330,8 +280,7 @@ static void Cookies_init() CookieControlAction action; char *piece; char *line_marker = line; - - cookie = dNew0(CookieData_t, 1); + CookieData_t *cookie = dNew0(CookieData_t, 1); cookie->session_only = FALSE; cookie->domain = dStrdup(dStrsep(&line_marker, "\t")); @@ -376,6 +325,62 @@ static void Cookies_init() } /* + * Initialize the cookies module + * (The 'disabled' variable is writeable only within Cookies_init) + */ +static void Cookies_init() +{ + char *filename; +#ifndef HAVE_LOCKF + struct flock lck; +#endif + struct tm future_tm = {7, 14, 3, 19, 0, 138, 0, 0, 0, 0, 0}; + + /* Default setting */ + disabled = TRUE; + + cookies_epoch_time = mktime(&cookies_epoch_tm); + cookies_future_time = mktime(&future_tm); + + /* Read and parse the cookie control file (cookiesrc) */ + if (Cookie_control_init() != 0) { + MSG("Disabling cookies.\n"); + return; + } + + /* Get a stream for the cookies file */ + filename = dStrconcat(dGethomedir(), "/.dillo/cookies.txt", NULL); + file_stream = Cookies_fopen(filename, "r+", cookies_txt_header_str); + + dFree(filename); + + if (!file_stream) { + MSG("ERROR: Can't open ~/.dillo/cookies.txt; disabling cookies\n"); + return; + } + + /* Try to get a lock from the file descriptor */ +#ifdef HAVE_LOCKF + disabled = (lockf(fileno(file_stream), F_TLOCK, 0) == -1); +#else /* POSIX lock */ + lck.l_start = 0; /* start at beginning of file */ + lck.l_len = 0; /* lock entire file */ + lck.l_type = F_WRLCK; + lck.l_whence = SEEK_SET; /* absolute offset */ + + disabled = (fcntl(fileno(file_stream), F_SETLK, &lck) == -1); +#endif + if (disabled) { + MSG("The cookies file has a file lock; disabling cookies!\n"); + fclose(file_stream); + return; + } + MSG("Enabling cookies as per cookiesrc...\n"); + + Cookies_load_cookies(file_stream); +} + +/* * Flush cookies to disk and free all the memory allocated. */ static void Cookies_save_and_free() |