1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-cookie-jar-text.c: cookies.txt-based cookie storage
5 * Copyright (C) 2007, 2008 Red Hat, Inc.
16 #include "soup-cookie-jar-text.h"
17 #include "soup-cookie.h"
18 #include "soup-date.h"
21 * SECTION:soup-cookie-jar-text
22 * @short_description: Text-file-based ("cookies.txt") Cookie Jar
24 * #SoupCookieJarText is a #SoupCookieJar that reads cookies from and
25 * writes them to a text file in the Mozilla "cookies.txt" format.
39 } SoupCookieJarTextPrivate;
40 #define SOUP_COOKIE_JAR_TEXT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_COOKIE_JAR_TEXT, SoupCookieJarTextPrivate))
42 G_DEFINE_TYPE (SoupCookieJarText, soup_cookie_jar_text, SOUP_TYPE_COOKIE_JAR)
44 static void load (SoupCookieJar *jar);
45 static void changed (SoupCookieJar *jar,
46 SoupCookie *old_cookie,
47 SoupCookie *new_cookie);
48 static gboolean is_persistent (SoupCookieJar *jar);
50 static void set_property (GObject *object, guint prop_id,
51 const GValue *value, GParamSpec *pspec);
52 static void get_property (GObject *object, guint prop_id,
53 GValue *value, GParamSpec *pspec);
56 soup_cookie_jar_text_init (SoupCookieJarText *text)
61 finalize (GObject *object)
63 SoupCookieJarTextPrivate *priv =
64 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
66 g_free (priv->filename);
68 G_OBJECT_CLASS (soup_cookie_jar_text_parent_class)->finalize (object);
72 soup_cookie_jar_text_class_init (SoupCookieJarTextClass *text_class)
74 SoupCookieJarClass *cookie_jar_class =
75 SOUP_COOKIE_JAR_CLASS (text_class);
76 GObjectClass *object_class = G_OBJECT_CLASS (text_class);
78 g_type_class_add_private (text_class, sizeof (SoupCookieJarTextPrivate));
80 cookie_jar_class->is_persistent = is_persistent;
81 cookie_jar_class->changed = changed;
83 object_class->finalize = finalize;
84 object_class->set_property = set_property;
85 object_class->get_property = get_property;
88 * SOUP_COOKIE_JAR_TEXT_FILENAME:
90 * Alias for the #SoupCookieJarText:filename property. (The
91 * cookie-storage filename.)
93 g_object_class_install_property (
94 object_class, PROP_FILENAME,
95 g_param_spec_string (SOUP_COOKIE_JAR_TEXT_FILENAME,
97 "Cookie-storage filename",
99 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
103 set_property (GObject *object, guint prop_id,
104 const GValue *value, GParamSpec *pspec)
106 SoupCookieJarTextPrivate *priv =
107 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
111 priv->filename = g_value_dup_string (value);
112 load (SOUP_COOKIE_JAR (object));
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 get_property (GObject *object, guint prop_id,
122 GValue *value, GParamSpec *pspec)
124 SoupCookieJarTextPrivate *priv =
125 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
129 g_value_set_string (value, priv->filename);
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 * soup_cookie_jar_text_new:
139 * @filename: the filename to read to/write from
140 * @read_only: %TRUE if @filename is read-only
142 * Creates a #SoupCookieJarText.
144 * @filename will be read in at startup to create an initial set of
145 * cookies. If @read_only is %FALSE, then the non-session cookies will
146 * be written to @filename when the 'changed' signal is emitted from
147 * the jar. (If @read_only is %TRUE, then the cookie jar will only be
148 * used for this session, and changes made to it will be lost when the
151 * Return value: the new #SoupCookieJar
156 soup_cookie_jar_text_new (const char *filename, gboolean read_only)
158 g_return_val_if_fail (filename != NULL, NULL);
160 return g_object_new (SOUP_TYPE_COOKIE_JAR_TEXT,
161 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
162 SOUP_COOKIE_JAR_READ_ONLY, read_only,
167 parse_cookie (char *line, time_t now)
170 SoupCookie *cookie = NULL;
174 char *host, *path, *secure, *expires, *name, *value;
176 if (g_str_has_prefix (line, "#HttpOnly_")) {
178 line += strlen ("#HttpOnly_");
179 } else if (*line == '#' || g_ascii_isspace (*line))
184 result = g_strsplit (line, "\t", -1);
185 if (g_strv_length (result) != 7)
188 /* Check this first */
190 expire_time = strtoul (expires, NULL, 10);
191 if (now >= expire_time)
193 max_age = (expire_time - now <= G_MAXINT ? expire_time - now : G_MAXINT);
197 /* result[1] is not used because it's redundat; it's a boolean
198 * value regarding whether the cookie should be used for
199 * sub-domains of the domain that is set for the cookie. It is
200 * TRUE if host starts with '.', and FALSE otherwise.
209 cookie = soup_cookie_new (name, value, host, path, max_age);
211 if (strcmp (secure, "FALSE") != 0)
212 soup_cookie_set_secure (cookie, TRUE);
214 soup_cookie_set_http_only (cookie, TRUE);
223 parse_line (SoupCookieJar *jar, char *line, time_t now)
227 cookie = parse_cookie (line, now);
229 soup_cookie_jar_add_cookie (jar, cookie);
233 load (SoupCookieJar *jar)
235 SoupCookieJarTextPrivate *priv =
236 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (jar);
237 char *contents = NULL, *line, *p;
239 time_t now = time (NULL);
242 if (!g_file_get_contents (priv->filename, &contents, &length, NULL))
246 for (p = contents; *p; p++) {
247 /* \r\n comes out as an extra empty line and gets ignored */
248 if (*p == '\r' || *p == '\n') {
250 parse_line (jar, line, now);
254 parse_line (jar, line, now);
260 write_cookie (FILE *out, SoupCookie *cookie)
262 fseek (out, 0, SEEK_END);
264 fprintf (out, "%s%s\t%s\t%s\t%s\t%lu\t%s\t%s\n",
265 cookie->http_only ? "#HttpOnly_" : "",
267 *cookie->domain == '.' ? "TRUE" : "FALSE",
269 cookie->secure ? "TRUE" : "FALSE",
270 (gulong)soup_date_to_time_t (cookie->expires),
276 delete_cookie (const char *filename, SoupCookie *cookie)
278 char *contents = NULL, *line, *p;
282 time_t now = time (NULL);
284 if (!g_file_get_contents (filename, &contents, &length, NULL))
287 f = fopen (filename, "w");
294 for (p = contents; *p; p++) {
295 /* \r\n comes out as an extra empty line and gets ignored */
296 if (*p == '\r' || *p == '\n') {
298 c = parse_cookie (line, now);
302 if (!soup_cookie_equal (cookie, c))
304 soup_cookie_free (c);
307 c = parse_cookie (line, now);
309 if (!soup_cookie_equal (cookie, c))
311 soup_cookie_free (c);
319 changed (SoupCookieJar *jar,
320 SoupCookie *old_cookie,
321 SoupCookie *new_cookie)
324 SoupCookieJarTextPrivate *priv =
325 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (jar);
327 /* We can sort of ignore the semantics of the 'changed'
328 * signal here and simply delete the old cookie if present
329 * and write the new cookie if present. That will do the
330 * right thing for all 'added', 'deleted' and 'modified'
333 /* Also, delete_cookie takes the filename and write_cookie
334 * a FILE pointer. Seems more convenient that way considering
335 * the implementations of the functions
338 delete_cookie (priv->filename, old_cookie);
341 gboolean write_header = FALSE;
343 if (!g_file_test (priv->filename, G_FILE_TEST_EXISTS))
346 out = fopen (priv->filename, "a");
353 fprintf (out, "# HTTP Cookie File\n");
354 fprintf (out, "# http://www.netscape.com/newsref/std/cookie_spec.html\n");
355 fprintf (out, "# This is a generated file! Do not edit.\n");
356 fprintf (out, "# To delete cookies, use the Cookie Manager.\n\n");
359 if (new_cookie->expires)
360 write_cookie (out, new_cookie);
362 if (fclose (out) != 0) {
370 is_persistent (SoupCookieJar *jar)