MediaWiki master
HeaderContainer.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
14 private $headerLists = [];
16 private $headerLines = [];
18 private $headerNames = [];
19
26 public function resetHeaders( $headers = [] ) {
27 $this->headerLines = [];
28 $this->headerLists = [];
29 $this->headerNames = [];
30 foreach ( $headers as $name => $value ) {
31 $this->headerNames[ strtolower( $name ) ] = $name;
32 [ $valueParts, $valueLine ] = $this->convertToListAndString( $value );
33 $this->headerLines[$name] = $valueLine;
34 $this->headerLists[$name] = $valueParts;
35 }
36 }
37
60 private function convertToListAndString( $value ) {
61 if ( is_array( $value ) ) {
62 return [ array_values( $value ), implode( ', ', $value ) ];
63 } else {
64 return [ [ $value ], $value ];
65 }
66 }
67
74 public function setHeader( $name, $value ) {
75 [ $valueParts, $valueLine ] = $this->convertToListAndString( $value );
76 $lowerName = strtolower( $name );
77 $origName = $this->headerNames[$lowerName] ?? null;
78 if ( $origName !== null ) {
79 unset( $this->headerLines[$origName] );
80 unset( $this->headerLists[$origName] );
81 }
82 $this->headerNames[$lowerName] = $name;
83 $this->headerLines[$name] = $valueLine;
84 $this->headerLists[$name] = $valueParts;
85 }
86
93 public function addHeader( $name, $value ) {
94 [ $valueParts, $valueLine ] = $this->convertToListAndString( $value );
95 $lowerName = strtolower( $name );
96 $origName = $this->headerNames[$lowerName] ?? null;
97 if ( $origName === null ) {
98 $origName = $name;
99 $this->headerNames[$lowerName] = $origName;
100 $this->headerLines[$origName] = $valueLine;
101 $this->headerLists[$origName] = $valueParts;
102 } else {
103 $this->headerLines[$origName] .= ', ' . $valueLine;
104 $this->headerLists[$origName] = array_merge( $this->headerLists[$origName],
105 $valueParts );
106 }
107 }
108
114 public function removeHeader( $name ) {
115 $lowerName = strtolower( $name );
116 $origName = $this->headerNames[$lowerName] ?? null;
117 if ( $origName !== null ) {
118 unset( $this->headerNames[$lowerName] );
119 unset( $this->headerLines[$origName] );
120 unset( $this->headerLists[$origName] );
121 }
122 }
123
129 public function getHeaders() {
130 return $this->headerLists;
131 }
132
140 public function getHeader( $name ) {
141 $headerName = $this->headerNames[ strtolower( $name ) ] ?? null;
142 if ( $headerName === null ) {
143 return [];
144 }
145 return $this->headerLists[$headerName];
146 }
147
153 public function hasHeader( $name ) {
154 return isset( $this->headerNames[ strtolower( $name ) ] );
155 }
156
164 public function getHeaderLine( $name ) {
165 $headerName = $this->headerNames[ strtolower( $name ) ] ?? null;
166 if ( $headerName === null ) {
167 return '';
168 }
169 return $this->headerLines[$headerName];
170 }
171
177 public function getHeaderLines() {
178 return $this->headerLines;
179 }
180
188 public function getRawHeaderLines() {
189 $lines = [];
190 foreach ( $this->headerNames as $lowerName => $name ) {
191 if ( $lowerName === 'set-cookie' ) {
192 // As noted by RFC 7230 section 3.2.2, Set-Cookie is the only
193 // header for which multiple values cannot be concatenated into
194 // a single comma-separated line.
195 foreach ( $this->headerLists[$name] as $value ) {
196 $lines[] = "$name: $value";
197 }
198 } else {
199 $lines[] = "$name: " . $this->headerLines[$name];
200 }
201 }
202 return $lines;
203 }
204}
This is a container for storing headers.
addHeader( $name, $value)
Set a header or append to an existing header.
getHeader( $name)
Get the header with a particular name, or an empty array if there is no such header.
removeHeader( $name)
Remove a header.
getHeaders()
Get header arrays indexed by original name.
resetHeaders( $headers=[])
Erase any existing headers and replace them with the specified header arrays or values.
setHeader( $name, $value)
Set or replace a header.
getRawHeaderLines()
Get an array of strings of the form "Name: Value", suitable for passing directly to header() to set r...
hasHeader( $name)
Return true if the header exists, false otherwise.
getHeaderLine( $name)
Get the specified header concatenated into a comma-separated string.
getHeaderLines()
Get all header lines.
if(!file_exists( $CREDITS)) $lines