1 module soup.Cookie; 2 3 private import glib.ConstructionException; 4 private import glib.MemorySlice; 5 private import glib.Str; 6 private import gobject.ObjectG; 7 private import soup.Date; 8 private import soup.URI; 9 private import soup.c.functions; 10 public import soup.c.types; 11 12 13 /** 14 * #SoupCookie implements HTTP cookies, as described by <ulink 15 * url="http://tools.ietf.org/html/rfc6265.txt">RFC 6265</ulink>. 16 * 17 * To have a #SoupSession handle cookies for your appliction 18 * automatically, use a #SoupCookieJar. 19 * 20 * Since: 2.24 21 */ 22 public final class Cookie 23 { 24 /** the main Gtk struct */ 25 protected SoupCookie* soupCookie; 26 protected bool ownedRef; 27 28 /** Get the main Gtk struct */ 29 public SoupCookie* getCookieStruct(bool transferOwnership = false) 30 { 31 if (transferOwnership) 32 ownedRef = false; 33 return soupCookie; 34 } 35 36 /** the main Gtk struct as a void* */ 37 protected void* getStruct() 38 { 39 return cast(void*)soupCookie; 40 } 41 42 /** 43 * Sets our main struct and passes it to the parent class. 44 */ 45 public this (SoupCookie* soupCookie, bool ownedRef = false) 46 { 47 this.soupCookie = soupCookie; 48 this.ownedRef = ownedRef; 49 } 50 51 ~this () 52 { 53 if ( ownedRef ) 54 soup_cookie_free(soupCookie); 55 } 56 57 58 /** 59 * the cookie name 60 */ 61 public @property string name() 62 { 63 return Str.toString(soupCookie.name); 64 } 65 66 /** Ditto */ 67 public @property void name(string value) 68 { 69 soupCookie.name = Str.toStringz(value); 70 } 71 72 /** 73 * the cookie value 74 */ 75 public @property string value() 76 { 77 return Str.toString(soupCookie.value); 78 } 79 80 /** Ditto */ 81 public @property void value(string value) 82 { 83 soupCookie.value = Str.toStringz(value); 84 } 85 86 /** 87 * the "domain" attribute, or else the hostname that the 88 * cookie came from. 89 */ 90 public @property string domain() 91 { 92 return Str.toString(soupCookie.domain); 93 } 94 95 /** Ditto */ 96 public @property void domain(string value) 97 { 98 soupCookie.domain = Str.toStringz(value); 99 } 100 101 /** 102 * the "path" attribute, or %NULL 103 */ 104 public @property string path() 105 { 106 return Str.toString(soupCookie.path); 107 } 108 109 /** Ditto */ 110 public @property void path(string value) 111 { 112 soupCookie.path = Str.toStringz(value); 113 } 114 115 /** 116 * the cookie expiration time, or %NULL for a session cookie 117 */ 118 public @property Date expires() 119 { 120 return ObjectG.getDObject!(Date)(soupCookie.expires, false); 121 } 122 123 /** Ditto */ 124 public @property void expires(Date value) 125 { 126 soupCookie.expires = value.getDateStruct(); 127 } 128 129 /** 130 * %TRUE if the cookie should only be tranferred over SSL 131 */ 132 public @property bool secure() 133 { 134 return soupCookie.secure != 0; 135 } 136 137 /** Ditto */ 138 public @property void secure(bool value) 139 { 140 soupCookie.secure = value; 141 } 142 143 /** 144 * %TRUE if the cookie should not be exposed to scripts 145 */ 146 public @property bool httpOnly() 147 { 148 return soupCookie.httpOnly != 0; 149 } 150 151 /** Ditto */ 152 public @property void httpOnly(bool value) 153 { 154 soupCookie.httpOnly = value; 155 } 156 157 /** */ 158 public static GType getType() 159 { 160 return soup_cookie_get_type(); 161 } 162 163 /** 164 * Creates a new #SoupCookie with the given attributes. (Use 165 * soup_cookie_set_secure() and soup_cookie_set_http_only() if you 166 * need to set those attributes on the returned cookie.) 167 * 168 * If @domain starts with ".", that indicates a domain (which matches 169 * the string after the ".", or any hostname that has @domain as a 170 * suffix). Otherwise, it is a hostname and must match exactly. 171 * 172 * @max_age is used to set the "expires" attribute on the cookie; pass 173 * -1 to not include the attribute (indicating that the cookie expires 174 * with the current session), 0 for an already-expired cookie, or a 175 * lifetime in seconds. You can use the constants 176 * %SOUP_COOKIE_MAX_AGE_ONE_HOUR, %SOUP_COOKIE_MAX_AGE_ONE_DAY, 177 * %SOUP_COOKIE_MAX_AGE_ONE_WEEK and %SOUP_COOKIE_MAX_AGE_ONE_YEAR (or 178 * multiples thereof) to calculate this value. (If you really care 179 * about setting the exact time that the cookie will expire, use 180 * soup_cookie_set_expires().) 181 * 182 * Params: 183 * name = cookie name 184 * value = cookie value 185 * domain = cookie domain or hostname 186 * path = cookie path, or %NULL 187 * maxAge = max age of the cookie, or -1 for a session cookie 188 * 189 * Returns: a new #SoupCookie. 190 * 191 * Since: 2.24 192 * 193 * Throws: ConstructionException GTK+ fails to create the object. 194 */ 195 public this(string name, string value, string domain, string path, int maxAge) 196 { 197 auto __p = soup_cookie_new(Str.toStringz(name), Str.toStringz(value), Str.toStringz(domain), Str.toStringz(path), maxAge); 198 199 if(__p is null) 200 { 201 throw new ConstructionException("null returned by new"); 202 } 203 204 this(cast(SoupCookie*) __p); 205 } 206 207 /** 208 * Tests if @cookie should be sent to @uri. 209 * 210 * (At the moment, this does not check that @cookie's domain matches 211 * @uri, because it assumes that the caller has already done that. 212 * But don't rely on that; it may change in the future.) 213 * 214 * Params: 215 * uri = a #SoupURI 216 * 217 * Returns: %TRUE if @cookie should be sent to @uri, %FALSE if 218 * not 219 * 220 * Since: 2.24 221 */ 222 public bool appliesToUri(URI uri) 223 { 224 return soup_cookie_applies_to_uri(soupCookie, (uri is null) ? null : uri.getURIStruct()) != 0; 225 } 226 227 /** 228 * Copies @cookie. 229 * 230 * Returns: a copy of @cookie 231 * 232 * Since: 2.24 233 */ 234 public Cookie copy() 235 { 236 auto __p = soup_cookie_copy(soupCookie); 237 238 if(__p is null) 239 { 240 return null; 241 } 242 243 return ObjectG.getDObject!(Cookie)(cast(SoupCookie*) __p, true); 244 } 245 246 /** 247 * Checks if the @cookie's domain and @host match in the sense that 248 * @cookie should be sent when making a request to @host, or that 249 * @cookie should be accepted when receiving a response from @host. 250 * 251 * Params: 252 * host = a URI 253 * 254 * Returns: %TRUE if the domains match, %FALSE otherwise 255 * 256 * Since: 2.30 257 */ 258 public bool domainMatches(string host) 259 { 260 return soup_cookie_domain_matches(soupCookie, Str.toStringz(host)) != 0; 261 } 262 263 /** 264 * Tests if @cookie1 and @cookie2 are equal. 265 * 266 * Note that currently, this does not check that the cookie domains 267 * match. This may change in the future. 268 * 269 * Params: 270 * cookie2 = a #SoupCookie 271 * 272 * Returns: whether the cookies are equal. 273 * 274 * Since: 2.24 275 */ 276 public bool equal(Cookie cookie2) 277 { 278 return soup_cookie_equal(soupCookie, (cookie2 is null) ? null : cookie2.getCookieStruct()) != 0; 279 } 280 281 /** 282 * Frees @cookie 283 * 284 * Since: 2.24 285 */ 286 public void free() 287 { 288 soup_cookie_free(soupCookie); 289 ownedRef = false; 290 } 291 292 /** 293 * Gets @cookie's domain 294 * 295 * Returns: @cookie's domain 296 * 297 * Since: 2.32 298 */ 299 public string getDomain() 300 { 301 return Str.toString(soup_cookie_get_domain(soupCookie)); 302 } 303 304 /** 305 * Gets @cookie's expiration time. 306 * 307 * Returns: @cookie's expiration 308 * time, which is owned by @cookie and should not be modified or 309 * freed. 310 * 311 * Since: 2.32 312 */ 313 public Date getExpires() 314 { 315 auto __p = soup_cookie_get_expires(soupCookie); 316 317 if(__p is null) 318 { 319 return null; 320 } 321 322 return ObjectG.getDObject!(Date)(cast(SoupDate*) __p); 323 } 324 325 /** 326 * Gets @cookie's HttpOnly attribute 327 * 328 * Returns: @cookie's HttpOnly attribute 329 * 330 * Since: 2.32 331 */ 332 public bool getHttpOnly() 333 { 334 return soup_cookie_get_http_only(soupCookie) != 0; 335 } 336 337 /** 338 * Gets @cookie's name 339 * 340 * Returns: @cookie's name 341 * 342 * Since: 2.32 343 */ 344 public string getName() 345 { 346 return Str.toString(soup_cookie_get_name(soupCookie)); 347 } 348 349 /** 350 * Gets @cookie's path 351 * 352 * Returns: @cookie's path 353 * 354 * Since: 2.32 355 */ 356 public string getPath() 357 { 358 return Str.toString(soup_cookie_get_path(soupCookie)); 359 } 360 361 /** 362 * Returns: a #SoupSameSitePolicy 363 * 364 * Since: 2.70 365 */ 366 public SoupSameSitePolicy getSameSitePolicy() 367 { 368 return soup_cookie_get_same_site_policy(soupCookie); 369 } 370 371 /** 372 * Gets @cookie's secure attribute 373 * 374 * Returns: @cookie's secure attribute 375 * 376 * Since: 2.32 377 */ 378 public bool getSecure() 379 { 380 return soup_cookie_get_secure(soupCookie) != 0; 381 } 382 383 /** 384 * Gets @cookie's value 385 * 386 * Returns: @cookie's value 387 * 388 * Since: 2.32 389 */ 390 public string getValue() 391 { 392 return Str.toString(soup_cookie_get_value(soupCookie)); 393 } 394 395 /** 396 * Sets @cookie's domain to @domain 397 * 398 * Params: 399 * domain = the new domain 400 * 401 * Since: 2.24 402 */ 403 public void setDomain(string domain) 404 { 405 soup_cookie_set_domain(soupCookie, Str.toStringz(domain)); 406 } 407 408 /** 409 * Sets @cookie's expiration time to @expires. If @expires is %NULL, 410 * @cookie will be a session cookie and will expire at the end of the 411 * client's session. 412 * 413 * (This sets the same property as soup_cookie_set_max_age().) 414 * 415 * Params: 416 * expires = the new expiration time, or %NULL 417 * 418 * Since: 2.24 419 */ 420 public void setExpires(Date expires) 421 { 422 soup_cookie_set_expires(soupCookie, (expires is null) ? null : expires.getDateStruct()); 423 } 424 425 /** 426 * Sets @cookie's HttpOnly attribute to @http_only. If %TRUE, @cookie 427 * will be marked as "http only", meaning it should not be exposed to 428 * web page scripts or other untrusted code. 429 * 430 * Params: 431 * httpOnly = the new value for the HttpOnly attribute 432 * 433 * Since: 2.24 434 */ 435 public void setHttpOnly(bool httpOnly) 436 { 437 soup_cookie_set_http_only(soupCookie, httpOnly); 438 } 439 440 /** 441 * Sets @cookie's max age to @max_age. If @max_age is -1, the cookie 442 * is a session cookie, and will expire at the end of the client's 443 * session. Otherwise, it is the number of seconds until the cookie 444 * expires. You can use the constants %SOUP_COOKIE_MAX_AGE_ONE_HOUR, 445 * %SOUP_COOKIE_MAX_AGE_ONE_DAY, %SOUP_COOKIE_MAX_AGE_ONE_WEEK and 446 * %SOUP_COOKIE_MAX_AGE_ONE_YEAR (or multiples thereof) to calculate 447 * this value. (A value of 0 indicates that the cookie should be 448 * considered already-expired.) 449 * 450 * (This sets the same property as soup_cookie_set_expires().) 451 * 452 * Params: 453 * maxAge = the new max age 454 * 455 * Since: 2.24 456 */ 457 public void setMaxAge(int maxAge) 458 { 459 soup_cookie_set_max_age(soupCookie, maxAge); 460 } 461 462 /** 463 * Sets @cookie's name to @name 464 * 465 * Params: 466 * name = the new name 467 * 468 * Since: 2.24 469 */ 470 public void setName(string name) 471 { 472 soup_cookie_set_name(soupCookie, Str.toStringz(name)); 473 } 474 475 /** 476 * Sets @cookie's path to @path 477 * 478 * Params: 479 * path = the new path 480 * 481 * Since: 2.24 482 */ 483 public void setPath(string path) 484 { 485 soup_cookie_set_path(soupCookie, Str.toStringz(path)); 486 } 487 488 /** 489 * When used in conjunction with soup_cookie_jar_get_cookie_list_with_same_site_info() this 490 * sets the policy of when this cookie should be exposed. 491 * 492 * Params: 493 * policy = a #SoupSameSitePolicy 494 * 495 * Since: 2.70 496 */ 497 public void setSameSitePolicy(SoupSameSitePolicy policy) 498 { 499 soup_cookie_set_same_site_policy(soupCookie, policy); 500 } 501 502 /** 503 * Sets @cookie's secure attribute to @secure. If %TRUE, @cookie will 504 * only be transmitted from the client to the server over secure 505 * (https) connections. 506 * 507 * Params: 508 * secure = the new value for the secure attribute 509 * 510 * Since: 2.24 511 */ 512 public void setSecure(bool secure) 513 { 514 soup_cookie_set_secure(soupCookie, secure); 515 } 516 517 /** 518 * Sets @cookie's value to @value 519 * 520 * Params: 521 * value = the new value 522 * 523 * Since: 2.24 524 */ 525 public void setValue(string value) 526 { 527 soup_cookie_set_value(soupCookie, Str.toStringz(value)); 528 } 529 530 /** 531 * Serializes @cookie in the format used by the Cookie header (ie, for 532 * returning a cookie from a #SoupSession to a server). 533 * 534 * Returns: the header 535 * 536 * Since: 2.24 537 */ 538 public string toCookieHeader() 539 { 540 auto retStr = soup_cookie_to_cookie_header(soupCookie); 541 542 scope(exit) Str.freeString(retStr); 543 return Str.toString(retStr); 544 } 545 546 /** 547 * Serializes @cookie in the format used by the Set-Cookie header 548 * (ie, for sending a cookie from a #SoupServer to a client). 549 * 550 * Returns: the header 551 * 552 * Since: 2.24 553 */ 554 public string toSetCookieHeader() 555 { 556 auto retStr = soup_cookie_to_set_cookie_header(soupCookie); 557 558 scope(exit) Str.freeString(retStr); 559 return Str.toString(retStr); 560 } 561 562 /** 563 * Parses @header and returns a #SoupCookie. (If @header contains 564 * multiple cookies, only the first one will be parsed.) 565 * 566 * If @header does not have "path" or "domain" attributes, they will 567 * be defaulted from @origin. If @origin is %NULL, path will default 568 * to "/", but domain will be left as %NULL. Note that this is not a 569 * valid state for a #SoupCookie, and you will need to fill in some 570 * appropriate string for the domain if you want to actually make use 571 * of the cookie. 572 * 573 * Params: 574 * header = a cookie string (eg, the value of a Set-Cookie header) 575 * origin = origin of the cookie, or %NULL 576 * 577 * Returns: a new #SoupCookie, or %NULL if it could 578 * not be parsed, or contained an illegal "domain" attribute for a 579 * cookie originating from @origin. 580 * 581 * Since: 2.24 582 */ 583 public static Cookie parse(string header, URI origin) 584 { 585 auto __p = soup_cookie_parse(Str.toStringz(header), (origin is null) ? null : origin.getURIStruct()); 586 587 if(__p is null) 588 { 589 return null; 590 } 591 592 return ObjectG.getDObject!(Cookie)(cast(SoupCookie*) __p, true); 593 } 594 }