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"
20 * SECTION:soup-cookie-jar-text
21 * @short_description: Text-file-based ("cookies.txt") Cookie Jar
23 * #SoupCookieJarText is a #SoupCookieJar that reads cookies from and
24 * writes them to a text file in the Mozilla "cookies.txt" format.
38 } SoupCookieJarTextPrivate;
39 #define SOUP_COOKIE_JAR_TEXT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_COOKIE_JAR_TEXT, SoupCookieJarTextPrivate))
41 G_DEFINE_TYPE (SoupCookieJarText, soup_cookie_jar_text, SOUP_TYPE_COOKIE_JAR)
43 static void load (SoupCookieJar *jar);
46 soup_cookie_jar_text_init (SoupCookieJarText *text)
51 soup_cookie_jar_text_finalize (GObject *object)
53 SoupCookieJarTextPrivate *priv =
54 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
56 g_free (priv->filename);
58 G_OBJECT_CLASS (soup_cookie_jar_text_parent_class)->finalize (object);
62 soup_cookie_jar_text_set_property (GObject *object, guint prop_id,
63 const GValue *value, GParamSpec *pspec)
65 SoupCookieJarTextPrivate *priv =
66 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
70 priv->filename = g_value_dup_string (value);
71 load (SOUP_COOKIE_JAR (object));
74 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
80 soup_cookie_jar_text_get_property (GObject *object, guint prop_id,
81 GValue *value, GParamSpec *pspec)
83 SoupCookieJarTextPrivate *priv =
84 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (object);
88 g_value_set_string (value, priv->filename);
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 * soup_cookie_jar_text_new:
98 * @filename: the filename to read to/write from
99 * @read_only: %TRUE if @filename is read-only
101 * Creates a #SoupCookieJarText.
103 * @filename will be read in at startup to create an initial set of
104 * cookies. If @read_only is %FALSE, then the non-session cookies will
105 * be written to @filename when the 'changed' signal is emitted from
106 * the jar. (If @read_only is %TRUE, then the cookie jar will only be
107 * used for this session, and changes made to it will be lost when the
110 * Return value: the new #SoupCookieJar
115 soup_cookie_jar_text_new (const char *filename, gboolean read_only)
117 g_return_val_if_fail (filename != NULL, NULL);
119 return g_object_new (SOUP_TYPE_COOKIE_JAR_TEXT,
120 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
121 SOUP_COOKIE_JAR_READ_ONLY, read_only,
126 parse_cookie (char *line, time_t now)
129 SoupCookie *cookie = NULL;
133 char *host, *path, *secure, *expires, *name, *value;
135 if (g_str_has_prefix (line, "#HttpOnly_")) {
137 line += strlen ("#HttpOnly_");
138 } else if (*line == '#' || g_ascii_isspace (*line))
143 result = g_strsplit (line, "\t", -1);
144 if (g_strv_length (result) != 7)
147 /* Check this first */
149 expire_time = strtoul (expires, NULL, 10);
150 if (now >= expire_time)
152 max_age = (expire_time - now <= G_MAXINT ? expire_time - now : G_MAXINT);
156 /* result[1] is not used because it's redundat; it's a boolean
157 * value regarding whether the cookie should be used for
158 * sub-domains of the domain that is set for the cookie. It is
159 * TRUE if host starts with '.', and FALSE otherwise.
168 cookie = soup_cookie_new (name, value, host, path, max_age);
170 if (strcmp (secure, "FALSE") != 0)
171 soup_cookie_set_secure (cookie, TRUE);
173 soup_cookie_set_http_only (cookie, TRUE);
182 parse_line (SoupCookieJar *jar, char *line, time_t now)
186 cookie = parse_cookie (line, now);
188 soup_cookie_jar_add_cookie (jar, cookie);
192 load (SoupCookieJar *jar)
194 SoupCookieJarTextPrivate *priv =
195 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (jar);
196 char *contents = NULL, *line, *p;
198 time_t now = time (NULL);
201 if (!g_file_get_contents (priv->filename, &contents, &length, NULL))
205 for (p = contents; *p; p++) {
206 /* \r\n comes out as an extra empty line and gets ignored */
207 if (*p == '\r' || *p == '\n') {
209 parse_line (jar, line, now);
213 parse_line (jar, line, now);
219 write_cookie (FILE *out, SoupCookie *cookie)
221 fseek (out, 0, SEEK_END);
223 fprintf (out, "%s%s\t%s\t%s\t%s\t%lu\t%s\t%s\n",
224 cookie->http_only ? "#HttpOnly_" : "",
226 *cookie->domain == '.' ? "TRUE" : "FALSE",
228 cookie->secure ? "TRUE" : "FALSE",
229 (gulong)soup_date_to_time_t (cookie->expires),
235 delete_cookie (const char *filename, SoupCookie *cookie)
237 char *contents = NULL, *line, *p;
241 time_t now = time (NULL);
243 if (!g_file_get_contents (filename, &contents, &length, NULL))
246 f = fopen (filename, "w");
253 for (p = contents; *p; p++) {
254 /* \r\n comes out as an extra empty line and gets ignored */
255 if (*p == '\r' || *p == '\n') {
257 c = parse_cookie (line, now);
261 if (!soup_cookie_equal (cookie, c))
263 soup_cookie_free (c);
266 c = parse_cookie (line, now);
268 if (!soup_cookie_equal (cookie, c))
270 soup_cookie_free (c);
278 soup_cookie_jar_text_changed (SoupCookieJar *jar,
279 SoupCookie *old_cookie,
280 SoupCookie *new_cookie)
283 SoupCookieJarTextPrivate *priv =
284 SOUP_COOKIE_JAR_TEXT_GET_PRIVATE (jar);
286 /* We can sort of ignore the semantics of the 'changed'
287 * signal here and simply delete the old cookie if present
288 * and write the new cookie if present. That will do the
289 * right thing for all 'added', 'deleted' and 'modified'
292 /* Also, delete_cookie takes the filename and write_cookie
293 * a FILE pointer. Seems more convenient that way considering
294 * the implementations of the functions
297 delete_cookie (priv->filename, old_cookie);
300 gboolean write_header = FALSE;
302 if (!g_file_test (priv->filename, G_FILE_TEST_EXISTS))
305 out = fopen (priv->filename, "a");
312 fprintf (out, "# HTTP Cookie File\n");
313 fprintf (out, "# http://www.netscape.com/newsref/std/cookie_spec.html\n");
314 fprintf (out, "# This is a generated file! Do not edit.\n");
315 fprintf (out, "# To delete cookies, use the Cookie Manager.\n\n");
318 if (new_cookie->expires)
319 write_cookie (out, new_cookie);
321 if (fclose (out) != 0) {
329 soup_cookie_jar_text_is_persistent (SoupCookieJar *jar)
335 soup_cookie_jar_text_class_init (SoupCookieJarTextClass *text_class)
337 SoupCookieJarClass *cookie_jar_class =
338 SOUP_COOKIE_JAR_CLASS (text_class);
339 GObjectClass *object_class = G_OBJECT_CLASS (text_class);
341 g_type_class_add_private (text_class, sizeof (SoupCookieJarTextPrivate));
343 cookie_jar_class->is_persistent = soup_cookie_jar_text_is_persistent;
344 cookie_jar_class->changed = soup_cookie_jar_text_changed;
346 object_class->finalize = soup_cookie_jar_text_finalize;
347 object_class->set_property = soup_cookie_jar_text_set_property;
348 object_class->get_property = soup_cookie_jar_text_get_property;
351 * SOUP_COOKIE_JAR_TEXT_FILENAME:
353 * Alias for the #SoupCookieJarText:filename property. (The
354 * cookie-storage filename.)
356 g_object_class_install_property (
357 object_class, PROP_FILENAME,
358 g_param_spec_string (SOUP_COOKIE_JAR_TEXT_FILENAME,
360 "Cookie-storage filename",
362 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));