soup-cookie-jar-text.c 9.65 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * soup-cookie-jar-text.c: cookies.txt-based cookie storage
 *
 * Copyright (C) 2007, 2008 Red Hat, Inc.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "soup-cookie-jar-text.h"
Dan Winship's avatar
Dan Winship committed
17
#include "soup.h"
18

19
20
21
/**
 * SoupCookieJarText:
 *
22
23
24
25
26
 * Text-file-based ("cookies.txt") Cookie Jar
 *
 * #SoupCookieJarText is a [class@CookieJar] that reads cookies from and writes
 * them to a text file in format similar to Mozilla's "cookies.txt".
 **/
27

28
29
30
31
32
enum {
	PROP_0,

	PROP_FILENAME,

33
	LAST_PROPERTY
34
35
};

36
37
static GParamSpec *properties[LAST_PROPERTY] = { NULL, };

38
39
40
41
42
43
struct _SoupCookieJarText {
	SoupCookieJar parent;

};


44
45
46
47
48
typedef struct {
	char *filename;

} SoupCookieJarTextPrivate;

49
G_DEFINE_FINAL_TYPE_WITH_PRIVATE (SoupCookieJarText, soup_cookie_jar_text, SOUP_TYPE_COOKIE_JAR)
50

51
static void load (SoupCookieJar *jar);
52
53
54
55
56
57
58

static void
soup_cookie_jar_text_init (SoupCookieJarText *text)
{
}

static void
59
soup_cookie_jar_text_finalize (GObject *object)
60
61
{
	SoupCookieJarTextPrivate *priv =
62
		soup_cookie_jar_text_get_instance_private (SOUP_COOKIE_JAR_TEXT (object));
63
64
65
66
67
68
69

	g_free (priv->filename);

	G_OBJECT_CLASS (soup_cookie_jar_text_parent_class)->finalize (object);
}

static void
70
71
soup_cookie_jar_text_set_property (GObject *object, guint prop_id,
				   const GValue *value, GParamSpec *pspec)
72
73
{
	SoupCookieJarTextPrivate *priv =
74
		soup_cookie_jar_text_get_instance_private (SOUP_COOKIE_JAR_TEXT (object));
75
76
77
78
79
80
81
82
83
84
85
86
87

	switch (prop_id) {
	case PROP_FILENAME:
		priv->filename = g_value_dup_string (value);
		load (SOUP_COOKIE_JAR (object));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
88
89
soup_cookie_jar_text_get_property (GObject *object, guint prop_id,
				   GValue *value, GParamSpec *pspec)
90
91
{
	SoupCookieJarTextPrivate *priv =
92
		soup_cookie_jar_text_get_instance_private (SOUP_COOKIE_JAR_TEXT (object));
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

	switch (prop_id) {
	case PROP_FILENAME:
		g_value_set_string (value, priv->filename);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

/**
 * soup_cookie_jar_text_new:
 * @filename: the filename to read to/write from
 * @read_only: %TRUE if @filename is read-only
 *
 * Creates a #SoupCookieJarText.
 *
111
112
113
114
115
 * @filename will be read in at startup to create an initial set of cookies. If
 * @read_only is %FALSE, then the non-session cookies will be written to
 * @filename when the [signal@CookieJar::changed] signal is emitted from the
 * jar. (If @read_only is %TRUE, then the cookie jar will only be used for this
 * session, and changes made to it will be lost when the jar is destroyed.)
116
 *
117
 * Returns: the new #SoupCookieJar
118
119
120
121
122
123
124
 **/
SoupCookieJar *
soup_cookie_jar_text_new (const char *filename, gboolean read_only)
{
	g_return_val_if_fail (filename != NULL, NULL);

	return g_object_new (SOUP_TYPE_COOKIE_JAR_TEXT,
Patrick Griffis's avatar
Patrick Griffis committed
125
126
			     "filename", filename,
			     "read-only", read_only,
127
128
129
			     NULL);
}

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
static SoupSameSitePolicy
string_to_same_site_policy (const char *string)
{
	if (strcmp (string, "Lax") == 0)
		return SOUP_SAME_SITE_POLICY_LAX;
	else if (strcmp (string, "Strict") == 0)
		return SOUP_SAME_SITE_POLICY_STRICT;
	else if (strcmp (string, "None") == 0)
		return SOUP_SAME_SITE_POLICY_NONE;
	else
		g_return_val_if_reached (SOUP_SAME_SITE_POLICY_NONE);
}

static const char *
same_site_policy_to_string (SoupSameSitePolicy policy)
{
	switch (policy) {
	case SOUP_SAME_SITE_POLICY_STRICT:
		return "Strict";
	case SOUP_SAME_SITE_POLICY_LAX:
		return "Lax";
	case SOUP_SAME_SITE_POLICY_NONE:
		return "None";
	}

	g_return_val_if_reached ("None");
}

158
159
160
161
162
163
static SoupCookie*
parse_cookie (char *line, time_t now)
{
	char **result;
	SoupCookie *cookie = NULL;
	gboolean http_only;
164
165
	gulong expire_time;
	int max_age;
166
167
	char *host, *path, *secure, *expires, *name, *value, *samesite = NULL;
	gsize result_length;
168
169
170
171
172
173
174
175
176
177

	if (g_str_has_prefix (line, "#HttpOnly_")) {
		http_only = TRUE;
		line += strlen ("#HttpOnly_");
	} else if (*line == '#' || g_ascii_isspace (*line))
		return cookie;
	else
		http_only = FALSE;

	result = g_strsplit (line, "\t", -1);
178
179
	result_length = g_strv_length (result);
	if (result_length < 7)
180
181
182
183
		goto out;

	/* Check this first */
	expires = result[4];
184
185
	expire_time = strtoul (expires, NULL, 10);
	if (now >= expire_time)
186
		goto out;
187
	max_age = (expire_time - now <= G_MAXINT ? expire_time - now : G_MAXINT);
188
189

	host = result[0];
190
191
192
193
194
195
196

	/* result[1] is not used because it's redundat; it's a boolean
	 * value regarding whether the cookie should be used for
	 * sub-domains of the domain that is set for the cookie. It is
	 * TRUE if host starts with '.', and FALSE otherwise.
	 */

197
198
199
200
201
202
	path = result[2];
	secure = result[3];

	name = result[5];
	value = result[6];

203
204
205
	if (result_length == 8)
		samesite = result[7];

206
207
	cookie = soup_cookie_new (name, value, host, path, max_age);

208
209
210
	if (samesite != NULL)
		soup_cookie_set_same_site_policy (cookie, string_to_same_site_policy (samesite));

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	if (strcmp (secure, "FALSE") != 0)
		soup_cookie_set_secure (cookie, TRUE);
	if (http_only)
		soup_cookie_set_http_only (cookie, TRUE);

 out:
	g_strfreev (result);

	return cookie;
}

static void
parse_line (SoupCookieJar *jar, char *line, time_t now)
{
	SoupCookie *cookie;

	cookie = parse_cookie (line, now);
	if (cookie)
		soup_cookie_jar_add_cookie (jar, cookie);
}

static void
load (SoupCookieJar *jar)
{
	SoupCookieJarTextPrivate *priv =
236
		soup_cookie_jar_text_get_instance_private (SOUP_COOKIE_JAR_TEXT (jar));
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	char *contents = NULL, *line, *p;
	gsize length = 0;
	time_t now = time (NULL);

	/* FIXME: error? */
	if (!g_file_get_contents (priv->filename, &contents, &length, NULL))
		return;

	line = contents;
	for (p = contents; *p; p++) {
		/* \r\n comes out as an extra empty line and gets ignored */
		if (*p == '\r' || *p == '\n') {
			*p = '\0';
			parse_line (jar, line, now);
			line = p + 1;
		}
	}
	parse_line (jar, line, now);

	g_free (contents);
}

static void
write_cookie (FILE *out, SoupCookie *cookie)
{
	fseek (out, 0, SEEK_END);

264
	fprintf (out, "%s%s\t%s\t%s\t%s\t%lu\t%s\t%s\t%s\n",
265
266
267
268
269
270
271
272
		 soup_cookie_get_http_only (cookie) ? "#HttpOnly_" : "",
		 soup_cookie_get_domain (cookie),
		 *soup_cookie_get_domain (cookie) == '.' ? "TRUE" : "FALSE",
		 soup_cookie_get_path (cookie),
		 soup_cookie_get_secure (cookie) ? "TRUE" : "FALSE",
		 (gulong)g_date_time_to_unix (soup_cookie_get_expires (cookie)),
		 soup_cookie_get_name (cookie),
		 soup_cookie_get_value (cookie),
273
		 same_site_policy_to_string (soup_cookie_get_same_site_policy (cookie)));
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
}

static void
delete_cookie (const char *filename, SoupCookie *cookie)
{
	char *contents = NULL, *line, *p;
	gsize length = 0;
	FILE *f;
	SoupCookie *c;
	time_t now = time (NULL);

	if (!g_file_get_contents (filename, &contents, &length, NULL))
		return;

	f = fopen (filename, "w");
	if (!f) {
		g_free (contents);
		return;
	}

	line = contents;
	for (p = contents; *p; p++) {
		/* \r\n comes out as an extra empty line and gets ignored */
		if (*p == '\r' || *p == '\n') {
			*p = '\0';
			c = parse_cookie (line, now);
300
			line = p + 1;
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
			if (!c)
				continue;
			if (!soup_cookie_equal (cookie, c))
				write_cookie (f, c);
			soup_cookie_free (c);
		}
	}
	c = parse_cookie (line, now);
	if (c) {
		if (!soup_cookie_equal (cookie, c))
			write_cookie (f, c);
		soup_cookie_free (c);
	}

	g_free (contents);
	fclose (f);
}

static void
320
321
322
soup_cookie_jar_text_changed (SoupCookieJar *jar,
			      SoupCookie    *old_cookie,
			      SoupCookie    *new_cookie)
323
324
325
{
	FILE *out;
	SoupCookieJarTextPrivate *priv =
326
		soup_cookie_jar_text_get_instance_private (SOUP_COOKIE_JAR_TEXT (jar));
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

	/* We can sort of ignore the semantics of the 'changed'
	 * signal here and simply delete the old cookie if present
	 * and write the new cookie if present. That will do the
	 * right thing for all 'added', 'deleted' and 'modified'
	 * meanings.
	 */
	/* Also, delete_cookie takes the filename and write_cookie
	 * a FILE pointer. Seems more convenient that way considering
	 * the implementations of the functions
	 */
	if (old_cookie)
		delete_cookie (priv->filename, old_cookie);

	if (new_cookie) {
		gboolean write_header = FALSE;

		if (!g_file_test (priv->filename, G_FILE_TEST_EXISTS))
			write_header = TRUE;

		out = fopen (priv->filename, "a");
		if (!out) {
			/* FIXME: error? */
			return;
		}

		if (write_header) {
			fprintf (out, "# HTTP Cookie File\n");
			fprintf (out, "# http://www.netscape.com/newsref/std/cookie_spec.html\n");
			fprintf (out, "# This is a generated file!  Do not edit.\n");
			fprintf (out, "# To delete cookies, use the Cookie Manager.\n\n");
		}

360
		if (soup_cookie_get_expires (new_cookie))
361
362
363
364
365
366
367
368
			write_cookie (out, new_cookie);

		if (fclose (out) != 0) {
			/* FIXME: error? */
			return;
		}
	}
}
369
370

static gboolean
371
soup_cookie_jar_text_is_persistent (SoupCookieJar *jar)
372
373
374
{
	return TRUE;
}
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

static void
soup_cookie_jar_text_class_init (SoupCookieJarTextClass *text_class)
{
	SoupCookieJarClass *cookie_jar_class =
		SOUP_COOKIE_JAR_CLASS (text_class);
	GObjectClass *object_class = G_OBJECT_CLASS (text_class);

	cookie_jar_class->is_persistent = soup_cookie_jar_text_is_persistent;
	cookie_jar_class->changed       = soup_cookie_jar_text_changed;

	object_class->finalize     = soup_cookie_jar_text_finalize;
	object_class->set_property = soup_cookie_jar_text_set_property;
	object_class->get_property = soup_cookie_jar_text_get_property;

390
391
392
393
394
	/**
	 * SoupCookieJarText:filename:
	 *
	 * Cookie-storage filename.
	 */
395
        properties[PROP_FILENAME] =
Patrick Griffis's avatar
Patrick Griffis committed
396
		g_param_spec_string ("filename",
397
398
399
				     "Filename",
				     "Cookie-storage filename",
				     NULL,
400
				     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
401
402
403
				     G_PARAM_STATIC_STRINGS);

        g_object_class_install_properties (object_class, LAST_PROPERTY, properties);
404
}