MediaWiki master
IfNoneMatch.php
Go to the documentation of this file.
1<?php
2
4
10 private $results = [];
11
13 private $lastError;
14
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
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 /* @phan-suppress-next-line PhanTypeInvalidDimOffset */
81 return $this->results[0];
82 }
83
89 public function getLastError() {
90 return $this->lastError;
91 }
92
99 private function parseHeader( $header ) {
100 $this->setInput( $header );
101 $this->consumeTagList();
102 $this->assertEnd();
103 }
104
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
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}
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.
getLastError()
Get the last parse error message, or null if there was none.
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