Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / libs / CookieJar.php
blob8f5700abb916b02ebb8d836ec466ba0baf75c77d
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup HTTP
22 /**
23 * Cookie jar to use with MWHttpRequest. Does not handle cookie unsetting.
25 class CookieJar {
26 /** @var Cookie[] */
27 private $cookie = [];
29 /**
30 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
31 * @see Cookie::set()
32 * @param string $name
33 * @param string $value
34 * @param array $attr
36 public function setCookie( $name, $value, $attr ) {
37 /* cookies: case insensitive, so this should work.
38 * We'll still send the cookies back in the same case we got them, though.
40 $index = strtoupper( $name );
42 if ( isset( $this->cookie[$index] ) ) {
43 $this->cookie[$index]->set( $value, $attr );
44 } else {
45 $this->cookie[$index] = new Cookie( $name, $value, $attr );
49 /**
50 * @see Cookie::serializeToHttpRequest
51 * @param string $path
52 * @param string $domain
53 * @return string
55 public function serializeToHttpRequest( $path, $domain ) {
56 $cookies = [];
58 foreach ( $this->cookie as $c ) {
59 $serialized = $c->serializeToHttpRequest( $path, $domain );
61 if ( $serialized ) {
62 $cookies[] = $serialized;
66 return implode( '; ', $cookies );
69 /**
70 * Parse the content of an Set-Cookie HTTP Response header.
72 * @param string $cookie
73 * @param string $domain Cookie's domain
74 * @return null
76 public function parseCookieResponseHeader( $cookie, $domain ) {
77 $len = strlen( 'Set-Cookie:' );
79 if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) {
80 $cookie = substr( $cookie, $len );
83 $bit = array_map( 'trim', explode( ';', $cookie ) );
85 if ( count( $bit ) >= 1 ) {
86 list( $name, $value ) = explode( '=', array_shift( $bit ), 2 );
87 $attr = [];
89 foreach ( $bit as $piece ) {
90 $parts = explode( '=', $piece );
91 if ( count( $parts ) > 1 ) {
92 $attr[strtolower( $parts[0] )] = $parts[1];
93 } else {
94 $attr[strtolower( $parts[0] )] = true;
98 if ( !isset( $attr['domain'] ) ) {
99 $attr['domain'] = $domain;
100 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
101 return null;
104 $this->setCookie( $name, $value, $attr );