MediaWiki REL1_37
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 $eTag;
14
30 $this->validatorsHaveBeenSet = true;
31 $this->eTag = $eTag;
32 if ( $lastModified === null ) {
33 $this->lastModified = null;
34 } else {
35 $this->lastModified = ConvertibleTimestamp::convert( TS_UNIX, $lastModified );
36 }
37 if ( $hasRepresentation === null ) {
38 $hasRepresentation = $eTag !== null;
39 }
40 $this->hasRepresentation = $hasRepresentation;
41 }
42
50 public function checkPreconditions( RequestInterface $request ) {
51 $parser = new IfNoneMatch;
52 if ( $this->eTag !== null ) {
53 $resourceTag = $parser->parseETag( $this->eTag );
54 if ( !$resourceTag ) {
55 throw new \Exception( 'Invalid ETag returned by handler: ' .
56 $parser->getLastError() );
57 }
58 } else {
59 $resourceTag = null;
60 }
61 $getOrHead = in_array( $request->getMethod(), [ 'GET', 'HEAD' ] );
62 if ( $request->hasHeader( 'If-Match' ) ) {
63 $im = $request->getHeader( 'If-Match' );
64 $match = false;
65 foreach ( $parser->parseHeaderList( $im ) as $tag ) {
66 if ( $tag['whole'] === '*' && $this->hasRepresentation ) {
67 $match = true;
68 break;
69 }
70 if ( $this->strongCompare( $resourceTag, $tag ) ) {
71 $match = true;
72 break;
73 }
74 }
75 if ( !$match ) {
76 return 412;
77 }
78 } elseif ( $request->hasHeader( 'If-Unmodified-Since' ) ) {
79 $requestDate = HttpDate::parse( $request->getHeader( 'If-Unmodified-Since' )[0] );
80 if ( $requestDate !== null
81 && ( $this->lastModified === null || $this->lastModified > $requestDate )
82 ) {
83 return 412;
84 }
85 }
86 if ( $request->hasHeader( 'If-None-Match' ) ) {
87 $inm = $request->getHeader( 'If-None-Match' );
88 foreach ( $parser->parseHeaderList( $inm ) as $tag ) {
89 if ( $this->weakCompare( $resourceTag, $tag ) ) {
90 if ( $getOrHead ) {
91 return 304;
92 } else {
93 return 412;
94 }
95 }
96 }
97 } elseif ( $getOrHead && $request->hasHeader( 'If-Modified-Since' ) ) {
98 $requestDate = HttpDate::parse( $request->getHeader( 'If-Modified-Since' )[0] );
99 if ( $requestDate !== null && $this->lastModified !== null
100 && $this->lastModified <= $requestDate
101 ) {
102 return 304;
103 }
104 }
105 // RFC 7232 states that If-Range should be evaluated here. However, the
106 // purpose of If-Range is to cause the Range request header to be
107 // conditionally ignored, not to immediately send a response, so it
108 // doesn't fit here. RFC 7232 only requires that If-Range be checked
109 // after the other conditional header fields, a requirement that is
110 // satisfied if it is processed in Handler::execute().
111 return null;
112 }
113
123 public function applyResponseHeaders( ResponseInterface $response ) {
124 if ( $this->lastModified !== null && !$response->hasHeader( 'Last-Modified' ) ) {
125 $response->setHeader( 'Last-Modified', HttpDate::format( $this->lastModified ) );
126 }
127 if ( $this->eTag !== null && !$response->hasHeader( 'ETag' ) ) {
128 $response->setHeader( 'ETag', $this->eTag );
129 }
130 }
131
139 private function weakCompare( $tag1, $tag2 ) {
140 return $tag1['contents'] === $tag2['contents'];
141 }
142
150 private function strongCompare( $tag1, $tag2 ) {
151 if ( $tag1 === null || $tag2 === null ) {
152 return false;
153 }
154 return !$tag1['weak'] && !$tag2['weak'] && $tag1['contents'] === $tag2['contents'];
155 }
156}
weakCompare( $tag1, $tag2)
The weak comparison function.
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.
strongCompare( $tag1, $tag2)
The strong comparison function.
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:73
static format( $unixTime)
A convenience function to convert a UNIX timestamp to the preferred IMF-fixdate format for HTTP heade...
Definition HttpDate.php:89
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.