MediaWiki master
CookieJar.php
Go to the documentation of this file.
1<?php
7
8namespace Wikimedia;
9
13class CookieJar {
14
16 private array $cookie = [];
17
25 public function setCookie( string $name, string $value, array $attr ): void {
26 /* cookies: case insensitive, so this should work.
27 * We'll still send the cookies back in the same case we got them, though.
28 */
29 $index = strtoupper( $name );
30
31 if ( isset( $this->cookie[$index] ) ) {
32 $this->cookie[$index]->set( $value, $attr );
33 } else {
34 $this->cookie[$index] = new Cookie( $name, $value, $attr );
35 }
36 }
37
41 public function serializeToHttpRequest( string $path, string $domain ): string {
42 $cookies = [];
43
44 foreach ( $this->cookie as $c ) {
45 $serialized = $c->serializeToHttpRequest( $path, $domain );
46 if ( $serialized ) {
47 $cookies[] = $serialized;
48 }
49 }
50
51 return implode( '; ', $cookies );
52 }
53
60 public function parseCookieResponseHeader( string $cookie, string $domain ): void {
61 $bit = array_map( 'trim', explode( ';', $cookie ) );
62
63 $parts = explode( '=', array_shift( $bit ), 2 );
64 $name = $parts[0];
65 $value = $parts[1] ?? '';
66
67 $attr = [];
68 foreach ( $bit as $piece ) {
69 $parts = explode( '=', $piece, 2 );
70 $attr[ strtolower( $parts[0] ) ] = $parts[1] ?? true;
71 }
72
73 if ( !isset( $attr['domain'] ) ) {
74 $attr['domain'] = $domain;
75 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
76 return;
77 }
78
79 $this->setCookie( $name, $value, $attr );
80 }
81
82}
83
85class_alias( CookieJar::class, 'CookieJar' );
Cookie jar to use with MWHttpRequest.
Definition CookieJar.php:13
parseCookieResponseHeader(string $cookie, string $domain)
Parse the content of an Set-Cookie HTTP Response header.
Definition CookieJar.php:60
serializeToHttpRequest(string $path, string $domain)
Definition CookieJar.php:41
setCookie(string $name, string $value, array $attr)
Set a cookie in the cookie jar.
Definition CookieJar.php:25
Cookie for HTTP requests.
Definition Cookie.php:16