1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright (C) 2008 Red Hat, Inc.
15 #include "soup-cookie.h"
16 #include "soup-cookie-jar.h"
17 #include "soup-date.h"
18 #include "soup-marshal.h"
19 #include "soup-message.h"
20 #include "soup-session-feature.h"
24 * SECTION:soup-cookie-jar
25 * @short_description: Automatic cookie handling for #SoupSession
27 * A #SoupCookieJar stores #SoupCookie<!-- -->s and arrange for them
28 * to be sent with the appropriate #SoupMessage<!-- -->s.
29 * #SoupCookieJar implements #SoupSessionFeature, so you can add a
30 * cookie jar to a session with soup_session_add_feature() or
31 * soup_session_add_feature_by_type().
33 * Note that the base #SoupCookieJar class does not support any form
34 * of long-term cookie persistence.
37 static void soup_cookie_jar_session_feature_init (SoupSessionFeatureInterface *feature_interface, gpointer interface_data);
38 static void request_queued (SoupSessionFeature *feature, SoupSession *session,
40 static void request_started (SoupSessionFeature *feature, SoupSession *session,
41 SoupMessage *msg, SoupSocket *socket);
42 static void request_unqueued (SoupSessionFeature *feature, SoupSession *session,
45 G_DEFINE_TYPE_WITH_CODE (SoupCookieJar, soup_cookie_jar, G_TYPE_OBJECT,
46 G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE,
47 soup_cookie_jar_session_feature_init))
54 static guint signals[LAST_SIGNAL] = { 0 };
65 gboolean constructed, read_only;
67 } SoupCookieJarPrivate;
68 #define SOUP_COOKIE_JAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_COOKIE_JAR, SoupCookieJarPrivate))
70 static void set_property (GObject *object, guint prop_id,
71 const GValue *value, GParamSpec *pspec);
72 static void get_property (GObject *object, guint prop_id,
73 GValue *value, GParamSpec *pspec);
76 soup_cookie_jar_init (SoupCookieJar *jar)
78 SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
80 priv->domains = g_hash_table_new_full (g_str_hash, g_str_equal,
85 constructed (GObject *object)
87 SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (object);
89 priv->constructed = TRUE;
93 finalize (GObject *object)
95 SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (object);
99 g_hash_table_iter_init (&iter, priv->domains);
100 while (g_hash_table_iter_next (&iter, &key, &value))
101 soup_cookies_free (value);
102 g_hash_table_destroy (priv->domains);
104 G_OBJECT_CLASS (soup_cookie_jar_parent_class)->finalize (object);
108 soup_cookie_jar_class_init (SoupCookieJarClass *jar_class)
110 GObjectClass *object_class = G_OBJECT_CLASS (jar_class);
112 g_type_class_add_private (jar_class, sizeof (SoupCookieJarPrivate));
114 object_class->constructed = constructed;
115 object_class->finalize = finalize;
116 object_class->set_property = set_property;
117 object_class->get_property = get_property;
120 * SoupCookieJar::changed
121 * @jar: the #SoupCookieJar
122 * @old_cookie: the old #SoupCookie value
123 * @new_cookie: the new #SoupCookie value
125 * Emitted when @jar changes. If a cookie has been added,
126 * @new_cookie will contain the newly-added cookie and
127 * @old_cookie will be %NULL. If a cookie has been deleted,
128 * @old_cookie will contain the to-be-deleted cookie and
129 * @new_cookie will be %NULL. If a cookie has been changed,
130 * @old_cookie will contain its old value, and @new_cookie its
134 g_signal_new ("changed",
135 G_OBJECT_CLASS_TYPE (object_class),
137 G_STRUCT_OFFSET (SoupCookieJarClass, changed),
139 soup_marshal_NONE__BOXED_BOXED,
141 SOUP_TYPE_COOKIE | G_SIGNAL_TYPE_STATIC_SCOPE,
142 SOUP_TYPE_COOKIE | G_SIGNAL_TYPE_STATIC_SCOPE);
145 * SOUP_COOKIE_JAR_READ_ONLY:
147 * Alias for the #SoupCookieJar:read-only property. (Whether
148 * or not the cookie jar is read-only.)
150 g_object_class_install_property (
151 object_class, PROP_READ_ONLY,
152 g_param_spec_boolean (SOUP_COOKIE_JAR_READ_ONLY,
154 "Whether or not the cookie jar is read-only",
156 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
160 soup_cookie_jar_session_feature_init (SoupSessionFeatureInterface *feature_interface,
161 gpointer interface_data)
163 feature_interface->request_queued = request_queued;
164 feature_interface->request_started = request_started;
165 feature_interface->request_unqueued = request_unqueued;
169 set_property (GObject *object, guint prop_id,
170 const GValue *value, GParamSpec *pspec)
172 SoupCookieJarPrivate *priv =
173 SOUP_COOKIE_JAR_GET_PRIVATE (object);
177 priv->read_only = g_value_get_boolean (value);
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186 get_property (GObject *object, guint prop_id,
187 GValue *value, GParamSpec *pspec)
189 SoupCookieJarPrivate *priv =
190 SOUP_COOKIE_JAR_GET_PRIVATE (object);
194 g_value_set_boolean (value, priv->read_only);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 * soup_cookie_jar_new:
205 * Creates a new #SoupCookieJar. The base #SoupCookieJar class does
206 * not support persistent storage of cookies; use a subclass for that.
208 * Returns: a new #SoupCookieJar
213 soup_cookie_jar_new (void)
215 return g_object_new (SOUP_TYPE_COOKIE_JAR, NULL);
219 soup_cookie_jar_save (SoupCookieJar *jar)
221 /* Does nothing, obsolete */
225 soup_cookie_jar_changed (SoupCookieJar *jar,
226 SoupCookie *old, SoupCookie *new)
228 SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
230 if (priv->read_only || !priv->constructed)
233 g_signal_emit (jar, signals[CHANGED], 0, old, new);
237 * soup_cookie_jar_get_cookies:
238 * @jar: a #SoupCookieJar
240 * @for_http: whether or not the return value is being passed directly
241 * to an HTTP operation
243 * Retrieves (in Cookie-header form) the list of cookies that would
244 * be sent with a request to @uri.
246 * If @for_http is %TRUE, the return value will include cookies marked
247 * "HttpOnly" (that is, cookies that the server wishes to keep hidden
248 * from client-side scripting operations such as the JavaScript
249 * document.cookies property). Since #SoupCookieJar sets the Cookie
250 * header itself when making the actual HTTP request, you should
251 * almost certainly be setting @for_http to %FALSE if you are calling
254 * Return value: the cookies, in string form, or %NULL if there are no
260 soup_cookie_jar_get_cookies (SoupCookieJar *jar, SoupURI *uri,
263 SoupCookieJarPrivate *priv;
264 GSList *cookies, *domain_cookies;
265 char *domain, *cur, *next_domain, *result;
266 GSList *new_head, *cookies_to_remove = NULL, *p;
268 g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), NULL);
269 priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
271 /* The logic here is a little weird, but the plan is that if
272 * uri->host is "www.foo.com", we will end up looking up
273 * cookies for ".www.foo.com", "www.foo.com", ".foo.com", and
274 * ".com", in that order. (Logic stolen from Mozilla.)
277 domain = cur = g_strdup_printf (".%s", uri->host);
278 next_domain = domain + 1;
280 new_head = domain_cookies = g_hash_table_lookup (priv->domains, cur);
281 while (domain_cookies) {
282 GSList *next = domain_cookies->next;
283 SoupCookie *cookie = domain_cookies->data;
285 if (cookie->expires && soup_date_is_past (cookie->expires)) {
286 cookies_to_remove = g_slist_append (cookies_to_remove,
288 new_head = g_slist_delete_link (new_head, domain_cookies);
289 g_hash_table_insert (priv->domains,
292 } else if (soup_cookie_applies_to_uri (cookie, uri) &&
293 (for_http || !cookie->http_only))
294 cookies = g_slist_append (cookies, cookie);
296 domain_cookies = next;
300 next_domain = strchr (cur + 1, '.');
304 for (p = cookies_to_remove; p; p = p->next) {
305 SoupCookie *cookie = p->data;
307 soup_cookie_jar_changed (jar, cookie, NULL);
308 soup_cookie_free (cookie);
310 g_slist_free (cookies_to_remove);
314 result = soup_cookies_to_cookie_header (cookies);
315 g_slist_free (cookies);
322 * soup_cookie_jar_add_cookie:
323 * @jar: a #SoupCookieJar
324 * @cookie: a #SoupCookie
326 * Adds @cookie to @jar, emitting the 'changed' signal if we are modifying
327 * an existing cookie or adding a valid new cookie ('valid' means
328 * that the cookie's expire date is not in the past).
330 * @cookie will be 'stolen' by the jar, so don't free it afterwards.
335 soup_cookie_jar_add_cookie (SoupCookieJar *jar, SoupCookie *cookie)
337 SoupCookieJarPrivate *priv;
338 GSList *old_cookies, *oc, *prev = NULL;
339 SoupCookie *old_cookie;
341 g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
342 g_return_if_fail (cookie != NULL);
344 priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
345 old_cookies = g_hash_table_lookup (priv->domains, cookie->domain);
346 for (oc = old_cookies; oc; oc = oc->next) {
347 old_cookie = oc->data;
348 if (!strcmp (cookie->name, old_cookie->name) &&
349 !g_strcmp0 (cookie->path, old_cookie->path)) {
350 if (cookie->expires && soup_date_is_past (cookie->expires)) {
351 /* The new cookie has an expired date,
352 * this is the way the the server has
353 * of telling us that we have to
356 old_cookies = g_slist_delete_link (old_cookies, oc);
357 g_hash_table_insert (priv->domains,
358 g_strdup (cookie->domain),
360 soup_cookie_jar_changed (jar, old_cookie, NULL);
361 soup_cookie_free (old_cookie);
362 soup_cookie_free (cookie);
365 soup_cookie_jar_changed (jar, old_cookie, cookie);
366 soup_cookie_free (old_cookie);
374 /* The new cookie is... a new cookie */
375 if (cookie->expires && soup_date_is_past (cookie->expires)) {
376 soup_cookie_free (cookie);
381 prev = g_slist_append (prev, cookie);
383 old_cookies = g_slist_append (NULL, cookie);
384 g_hash_table_insert (priv->domains, g_strdup (cookie->domain),
388 soup_cookie_jar_changed (jar, NULL, cookie);
392 * soup_cookie_jar_set_cookie:
393 * @jar: a #SoupCookieJar
394 * @uri: the URI setting the cookie
395 * @cookie: the stringified cookie to set
397 * Adds @cookie to @jar, exactly as though it had appeared in a
398 * Set-Cookie header returned from a request to @uri.
403 soup_cookie_jar_set_cookie (SoupCookieJar *jar, SoupURI *uri,
406 SoupCookie *soup_cookie;
408 g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
409 g_return_if_fail (cookie != NULL);
411 soup_cookie = soup_cookie_parse (cookie, uri);
413 /* will steal or free soup_cookie */
414 soup_cookie_jar_add_cookie (jar, soup_cookie);
419 process_set_cookie_header (SoupMessage *msg, gpointer user_data)
421 SoupCookieJar *jar = user_data;
422 GSList *new_cookies, *nc;
424 new_cookies = soup_cookies_from_response (msg);
425 for (nc = new_cookies; nc; nc = nc->next)
426 soup_cookie_jar_add_cookie (jar, nc->data);
427 g_slist_free (new_cookies);
431 request_queued (SoupSessionFeature *feature, SoupSession *session,
434 soup_message_add_header_handler (msg, "got-headers",
436 G_CALLBACK (process_set_cookie_header),
441 request_started (SoupSessionFeature *feature, SoupSession *session,
442 SoupMessage *msg, SoupSocket *socket)
444 SoupCookieJar *jar = SOUP_COOKIE_JAR (feature);
447 cookies = soup_cookie_jar_get_cookies (jar, soup_message_get_uri (msg), TRUE);
449 soup_message_headers_replace (msg->request_headers,
453 soup_message_headers_remove (msg->request_headers, "Cookie");
457 request_unqueued (SoupSessionFeature *feature, SoupSession *session,
460 g_signal_handlers_disconnect_by_func (msg, process_set_cookie_header, feature);
464 * soup_cookie_jar_all_cookies:
465 * @jar: a #SoupCookieJar
467 * Constructs a #GSList with every cookie inside the @jar.
468 * The cookies in the list are a copy of the original, so
469 * you have to free them when you are done with them.
471 * Return value: a #GSList with all the cookies in the @jar.
476 soup_cookie_jar_all_cookies (SoupCookieJar *jar)
478 SoupCookieJarPrivate *priv;
483 g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), NULL);
485 priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
487 g_hash_table_iter_init (&iter, priv->domains);
489 while (g_hash_table_iter_next (&iter, &key, &value)) {
490 GSList *p, *cookies = value;
491 for (p = cookies; p; p = p->next)
492 l = g_slist_prepend (l, soup_cookie_copy (p->data));
499 * soup_cookie_jar_delete_cookie:
500 * @jar: a #SoupCookieJar
501 * @cookie: a #SoupCookie
503 * Deletes @cookie from @jar, emitting the 'changed' signal.
508 soup_cookie_jar_delete_cookie (SoupCookieJar *jar,
511 SoupCookieJarPrivate *priv;
515 g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
516 g_return_if_fail (cookie != NULL);
518 priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
520 domain = g_strdup (cookie->domain);
522 cookies = g_hash_table_lookup (priv->domains, domain);
526 for (p = cookies; p; p = p->next ) {
527 SoupCookie *c = (SoupCookie*)p->data;
528 if (soup_cookie_equal (cookie, c)) {
529 cookies = g_slist_delete_link (cookies, p);
530 g_hash_table_insert (priv->domains,
533 soup_cookie_jar_changed (jar, c, NULL);
534 soup_cookie_free (c);