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);
49 static void set_property (GObject *object, guint prop_id,
50 const GValue *value, GParamSpec *pspec);
51 static void get_property (GObject *object, guint prop_id,
52 GValue *value, GParamSpec *pspec);
55 soup_cookie_jar_text_init (SoupCookieJarText *text)
60 finalize (GObject *object)
62 SoupCookieJarTextPrivate *priv =
63 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
65 g_free (priv->filename);
67 G_OBJECT_CLASS (soup_cookie_jar_text_parent_class)->finalize (object);
71 soup_cookie_jar_text_class_init (SoupCookieJarTextClass *text_class)
73 SoupCookieJarClass *cookie_jar_class =
74 SOUP_COOKIE_JAR_CLASS (text_class);
75 GObjectClass *object_class = G_OBJECT_CLASS (text_class);
77 g_type_class_add_private (text_class, sizeof (SoupCookieJarTextPrivate));
79 cookie_jar_class->changed = changed;
81 object_class->finalize = finalize;
82 object_class->set_property = set_property;
83 object_class->get_property = get_property;
86 * SOUP_COOKIE_JAR_TEXT_FILENAME:
88 * Alias for the #SoupCookieJarText:filename property. (The
89 * cookie-storage filename.)
91 g_object_class_install_property (
92 object_class, PROP_FILENAME,
93 g_param_spec_string (SOUP_COOKIE_JAR_TEXT_FILENAME,
95 "Cookie-storage filename",
97 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
101 set_property (GObject *object, guint prop_id,
102 const GValue *value, GParamSpec *pspec)
104 SoupCookieJarTextPrivate *priv =
105 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
109 priv->filename = g_value_dup_string (value);
110 load (SOUP_COOKIE_JAR (object));
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 get_property (GObject *object, guint prop_id,
120 GValue *value, GParamSpec *pspec)
122 SoupCookieJarTextPrivate *priv =
123 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
127 g_value_set_string (value, priv->filename);
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 * soup_cookie_jar_text_new:
137 * @filename: the filename to read to/write from
138 * @read_only: %TRUE if @filename is read-only
140 * Creates a #SoupCookieJarText.
142 * @filename will be read in at startup to create an initial set of
143 * cookies. If @read_only is %FALSE, then the non-session cookies will
144 * be written to @filename when the 'changed' signal is emitted from
145 * the jar. (If @read_only is %TRUE, then the cookie jar will only be
146 * used for this session, and changes made to it will be lost when the
149 * Return value: the new #SoupCookieJar
154 soup_cookie_jar_text_new (const char *filename, gboolean read_only)
156 g_return_val_if_fail (filename != NULL, NULL);
158 return g_object_new (SOUP_TYPE_COOKIE_JAR_TEXT,
159 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
160 SOUP_COOKIE_JAR_READ_ONLY, read_only,
165 parse_cookie (char *line, time_t now)
168 SoupCookie *cookie = NULL;
172 char *host, *path, *secure, *expires, *name, *value;
174 if (g_str_has_prefix (line, "#HttpOnly_")) {
176 line += strlen ("#HttpOnly_");
177 } else if (*line == '#' || g_ascii_isspace (*line))
182 result = g_strsplit (line, "\t", -1);
183 if (g_strv_length (result) != 7)
186 /* Check this first */
188 expire_time = strtoul (expires, NULL, 10);
189 if (now >= expire_time)
191 max_age = (expire_time - now <= G_MAXINT ? expire_time - now : G_MAXINT);
195 /* result[1] is not used because it's redundat; it's a boolean
196 * value regarding whether the cookie should be used for
197 * sub-domains of the domain that is set for the cookie. It is
198 * TRUE if host starts with '.', and FALSE otherwise.
207 cookie = soup_cookie_new (name, value, host, path, max_age);
209 if (strcmp (secure, "FALSE") != 0)
210 soup_cookie_set_secure (cookie, TRUE);
212 soup_cookie_set_http_only (cookie, TRUE);
221 parse_line (SoupCookieJar *jar, char *line, time_t now)
225 cookie = parse_cookie (line, now);
227 soup_cookie_jar_add_cookie (jar, cookie);
231 load (SoupCookieJar *jar)
233 SoupCookieJarTextPrivate *priv =
234 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (jar);
235 char *contents = NULL, *line, *p;
237 time_t now = time (NULL);
240 if (!g_file_get_contents (priv->filename, &contents, &length, NULL))
244 for (p = contents; *p; p++) {
245 /* \r\n comes out as an extra empty line and gets ignored */
246 if (*p == '\r' || *p == '\n') {
248 parse_line (jar, line, now);
252 parse_line (jar, line, now);
258 write_cookie (FILE *out, SoupCookie *cookie)
260 fseek (out, 0, SEEK_END);
262 fprintf (out, "%s%s\t%s\t%s\t%s\t%lu\t%s\t%s\n",
263 cookie->http_only ? "#HttpOnly_" : "",
265 *cookie->domain == '.' ? "TRUE" : "FALSE",
267 cookie->secure ? "TRUE" : "FALSE",
268 (gulong)soup_date_to_time_t (cookie->expires),
274 delete_cookie (const char *filename, SoupCookie *cookie)
276 char *contents = NULL, *line, *p;
280 time_t now = time (NULL);
282 if (!g_file_get_contents (filename, &contents, &length, NULL))
285 f = fopen (filename, "w");
292 for (p = contents; *p; p++) {
293 /* \r\n comes out as an extra empty line and gets ignored */
294 if (*p == '\r' || *p == '\n') {
296 c = parse_cookie (line, now);
300 if (!soup_cookie_equal (cookie, c))
302 soup_cookie_free (c);
305 c = parse_cookie (line, now);
307 if (!soup_cookie_equal (cookie, c))
309 soup_cookie_free (c);
317 changed (SoupCookieJar *jar,
318 SoupCookie *old_cookie,
319 SoupCookie *new_cookie)
322 SoupCookieJarTextPrivate *priv =
323 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (jar);
325 /* We can sort of ignore the semantics of the 'changed'
326 * signal here and simply delete the old cookie if present
327 * and write the new cookie if present. That will do the
328 * right thing for all 'added', 'deleted' and 'modified'
331 /* Also, delete_cookie takes the filename and write_cookie
332 * a FILE pointer. Seems more convenient that way considering
333 * the implementations of the functions
336 delete_cookie (priv->filename, old_cookie);
339 gboolean write_header = FALSE;
341 if (!g_file_test (priv->filename, G_FILE_TEST_EXISTS))
344 out = fopen (priv->filename, "a");
351 fprintf (out, "# HTTP Cookie File\n");
352 fprintf (out, "# http://www.netscape.com/newsref/std/cookie_spec.html\n");
353 fprintf (out, "# This is a generated file! Do not edit.\n");
354 fprintf (out, "# To delete cookies, use the Cookie Manager.\n\n");
357 if (new_cookie->expires)
358 write_cookie (out, new_cookie);
360 if (fclose (out) != 0) {