aboutsummaryrefslogtreecommitdiffstats
path: root/includes/Rest/HeaderContainer.php
blob: e0206e99b6abc41afa40b366f6a9d45eed16e1aa (plain) (blame)
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
<?php

namespace MediaWiki\Rest;

/**
 * This is a container for storing headers. The header names are case-insensitive,
 * but the case is preserved for methods that return headers in bulk. The
 * header values are a comma-separated list, or equivalently, an array of strings.
 *
 * Unlike PSR-7, the container is mutable.
 */
class HeaderContainer {
	/** @var array[] */
	private $headerLists = [];
	/** @var string[] */
	private $headerLines = [];
	/** @var string[] */
	private $headerNames = [];

	/**
	 * Erase any existing headers and replace them with the specified
	 * header arrays or values.
	 *
	 * @param array $headers
	 */
	public function resetHeaders( $headers = [] ) {
		$this->headerLines = [];
		$this->headerLists = [];
		$this->headerNames = [];
		foreach ( $headers as $name => $value ) {
			$this->headerNames[ strtolower( $name ) ] = $name;
			[ $valueParts, $valueLine ] = $this->convertToListAndString( $value );
			$this->headerLines[$name] = $valueLine;
			$this->headerLists[$name] = $valueParts;
		}
	}

	/**
	 * Take an input header value, which may either be a string or an array,
	 * and convert it to an array of header values and a header line.
	 *
	 * The return value is an array where element 0 has the array of header
	 * values, and element 1 has the header line.
	 *
	 * Theoretically, if the input is a string, this could parse the string
	 * and split it on commas. Doing this is complicated, because some headers
	 * can contain double-quoted strings containing commas. The User-Agent
	 * header allows commas in comments delimited by parentheses. So it is not
	 * just explode(",", $value), we would need to parse a grammar defined by
	 * RFC 7231 appendix D which depends on header name.
	 *
	 * It's unclear how much it would help handlers to have fully spec-aware
	 * HTTP header handling just to split on commas. They would probably be
	 * better served by an HTTP header parsing library which provides the full
	 * parse tree.
	 *
	 * @param string|string[] $value The input header value
	 * @return array
	 */
	private function convertToListAndString( $value ) {
		if ( is_array( $value ) ) {
			return [ array_values( $value ), implode( ', ', $value ) ];
		} else {
			return [ [ $value ], $value ];
		}
	}

	/**
	 * Set or replace a header
	 *
	 * @param string $name
	 * @param string|string[] $value
	 */
	public function setHeader( $name, $value ) {
		[ $valueParts, $valueLine ] = $this->convertToListAndString( $value );
		$lowerName = strtolower( $name );
		$origName = $this->headerNames[$lowerName] ?? null;
		if ( $origName !== null ) {
			unset( $this->headerLines[$origName] );
			unset( $this->headerLists[$origName] );
		}
		$this->headerNames[$lowerName] = $name;
		$this->headerLines[$name] = $valueLine;
		$this->headerLists[$name] = $valueParts;
	}

	/**
	 * Set a header or append to an existing header
	 *
	 * @param string $name
	 * @param string|string[] $value
	 */
	public function addHeader( $name, $value ) {
		[ $valueParts, $valueLine ] = $this->convertToListAndString( $value );
		$lowerName = strtolower( $name );
		$origName = $this->headerNames[$lowerName] ?? null;
		if ( $origName === null ) {
			$origName = $name;
			$this->headerNames[$lowerName] = $origName;
			$this->headerLines[$origName] = $valueLine;
			$this->headerLists[$origName] = $valueParts;
		} else {
			$this->headerLines[$origName] .= ', ' . $valueLine;
			$this->headerLists[$origName] = array_merge( $this->headerLists[$origName],
				$valueParts );
		}
	}

	/**
	 * Remove a header
	 *
	 * @param string $name
	 */
	public function removeHeader( $name ) {
		$lowerName = strtolower( $name );
		$origName = $this->headerNames[$lowerName] ?? null;
		if ( $origName !== null ) {
			unset( $this->headerNames[$lowerName] );
			unset( $this->headerLines[$origName] );
			unset( $this->headerLists[$origName] );
		}
	}

	/**
	 * Get header arrays indexed by original name
	 *
	 * @return string[][]
	 */
	public function getHeaders() {
		return $this->headerLists;
	}

	/**
	 * Get the header with a particular name, or an empty array if there is no
	 * such header.
	 *
	 * @param string $name
	 * @return string[]
	 */
	public function getHeader( $name ) {
		$headerName = $this->headerNames[ strtolower( $name ) ] ?? null;
		if ( $headerName === null ) {
			return [];
		}
		return $this->headerLists[$headerName];
	}

	/**
	 * Return true if the header exists, false otherwise
	 * @param string $name
	 * @return bool
	 */
	public function hasHeader( $name ) {
		return isset( $this->headerNames[ strtolower( $name ) ] );
	}

	/**
	 * Get the specified header concatenated into a comma-separated string.
	 * If the header does not exist, an empty string is returned.
	 *
	 * @param string $name
	 * @return string
	 */
	public function getHeaderLine( $name ) {
		$headerName = $this->headerNames[ strtolower( $name ) ] ?? null;
		if ( $headerName === null ) {
			return '';
		}
		return $this->headerLines[$headerName];
	}

	/**
	 * Get all header lines
	 *
	 * @return string[]
	 */
	public function getHeaderLines() {
		return $this->headerLines;
	}

	/**
	 * Get an array of strings of the form "Name: Value", suitable for passing
	 * directly to header() to set response headers. The PHP manual describes
	 * these strings as "raw HTTP headers", so we adopt that terminology.
	 *
	 * @return string[] Header list (integer indexed)
	 */
	public function getRawHeaderLines() {
		$lines = [];
		foreach ( $this->headerNames as $lowerName => $name ) {
			if ( $lowerName === 'set-cookie' ) {
				// As noted by RFC 7230 section 3.2.2, Set-Cookie is the only
				// header for which multiple values cannot be concatenated into
				// a single comma-separated line.
				foreach ( $this->headerLists[$name] as $value ) {
					$lines[] = "$name: $value";
				}
			} else {
				$lines[] = "$name: " . $this->headerLines[$name];
			}
		}
		return $lines;
	}
}