MediaWiki  1.34.0
MediaWiki\Rest\HeaderContainer Class Reference

This is a container for storing headers. More...

Public Member Functions

 addHeader ( $name, $value)
 Set a header or append to an existing header. More...
 
 getHeader ( $name)
 Get the header with a particular name, or an empty array if there is no such header. More...
 
 getHeaderLine ( $name)
 Get the specified header concatenated into a comma-separated string. More...
 
 getHeaderLines ()
 Get all header lines. More...
 
 getHeaders ()
 Get header arrays indexed by original name. More...
 
 getRawHeaderLines ()
 Get an array of strings of the form "Name: Value", suitable for passing directly to header() to set response headers. More...
 
 hasHeader ( $name)
 Return true if the header exists, false otherwise. More...
 
 removeHeader ( $name)
 Remove a header. More...
 
 resetHeaders ( $headers=[])
 Erase any existing headers and replace them with the specified header arrays or values. More...
 
 setHeader ( $name, $value)
 Set or replace a header. More...
 

Private Member Functions

 convertToListAndString ( $value)
 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. More...
 

Private Attributes

 $headerLines = []
 
 $headerLists = []
 
 $headerNames = []
 

Detailed Description

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.

Definition at line 12 of file HeaderContainer.php.

Member Function Documentation

◆ addHeader()

MediaWiki\Rest\HeaderContainer::addHeader (   $name,
  $value 
)

Set a header or append to an existing header.

Parameters
string$name
string | string[]$value

Definition at line 90 of file HeaderContainer.php.

References MediaWiki\Rest\HeaderContainer\convertToListAndString().

◆ convertToListAndString()

MediaWiki\Rest\HeaderContainer::convertToListAndString (   $value)
private

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.

Parameters
string | string[]$valueThe input header value
Returns
array

Definition at line 57 of file HeaderContainer.php.

Referenced by MediaWiki\Rest\HeaderContainer\addHeader(), MediaWiki\Rest\HeaderContainer\resetHeaders(), and MediaWiki\Rest\HeaderContainer\setHeader().

◆ getHeader()

MediaWiki\Rest\HeaderContainer::getHeader (   $name)

Get the header with a particular name, or an empty array if there is no such header.

Parameters
string$name
Returns
string[]

Definition at line 137 of file HeaderContainer.php.

◆ getHeaderLine()

MediaWiki\Rest\HeaderContainer::getHeaderLine (   $name)

Get the specified header concatenated into a comma-separated string.

If the header does not exist, an empty string is returned.

Parameters
string$name
Returns
string

Definition at line 161 of file HeaderContainer.php.

◆ getHeaderLines()

MediaWiki\Rest\HeaderContainer::getHeaderLines ( )

Get all header lines.

Returns
string[]

Definition at line 174 of file HeaderContainer.php.

References MediaWiki\Rest\HeaderContainer\$headerLines.

◆ getHeaders()

MediaWiki\Rest\HeaderContainer::getHeaders ( )

Get header arrays indexed by original name.

Returns
string[][]

Definition at line 126 of file HeaderContainer.php.

References MediaWiki\Rest\HeaderContainer\$headerLists.

◆ getRawHeaderLines()

MediaWiki\Rest\HeaderContainer::getRawHeaderLines ( )

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.

Returns
string[] Header list (integer indexed)

Definition at line 185 of file HeaderContainer.php.

References $lines.

◆ hasHeader()

MediaWiki\Rest\HeaderContainer::hasHeader (   $name)

Return true if the header exists, false otherwise.

Parameters
string$name
Returns
bool

Definition at line 150 of file HeaderContainer.php.

◆ removeHeader()

MediaWiki\Rest\HeaderContainer::removeHeader (   $name)

Remove a header.

Parameters
string$name

Definition at line 111 of file HeaderContainer.php.

◆ resetHeaders()

MediaWiki\Rest\HeaderContainer::resetHeaders (   $headers = [])

Erase any existing headers and replace them with the specified header arrays or values.

Parameters
array$headers

Definition at line 23 of file HeaderContainer.php.

References MediaWiki\Rest\HeaderContainer\convertToListAndString().

Referenced by MediaWiki\Rest\RequestBase\setHeaders().

◆ setHeader()

MediaWiki\Rest\HeaderContainer::setHeader (   $name,
  $value 
)

Set or replace a header.

Parameters
string$name
string | string[]$value

Definition at line 71 of file HeaderContainer.php.

References MediaWiki\Rest\HeaderContainer\convertToListAndString().

Member Data Documentation

◆ $headerLines

MediaWiki\Rest\HeaderContainer::$headerLines = []
private

Definition at line 14 of file HeaderContainer.php.

Referenced by MediaWiki\Rest\HeaderContainer\getHeaderLines().

◆ $headerLists

MediaWiki\Rest\HeaderContainer::$headerLists = []
private

Definition at line 13 of file HeaderContainer.php.

Referenced by MediaWiki\Rest\HeaderContainer\getHeaders().

◆ $headerNames

MediaWiki\Rest\HeaderContainer::$headerNames = []
private

Definition at line 15 of file HeaderContainer.php.


The documentation for this class was generated from the following file: