MediaWiki 1.40.4
ConditionalHeaderUtil.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
7use 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 $this->hasRepresentation = $hasRepresentation ?? ( $eTag !== null );
37 }
38
46 public function setVarnishETagHack( $hack ) {
47 $this->varnishETagHack = $hack;
48 }
49
57 public function checkPreconditions( RequestInterface $request ) {
58 $parser = new IfNoneMatch;
59 if ( $this->eTag !== null ) {
60 $resourceTag = $parser->parseETag( $this->eTag );
61 if ( !$resourceTag ) {
62 throw new \Exception( 'Invalid ETag returned by handler: ' .
63 $parser->getLastError() );
64 }
65 } else {
66 $resourceTag = null;
67 }
68 $getOrHead = in_array( $request->getMethod(), [ 'GET', 'HEAD' ] );
69 if ( $request->hasHeader( 'If-Match' ) ) {
70 $im = $request->getHeader( 'If-Match' );
71 $match = false;
72 foreach ( $parser->parseHeaderList( $im ) as $tag ) {
73 if ( $tag['whole'] === '*' && $this->hasRepresentation ) {
74 $match = true;
75 break;
76 }
77
78 if ( $this->strongCompare( $resourceTag, $tag ) ) {
79 $match = true;
80 break;
81 }
82 }
83 if ( !$match ) {
84 return 412;
85 }
86 } elseif ( $request->hasHeader( 'If-Unmodified-Since' ) ) {
87 $requestDate = HttpDate::parse( $request->getHeader( 'If-Unmodified-Since' )[0] );
88 if ( $requestDate !== null
89 && ( $this->lastModified === null || $this->lastModified > $requestDate )
90 ) {
91 return 412;
92 }
93 }
94 if ( $request->hasHeader( 'If-None-Match' ) ) {
95 $inm = $request->getHeader( 'If-None-Match' );
96 foreach ( $parser->parseHeaderList( $inm ) as $tag ) {
97 if ( $tag['whole'] === '*' && $this->hasRepresentation ) {
98 return $getOrHead ? 304 : 412;
99 }
100 if ( $this->weakCompare( $resourceTag, $tag ) ) {
101 if ( $getOrHead ) {
102 return 304;
103 } else {
104 return 412;
105 }
106 }
107 }
108 } elseif ( $getOrHead && $request->hasHeader( 'If-Modified-Since' ) ) {
109 $requestDate = HttpDate::parse( $request->getHeader( 'If-Modified-Since' )[0] );
110 if ( $requestDate !== null && $this->lastModified !== null
111 && $this->lastModified <= $requestDate
112 ) {
113 return 304;
114 }
115 }
116 // RFC 7232 states that If-Range should be evaluated here. However, the
117 // purpose of If-Range is to cause the Range request header to be
118 // conditionally ignored, not to immediately send a response, so it
119 // doesn't fit here. RFC 7232 only requires that If-Range be checked
120 // after the other conditional header fields, a requirement that is
121 // satisfied if it is processed in Handler::execute().
122 return null;
123 }
124
134 public function applyResponseHeaders( ResponseInterface $response ) {
135 if ( $this->lastModified !== null && !$response->hasHeader( 'Last-Modified' ) ) {
136 $response->setHeader( 'Last-Modified', HttpDate::format( $this->lastModified ) );
137 }
138 if ( $this->eTag !== null && !$response->hasHeader( 'ETag' ) ) {
139 $response->setHeader( 'ETag', $this->eTag );
140 }
141 }
142
150 private function weakCompare( $resourceETag, $headerETag ) {
151 if ( $resourceETag === null || $headerETag === null ) {
152 return false;
153 }
154 return $resourceETag['contents'] === $headerETag['contents'];
155 }
156
170 private function strongCompare( $resourceETag, $headerETag ) {
171 if ( $resourceETag === null || $headerETag === null ) {
172 return false;
173 }
174
175 return !$resourceETag['weak']
176 && ( $this->varnishETagHack || !$headerETag['weak'] )
177 && $resourceETag['contents'] === $headerETag['contents'];
178 }
179
180}
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.