| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class HeaderContainer { |
| 13 |
|
| 14 |
private $headerLists = []; |
| 15 |
|
| 16 |
private $headerLines = []; |
| 17 |
|
| 18 |
private $headerNames = []; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function getHeaders() { |
| 130 |
return $this->headerLists; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function hasHeader( $name ) { |
| 154 |
return isset( $this->headerNames[ strtolower( $name ) ] ); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 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 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function getHeaderLines() { |
| 178 |
return $this->headerLines; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function getRawHeaderLines() { |
| 189 |
$lines = []; |
| 190 |
foreach ( $this->headerNames as $lowerName => $name ) { |
| 191 |
if ( $lowerName === 'set-cookie' ) { |
| 192 |
|
| 193 |
|
| 194 |
|
| 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 |
} |