3 namespace MediaWiki\Rest
;
6 * This is a container for storing headers. The header names are case-insensitive,
7 * but the case is preserved for methods that return headers in bulk. The
8 * header values are a comma-separated list, or equivalently, an array of strings.
10 * Unlike PSR-7, the container is mutable.
12 class HeaderContainer
{
13 private $headerLists = [];
14 private $headerLines = [];
15 private $headerNames = [];
18 * Erase any existing headers and replace them with the specified
19 * header arrays or values.
21 * @param array $headers
23 public function resetHeaders( $headers = [] ) {
24 $this->headerLines
= [];
25 $this->headerLists
= [];
26 $this->headerNames
= [];
27 foreach ( $headers as $name => $value ) {
28 $this->headerNames
[ strtolower( $name ) ] = $name;
29 list( $valueParts, $valueLine ) = $this->convertToListAndString( $value );
30 $this->headerLines
[$name] = $valueLine;
31 $this->headerLists
[$name] = $valueParts;
36 * Take an input header value, which may either be a string or an array,
37 * and convert it to an array of header values and a header line.
39 * The return value is an array where element 0 has the array of header
40 * values, and element 1 has the header line.
42 * Theoretically, if the input is a string, this could parse the string
43 * and split it on commas. Doing this is complicated, because some headers
44 * can contain double-quoted strings containing commas. The User-Agent
45 * header allows commas in comments delimited by parentheses. So it is not
46 * just explode(",", $value), we would need to parse a grammar defined by
47 * RFC 7231 appendix D which depends on header name.
49 * It's unclear how much it would help handlers to have fully spec-aware
50 * HTTP header handling just to split on commas. They would probably be
51 * better served by an HTTP header parsing library which provides the full
54 * @param string $name The header name
55 * @param string|string[] $value The input header value
58 private function convertToListAndString( $value ) {
59 if ( is_array( $value ) ) {
60 return [ array_values( $value ), implode( ', ', $value ) ];
62 return [ [ $value ], $value ];
67 * Set or replace a header
70 * @param string|string[] $value
72 public function setHeader( $name, $value ) {
73 list( $valueParts, $valueLine ) = $this->convertToListAndString( $value );
74 $lowerName = strtolower( $name );
75 $origName = $this->headerNames
[$lowerName] ??
null;
76 if ( $origName !== null ) {
77 unset( $this->headerLines
[$origName] );
78 unset( $this->headerLists
[$origName] );
80 $this->headerNames
[$lowerName] = $name;
81 $this->headerLines
[$name] = $valueLine;
82 $this->headerLists
[$name] = $valueParts;
86 * Set a header or append to an existing header
89 * @param string|string[] $value
91 public function addHeader( $name, $value ) {
92 list( $valueParts, $valueLine ) = $this->convertToListAndString( $value );
93 $lowerName = strtolower( $name );
94 $origName = $this->headerNames
[$lowerName] ??
null;
95 if ( $origName === null ) {
97 $this->headerNames
[$lowerName] = $origName;
98 $this->headerLines
[$origName] = $valueLine;
99 $this->headerLists
[$origName] = $valueParts;
101 $this->headerLines
[$origName] .= ', ' . $valueLine;
102 $this->headerLists
[$origName] = array_merge( $this->headerLists
[$origName],
110 * @param string $name
112 public function removeHeader( $name ) {
113 $lowerName = strtolower( $name );
114 $origName = $this->headerNames
[$lowerName] ??
null;
115 if ( $origName !== null ) {
116 unset( $this->headerNames
[$lowerName] );
117 unset( $this->headerLines
[$origName] );
118 unset( $this->headerLists
[$origName] );
123 * Get header arrays indexed by original name
127 public function getHeaders() {
128 return $this->headerLists
;
132 * Get the header with a particular name, or an empty array if there is no
135 * @param string $name
138 public function getHeader( $name ) {
139 $headerName = $this->headerNames
[ strtolower( $name ) ] ??
null;
140 if ( $headerName === null ) {
143 return $this->headerLists
[$headerName];
147 * Return true if the header exists, false otherwise
148 * @param string $name
151 public function hasHeader( $name ) {
152 return isset( $this->headerNames
[ strtolower( $name ) ] );
156 * Get the specified header concatenated into a comma-separated string.
157 * If the header does not exist, an empty string is returned.
159 * @param string $name
162 public function getHeaderLine( $name ) {
163 $headerName = $this->headerNames
[ strtolower( $name ) ] ??
null;
164 if ( $headerName === null ) {
167 return $this->headerLines
[$headerName];
171 * Get all header lines
175 public function getHeaderLines() {
176 return $this->headerLines
;
180 * Get an array of strings of the form "Name: Value", suitable for passing
181 * directly to header() to set response headers. The PHP manual describes
182 * these strings as "raw HTTP headers", so we adopt that terminology.
184 * @return string[] Header list (integer indexed)
186 public function getRawHeaderLines() {
188 foreach ( $this->headerNames
as $lowerName => $name ) {
189 if ( $lowerName === 'set-cookie' ) {
190 // As noted by RFC 7230 section 3.2.2, Set-Cookie is the only
191 // header for which multiple values cannot be concatenated into
192 // a single comma-separated line.
193 foreach ( $this->headerLists
[$name] as $value ) {
194 $lines[] = "$name: $value";
197 $lines[] = "$name: " . $this->headerLines
[$name];