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
12 private $varnishETagHack = true;
14 private $eTag;
16 private $lastModified;
18 private $hasRepresentation;
19
34 public function setValidators( $eTag, $lastModified, $hasRepresentation ) {
35 $this->eTag = $eTag;
36 if ( $lastModified === null ) {
37 $this->lastModified = null;
38 } else {
39 $this->lastModified = (int)ConvertibleTimestamp::convert( TS_UNIX, $lastModified );
40 }
41 $this->hasRepresentation = $hasRepresentation ?? ( $eTag !== null );
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 RuntimeException( '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 $this->strongCompare( $resourceTag, $tag )
80 ) {
81 $match = true;
82 break;
83 }
84 }
85 if ( !$match ) {
86 return 412;
87 }
88 } elseif ( $request->hasHeader( 'If-Unmodified-Since' ) ) {
89 $requestDate = HttpDate::parse( $request->getHeader( 'If-Unmodified-Since' )[0] );
90 if ( $requestDate !== null
91 && ( $this->lastModified === null || $this->lastModified > $requestDate )
92 ) {
93 return 412;
94 }
95 }
96 if ( $request->hasHeader( 'If-None-Match' ) ) {
97 $inm = $request->getHeader( 'If-None-Match' );
98 foreach ( $parser->parseHeaderList( $inm ) as $tag ) {
99 if ( ( $tag['whole'] === '*' && $this->hasRepresentation ) ||
100 $this->weakCompare( $resourceTag, $tag )
101 ) {
102 return $getOrHead ? 304 : 412;
103 }
104 }
105 } elseif ( $getOrHead && $request->hasHeader( 'If-Modified-Since' ) ) {
106 $requestDate = HttpDate::parse( $request->getHeader( 'If-Modified-Since' )[0] );
107 if ( $requestDate !== null && $this->lastModified !== null
108 && $this->lastModified <= $requestDate
109 ) {
110 return 304;
111 }
112 }
113 // RFC 7232 states that If-Range should be evaluated here. However, the
114 // purpose of If-Range is to cause the Range request header to be
115 // conditionally ignored, not to immediately send a response, so it
116 // doesn't fit here. RFC 7232 only requires that If-Range be checked
117 // after the other conditional header fields, a requirement that is
118 // satisfied if it is processed in Handler::execute().
119 return null;
120 }
121
131 public function applyResponseHeaders( ResponseInterface $response ) {
132 if ( $this->lastModified !== null && !$response->hasHeader( 'Last-Modified' ) ) {
133 $response->setHeader( 'Last-Modified', HttpDate::format( $this->lastModified ) );
134 }
135 if ( $this->eTag !== null && !$response->hasHeader( 'ETag' ) ) {
136 $response->setHeader( 'ETag', $this->eTag );
137 }
138 }
139
147 private function weakCompare( $resourceETag, $headerETag ) {
148 if ( $resourceETag === null || $headerETag === null ) {
149 return false;
150 }
151 return $resourceETag['contents'] === $headerETag['contents'];
152 }
153
167 private function strongCompare( $resourceETag, $headerETag ) {
168 if ( $resourceETag === null || $headerETag === null ) {
169 return false;
170 }
171
172 return !$resourceETag['weak']
173 && ( $this->varnishETagHack || !$headerETag['weak'] )
174 && $resourceETag['contents'] === $headerETag['contents'];
175 }
176
177}
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.