MediaWiki  master
ConditionalHeaderUtil.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
7 use Wikimedia\Timestamp\ConvertibleTimestamp;
8 
10  private $varnishETagHack = true;
11  private $eTag;
12  private $lastModified;
13  private $hasRepresentation;
14 
29  public function setValidators( $eTag, $lastModified, $hasRepresentation ) {
30  $this->eTag = $eTag;
31  if ( $lastModified === null ) {
32  $this->lastModified = null;
33  } else {
34  $this->lastModified = (int)ConvertibleTimestamp::convert( TS_UNIX, $lastModified );
35  }
36  if ( $hasRepresentation === null ) {
37  $hasRepresentation = $eTag !== null;
38  }
39  $this->hasRepresentation = $hasRepresentation;
40  }
41 
49  public function setVarnishETagHack( $hack ) {
50  $this->varnishETagHack = $hack;
51  }
52 
60  public function checkPreconditions( RequestInterface $request ) {
61  $parser = new IfNoneMatch;
62  if ( $this->eTag !== null ) {
63  $resourceTag = $parser->parseETag( $this->eTag );
64  if ( !$resourceTag ) {
65  throw new \Exception( 'Invalid ETag returned by handler: ' .
66  $parser->getLastError() );
67  }
68  } else {
69  $resourceTag = null;
70  }
71  $getOrHead = in_array( $request->getMethod(), [ 'GET', 'HEAD' ] );
72  if ( $request->hasHeader( 'If-Match' ) ) {
73  $im = $request->getHeader( 'If-Match' );
74  $match = false;
75  foreach ( $parser->parseHeaderList( $im ) as $tag ) {
76  if ( $tag['whole'] === '*' && $this->hasRepresentation ) {
77  $match = true;
78  break;
79  }
80 
81  if ( $this->strongCompare( $resourceTag, $tag ) ) {
82  $match = true;
83  break;
84  }
85  }
86  if ( !$match ) {
87  return 412;
88  }
89  } elseif ( $request->hasHeader( 'If-Unmodified-Since' ) ) {
90  $requestDate = HttpDate::parse( $request->getHeader( 'If-Unmodified-Since' )[0] );
91  if ( $requestDate !== null
92  && ( $this->lastModified === null || $this->lastModified > $requestDate )
93  ) {
94  return 412;
95  }
96  }
97  if ( $request->hasHeader( 'If-None-Match' ) ) {
98  $inm = $request->getHeader( 'If-None-Match' );
99  foreach ( $parser->parseHeaderList( $inm ) as $tag ) {
100  if ( $tag['whole'] === '*' && $this->hasRepresentation ) {
101  return $getOrHead ? 304 : 412;
102  }
103  if ( $this->weakCompare( $resourceTag, $tag ) ) {
104  if ( $getOrHead ) {
105  return 304;
106  } else {
107  return 412;
108  }
109  }
110  }
111  } elseif ( $getOrHead && $request->hasHeader( 'If-Modified-Since' ) ) {
112  $requestDate = HttpDate::parse( $request->getHeader( 'If-Modified-Since' )[0] );
113  if ( $requestDate !== null && $this->lastModified !== null
114  && $this->lastModified <= $requestDate
115  ) {
116  return 304;
117  }
118  }
119  // RFC 7232 states that If-Range should be evaluated here. However, the
120  // purpose of If-Range is to cause the Range request header to be
121  // conditionally ignored, not to immediately send a response, so it
122  // doesn't fit here. RFC 7232 only requires that If-Range be checked
123  // after the other conditional header fields, a requirement that is
124  // satisfied if it is processed in Handler::execute().
125  return null;
126  }
127 
137  public function applyResponseHeaders( ResponseInterface $response ) {
138  if ( $this->lastModified !== null && !$response->hasHeader( 'Last-Modified' ) ) {
139  $response->setHeader( 'Last-Modified', HttpDate::format( $this->lastModified ) );
140  }
141  if ( $this->eTag !== null && !$response->hasHeader( 'ETag' ) ) {
142  $response->setHeader( 'ETag', $this->eTag );
143  }
144  }
145 
153  private function weakCompare( $resourceETag, $headerETag ) {
154  if ( $resourceETag === null || $headerETag === null ) {
155  return false;
156  }
157  return $resourceETag['contents'] === $headerETag['contents'];
158  }
159 
173  private function strongCompare( $resourceETag, $headerETag ) {
174  if ( $resourceETag === null || $headerETag === null ) {
175  return false;
176  }
177 
178  return !$resourceETag['weak']
179  && ( $this->varnishETagHack || !$headerETag['weak'] )
180  && $resourceETag['contents'] === $headerETag['contents'];
181  }
182 
183 }
setVarnishETagHack( $hack)
If the Varnish ETag hack is disabled by calling this method, strong ETag comparison will follow RFC 7...
setValidators( $eTag, $lastModified, $hasRepresentation)
Initialize the object with information about the requested resource.
checkPreconditions(RequestInterface $request)
Check conditional request headers in the order required by RFC 7232 section 6.
applyResponseHeaders(ResponseInterface $response)
Set Last-Modified and ETag headers in the response according to the cached values set by setValidator...
This is a parser for "HTTP-date" as defined by RFC 7231.
Definition: HttpDate.php:23
static parse( $dateString)
Parse an HTTP-date string.
Definition: HttpDate.php:80
static format( $unixTime)
A convenience function to convert a UNIX timestamp to the preferred IMF-fixdate format for HTTP heade...
Definition: HttpDate.php:96
A class to assist with the parsing of If-None-Match, If-Match and ETag headers.
Definition: IfNoneMatch.php:8
parseETag( $eTag)
Parse an entity-tag such as might be found in an ETag response header.
Definition: IfNoneMatch.php:68
A request interface similar to PSR-7's ServerRequestInterface.
getMethod()
Retrieves the HTTP method of the request.
hasHeader( $name)
Checks if a header exists by the given case-insensitive name.
getHeader( $name)
Retrieves a message header value by the given case-insensitive name.
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.
setHeader( $name, $value)
Set or replace the specified header.
hasHeader( $name)
Checks if a header exists by the given case-insensitive name.