1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 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 236 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 264 265 266 267 268 269 270 271 272 273 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-cookie.c
*
* Copyright (C) 2007 Red Hat, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "soup-cookie.h"
#include "soup-misc-private.h"
#include "soup.h"
/**
* SECTION:soup-cookie
* @short_description: HTTP Cookies
* @see_also: #SoupMessage, #SoupCookieJar
*
* #SoupCookie implements HTTP cookies, as described by <ulink
* url="http://tools.ietf.org/html/rfc6265.txt">RFC 6265</ulink>.
*
* To have a #SoupSession handle cookies for your appliction
* automatically, use a #SoupCookieJar.
**/
/**
* SoupCookie:
* @name: the cookie name
* @value: the cookie value
* @domain: the "domain" attribute, or else the hostname that the
* cookie came from.
* @path: the "path" attribute, or %NULL
* @expires: the cookie expiration time, or %NULL for a session cookie
* @secure: %TRUE if the cookie should only be tranferred over SSL
* @http_only: %TRUE if the cookie should not be exposed to scripts
*
* An HTTP cookie.
*
* @name and @value will be set for all cookies. If the cookie is
* generated from a string that appears to have no name, then @name
* will be the empty string.
*
* @domain and @path give the host or domain, and path within that
* host/domain, to restrict this cookie to. If @domain starts with
* ".", that indicates a domain (which matches the string after the
* ".", or any hostname that has @domain as a suffix). Otherwise, it
* is a hostname and must match exactly.
*
* @expires will be non-%NULL if the cookie uses either the original
* "expires" attribute, or the newer "max-age" attribute. If @expires
* is %NULL, it indicates that neither "expires" nor "max-age" was
* specified, and the cookie expires at the end of the session.
*
* If @http_only is set, the cookie should not be exposed to untrusted
* code (eg, javascript), so as to minimize the danger posed by
* cross-site scripting attacks.
*
* Since: 2.24
**/
G_DEFINE_BOXED_TYPE (SoupCookie, soup_cookie, soup_cookie_copy, soup_cookie_free)
/**
* soup_cookie_copy:
* @cookie: a #SoupCookie
*
* Copies @cookie.
*
* Return value: a copy of @cookie
*
* Since: 2.24
**/
SoupCookie *
soup_cookie_copy (SoupCookie *cookie)
{
SoupCookie *copy = g_slice_new0 (SoupCookie);
copy->name = g_strdup (cookie->name);
copy->value = g_strdup (cookie->value);
copy->domain = g_strdup (cookie->domain);
copy->path = g_strdup (cookie->path);
if (cookie->expires)
copy->expires = soup_date_copy(cookie->expires);
copy->secure = cookie->secure;
copy->http_only = cookie->http_only;
soup_cookie_set_same_site_policy (copy, soup_cookie_get_same_site_policy (cookie));
return copy;
}
/**
* soup_cookie_domain_matches:
* @cookie: a #SoupCookie
* @host: a URI
*
* Checks if the @cookie's domain and @host match in the sense that
* @cookie should be sent when making a request to @host, or that
* @cookie should be accepted when receiving a response from @host.
*
* Return value: %TRUE if the domains match, %FALSE otherwise
*
* Since: 2.30
**/
gboolean
soup_cookie_domain_matches (SoupCookie *cookie, const char *host)
{
g_return_val_if_fail (cookie != NULL, FALSE);
g_return_val_if_fail (host != NULL, FALSE);
return soup_host_matches_host (cookie->domain, host);
}
static inline const char *
skip_lws (const char *s)
{
while (g_ascii_isspace (*s))
s++;
return s;
}
static inline const char *
unskip_lws (const char *s, const char *start)
{
while (s > start && g_ascii_isspace (*(s - 1)))
s--;
return s;
}
#define is_attr_ender(ch) ((ch) < ' ' || (ch) == ';' || (ch) == ',' || (ch) == '=')
#define is_value_ender(ch) ((ch) < ' ' || (ch) == ';')
static char *
parse_value (const char **val_p, gboolean copy)
{
const char *start, *end, *p;
char *value;
p = *val_p;
if (*p == '=')
p++;
start = skip_lws (p);
for (p = start; !is_value_ender (*p); p++)
;
end = unskip_lws (p, start);
if (copy)
value = g_strndup (start, end - start);
else
value = NULL;
*val_p = p;
return value;
}
static SoupDate *
parse_date (const char **val_p)
{
char *value;
SoupDate *date;
value = parse_value (val_p, TRUE);
date = soup_date_new_from_string (value);
g_free (value);
return date;
}
static SoupCookie *
parse_one_cookie (const char *header, SoupURI *origin)
{
const char *start, *end, *p;
gboolean has_value;
SoupCookie *cookie;
g_return_val_if_fail (origin == NULL || origin->host, NULL);
cookie = g_slice_new0 (SoupCookie);
/* Parse the NAME */
start = skip_lws (header);
for (p = start; !is_attr_ender (*p); p++)
;
if (*p == '=') {
end = unskip_lws (p, start);
cookie->name = g_strndup (start, end - start);
} else {
/* No NAME; Set cookie->name to "" and then rewind to
* re-parse the string as a VALUE.
*/
cookie->name = g_strdup ("");
p = start;
}
/* Parse the VALUE */
cookie->value = parse_value (&p, TRUE);
/* Parse attributes */
while (*p == ';') {
start = skip_lws (p + 1);
for (p = start; !is_attr_ender (*p); p++)
;
end = unskip_lws (p, start);
has_value = (*p == '=');
#define MATCH_NAME(name) ((end - start == strlen (name)) && !g_ascii_strncasecmp (start, name, end - start))
if (MATCH_NAME ("domain") && has_value) {
cookie->domain = parse_value (&p, TRUE);
if (!*cookie->domain) {
g_free (cookie->domain);
cookie->domain = NULL;
}
} else if (MATCH_NAME ("expires") && has_value) {
cookie->expires = parse_date (&p);
} else if (MATCH_NAME ("httponly")) {
cookie->http_only = TRUE;
if (has_value)
parse_value (&p, FALSE);
} else if (MATCH_NAME ("max-age") && has_value) {
char *max_age_str = parse_value (&p, TRUE), *mae;
long max_age = strtol (max_age_str, &mae, 10);
if (!*mae) {
if (max_age < 0)
max_age = 0;
soup_cookie_set_max_age (cookie, max_age);
}
g_free (max_age_str);
} else if (MATCH_NAME ("path") && has_value) {
cookie->path = parse_value (&p, TRUE);
if (*cookie->path != '/') {
g_free (cookie->path);
cookie->path = NULL;
}
} else if (MATCH_NAME ("secure")) {
cookie->secure = TRUE;
if (has_value)
parse_value (&p, FALSE);
} else if (MATCH_NAME ("samesite")) {
if (has_value) {
char *policy = parse_value (&p, TRUE);
if (g_ascii_strcasecmp (policy, "Lax") == 0)
soup_cookie_set_same_site_policy (cookie, SOUP_SAME_SITE_POLICY_LAX);
else if (g_ascii_strcasecmp (policy, "Strict") == 0)
soup_cookie_set_same_site_policy (cookie, SOUP_SAME_SITE_POLICY_STRICT);
/* There is an explicit "None" value which is the default. */
g_free (policy);
}
/* Note that earlier versions of the same-site RFC treated invalid values as strict but
the latest revision simply ignores them. */
} else {
/* Ignore unknown attributes, but we still have
* to skip over the value.
*/
if (has_value)
parse_value (&p, FALSE);
}
}
if (cookie->domain) {
/* Domain must have at least one '.' (not counting an
* initial one. (We check this now, rather than
* bailing out sooner, because we don't want to force
* any cookies after this one in the Set-Cookie header
* to be discarded.)
*/
if (!strchr (cookie->domain + 1, '.')) {
soup_cookie_free (cookie);
return NULL;
}
/* If the domain string isn't an IP addr, and doesn't
* start with a '.', prepend one.
*/
if (!g_hostname_is_ip_address (cookie->domain) &&
cookie->domain[0] != '.') {
char *tmp = g_strdup_printf (".%s", cookie->domain);
g_free (cookie->domain);
cookie->domain = tmp;
}
}
if (origin) {
/* Sanity-check domain */
if (cookie->domain) {
if (!soup_cookie_domain_matches (cookie, origin->host)) {
soup_cookie_free (cookie);
return NULL;
}
} else
cookie->domain = g_strdup (origin->host);
/* The original cookie spec didn't say that pages
* could only set cookies for paths they were under.
* RFC 2109 adds that requirement, but some sites
* depend on the old behavior
* (https://bugzilla.mozilla.org/show_bug.cgi?id=156725#c20).
* So we don't check the path.
*/
if (!cookie->path) {
char *slash;
slash = strrchr (origin->path, '/');
if (!slash || slash == origin->path)
cookie->path = g_strdup ("/");
else {
cookie->path = g_strndup (origin->path,
slash - origin->path);
}
}
} else if (!cookie->path) {
cookie->path = g_strdup ("/");
}
return cookie;
}
static SoupCookie *
cookie_new_internal (const char *name, const char *value,
const char *domain, const char *path,
int max_age)
{
SoupCookie *cookie;
cookie = g_slice_new0 (SoupCookie);
cookie->name = g_strdup (name);
cookie->value = g_strdup (value);
cookie->domain = g_strdup (domain);
cookie->path = g_strdup (path);
soup_cookie_set_max_age (cookie, max_age);
return cookie;
}
/**
* soup_cookie_new:
* @name: cookie name
* @value: cookie value
* @domain: cookie domain or hostname
* @path: cookie path, or %NULL
* @max_age: max age of the cookie, or -1 for a session cookie
*
* Creates a new #SoupCookie with the given attributes. (Use
* soup_cookie_set_secure() and soup_cookie_set_http_only() if you
* need to set those attributes on the returned cookie.)
*
* If @domain starts with ".", that indicates a domain (which matches
* the string after the ".", or any hostname that has @domain as a
* suffix). Otherwise, it is a hostname and must match exactly.
*
* @max_age is used to set the "expires" attribute on the cookie; pass
* -1 to not include the attribute (indicating that the cookie expires
* with the current session), 0 for an already-expired cookie, or a
* lifetime in seconds. You can use the constants
* %SOUP_COOKIE_MAX_AGE_ONE_HOUR, %SOUP_COOKIE_MAX_AGE_ONE_DAY,
* %SOUP_COOKIE_MAX_AGE_ONE_WEEK and %SOUP_COOKIE_MAX_AGE_ONE_YEAR (or
* multiples thereof) to calculate this value. (If you really care
* about setting the exact time that the cookie will expire, use
* soup_cookie_set_expires().)
*
* Return value: a new #SoupCookie.
*
* Since: 2.24
**/
SoupCookie *
soup_cookie_new (const char *name, const char *value,
const char *domain, const char *path,
int max_age)
{
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (value != NULL, NULL);
/* We ought to return if domain is NULL too, but this used to
* do be incorrectly documented as legal, and it wouldn't
* break anything as long as you called
* soup_cookie_set_domain() immediately after. So we warn but
* don't return, to discourage that behavior but not actually
* break anyone doing it.
*/
g_warn_if_fail (domain != NULL);
return cookie_new_internal (name, value, domain, path, max_age);
}
/**
* soup_cookie_parse:
* @header: a cookie string (eg, the value of a Set-Cookie header)
* @origin: origin of the cookie, or %NULL
*
* Parses @header and returns a #SoupCookie. (If @header contains
* multiple cookies, only the first one will be parsed.)
*
* If @header does not have "path" or "domain" attributes, they will
* be defaulted from @origin. If @origin is %NULL, path will default
* to "/", but domain will be left as %NULL. Note that this is not a
* valid state for a #SoupCookie, and you will need to fill in some
* appropriate string for the domain if you want to actually make use
* of the cookie.
*
* Return value: (nullable): a new #SoupCookie, or %NULL if it could
* not be parsed, or contained an illegal "domain" attribute for a
* cookie originating from @origin.
*
* Since: 2.24
**/
SoupCookie *
soup_cookie_parse (const char *cookie, SoupURI *origin)
{
return parse_one_cookie (cookie, origin);
}
/**
* soup_cookie_get_name:
* @cookie: a #SoupCookie
*
* Gets @cookie's name
*
* Return value: @cookie's name
*
* Since: 2.32
**/
const char *
soup_cookie_get_name (SoupCookie *cookie)
{
return cookie->name;
}
/**
* soup_cookie_set_name:
* @cookie: a #SoupCookie
* @name: the new name
*
* Sets @cookie's name to @name
*
* Since: 2.24
**/
void
soup_cookie_set_name (SoupCookie *cookie, const char *name)
{
g_free (cookie->name);
cookie->name = g_strdup (name);
}
/**
* soup_cookie_get_value:
* @cookie: a #SoupCookie
*
* Gets @cookie's value
*
* Return value: @cookie's value
*
* Since: 2.32
**/
const char *
soup_cookie_get_value (SoupCookie *cookie)
{
return cookie->value;
}
/**
* soup_cookie_set_value:
* @cookie: a #SoupCookie
* @value: the new value
*
* Sets @cookie's value to @value
*
* Since: 2.24
**/
void
soup_cookie_set_value (SoupCookie *cookie, const char *value)
{
g_free (cookie->value);
cookie->value = g_strdup (value);
}
/**
* soup_cookie_get_domain:
* @cookie: a #SoupCookie
*
* Gets @cookie's domain
*
* Return value: @cookie's domain
*
* Since: 2.32
**/
const char *
soup_cookie_get_domain (SoupCookie *cookie)
{
return cookie->domain;
}
/**
* soup_cookie_set_domain:
* @cookie: a #SoupCookie
* @domain: the new domain
*
* Sets @cookie's domain to @domain
*
* Since: 2.24
**/
void
soup_cookie_set_domain (SoupCookie *cookie, const char *domain)
{
g_free (cookie->domain);
cookie->domain = g_strdup (domain);
}
/**
* soup_cookie_get_path:
* @cookie: a #SoupCookie
*
* Gets @cookie's path
*
* Return value: @cookie's path
*
* Since: 2.32
**/
const char *
soup_cookie_get_path (SoupCookie *cookie)
{
return cookie->path;
}
/**
* soup_cookie_set_path:
* @cookie: a #SoupCookie
* @path: the new path
*
* Sets @cookie's path to @path
*
* Since: 2.24
**/
void
soup_cookie_set_path (SoupCookie *cookie, const char *path)
{
g_free (cookie->path);
cookie->path = g_strdup (path);
}
/**
* soup_cookie_set_max_age:
* @cookie: a #SoupCookie
* @max_age: the new max age
*
* Sets @cookie's max age to @max_age. If @max_age is -1, the cookie
* is a session cookie, and will expire at the end of the client's
* session. Otherwise, it is the number of seconds until the cookie
* expires. You can use the constants %SOUP_COOKIE_MAX_AGE_ONE_HOUR,
* %SOUP_COOKIE_MAX_AGE_ONE_DAY, %SOUP_COOKIE_MAX_AGE_ONE_WEEK and
* %SOUP_COOKIE_MAX_AGE_ONE_YEAR (or multiples thereof) to calculate
* this value. (A value of 0 indicates that the cookie should be
* considered already-expired.)
*
* (This sets the same property as soup_cookie_set_expires().)
*
* Since: 2.24
**/
void
soup_cookie_set_max_age (SoupCookie *cookie, int max_age)
{
if (cookie->expires)
soup_date_free (cookie->expires);
if (max_age == -1)
cookie->expires = NULL;
else if (max_age == 0) {
/* Use a date way in the past, to protect against
* clock skew.
*/
cookie->expires = soup_date_new (1970, 1, 1, 0, 0, 0);
} else
cookie->expires = soup_date_new_from_now (max_age);
}
/**
* SOUP_COOKIE_MAX_AGE_ONE_HOUR:
*
* A constant corresponding to 1 hour, for use with soup_cookie_new()
* and soup_cookie_set_max_age().
*
* Since: 2.24
**/
/**
* SOUP_COOKIE_MAX_AGE_ONE_DAY:
*
* A constant corresponding to 1 day, for use with soup_cookie_new()
* and soup_cookie_set_max_age().
*
* Since: 2.24
**/
/**
* SOUP_COOKIE_MAX_AGE_ONE_WEEK:
*
* A constant corresponding to 1 week, for use with soup_cookie_new()
* and soup_cookie_set_max_age().
*
* Since: 2.24
**/
/**
* SOUP_COOKIE_MAX_AGE_ONE_YEAR:
*
* A constant corresponding to 1 year, for use with soup_cookie_new()
* and soup_cookie_set_max_age().
*
* Since: 2.24
**/
/**
* soup_cookie_get_expires:
* @cookie: a #SoupCookie
*
* Gets @cookie's expiration time.
*
* Return value: (nullable) (transfer none): @cookie's expiration
* time, which is owned by @cookie and should not be modified or
* freed.
*
* Since: 2.32
**/
SoupDate *
soup_cookie_get_expires (SoupCookie *cookie)
{
return cookie->expires;
}
/**
* soup_cookie_set_expires:
* @cookie: a #SoupCookie
* @expires: the new expiration time, or %NULL
*
* Sets @cookie's expiration time to @expires. If @expires is %NULL,
* @cookie will be a session cookie and will expire at the end of the
* client's session.
*
* (This sets the same property as soup_cookie_set_max_age().)
*
* Since: 2.24
**/
void
soup_cookie_set_expires (SoupCookie *cookie, SoupDate *expires)
{
if (cookie->expires)
soup_date_free (cookie->expires);
if (expires)
cookie->expires = soup_date_copy (expires);
else
cookie->expires = NULL;
}
/**
* soup_cookie_get_secure:
* @cookie: a #SoupCookie
*
* Gets @cookie's secure attribute
*
* Return value: @cookie's secure attribute
*
* Since: 2.32
**/
gboolean
soup_cookie_get_secure (SoupCookie *cookie)
{
return cookie->secure;
}
/**
* soup_cookie_set_secure:
* @cookie: a #SoupCookie
* @secure: the new value for the secure attribute
*
* Sets @cookie's secure attribute to @secure. If %TRUE, @cookie will
* only be transmitted from the client to the server over secure
* (https) connections.
*
* Since: 2.24
**/
void
soup_cookie_set_secure (SoupCookie *cookie, gboolean secure)
{
cookie->secure = secure;
}
/**
* soup_cookie_get_http_only:
* @cookie: a #SoupCookie
*
* Gets @cookie's HttpOnly attribute
*
* Return value: @cookie's HttpOnly attribute
*
* Since: 2.32
**/
gboolean
soup_cookie_get_http_only (SoupCookie *cookie)
{
return cookie->http_only;
}
/**
* soup_cookie_set_http_only:
* @cookie: a #SoupCookie
* @http_only: the new value for the HttpOnly attribute
*
* Sets @cookie's HttpOnly attribute to @http_only. If %TRUE, @cookie
* will be marked as "http only", meaning it should not be exposed to
* web page scripts or other untrusted code.
*
* Since: 2.24
**/
void
soup_cookie_set_http_only (SoupCookie *cookie, gboolean http_only)
{
cookie->http_only = http_only;
}
static void
serialize_cookie (SoupCookie *cookie, GString *header, gboolean set_cookie)
{
SoupSameSitePolicy same_site_policy;
if (!*cookie->name && !*cookie->value)
return;
if (header->len) {
if (set_cookie)
g_string_append (header, ", ");
else
g_string_append (header, "; ");
}
if (set_cookie || *cookie->name) {
g_string_append (header, cookie->name);
g_string_append (header, "=");
}
g_string_append (header, cookie->value);
if (!set_cookie)
return;
if (cookie->expires) {
char *timestamp;
g_string_append (header, "; expires=");
timestamp = soup_date_to_string (cookie->expires,
SOUP_DATE_COOKIE);
g_string_append (header, timestamp);
g_free (timestamp);
}
if (cookie->path) {
g_string_append (header, "; path=");
g_string_append (header, cookie->path);
}
if (cookie->domain) {
g_string_append (header, "; domain=");
g_string_append (header, cookie->domain);
}
same_site_policy = soup_cookie_get_same_site_policy (cookie);
if (same_site_policy != SOUP_SAME_SITE_POLICY_NONE) {
g_string_append (header, "; SameSite=");
if (same_site_policy == SOUP_SAME_SITE_POLICY_LAX)
g_string_append (header, "Lax");
else
g_string_append (header, "Strict");
}
if (cookie->secure)
g_string_append (header, "; secure");
if (cookie->http_only)
g_string_append (header, "; HttpOnly");
}
static const char *same_site_policy_string = "soup-same-site-policy";
#define SAME_SITE_POLICY_QUARK (g_quark_from_static_string (same_site_policy_string))
/**
* soup_cookie_set_same_site_policy:
* @cookie: a #SoupCookie
* @policy: a #SoupSameSitePolicy
*
* When used in conjunction with soup_cookie_jar_get_cookie_list_with_same_site_info() this
* sets the policy of when this cookie should be exposed.
*
* Since: 2.70
**/
void
soup_cookie_set_same_site_policy (SoupCookie *cookie,
SoupSameSitePolicy policy)
{
switch (policy) {
case SOUP_SAME_SITE_POLICY_NONE:
case SOUP_SAME_SITE_POLICY_STRICT:
case SOUP_SAME_SITE_POLICY_LAX:
g_dataset_id_set_data (cookie, SAME_SITE_POLICY_QUARK, GUINT_TO_POINTER (policy));
break;
default:
g_return_if_reached ();
}
}
/**
* soup_cookie_get_same_site_policy:
* @cookie: a #SoupCookie
*
* Returns: a #SoupSameSitePolicy
*
* Since: 2.70
**/
SoupSameSitePolicy
soup_cookie_get_same_site_policy (SoupCookie *cookie)
{
return GPOINTER_TO_UINT (g_dataset_id_get_data (cookie, SAME_SITE_POLICY_QUARK));
}
/**
* soup_cookie_to_set_cookie_header:
* @cookie: a #SoupCookie
*
* Serializes @cookie in the format used by the Set-Cookie header
* (ie, for sending a cookie from a #SoupServer to a client).
*
* Return value: the header
*
* Since: 2.24
**/
char *
soup_cookie_to_set_cookie_header (SoupCookie *cookie)
{
GString *header = g_string_new (NULL);
serialize_cookie (cookie, header, TRUE);
return g_string_free (header, FALSE);
}
/**
* soup_cookie_to_cookie_header:
* @cookie: a #SoupCookie
*
* Serializes @cookie in the format used by the Cookie header (ie, for
* returning a cookie from a #SoupSession to a server).
*
* Return value: the header
*
* Since: 2.24
**/
char *
soup_cookie_to_cookie_header (SoupCookie *cookie)
{
GString *header = g_string_new (NULL);
serialize_cookie (cookie, header, FALSE);
return g_string_free (header, FALSE);
}
/**
* soup_cookie_free:
* @cookie: a #SoupCookie
*
* Frees @cookie
*
* Since: 2.24
**/
void
soup_cookie_free (SoupCookie *cookie)
{
g_return_if_fail (cookie != NULL);
g_free (cookie->name);
g_free (cookie->value);
g_free (cookie->domain);
g_free (cookie->path);
g_clear_pointer (&cookie->expires, soup_date_free);
g_dataset_destroy (cookie);
g_slice_free (SoupCookie, cookie);
}
/**
* soup_cookies_from_response:
* @msg: a #SoupMessage containing a "Set-Cookie" response header
*
* Parses @msg's Set-Cookie response headers and returns a #GSList of
* #SoupCookie<!-- -->s. Cookies that do not specify "path" or
* "domain" attributes will have their values defaulted from @msg.
*
* Return value: (element-type SoupCookie) (transfer full): a #GSList
* of #SoupCookie<!-- -->s, which can be freed with
* soup_cookies_free().
*
* Since: 2.24
**/
GSList *
soup_cookies_from_response (SoupMessage *msg)
{
SoupURI *origin;
const char *name, *value;
SoupCookie *cookie;
GSList *cookies = NULL;
SoupMessageHeadersIter iter;
origin = soup_message_get_uri (msg);
/* We have to use soup_message_headers_iter rather than
* soup_message_headers_get_list() since Set-Cookie isn't
* properly mergeable/unmergeable.
*/
soup_message_headers_iter_init (&iter, msg->response_headers);
while (soup_message_headers_iter_next (&iter, &name, &value)) {
if (g_ascii_strcasecmp (name, "Set-Cookie") != 0)
continue;
cookie = parse_one_cookie (value, origin);
if (cookie)
cookies = g_slist_prepend (cookies, cookie);
}
return g_slist_reverse (cookies);
}
/**
* soup_cookies_from_request:
* @msg: a #SoupMessage containing a "Cookie" request header
*
* Parses @msg's Cookie request header and returns a #GSList of
* #SoupCookie<!-- -->s. As the "Cookie" header, unlike "Set-Cookie",
* only contains cookie names and values, none of the other
* #SoupCookie fields will be filled in. (Thus, you can't generally
* pass a cookie returned from this method directly to
* soup_cookies_to_response().)
*
* Return value: (element-type SoupCookie) (transfer full): a #GSList
* of #SoupCookie<!-- -->s, which can be freed with
* soup_cookies_free().
*
* Since: 2.24
**/
GSList *
soup_cookies_from_request (SoupMessage *msg)
{
SoupCookie *cookie;
GSList *cookies = NULL;
GHashTable *params;
GHashTableIter iter;
gpointer name, value;
const char *header;
header = soup_message_headers_get_one (msg->request_headers, "Cookie");
if (!header)
return NULL;
params = soup_header_parse_semi_param_list (header);
g_hash_table_iter_init (&iter, params);
while (g_hash_table_iter_next (&iter, &name, &value)) {
if (name && value) {
cookie = cookie_new_internal (name, value,
NULL, NULL, 0);
cookies = g_slist_prepend (cookies, cookie);
}
}
soup_header_free_param_list (params);
return g_slist_reverse (cookies);
}
/**
* soup_cookies_to_response:
* @cookies: (element-type SoupCookie): a #GSList of #SoupCookie
* @msg: a #SoupMessage
*
* Appends a "Set-Cookie" response header to @msg for each cookie in
* @cookies. (This is in addition to any other "Set-Cookie" headers
* @msg may already have.)
*
* Since: 2.24
**/
void
soup_cookies_to_response (GSList *cookies, SoupMessage *msg)
{
GString *header;
header = g_string_new (NULL);
while (cookies) {
serialize_cookie (cookies->data, header, TRUE);
soup_message_headers_append (msg->response_headers,
"Set-Cookie", header->str);
g_string_truncate (header, 0);
cookies = cookies->next;
}
g_string_free (header, TRUE);
}
/**
* soup_cookies_to_request:
* @cookies: (element-type SoupCookie): a #GSList of #SoupCookie
* @msg: a #SoupMessage
*
* Adds the name and value of each cookie in @cookies to @msg's
* "Cookie" request. (If @msg already has a "Cookie" request header,
* these cookies will be appended to the cookies already present. Be
* careful that you do not append the same cookies twice, eg, when
* requeuing a message.)
*
* Since: 2.24
**/
void
soup_cookies_to_request (GSList *cookies, SoupMessage *msg)
{
GString *header;
header = g_string_new (soup_message_headers_get_one (msg->request_headers,
"Cookie"));
while (cookies) {
serialize_cookie (cookies->data, header, FALSE);
cookies = cookies->next;
}
soup_message_headers_replace (msg->request_headers,
"Cookie", header->str);
g_string_free (header, TRUE);
}
/**
* soup_cookies_free: (skip)
* @cookies: (element-type SoupCookie): a #GSList of #SoupCookie
*
* Frees @cookies.
*
* Since: 2.24
**/
void
soup_cookies_free (GSList *cookies)
{
g_slist_free_full (cookies, (GDestroyNotify)soup_cookie_free);
}
/**
* soup_cookies_to_cookie_header:
* @cookies: (element-type SoupCookie): a #GSList of #SoupCookie
*
* Serializes a #GSList of #SoupCookie into a string suitable for
* setting as the value of the "Cookie" header.
*
* Return value: the serialization of @cookies
*
* Since: 2.24
**/
char *
soup_cookies_to_cookie_header (GSList *cookies)
{
GString *str;
g_return_val_if_fail (cookies != NULL, NULL);
str = g_string_new (NULL);
while (cookies) {
serialize_cookie (cookies->data, str, FALSE);
cookies = cookies->next;
}
return g_string_free (str, FALSE);
}
/**
* soup_cookie_applies_to_uri:
* @cookie: a #SoupCookie
* @uri: a #SoupURI
*
* Tests if @cookie should be sent to @uri.
*
* (At the moment, this does not check that @cookie's domain matches
* @uri, because it assumes that the caller has already done that.
* But don't rely on that; it may change in the future.)
*
* Return value: %TRUE if @cookie should be sent to @uri, %FALSE if
* not
*
* Since: 2.24
**/
gboolean
soup_cookie_applies_to_uri (SoupCookie *cookie, SoupURI *uri)
{
int plen;
if (cookie->secure && !soup_uri_is_https (uri, NULL))
return FALSE;
if (cookie->expires && soup_date_is_past (cookie->expires))
return FALSE;
/* uri->path is required to be non-NULL */
g_return_val_if_fail (uri->path != NULL, FALSE);
plen = strlen (cookie->path);
if (plen == 0)
return TRUE;
if (strncmp (cookie->path, uri->path, plen) != 0)
return FALSE;
if (cookie->path[plen - 1] != '/' &&
uri->path[plen] && uri->path[plen] != '/')
return FALSE;
return TRUE;
}
/**
* soup_cookie_equal:
* @cookie1: a #SoupCookie
* @cookie2: a #SoupCookie
*
* Tests if @cookie1 and @cookie2 are equal.
*
* Note that currently, this does not check that the cookie domains
* match. This may change in the future.
*
* Return value: whether the cookies are equal.
*
* Since: 2.24
*/
gboolean
soup_cookie_equal (SoupCookie *cookie1, SoupCookie *cookie2)
{
g_return_val_if_fail (cookie1, FALSE);
g_return_val_if_fail (cookie2, FALSE);
return (!strcmp (cookie1->name, cookie2->name) &&
!strcmp (cookie1->value, cookie2->value) &&
!strcmp (cookie1->path, cookie2->path));
}
|