| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\HeaderParser; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class IfNoneMatch extends HeaderParserBase { |
| 9 |
|
| 10 |
private $results = []; |
| 11 |
|
| 12 |
|
| 13 |
private $lastError; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function parseHeaderList( $headerList ) { |
| 39 |
$this->lastError = null; |
| 40 |
if ( count( $headerList ) === 1 && $headerList[0] === '*' ) { |
| 41 |
return [ [ |
| 42 |
'weak' => true, |
| 43 |
'contents' => null, |
| 44 |
'whole' => '*' |
| 45 |
] ]; |
| 46 |
} |
| 47 |
$this->results = []; |
| 48 |
try { |
| 49 |
foreach ( $headerList as $header ) { |
| 50 |
$this->parseHeader( $header ); |
| 51 |
} |
| 52 |
return $this->results; |
| 53 |
} catch ( HeaderParserError $e ) { |
| 54 |
$this->lastError = $e->getMessage(); |
| 55 |
return []; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function parseETag( $eTag ) { |
| 70 |
$this->setInput( $eTag ); |
| 71 |
$this->results = []; |
| 72 |
|
| 73 |
try { |
| 74 |
$this->consumeTag(); |
| 75 |
$this->assertEnd(); |
| 76 |
} catch ( HeaderParserError $e ) { |
| 77 |
$this->lastError = $e->getMessage(); |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
return $this->results[0]; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function getLastError() { |
| 90 |
return $this->lastError; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private function parseHeader( $header ) { |
| 100 |
$this->setInput( $header ); |
| 101 |
$this->consumeTagList(); |
| 102 |
$this->assertEnd(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private function consumeTagList() { |
| 111 |
while ( true ) { |
| 112 |
$this->skipWhitespace(); |
| 113 |
$this->consumeTag(); |
| 114 |
$this->skipWhitespace(); |
| 115 |
if ( $this->pos === $this->inputLength ) { |
| 116 |
break; |
| 117 |
} else { |
| 118 |
$this->consumeString( ',' ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private function consumeTag() { |
| 129 |
$weak = false; |
| 130 |
$whole = ''; |
| 131 |
if ( substr( $this->input, $this->pos, 2 ) === 'W/' ) { |
| 132 |
$weak = true; |
| 133 |
$whole .= 'W/'; |
| 134 |
$this->pos += 2; |
| 135 |
} |
| 136 |
$this->consumeString( '"' ); |
| 137 |
if ( !preg_match( '/[!#-~\x80-\xff]*/A', $this->input, $m, 0, $this->pos ) ) { |
| 138 |
$this->error( 'unexpected regex failure' ); |
| 139 |
} |
| 140 |
$contents = $m[0]; |
| 141 |
$this->pos += strlen( $contents ); |
| 142 |
$this->consumeString( '"' ); |
| 143 |
$whole .= '"' . $contents . '"'; |
| 144 |
$this->results[] = [ |
| 145 |
'weak' => $weak, |
| 146 |
'contents' => $contents, |
| 147 |
'whole' => $whole |
| 148 |
]; |
| 149 |
} |
| 150 |
} |