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