Fix some incorrect "Since" tags in SoupCookieJar docs.
[platform/upstream/libsoup.git] / libsoup / soup-cookie-jar.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-cookie-jar.c
4  *
5  * Copyright (C) 2008 Red Hat, Inc.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include <string.h>
14
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"
21 #include "soup-uri.h"
22
23 /**
24  * SECTION:soup-cookie-jar
25  * @short_description: Automatic cookie handling for #SoupSession
26  *
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().
32  *
33  * Note that the base #SoupCookieJar class does not support any form
34  * of long-term cookie persistence.
35  **/
36
37 static void soup_cookie_jar_session_feature_init (SoupSessionFeatureInterface *feature_interface, gpointer interface_data);
38 static void request_queued (SoupSessionFeature *feature, SoupSession *session,
39                             SoupMessage *msg);
40 static void request_started (SoupSessionFeature *feature, SoupSession *session,
41                              SoupMessage *msg, SoupSocket *socket);
42 static void request_unqueued (SoupSessionFeature *feature, SoupSession *session,
43                               SoupMessage *msg);
44
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))
48
49 enum {
50         CHANGED,
51         LAST_SIGNAL
52 };
53
54 static guint signals[LAST_SIGNAL] = { 0 };
55
56 enum {
57         PROP_0,
58
59         PROP_READ_ONLY,
60
61         LAST_PROP
62 };
63
64 typedef struct {
65         gboolean constructed, read_only;
66         GHashTable *domains;
67 } SoupCookieJarPrivate;
68 #define SOUP_COOKIE_JAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_COOKIE_JAR, SoupCookieJarPrivate))
69
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);
74
75 static void
76 soup_cookie_jar_init (SoupCookieJar *jar)
77 {
78         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
79
80         priv->domains = g_hash_table_new_full (g_str_hash, g_str_equal,
81                                                g_free, NULL);
82 }
83
84 static void
85 constructed (GObject *object)
86 {
87         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (object);
88
89         priv->constructed = TRUE;
90 }
91
92 static void
93 finalize (GObject *object)
94 {
95         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (object);
96         GHashTableIter iter;
97         gpointer key, value;
98
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);
103
104         G_OBJECT_CLASS (soup_cookie_jar_parent_class)->finalize (object);
105 }
106
107 static void
108 soup_cookie_jar_class_init (SoupCookieJarClass *jar_class)
109 {
110         GObjectClass *object_class = G_OBJECT_CLASS (jar_class);
111
112         g_type_class_add_private (jar_class, sizeof (SoupCookieJarPrivate));
113
114         object_class->constructed = constructed;
115         object_class->finalize = finalize;
116         object_class->set_property = set_property;
117         object_class->get_property = get_property;
118
119         /**
120          * SoupCookieJar::changed
121          * @jar: the #SoupCookieJar
122          * @old_cookie: the old #SoupCookie value
123          * @new_cookie: the new #SoupCookie value
124          *
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
131          * new value.
132          **/
133         signals[CHANGED] =
134                 g_signal_new ("changed",
135                               G_OBJECT_CLASS_TYPE (object_class),
136                               G_SIGNAL_RUN_FIRST,
137                               G_STRUCT_OFFSET (SoupCookieJarClass, changed),
138                               NULL, NULL,
139                               soup_marshal_NONE__BOXED_BOXED,
140                               G_TYPE_NONE, 2, 
141                               SOUP_TYPE_COOKIE | G_SIGNAL_TYPE_STATIC_SCOPE,
142                               SOUP_TYPE_COOKIE | G_SIGNAL_TYPE_STATIC_SCOPE);
143
144         /**
145          * SOUP_COOKIE_JAR_READ_ONLY:
146          *
147          * Alias for the #SoupCookieJar:read-only property. (Whether
148          * or not the cookie jar is read-only.)
149          **/
150         g_object_class_install_property (
151                 object_class, PROP_READ_ONLY,
152                 g_param_spec_boolean (SOUP_COOKIE_JAR_READ_ONLY,
153                                       "Read-only",
154                                       "Whether or not the cookie jar is read-only",
155                                       FALSE,
156                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
157 }
158
159 static void
160 soup_cookie_jar_session_feature_init (SoupSessionFeatureInterface *feature_interface,
161                                       gpointer interface_data)
162 {
163         feature_interface->request_queued = request_queued;
164         feature_interface->request_started = request_started;
165         feature_interface->request_unqueued = request_unqueued;
166 }
167
168 static void
169 set_property (GObject *object, guint prop_id,
170               const GValue *value, GParamSpec *pspec)
171 {
172         SoupCookieJarPrivate *priv =
173                 SOUP_COOKIE_JAR_GET_PRIVATE (object);
174
175         switch (prop_id) {
176         case PROP_READ_ONLY:
177                 priv->read_only = g_value_get_boolean (value);
178                 break;
179         default:
180                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181                 break;
182         }
183 }
184
185 static void
186 get_property (GObject *object, guint prop_id,
187               GValue *value, GParamSpec *pspec)
188 {
189         SoupCookieJarPrivate *priv =
190                 SOUP_COOKIE_JAR_GET_PRIVATE (object);
191
192         switch (prop_id) {
193         case PROP_READ_ONLY:
194                 g_value_set_boolean (value, priv->read_only);
195                 break;
196         default:
197                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198                 break;
199         }
200 }
201
202 /**
203  * soup_cookie_jar_new:
204  *
205  * Creates a new #SoupCookieJar. The base #SoupCookieJar class does
206  * not support persistent storage of cookies; use a subclass for that.
207  *
208  * Returns: a new #SoupCookieJar
209  *
210  * Since: 2.24
211  **/
212 SoupCookieJar *
213 soup_cookie_jar_new (void) 
214 {
215         return g_object_new (SOUP_TYPE_COOKIE_JAR, NULL);
216 }
217
218 void
219 soup_cookie_jar_save (SoupCookieJar *jar)
220 {
221         /* Does nothing, obsolete */
222 }
223
224 static void
225 soup_cookie_jar_changed (SoupCookieJar *jar,
226                          SoupCookie *old, SoupCookie *new)
227 {
228         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
229
230         if (priv->read_only || !priv->constructed)
231                 return;
232
233         g_signal_emit (jar, signals[CHANGED], 0, old, new);
234 }
235
236 /**
237  * soup_cookie_jar_get_cookies:
238  * @jar: a #SoupCookieJar
239  * @uri: a #SoupURI
240  * @for_http: whether or not the return value is being passed directly
241  * to an HTTP operation
242  *
243  * Retrieves (in Cookie-header form) the list of cookies that would
244  * be sent with a request to @uri.
245  *
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
252  * this.
253  *
254  * Return value: the cookies, in string form, or %NULL if there are no
255  * cookies for @uri.
256  *
257  * Since: 2.24
258  **/
259 char *
260 soup_cookie_jar_get_cookies (SoupCookieJar *jar, SoupURI *uri,
261                              gboolean for_http)
262 {
263         SoupCookieJarPrivate *priv;
264         GSList *cookies, *domain_cookies;
265         char *domain, *cur, *next_domain, *result;
266         GSList *new_head, *cookies_to_remove = NULL, *p;
267
268         g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), NULL);
269         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
270
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.)
275          */
276         cookies = NULL;
277         domain = cur = g_strdup_printf (".%s", uri->host);
278         next_domain = domain + 1;
279         do {
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;
284
285                         if (cookie->expires && soup_date_is_past (cookie->expires)) {
286                                 cookies_to_remove = g_slist_append (cookies_to_remove,
287                                                                     cookie);
288                                 new_head = g_slist_delete_link (new_head, domain_cookies);
289                                 g_hash_table_insert (priv->domains,
290                                                      g_strdup (cur),
291                                                      new_head);
292                         } else if (soup_cookie_applies_to_uri (cookie, uri) &&
293                                    (for_http || !cookie->http_only))
294                                 cookies = g_slist_append (cookies, cookie);
295
296                         domain_cookies = next;
297                 }
298                 cur = next_domain;
299                 if (cur)
300                         next_domain = strchr (cur + 1, '.');
301         } while (cur);
302         g_free (domain);
303
304         for (p = cookies_to_remove; p; p = p->next) {
305                 SoupCookie *cookie = p->data;
306
307                 soup_cookie_jar_changed (jar, cookie, NULL);
308                 soup_cookie_free (cookie);
309         }
310         g_slist_free (cookies_to_remove);
311
312         if (cookies) {
313                 /* FIXME: sort? */
314                 result = soup_cookies_to_cookie_header (cookies);
315                 g_slist_free (cookies);
316                 return result;
317         } else
318                 return NULL;
319 }
320
321 /**
322  * soup_cookie_jar_add_cookie:
323  * @jar: a #SoupCookieJar
324  * @cookie: a #SoupCookie
325  *
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).
329  *
330  * @cookie will be 'stolen' by the jar, so don't free it afterwards.
331  *
332  * Since: 2.26
333  **/
334 void
335 soup_cookie_jar_add_cookie (SoupCookieJar *jar, SoupCookie *cookie)
336 {
337         SoupCookieJarPrivate *priv;
338         GSList *old_cookies, *oc, *prev = NULL;
339         SoupCookie *old_cookie;
340
341         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
342         g_return_if_fail (cookie != NULL);
343
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
354                                  * remove the cookie.
355                                  */
356                                 old_cookies = g_slist_delete_link (old_cookies, oc);
357                                 g_hash_table_insert (priv->domains,
358                                                      g_strdup (cookie->domain),
359                                                      old_cookies);
360                                 soup_cookie_jar_changed (jar, old_cookie, NULL);
361                                 soup_cookie_free (old_cookie);
362                                 soup_cookie_free (cookie);
363                         } else {
364                                 oc->data = cookie;
365                                 soup_cookie_jar_changed (jar, old_cookie, cookie);
366                                 soup_cookie_free (old_cookie);
367                         }
368
369                         return;
370                 }
371                 prev = oc;
372         }
373
374         /* The new cookie is... a new cookie */
375         if (cookie->expires && soup_date_is_past (cookie->expires)) {
376                 soup_cookie_free (cookie);
377                 return;
378         }
379
380         if (prev)
381                 prev = g_slist_append (prev, cookie);
382         else {
383                 old_cookies = g_slist_append (NULL, cookie);
384                 g_hash_table_insert (priv->domains, g_strdup (cookie->domain),
385                                      old_cookies);
386         }
387
388         soup_cookie_jar_changed (jar, NULL, cookie);
389 }
390
391 /**
392  * soup_cookie_jar_set_cookie:
393  * @jar: a #SoupCookieJar
394  * @uri: the URI setting the cookie
395  * @cookie: the stringified cookie to set
396  *
397  * Adds @cookie to @jar, exactly as though it had appeared in a
398  * Set-Cookie header returned from a request to @uri.
399  *
400  * Since: 2.24
401  **/
402 void
403 soup_cookie_jar_set_cookie (SoupCookieJar *jar, SoupURI *uri,
404                             const char *cookie)
405 {
406         SoupCookie *soup_cookie;
407
408         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
409         g_return_if_fail (cookie != NULL);
410
411         soup_cookie = soup_cookie_parse (cookie, uri);
412         if (soup_cookie) {
413                 /* will steal or free soup_cookie */
414                 soup_cookie_jar_add_cookie (jar, soup_cookie);
415         }
416 }
417
418 static void
419 process_set_cookie_header (SoupMessage *msg, gpointer user_data)
420 {
421         SoupCookieJar *jar = user_data;
422         GSList *new_cookies, *nc;
423
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);
428 }
429
430 static void
431 request_queued (SoupSessionFeature *feature, SoupSession *session,
432                 SoupMessage *msg)
433 {
434         soup_message_add_header_handler (msg, "got-headers",
435                                          "Set-Cookie",
436                                          G_CALLBACK (process_set_cookie_header),
437                                          feature);
438 }
439
440 static void
441 request_started (SoupSessionFeature *feature, SoupSession *session,
442                  SoupMessage *msg, SoupSocket *socket)
443 {
444         SoupCookieJar *jar = SOUP_COOKIE_JAR (feature);
445         char *cookies;
446
447         cookies = soup_cookie_jar_get_cookies (jar, soup_message_get_uri (msg), TRUE);
448         if (cookies) {
449                 soup_message_headers_replace (msg->request_headers,
450                                               "Cookie", cookies);
451                 g_free (cookies);
452         } else
453                 soup_message_headers_remove (msg->request_headers, "Cookie");
454 }
455
456 static void
457 request_unqueued (SoupSessionFeature *feature, SoupSession *session,
458                   SoupMessage *msg)
459 {
460         g_signal_handlers_disconnect_by_func (msg, process_set_cookie_header, feature);
461 }
462
463 /**
464  * soup_cookie_jar_all_cookies:
465  * @jar: a #SoupCookieJar
466  *
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.
470  *
471  * Return value: a #GSList with all the cookies in the @jar.
472  *
473  * Since: 2.26
474  **/
475 GSList *
476 soup_cookie_jar_all_cookies (SoupCookieJar *jar)
477 {
478         SoupCookieJarPrivate *priv;
479         GHashTableIter iter;
480         GSList *l = NULL;
481         gpointer key, value;
482
483         g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), NULL);
484
485         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
486
487         g_hash_table_iter_init (&iter, priv->domains);
488
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));
493         }
494
495         return l;
496 }
497
498 /**
499  * soup_cookie_jar_delete_cookie:
500  * @jar: a #SoupCookieJar
501  * @cookie: a #SoupCookie
502  *
503  * Deletes @cookie from @jar, emitting the 'changed' signal.
504  *
505  * Since: 2.26
506  **/
507 void
508 soup_cookie_jar_delete_cookie (SoupCookieJar *jar,
509                                SoupCookie    *cookie)
510 {
511         SoupCookieJarPrivate *priv;
512         GSList *cookies, *p;
513         char *domain;
514
515         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
516         g_return_if_fail (cookie != NULL);
517
518         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
519
520         domain = g_strdup (cookie->domain);
521
522         cookies = g_hash_table_lookup (priv->domains, domain);
523         if (cookies == NULL)
524                 return;
525
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,
531                                              domain,
532                                              cookies);
533                         soup_cookie_jar_changed (jar, c, NULL);
534                         soup_cookie_free (c);
535                         return;
536                 }
537         }
538 }