| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest; |
| 4 |
|
| 5 |
use MediaWiki\Rest\HeaderParser\HttpDate; |
| 6 |
use MediaWiki\Rest\HeaderParser\IfNoneMatch; |
| 7 |
use RuntimeException; |
| 8 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 9 |
|
| 10 |
class ConditionalHeaderUtil { |
| 11 |
|
| 12 |
private $varnishETagHack = true; |
| 13 |
|
| 14 |
private $eTag; |
| 15 |
|
| 16 |
private $lastModified; |
| 17 |
|
| 18 |
private $hasRepresentation; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function setVarnishETagHack( $hack ) { |
| 52 |
$this->varnishETagHack = $hack; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
return null; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private function weakCompare( $resourceETag, $headerETag ) { |
| 148 |
if ( $resourceETag === null || $headerETag === null ) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
return $resourceETag['contents'] === $headerETag['contents']; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 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 |
} |