MediaWiki REL1_37
IfNoneMatch.php
Go to the documentation of this file.
1<?php
2
4
9 private $results = [];
10
12 private $lastError;
13
37 public function parseHeaderList( $headerList ) {
38 $this->lastError = null;
39 if ( count( $headerList ) === 1 && $headerList[0] === '*' ) {
40 return [ [
41 'weak' => true,
42 'contents' => null,
43 'whole' => '*'
44 ] ];
45 }
46 $this->results = [];
47 try {
48 foreach ( $headerList as $header ) {
49 $this->parseHeader( $header );
50 }
51 return $this->results;
52 } catch ( HeaderParserError $e ) {
53 $this->lastError = $e->getMessage();
54 return [];
55 }
56 }
57
68 public function parseETag( $eTag ) {
69 $this->setInput( $eTag );
70 $this->results = [];
71
72 try {
73 $this->consumeTag();
74 $this->assertEnd();
75 } catch ( HeaderParserError $e ) {
76 $this->lastError = $e->getMessage();
77 return null;
78 }
79 /* @phan-suppress-next-line PhanTypeInvalidDimOffset */
80 return $this->results[0];
81 }
82
88 public function getLastError() {
89 return $this->lastError;
90 }
91
98 private function parseHeader( $header ) {
99 $this->setInput( $header );
100 $this->consumeTagList();
101 $this->assertEnd();
102 }
103
109 private function consumeTagList() {
110 while ( true ) {
111 $this->skipWhitespace();
112 $this->consumeTag();
113 $this->skipWhitespace();
114 if ( $this->pos === $this->inputLength ) {
115 break;
116 } else {
117 $this->consumeString( ',' );
118 }
119 }
120 }
121
127 private function consumeTag() {
128 $weak = false;
129 $whole = '';
130 if ( substr( $this->input, $this->pos, 2 ) === 'W/' ) {
131 $weak = true;
132 $whole .= 'W/';
133 $this->pos += 2;
134 }
135 $this->consumeString( '"' );
136 if ( !preg_match( '/[!#-~\x80-\xff]*/A', $this->input, $m, 0, $this->pos ) ) {
137 $this->error( 'unexpected regex failure' );
138 }
139 $contents = $m[0];
140 $this->pos += strlen( $contents );
141 $this->consumeString( '"' );
142 $whole .= '"' . $contents . '"';
143 $this->results[] = [
144 'weak' => $weak,
145 'contents' => $contents,
146 'whole' => $whole
147 ];
148 }
149}
setInput( $input)
Set the input, and derived convenience properties.
assertEnd()
If the position is not at the end of the input string, raise an error, complaining of trailing charac...
consumeString( $s)
Consume a specified string, or throw an exception.
skipWhitespace()
Skip whitespace at the input position (OWS)
error( $message)
Throw an exception to indicate a parse error.
A class to assist with the parsing of If-None-Match, If-Match and ETag headers.
consumeTagList()
Consume a comma-separated list of entity-tags.
getLastError()
Get the last parse error message, or null if there was none.
consumeTag()
Consume a single entity-tag and append to the result array.
parseHeader( $header)
Parse a single header.
parseETag( $eTag)
Parse an entity-tag such as might be found in an ETag response header.
parseHeaderList( $headerList)
Parse an If-None-Match or If-Match header list as returned by RequestInterface::getHeader().
$header