cookie-jar: bail if hostname is an empty string (CVE-2018-12910)
[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 <string.h>
13
14 #include "soup-cookie-jar.h"
15 #include "soup.h"
16 #include "TIZEN.h"
17
18 /**
19  * SECTION:soup-cookie-jar
20  * @short_description: Automatic cookie handling for SoupSession
21  *
22  * A #SoupCookieJar stores #SoupCookie<!-- -->s and arrange for them
23  * to be sent with the appropriate #SoupMessage<!-- -->s.
24  * #SoupCookieJar implements #SoupSessionFeature, so you can add a
25  * cookie jar to a session with soup_session_add_feature() or
26  * soup_session_add_feature_by_type().
27  *
28  * Note that the base #SoupCookieJar class does not support any form
29  * of long-term cookie persistence.
30  **/
31
32 static void soup_cookie_jar_session_feature_init (SoupSessionFeatureInterface *feature_interface, gpointer interface_data);
33
34 G_DEFINE_TYPE_WITH_CODE (SoupCookieJar, soup_cookie_jar, G_TYPE_OBJECT,
35                          G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE,
36                                                 soup_cookie_jar_session_feature_init))
37
38 enum {
39         CHANGED,
40         LAST_SIGNAL
41 };
42
43 static guint signals[LAST_SIGNAL] = { 0 };
44
45 enum {
46         PROP_0,
47
48         PROP_READ_ONLY,
49         PROP_ACCEPT_POLICY,
50
51         LAST_PROP
52 };
53
54 typedef struct {
55         gboolean constructed, read_only;
56         GHashTable *domains, *serials;
57         guint serial;
58         SoupCookieJarAcceptPolicy accept_policy;
59 } SoupCookieJarPrivate;
60 #define SOUP_COOKIE_JAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_COOKIE_JAR, SoupCookieJarPrivate))
61
62 static void
63 soup_cookie_jar_init (SoupCookieJar *jar)
64 {
65         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
66
67         priv->domains = g_hash_table_new_full (soup_str_case_hash,
68                                                soup_str_case_equal,
69                                                g_free, NULL);
70         priv->serials = g_hash_table_new (NULL, NULL);
71         priv->accept_policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
72 }
73
74 static void
75 soup_cookie_jar_constructed (GObject *object)
76 {
77         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (object);
78
79         priv->constructed = TRUE;
80 }
81
82 static void
83 soup_cookie_jar_finalize (GObject *object)
84 {
85         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (object);
86         GHashTableIter iter;
87         gpointer key, value;
88
89         g_hash_table_iter_init (&iter, priv->domains);
90         while (g_hash_table_iter_next (&iter, &key, &value))
91                 soup_cookies_free (value);
92         g_hash_table_destroy (priv->domains);
93         g_hash_table_destroy (priv->serials);
94
95         G_OBJECT_CLASS (soup_cookie_jar_parent_class)->finalize (object);
96 }
97
98 static void
99 soup_cookie_jar_set_property (GObject *object, guint prop_id,
100                               const GValue *value, GParamSpec *pspec)
101 {
102         SoupCookieJarPrivate *priv =
103                 SOUP_COOKIE_JAR_GET_PRIVATE (object);
104
105         switch (prop_id) {
106         case PROP_READ_ONLY:
107                 priv->read_only = g_value_get_boolean (value);
108                 break;
109         case PROP_ACCEPT_POLICY:
110                 priv->accept_policy = g_value_get_enum (value);
111                 break;
112         default:
113                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114                 break;
115         }
116 }
117
118 static void
119 soup_cookie_jar_get_property (GObject *object, guint prop_id,
120                               GValue *value, GParamSpec *pspec)
121 {
122         SoupCookieJarPrivate *priv =
123                 SOUP_COOKIE_JAR_GET_PRIVATE (object);
124
125         switch (prop_id) {
126         case PROP_READ_ONLY:
127                 g_value_set_boolean (value, priv->read_only);
128                 break;
129         case PROP_ACCEPT_POLICY:
130                 g_value_set_enum (value, priv->accept_policy);
131                 break;
132         default:
133                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134                 break;
135         }
136 }
137
138 static gboolean
139 soup_cookie_jar_real_is_persistent (SoupCookieJar *jar)
140 {
141         return FALSE;
142 }
143
144 static void
145 soup_cookie_jar_class_init (SoupCookieJarClass *jar_class)
146 {
147         GObjectClass *object_class = G_OBJECT_CLASS (jar_class);
148
149         g_type_class_add_private (jar_class, sizeof (SoupCookieJarPrivate));
150
151         object_class->constructed = soup_cookie_jar_constructed;
152         object_class->finalize = soup_cookie_jar_finalize;
153         object_class->set_property = soup_cookie_jar_set_property;
154         object_class->get_property = soup_cookie_jar_get_property;
155
156         jar_class->is_persistent = soup_cookie_jar_real_is_persistent;
157
158         /**
159          * SoupCookieJar::changed:
160          * @jar: the #SoupCookieJar
161          * @old_cookie: the old #SoupCookie value
162          * @new_cookie: the new #SoupCookie value
163          *
164          * Emitted when @jar changes. If a cookie has been added,
165          * @new_cookie will contain the newly-added cookie and
166          * @old_cookie will be %NULL. If a cookie has been deleted,
167          * @old_cookie will contain the to-be-deleted cookie and
168          * @new_cookie will be %NULL. If a cookie has been changed,
169          * @old_cookie will contain its old value, and @new_cookie its
170          * new value.
171          **/
172         signals[CHANGED] =
173                 g_signal_new ("changed",
174                               G_OBJECT_CLASS_TYPE (object_class),
175                               G_SIGNAL_RUN_FIRST,
176                               G_STRUCT_OFFSET (SoupCookieJarClass, changed),
177                               NULL, NULL,
178                               NULL,
179                               G_TYPE_NONE, 2, 
180                               SOUP_TYPE_COOKIE | G_SIGNAL_TYPE_STATIC_SCOPE,
181                               SOUP_TYPE_COOKIE | G_SIGNAL_TYPE_STATIC_SCOPE);
182
183         /**
184          * SOUP_COOKIE_JAR_READ_ONLY:
185          *
186          * Alias for the #SoupCookieJar:read-only property. (Whether
187          * or not the cookie jar is read-only.)
188          **/
189         g_object_class_install_property (
190                 object_class, PROP_READ_ONLY,
191                 g_param_spec_boolean (SOUP_COOKIE_JAR_READ_ONLY,
192                                       "Read-only",
193                                       "Whether or not the cookie jar is read-only",
194                                       FALSE,
195                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
196
197         /**
198          * SOUP_COOKIE_JAR_ACCEPT_POLICY:
199          *
200          * Alias for the #SoupCookieJar:accept-policy property.
201          *
202          * Since: 2.30
203          */
204         /**
205          * SoupCookieJar:accept-policy:
206          *
207          * The policy the jar should follow to accept or reject cookies
208          *
209          * Since: 2.30
210          */
211         g_object_class_install_property (
212                 object_class, PROP_ACCEPT_POLICY,
213                 g_param_spec_enum (SOUP_COOKIE_JAR_ACCEPT_POLICY,
214                                    "Accept-policy",
215                                    "The policy the jar should follow to accept or reject cookies",
216                                    SOUP_TYPE_COOKIE_JAR_ACCEPT_POLICY,
217                                    SOUP_COOKIE_JAR_ACCEPT_ALWAYS,
218                                    G_PARAM_READWRITE));
219 }
220
221 /**
222  * soup_cookie_jar_new:
223  *
224  * Creates a new #SoupCookieJar. The base #SoupCookieJar class does
225  * not support persistent storage of cookies; use a subclass for that.
226  *
227  * Returns: a new #SoupCookieJar
228  *
229  * Since: 2.24
230  **/
231 SoupCookieJar *
232 soup_cookie_jar_new (void) 
233 {
234         return g_object_new (SOUP_TYPE_COOKIE_JAR, NULL);
235 }
236
237 /**
238  * soup_cookie_jar_save:
239  * @jar: a #SoupCookieJar
240  *
241  * This function exists for backward compatibility, but does not do
242  * anything any more; cookie jars are saved automatically when they
243  * are changed.
244  *
245  * Since: 2.24
246  *
247  * Deprecated: This is a no-op.
248  */
249 void
250 soup_cookie_jar_save (SoupCookieJar *jar)
251 {
252         /* Does nothing, obsolete */
253 }
254
255 static void
256 soup_cookie_jar_changed (SoupCookieJar *jar,
257                          SoupCookie *old, SoupCookie *new)
258 {
259         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
260
261         if (old && old != new)
262                 g_hash_table_remove (priv->serials, old);
263         if (new) {
264                 priv->serial++;
265                 g_hash_table_insert (priv->serials, new, GUINT_TO_POINTER (priv->serial));
266         }
267
268         if (priv->read_only || !priv->constructed)
269                 return;
270
271         g_signal_emit (jar, signals[CHANGED], 0, old, new);
272 }
273
274 static int
275 compare_cookies (gconstpointer a, gconstpointer b, gpointer jar)
276 {
277         SoupCookie *ca = (SoupCookie *)a;
278         SoupCookie *cb = (SoupCookie *)b;
279         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
280         int alen, blen;
281         guint aserial, bserial;
282
283         /* "Cookies with longer path fields are listed before cookies
284          * with shorter path field."
285          */
286         alen = ca->path ? strlen (ca->path) : 0;
287         blen = cb->path ? strlen (cb->path) : 0;
288         if (alen != blen)
289                 return blen - alen;
290
291         /* "Among cookies that have equal length path fields, cookies
292          * with earlier creation dates are listed before cookies with
293          * later creation dates."
294          */
295         aserial = GPOINTER_TO_UINT (g_hash_table_lookup (priv->serials, ca));
296         bserial = GPOINTER_TO_UINT (g_hash_table_lookup (priv->serials, cb));
297         return aserial - bserial;
298 }
299
300 static GSList *
301 get_cookies (SoupCookieJar *jar, SoupURI *uri, gboolean for_http, gboolean copy_cookies)
302 {
303         SoupCookieJarPrivate *priv;
304         GSList *cookies, *domain_cookies;
305         char *domain, *cur, *next_domain;
306         GSList *new_head, *cookies_to_remove = NULL, *p;
307
308         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
309
310         if (!uri->host || !uri->host[0])
311                 return NULL;
312
313         /* The logic here is a little weird, but the plan is that if
314          * uri->host is "www.foo.com", we will end up looking up
315          * cookies for ".www.foo.com", "www.foo.com", ".foo.com", and
316          * ".com", in that order. (Logic stolen from Mozilla.)
317          */
318         cookies = NULL;
319         domain = cur = g_strdup_printf (".%s", uri->host);
320         next_domain = domain + 1;
321         do {
322                 new_head = domain_cookies = g_hash_table_lookup (priv->domains, cur);
323                 while (domain_cookies) {
324                         GSList *next = domain_cookies->next;
325                         SoupCookie *cookie = domain_cookies->data;
326
327                         if (cookie->expires && soup_date_is_past (cookie->expires)) {
328                                 cookies_to_remove = g_slist_append (cookies_to_remove,
329                                                                     cookie);
330                                 new_head = g_slist_delete_link (new_head, domain_cookies);
331                                 g_hash_table_insert (priv->domains,
332                                                      g_strdup (cur),
333                                                      new_head);
334                         } else if (soup_cookie_applies_to_uri (cookie, uri) &&
335                                    (for_http || !cookie->http_only))
336                                 cookies = g_slist_append (cookies, copy_cookies ? soup_cookie_copy (cookie) : cookie);
337
338                         domain_cookies = next;
339                 }
340                 cur = next_domain;
341
342                 if (cur && *cur)
343                         next_domain = strchr (cur + 1, '.');
344                 else
345                         next_domain = NULL;
346
347         } while (cur);
348         g_free (domain);
349
350         for (p = cookies_to_remove; p; p = p->next) {
351                 SoupCookie *cookie = p->data;
352
353                 soup_cookie_jar_changed (jar, cookie, NULL);
354                 soup_cookie_free (cookie);
355         }
356         g_slist_free (cookies_to_remove);
357
358         return g_slist_sort_with_data (cookies, compare_cookies, jar);
359 }
360
361 /**
362  * soup_cookie_jar_get_cookies:
363  * @jar: a #SoupCookieJar
364  * @uri: a #SoupURI
365  * @for_http: whether or not the return value is being passed directly
366  * to an HTTP operation
367  *
368  * Retrieves (in Cookie-header form) the list of cookies that would
369  * be sent with a request to @uri.
370  *
371  * If @for_http is %TRUE, the return value will include cookies marked
372  * "HttpOnly" (that is, cookies that the server wishes to keep hidden
373  * from client-side scripting operations such as the JavaScript
374  * document.cookies property). Since #SoupCookieJar sets the Cookie
375  * header itself when making the actual HTTP request, you should
376  * almost certainly be setting @for_http to %FALSE if you are calling
377  * this.
378  *
379  * Return value: the cookies, in string form, or %NULL if there are no
380  * cookies for @uri.
381  *
382  * Since: 2.24
383  **/
384 char *
385 soup_cookie_jar_get_cookies (SoupCookieJar *jar, SoupURI *uri,
386                              gboolean for_http)
387 {
388         GSList *cookies;
389
390         g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), NULL);
391         g_return_val_if_fail (uri != NULL, NULL);
392
393         cookies = get_cookies (jar, uri, for_http, FALSE);
394
395         if (cookies) {
396                 char *result = soup_cookies_to_cookie_header (cookies);
397                 g_slist_free (cookies);
398
399                 if (!*result) {
400                         g_free (result);
401                         result = NULL;
402                 }
403                 return result;
404         } else
405                 return NULL;
406 }
407
408 /**
409  * soup_cookie_jar_get_cookie_list:
410  * @jar: a #SoupCookieJar
411  * @uri: a #SoupURI
412  * @for_http: whether or not the return value is being passed directly
413  * to an HTTP operation
414  *
415  * Retrieves the list of cookies that would be sent with a request to @uri
416  * as a #GSList of #SoupCookie objects.
417  *
418  * If @for_http is %TRUE, the return value will include cookies marked
419  * "HttpOnly" (that is, cookies that the server wishes to keep hidden
420  * from client-side scripting operations such as the JavaScript
421  * document.cookies property). Since #SoupCookieJar sets the Cookie
422  * header itself when making the actual HTTP request, you should
423  * almost certainly be setting @for_http to %FALSE if you are calling
424  * this.
425  *
426  * Return value: (transfer full) (element-type Soup.Cookie): a #GSList
427  * with the cookies in the @jar that would be sent with a request to @uri.
428  *
429  * Since: 2.40
430  **/
431 GSList *
432 soup_cookie_jar_get_cookie_list (SoupCookieJar *jar, SoupURI *uri, gboolean for_http)
433 {
434         g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), NULL);
435         g_return_val_if_fail (uri != NULL, NULL);
436
437         return get_cookies (jar, uri, for_http, TRUE);
438 }
439
440 /**
441  * soup_cookie_jar_add_cookie:
442  * @jar: a #SoupCookieJar
443  * @cookie: (transfer full): a #SoupCookie
444  *
445  * Adds @cookie to @jar, emitting the 'changed' signal if we are modifying
446  * an existing cookie or adding a valid new cookie ('valid' means
447  * that the cookie's expire date is not in the past).
448  *
449  * @cookie will be 'stolen' by the jar, so don't free it afterwards.
450  *
451  * Since: 2.26
452  **/
453 void
454 soup_cookie_jar_add_cookie (SoupCookieJar *jar, SoupCookie *cookie)
455 {
456         SoupCookieJarPrivate *priv;
457         GSList *old_cookies, *oc, *last = NULL;
458         SoupCookie *old_cookie;
459
460         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
461         g_return_if_fail (cookie != NULL);
462
463         /* Never accept cookies for public domains. */
464         if (!g_hostname_is_ip_address (cookie->domain) &&
465             soup_tld_domain_is_public_suffix (cookie->domain)) {
466                 soup_cookie_free (cookie);
467                 return;
468         }
469
470         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
471         old_cookies = g_hash_table_lookup (priv->domains, cookie->domain);
472         for (oc = old_cookies; oc; oc = oc->next) {
473                 old_cookie = oc->data;
474                 if (!strcmp (cookie->name, old_cookie->name) &&
475                     !g_strcmp0 (cookie->path, old_cookie->path)) {
476                         if (cookie->expires && soup_date_is_past (cookie->expires)) {
477                                 /* The new cookie has an expired date,
478                                  * this is the way the the server has
479                                  * of telling us that we have to
480                                  * remove the cookie.
481                                  */
482                                 old_cookies = g_slist_delete_link (old_cookies, oc);
483                                 g_hash_table_insert (priv->domains,
484                                                      g_strdup (cookie->domain),
485                                                      old_cookies);
486                                 soup_cookie_jar_changed (jar, old_cookie, NULL);
487                                 soup_cookie_free (old_cookie);
488                                 soup_cookie_free (cookie);
489                         } else {
490                                 oc->data = cookie;
491                                 soup_cookie_jar_changed (jar, old_cookie, cookie);
492                                 soup_cookie_free (old_cookie);
493                         }
494
495                         return;
496                 }
497                 last = oc;
498         }
499
500         /* The new cookie is... a new cookie */
501         if (cookie->expires && soup_date_is_past (cookie->expires)) {
502                 soup_cookie_free (cookie);
503                 return;
504         }
505
506         if (last)
507                 last->next = g_slist_append (NULL, cookie);
508         else {
509                 old_cookies = g_slist_append (NULL, cookie);
510                 g_hash_table_insert (priv->domains, g_strdup (cookie->domain),
511                                      old_cookies);
512         }
513
514         soup_cookie_jar_changed (jar, NULL, cookie);
515 }
516
517 /**
518  * soup_cookie_jar_add_cookie_with_first_party:
519  * @jar: a #SoupCookieJar
520  * @first_party: the URI for the main document
521  * @cookie: (transfer full): a #SoupCookie
522  *
523  * Adds @cookie to @jar, emitting the 'changed' signal if we are modifying
524  * an existing cookie or adding a valid new cookie ('valid' means
525  * that the cookie's expire date is not in the past).
526  *
527  * @first_party will be used to reject cookies coming from third party
528  * resources in case such a security policy is set in the @jar.
529  *
530  * @cookie will be 'stolen' by the jar, so don't free it afterwards.
531  *
532  * Since: 2.40
533  **/
534 void
535 soup_cookie_jar_add_cookie_with_first_party (SoupCookieJar *jar, SoupURI *first_party, SoupCookie *cookie)
536 {
537         SoupCookieJarPrivate *priv;
538
539         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
540         g_return_if_fail (first_party != NULL);
541         g_return_if_fail (cookie != NULL);
542
543         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
544         if (priv->accept_policy == SOUP_COOKIE_JAR_ACCEPT_NEVER) {
545                 soup_cookie_free (cookie);
546                 return;
547         }
548
549         if (priv->accept_policy == SOUP_COOKIE_JAR_ACCEPT_ALWAYS ||
550             soup_cookie_domain_matches (cookie, first_party->host)) {
551                 /* will steal or free soup_cookie */
552                 soup_cookie_jar_add_cookie (jar, cookie);
553         } else {
554                 soup_cookie_free (cookie);
555         }
556 }
557
558 /**
559  * soup_cookie_jar_set_cookie:
560  * @jar: a #SoupCookieJar
561  * @uri: the URI setting the cookie
562  * @cookie: the stringified cookie to set
563  *
564  * Adds @cookie to @jar, exactly as though it had appeared in a
565  * Set-Cookie header returned from a request to @uri.
566  *
567  * Keep in mind that if the #SoupCookieJarAcceptPolicy
568  * %SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY is set you'll need to use
569  * soup_cookie_jar_set_cookie_with_first_party(), otherwise the jar
570  * will have no way of knowing if the cookie is being set by a third
571  * party or not.
572  *
573  * Since: 2.24
574  **/
575 void
576 soup_cookie_jar_set_cookie (SoupCookieJar *jar, SoupURI *uri,
577                             const char *cookie)
578 {
579         SoupCookie *soup_cookie;
580         SoupCookieJarPrivate *priv;
581
582         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
583         g_return_if_fail (uri != NULL);
584         g_return_if_fail (cookie != NULL);
585
586         if (!uri->host)
587                 return;
588
589         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
590         if (priv->accept_policy == SOUP_COOKIE_JAR_ACCEPT_NEVER)
591                 return;
592
593         g_return_if_fail (priv->accept_policy != SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY);
594
595         soup_cookie = soup_cookie_parse (cookie, uri);
596         if (soup_cookie) {
597                 /* will steal or free soup_cookie */
598                 soup_cookie_jar_add_cookie (jar, soup_cookie);
599         }
600 }
601
602 /**
603  * soup_cookie_jar_set_cookie_with_first_party:
604  * @jar: a #SoupCookieJar
605  * @uri: the URI setting the cookie
606  * @first_party: the URI for the main document
607  * @cookie: the stringified cookie to set
608  *
609  * Adds @cookie to @jar, exactly as though it had appeared in a
610  * Set-Cookie header returned from a request to @uri. @first_party
611  * will be used to reject cookies coming from third party resources in
612  * case such a security policy is set in the @jar.
613  *
614  * Since: 2.30
615  **/
616 void
617 soup_cookie_jar_set_cookie_with_first_party (SoupCookieJar *jar,
618                                              SoupURI *uri,
619                                              SoupURI *first_party,
620                                              const char *cookie)
621 {
622         SoupCookie *soup_cookie;
623
624         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
625         g_return_if_fail (uri != NULL);
626         g_return_if_fail (first_party != NULL);
627         g_return_if_fail (cookie != NULL);
628
629         if (!uri->host)
630                 return;
631
632         soup_cookie = soup_cookie_parse (cookie, uri);
633         if (soup_cookie)
634                 soup_cookie_jar_add_cookie_with_first_party (jar, first_party, soup_cookie);
635 }
636
637 static void
638 process_set_cookie_header (SoupMessage *msg, gpointer user_data)
639 {
640         SoupCookieJar *jar = user_data;
641         SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
642         GSList *new_cookies, *nc;
643
644         if (!priv) {
645                 TIZEN_LOGI("error SoupCookieJar is null");
646                 return;
647         }
648         if (priv->accept_policy == SOUP_COOKIE_JAR_ACCEPT_NEVER)
649                 return;
650
651         new_cookies = soup_cookies_from_response (msg);
652         for (nc = new_cookies; nc; nc = nc->next) {
653                 SoupURI *first_party = soup_message_get_first_party (msg);
654                 
655                 if ((priv->accept_policy == SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY &&
656                      first_party != NULL && first_party->host &&
657                      soup_cookie_domain_matches (nc->data, first_party->host)) ||
658                     priv->accept_policy == SOUP_COOKIE_JAR_ACCEPT_ALWAYS)
659                         soup_cookie_jar_add_cookie (jar, nc->data);
660                 else
661                         soup_cookie_free (nc->data);
662         }
663         g_slist_free (new_cookies);
664 }
665
666 static void
667 soup_cookie_jar_request_queued (SoupSessionFeature *feature,
668                                 SoupSession *session,
669                                 SoupMessage *msg)
670 {
671         soup_message_add_header_handler (msg, "got-headers",
672                                          "Set-Cookie",
673                                          G_CALLBACK (process_set_cookie_header),
674                                          feature);
675 }
676
677 static void
678 soup_cookie_jar_request_started (SoupSessionFeature *feature,
679                                  SoupSession *session,
680                                  SoupMessage *msg,
681                                  SoupSocket *socket)
682 {
683         SoupCookieJar *jar = SOUP_COOKIE_JAR (feature);
684         char *cookies;
685
686         cookies = soup_cookie_jar_get_cookies (jar, soup_message_get_uri (msg), TRUE);
687         if (cookies) {
688                 soup_message_headers_replace (msg->request_headers,
689                                               "Cookie", cookies);
690                 g_free (cookies);
691         } else
692                 soup_message_headers_remove (msg->request_headers, "Cookie");
693 }
694
695 static void
696 soup_cookie_jar_request_unqueued (SoupSessionFeature *feature,
697                                   SoupSession *session,
698                                   SoupMessage *msg)
699 {
700         g_signal_handlers_disconnect_by_func (msg, process_set_cookie_header, feature);
701 }
702
703 static void
704 soup_cookie_jar_session_feature_init (SoupSessionFeatureInterface *feature_interface,
705                                       gpointer interface_data)
706 {
707         feature_interface->request_queued = soup_cookie_jar_request_queued;
708         feature_interface->request_started = soup_cookie_jar_request_started;
709         feature_interface->request_unqueued = soup_cookie_jar_request_unqueued;
710 }
711
712 /**
713  * soup_cookie_jar_all_cookies:
714  * @jar: a #SoupCookieJar
715  *
716  * Constructs a #GSList with every cookie inside the @jar.
717  * The cookies in the list are a copy of the original, so
718  * you have to free them when you are done with them.
719  *
720  * Return value: (transfer full) (element-type Soup.Cookie): a #GSList
721  * with all the cookies in the @jar.
722  *
723  * Since: 2.26
724  **/
725 GSList *
726 soup_cookie_jar_all_cookies (SoupCookieJar *jar)
727 {
728         SoupCookieJarPrivate *priv;
729         GHashTableIter iter;
730         GSList *l = NULL;
731         gpointer key, value;
732
733         g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), NULL);
734
735         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
736
737         g_hash_table_iter_init (&iter, priv->domains);
738
739         while (g_hash_table_iter_next (&iter, &key, &value)) {
740                 GSList *p, *cookies = value;
741                 for (p = cookies; p; p = p->next)
742                         l = g_slist_prepend (l, soup_cookie_copy (p->data));
743         }
744
745         return l;
746 }
747
748 /**
749  * soup_cookie_jar_delete_cookie:
750  * @jar: a #SoupCookieJar
751  * @cookie: a #SoupCookie
752  *
753  * Deletes @cookie from @jar, emitting the 'changed' signal.
754  *
755  * Since: 2.26
756  **/
757 void
758 soup_cookie_jar_delete_cookie (SoupCookieJar *jar,
759                                SoupCookie    *cookie)
760 {
761         SoupCookieJarPrivate *priv;
762         GSList *cookies, *p;
763         char *domain;
764
765         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
766         g_return_if_fail (cookie != NULL);
767
768         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
769
770         domain = g_strdup (cookie->domain);
771
772         cookies = g_hash_table_lookup (priv->domains, domain);
773         if (cookies == NULL)
774                 return;
775
776         for (p = cookies; p; p = p->next ) {
777                 SoupCookie *c = (SoupCookie*)p->data;
778                 if (soup_cookie_equal (cookie, c)) {
779                         cookies = g_slist_delete_link (cookies, p);
780                         g_hash_table_insert (priv->domains,
781                                              domain,
782                                              cookies);
783                         soup_cookie_jar_changed (jar, c, NULL);
784                         soup_cookie_free (c);
785                         return;
786                 }
787         }
788 }
789
790 /**
791  * SoupCookieJarAcceptPolicy:
792  * @SOUP_COOKIE_JAR_ACCEPT_ALWAYS: accept all cookies unconditionally.
793  * @SOUP_COOKIE_JAR_ACCEPT_NEVER: reject all cookies unconditionally.
794  * @SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY: accept all cookies set by
795  * the main document loaded in the application using libsoup. An
796  * example of the most common case, web browsers, would be: If
797  * http://www.example.com is the page loaded, accept all cookies set
798  * by example.com, but if a resource from http://www.third-party.com
799  * is loaded from that page reject any cookie that it could try to
800  * set. For libsoup to be able to tell apart first party cookies from
801  * the rest, the application must call soup_message_set_first_party()
802  * on each outgoing #SoupMessage, setting the #SoupURI of the main
803  * document. If no first party is set in a message when this policy is
804  * in effect, cookies will be assumed to be third party by default.
805  *
806  * The policy for accepting or rejecting cookies returned in
807  * responses.
808  *
809  * Since: 2.30
810  */
811
812 /**
813  * soup_cookie_jar_get_accept_policy:
814  * @jar: a #SoupCookieJar
815  *
816  * Gets @jar's #SoupCookieJarAcceptPolicy
817  *
818  * Returns: the #SoupCookieJarAcceptPolicy set in the @jar
819  *
820  * Since: 2.30
821  **/
822 SoupCookieJarAcceptPolicy
823 soup_cookie_jar_get_accept_policy (SoupCookieJar *jar)
824 {
825         SoupCookieJarPrivate *priv;
826
827         g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), SOUP_COOKIE_JAR_ACCEPT_ALWAYS);
828
829         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
830         return priv->accept_policy;
831 }
832
833 /**
834  * soup_cookie_jar_set_accept_policy:
835  * @jar: a #SoupCookieJar
836  * @policy: a #SoupCookieJarAcceptPolicy
837  * 
838  * Sets @policy as the cookie acceptance policy for @jar.
839  *
840  * Since: 2.30
841  **/
842 void
843 soup_cookie_jar_set_accept_policy (SoupCookieJar *jar,
844                                    SoupCookieJarAcceptPolicy policy)
845 {
846         SoupCookieJarPrivate *priv;
847
848         g_return_if_fail (SOUP_IS_COOKIE_JAR (jar));
849
850         priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
851
852         if (priv->accept_policy != policy) {
853                 priv->accept_policy = policy;
854                 g_object_notify (G_OBJECT (jar), SOUP_COOKIE_JAR_ACCEPT_POLICY);
855         }
856 }
857
858 /**
859  * soup_cookie_jar_is_persistent:
860  * @jar: a #SoupCookieJar
861  *
862  * Gets whether @jar stores cookies persistenly.
863  *
864  * Returns: %TRUE if @jar storage is persistent or %FALSE otherwise.
865  *
866  * Since: 2.40
867  **/
868 gboolean
869 soup_cookie_jar_is_persistent (SoupCookieJar *jar)
870 {
871         g_return_val_if_fail (SOUP_IS_COOKIE_JAR (jar), FALSE);
872
873         return SOUP_COOKIE_JAR_GET_CLASS (jar)->is_persistent (jar);
874 }