📄 cookies.c
字号:
/* * File: cookies.c * * Copyright 2001 Lars Clausen <lrclause@cs.uiuc.edu> * J鰎gen Viksell <jorgen.viksell@telia.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. *//* Handling of cookies takes place here. * This implementation aims to follow RFC 2965: * http://www.cis.ohio-state.edu/cs/Services/rfc/rfc-text/rfc2965.txt */#ifndef DISABLE_COOKIES#include <sys/types.h>#include <sys/stat.h>#include <sys/file.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#include <time.h> /* for time() and time_t */#include <ctype.h>#include "IO/Url.h"#include "misc.h"#include "list.h"#include "cookies.h"#define DEBUG_LEVEL 8#include "debug.h"/* The maximum length of a line in the cookie file */#define LINE_MAXLEN 4096typedef enum { COOKIE_ACCEPT, COOKIE_ACCEPT_SESSION, COOKIE_DENY} CookieControlAction;typedef struct { CookieControlAction action; char *domain;} CookieControl;typedef struct { char *name; char *value; char *domain; char *path; time_t expires_at; guint version; char *comment; char *comment_url; gboolean secure; gboolean session_only; GList *ports;} CookieData_t;/* Hashtable indexed by domain, each value is a set of cookies for * that domain. */static GHashTable *cookies;/* Variables for access control */static CookieControl *ccontrol = NULL;static int num_ccontrol = 0;static int num_ccontrol_max = 1;static CookieControlAction default_action = COOKIE_DENY;static gboolean disabled;static FILE *file_stream;static FILE *Cookies_fopen(const char *file, gchar *init_str);static CookieControlAction Cookies_control_check(const DilloUrl *url);static CookieControlAction Cookies_control_check_domain(const char *domain);static void Cookie_control_init();static void Cookies_parse_ports(CookieData_t *cookie, const char *port_str);static char *Cookies_build_ports_str(CookieData_t *cookie);/* * Return a file pointer. If the file doesn't exist, try to create it, * with the optional 'init_str' as its content. * This function is either successful or exits Dillo. */static FILE *Cookies_fopen(const char *filename, gchar *init_str){ FILE *F_in; int fd; if ((F_in = fopen(filename, "r+")) == NULL) { /* Create the file */ fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); if (fd != -1) { if (init_str) write(fd, init_str, strlen(init_str)); close(fd); DEBUG_MSG(10, "Cookies: Created file: %s\n", filename); F_in = Cookies_fopen(filename, NULL); } else { DEBUG_MSG(10, "Cookies: Could not create file: %s!\n", filename); } } return F_in;}static void Cookies_free_cookie(CookieData_t *cookie){ if (cookie) { g_free(cookie->name); g_free(cookie->value); g_free(cookie->domain); g_free(cookie->path); g_free(cookie->comment); g_free(cookie->comment_url); g_list_free(cookie->ports); g_free(cookie); }}void a_Cookies_init(){ CookieData_t *cookie; GList *domain_cookies; char *filename; char line[LINE_MAXLEN]; Cookie_control_init(); /* Get a stream for the cookies file */ filename = a_Misc_prepend_user_home(".dillo/cookies"); file_stream = Cookies_fopen(filename, NULL); g_free(filename); if (!file_stream) { /* Can't open the file, so we disable cookies */ disabled = TRUE; return; } /* Try to get a lock from the file descriptor */ disabled = (lockf(fileno(file_stream), F_TLOCK, 0) == -1); if (disabled) { DEBUG_MSG(10, "The cookies file has a file lock:\n" " disabling cookies in this dillo!\n"); fclose(file_stream); return; } cookies = g_hash_table_new(g_str_hash, g_str_equal); /* Get all lines in the file */ while (!feof(file_stream)) { line[0] = '\0'; fgets(line, LINE_MAXLEN, file_stream); /* Remove leading and trailing whitespaces */ g_strstrip(line); if (line[0] != '\0') { /* Would use g_strsplit, but it doesn't give empty trailing pieces. */ /* Split the row into pieces using a tab as the delimiter. * pieces[0] The version this cookie was set as (0 / 1) * pieces[1] The domain name * pieces[2] A comma separated list of accepted ports * pieces[3] The path * pieces[4] Is the cookie unsecure or secure (0 / 1) * pieces[5] Timestamp of expire date * pieces[6] Name of the cookie * pieces[7] Value of the cookie * pieces[8] Comment * pieces[9] Comment url */ CookieControlAction action; char *piece; char *line_marker = line; cookie = g_new0(CookieData_t, 1); cookie->session_only = FALSE; piece = d_strsep(&line_marker, "\t"); if (piece != NULL) cookie->version = strtol(piece, NULL, 10); cookie->domain = g_strdup(d_strsep(&line_marker, "\t")); Cookies_parse_ports(cookie, d_strsep(&line_marker, "\t")); cookie->path = g_strdup(d_strsep(&line_marker, "\t")); piece = d_strsep(&line_marker, "\t"); if (piece != NULL && piece[0] == '1') cookie->secure = TRUE; piece = d_strsep(&line_marker, "\t"); if (piece != NULL) cookie->expires_at = (time_t) strtol(piece, NULL, 10); cookie->name = g_strdup(d_strsep(&line_marker, "\t")); cookie->value = g_strdup(d_strsep(&line_marker, "\t")); cookie->comment = g_strdup(d_strsep(&line_marker, "\t")); cookie->comment_url = g_strdup(d_strsep(&line_marker, "\t")); action = Cookies_control_check_domain(cookie->domain); if (action == COOKIE_DENY) { Cookies_free_cookie(cookie); continue; } else if (action == COOKIE_ACCEPT_SESSION) { cookie->session_only = TRUE; } /* Save cookie in memory */ if (cookie->expires_at > time(NULL)) { domain_cookies = (GList *) g_hash_table_lookup(cookies, cookie->domain); if (domain_cookies == NULL) { domain_cookies = g_list_append(domain_cookies, cookie); g_hash_table_insert(cookies, g_strdup(cookie->domain), domain_cookies); } else { domain_cookies = g_list_append(domain_cookies, cookie); } } else { /* Has expired */ Cookies_free_cookie(cookie); } } }}/* * Save the cookies and remove them from the hash table */static gboolean Cookies_freeall_cb(gpointer key, gpointer value, gpointer data){ CookieData_t *cookie; GList *domain_cookies; char *ports_str; for (domain_cookies = g_list_first((GList *) value); domain_cookies != NULL; domain_cookies = g_list_next(domain_cookies)) { cookie = (CookieData_t *) domain_cookies->data; if (cookie != NULL) { if (!cookie->session_only) { ports_str = Cookies_build_ports_str(cookie); fprintf(file_stream, "%d\t%s\t%s\t%s\t%d\t%ld\t%s\t%s\t%s\t%s\n", cookie->version, cookie->domain, ports_str, cookie->path, cookie->secure ? 1 : 0, (long)cookie->expires_at, cookie->name, cookie->value, (cookie->comment) ? cookie->comment : "", (cookie->comment_url) ? cookie->comment_url : ""); g_free(ports_str); } Cookies_free_cookie(cookie); } } g_list_free((GList *) value); g_free(key); /* Return TRUE to tell GLIB to free this key from the hash table */ return TRUE;}/* * Flush cookies to disk and free all the memory allocated. */void a_Cookies_freeall(){ int fd; if (disabled) return; rewind(file_stream); fd = fileno(file_stream); ftruncate(fd, 0); g_hash_table_foreach_remove(cookies, Cookies_freeall_cb, NULL); lockf(fd, F_ULOCK, 0); fclose(file_stream);}/* * ? */static char *Cookies_try_header(char **string, char *header, gboolean compat){ char *marker; char *result; char *value_start; if (*string == NULL) return NULL; if (g_strncasecmp(*string, header, strlen(header))) { return NULL; } value_start = *string + strlen(header); marker = strchr(value_start, '='); if (marker == NULL) { *string = NULL; return g_strdup(""); /* Some attributes don't use '=' */ } marker++; g_strstrip(marker); value_start = marker; marker = strchr(value_start, ';'); if (marker == NULL) { if (!compat) { marker = strchr(value_start, ','); if (marker == NULL) { result = g_strdup(value_start); *string = NULL; return result; } } else { /* Netscape Expires combatability -- commas in string (doh!) */ result = g_strdup(value_start); *string = NULL; return result; } } result = g_strndup(value_start, marker - value_start); *string = marker; return result;}static char *months[] ={ "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};/* * Take a months name and return a number between 1-12. * E.g. 'April' -> 4 */static int Cookies_get_month(const char *month_name){ int i; for (i = 1; i <= 12; i++) { if (!g_strncasecmp(months[i], month_name, 3)) return i; } return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -