MediaWiki REL1_39
ConditionalHeaderUtil.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
7use Wikimedia\Timestamp\ConvertibleTimestamp;
8
10 private $validatorsHaveBeenSet = false;
11 private $varnishETagHack = true;
12 private $eTag;
13 private $lastModified;
14 private $hasRepresentation;
15
30 public function setValidators( $eTag, $lastModified, $hasRepresentation ) {
31 $this->validatorsHaveBeenSet = true;
32 $this->eTag = $eTag;
33 if ( $lastModified === null ) {
34 $this->lastModified = null;
35 } else {
36 $this->lastModified = (int)ConvertibleTimestamp::convert( TS_UNIX, $lastModified );
37 }
38 if ( $hasRepresentation === null ) {
39 $hasRepresentation = $eTag !== null;
40 }
41 $this->hasRepresentation = $hasRepresentation;
42 }
43
51 public function setVarnishETagHack( $hack ) {
52 $this->varnishETagHack = $hack;
53 }
54
62 public function checkPreconditions( RequestInterface $request ) {
63 $parser = new IfNoneMatch;
64 if ( $this->eTag !== null ) {
65 $resourceTag = $parser->parseETag( $this->eTag );
66 if ( !$resourceTag ) {
67 throw new \Exception( 'Invalid ETag returned by handler: ' .
68 $parser->getLastError() );
69 }
70 } else {
71 $resourceTag = null;
72 }
73 $getOrHead = in_array( $request->getMethod(), [ 'GET', 'HEAD' ] );
74 if ( $request->hasHeader( 'If-Match' ) ) {
75 $im = $request->getHeader( 'If-Match' );
76 $match = false;
77 foreach ( $parser->parseHeaderList( $im ) as $tag ) {
78 if ( $tag['whole'] === '*' && $this->hasRepresentation ) {
79 $match = true;
80 break;
81 }
82
83 if ( $this->strongCompare( $resourceTag, $tag ) ) {
84 $match = true;
85 break;
86 }
87 }
88 if ( !$match ) {
89 return 412;
90 }
91 } elseif ( $request->hasHeader( 'If-Unmodified-Since' ) ) {
92 $requestDate = HttpDate::parse( $request->getHeader( 'If-Unmodified-Since' )[0] );
93 if ( $requestDate !== null
94 && ( $this->lastModified === null || $this->lastModified > $requestDate )
95 ) {
96 return 412;
97 }
98 }
99 if ( $request->hasHeader( 'If-None-Match' ) ) {
100 $inm = $request->getHeader( 'If-None-Match' );
101 foreach ( $parser->parseHeaderList( $inm ) as $tag ) {
102 if ( $this->weakCompare( $resourceTag, $tag ) ) {
103 if ( $getOrHead ) {
104 return 304;
105 } else {
106 return 412;
107 }
108 }
109 }
110 } elseif ( $getOrHead && $request->hasHeader( 'If-Modified-Since' ) ) {
111 $requestDate = HttpDate::parse( $request->getHeader( 'If-Modified-Since' )[0] );
112 if ( $requestDate !== null && $this->lastModified !== null
113 && $this->lastModified <= $requestDate
114 ) {
115 return 304;
116 }
117 }
118 // RFC 7232 states that If-Range should be evaluated here. However, the
119 // purpose of If-Range is to cause the Range request header to be
120 // conditionally ignored, not to immediately send a response, so it
121 // doesn't fit here. RFC 7232 only requires that If-Range be checked
122 // after the other conditional header fields, a requirement that is
123 // satisfied if it is processed in Handler::execute().
124 return null;
125 }
126
136 public function applyResponseHeaders( ResponseInterface $response ) {
137 if ( $this->lastModified !== null && !$response->hasHeader( 'Last-Modified' ) ) {
138 $response->setHeader( 'Last-Modified', HttpDate::format( $this->lastModified ) );
139 }
140 if ( $this->eTag !== null && !$response->hasHeader( 'ETag' ) ) {
141 $response->setHeader( 'ETag', $this->eTag );
142 }
143 }
144
152 private function weakCompare( $resourceETag, $headerETag ) {
153 if ( $resourceETag === null || $headerETag === null ) {
154 return false;
155 }
156 return $resourceETag['contents'] === $headerETag['contents'];
157 }
158
172 private function strongCompare( $resourceETag, $headerETag ) {
173 if ( $resourceETag === null || $headerETag === null ) {
174 return false;
175 }
176
177 return !$resourceETag['weak']
178 && ( $this->varnishETagHack || !$headerETag['weak'] )
179 && $resourceETag['contents'] === $headerETag['contents'];
180 }
181
182}
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.
parseETag( $eTag)
Parse an entity-tag such as might be found in an ETag response header.
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.