MediaWiki  master
HeaderContainer.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
13  private $headerLists = [];
14  private $headerLines = [];
15  private $headerNames = [];
16 
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;
32  }
33  }
34 
57  private function convertToListAndString( $value ) {
58  if ( is_array( $value ) ) {
59  return [ array_values( $value ), implode( ', ', $value ) ];
60  } else {
61  return [ [ $value ], $value ];
62  }
63  }
64 
71  public function setHeader( $name, $value ) {
72  list( $valueParts, $valueLine ) = $this->convertToListAndString( $value );
73  $lowerName = strtolower( $name );
74  $origName = $this->headerNames[$lowerName] ?? null;
75  if ( $origName !== null ) {
76  unset( $this->headerLines[$origName] );
77  unset( $this->headerLists[$origName] );
78  }
79  $this->headerNames[$lowerName] = $name;
80  $this->headerLines[$name] = $valueLine;
81  $this->headerLists[$name] = $valueParts;
82  }
83 
90  public function addHeader( $name, $value ) {
91  list( $valueParts, $valueLine ) = $this->convertToListAndString( $value );
92  $lowerName = strtolower( $name );
93  $origName = $this->headerNames[$lowerName] ?? null;
94  if ( $origName === null ) {
95  $origName = $name;
96  $this->headerNames[$lowerName] = $origName;
97  $this->headerLines[$origName] = $valueLine;
98  $this->headerLists[$origName] = $valueParts;
99  } else {
100  $this->headerLines[$origName] .= ', ' . $valueLine;
101  $this->headerLists[$origName] = array_merge( $this->headerLists[$origName],
102  $valueParts );
103  }
104  }
105 
111  public function removeHeader( $name ) {
112  $lowerName = strtolower( $name );
113  $origName = $this->headerNames[$lowerName] ?? null;
114  if ( $origName !== null ) {
115  unset( $this->headerNames[$lowerName] );
116  unset( $this->headerLines[$origName] );
117  unset( $this->headerLists[$origName] );
118  }
119  }
120 
126  public function getHeaders() {
127  return $this->headerLists;
128  }
129 
137  public function getHeader( $name ) {
138  $headerName = $this->headerNames[ strtolower( $name ) ] ?? null;
139  if ( $headerName === null ) {
140  return [];
141  }
142  return $this->headerLists[$headerName];
143  }
144 
150  public function hasHeader( $name ) {
151  return isset( $this->headerNames[ strtolower( $name ) ] );
152  }
153 
161  public function getHeaderLine( $name ) {
162  $headerName = $this->headerNames[ strtolower( $name ) ] ?? null;
163  if ( $headerName === null ) {
164  return '';
165  }
166  return $this->headerLines[$headerName];
167  }
168 
174  public function getHeaderLines() {
175  return $this->headerLines;
176  }
177 
185  public function getRawHeaderLines() {
186  $lines = [];
187  foreach ( $this->headerNames as $lowerName => $name ) {
188  if ( $lowerName === 'set-cookie' ) {
189  // As noted by RFC 7230 section 3.2.2, Set-Cookie is the only
190  // header for which multiple values cannot be concatenated into
191  // a single comma-separated line.
192  foreach ( $this->headerLists[$name] as $value ) {
193  $lines[] = "$name: $value";
194  }
195  } else {
196  $lines[] = "$name: " . $this->headerLines[$name];
197  }
198  }
199  return $lines;
200  }
201 }
MediaWiki\Rest\HeaderContainer\$headerLines
$headerLines
Definition: HeaderContainer.php:14
MediaWiki\Rest\HeaderContainer\removeHeader
removeHeader( $name)
Remove a header.
Definition: HeaderContainer.php:111
MediaWiki\Rest\HeaderContainer\getRawHeaderLines
getRawHeaderLines()
Get an array of strings of the form "Name: Value", suitable for passing directly to header() to set r...
Definition: HeaderContainer.php:185
MediaWiki\Rest\HeaderContainer\resetHeaders
resetHeaders( $headers=[])
Erase any existing headers and replace them with the specified header arrays or values.
Definition: HeaderContainer.php:23
MediaWiki\Rest\HeaderContainer\addHeader
addHeader( $name, $value)
Set a header or append to an existing header.
Definition: HeaderContainer.php:90
MediaWiki\Rest\HeaderContainer\$headerLists
$headerLists
Definition: HeaderContainer.php:13
MediaWiki\Rest\HeaderContainer\getHeaders
getHeaders()
Get header arrays indexed by original name.
Definition: HeaderContainer.php:126
MediaWiki\Rest\HeaderContainer\hasHeader
hasHeader( $name)
Return true if the header exists, false otherwise.
Definition: HeaderContainer.php:150
MediaWiki\Rest\HeaderContainer\$headerNames
$headerNames
Definition: HeaderContainer.php:15
MediaWiki\Rest
MediaWiki\Rest\HeaderContainer\setHeader
setHeader( $name, $value)
Set or replace a header.
Definition: HeaderContainer.php:71
MediaWiki\Rest\HeaderContainer\getHeaderLine
getHeaderLine( $name)
Get the specified header concatenated into a comma-separated string.
Definition: HeaderContainer.php:161
$lines
if(!file_exists( $CREDITS)) $lines
Definition: updateCredits.php:49
MediaWiki\Rest\HeaderContainer\getHeader
getHeader( $name)
Get the header with a particular name, or an empty array if there is no such header.
Definition: HeaderContainer.php:137
MediaWiki\Rest\HeaderContainer\getHeaderLines
getHeaderLines()
Get all header lines.
Definition: HeaderContainer.php:174
MediaWiki\Rest\HeaderContainer\convertToListAndString
convertToListAndString( $value)
Take an input header value, which may either be a string or an array, and convert it to an array of h...
Definition: HeaderContainer.php:57
MediaWiki\Rest\HeaderContainer
This is a container for storing headers.
Definition: HeaderContainer.php:12