2019-09-30 06:15:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Rest;
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Rest\HeaderParser\HttpDate;
|
|
|
|
|
use MediaWiki\Rest\HeaderParser\IfNoneMatch;
|
2024-02-08 23:12:50 +00:00
|
|
|
use RuntimeException;
|
2019-09-30 06:15:30 +00:00
|
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
|
|
|
|
|
|
|
|
|
class ConditionalHeaderUtil {
|
2024-09-07 19:49:56 +00:00
|
|
|
/** @var bool */
|
2022-06-22 12:44:20 +00:00
|
|
|
private $varnishETagHack = true;
|
2024-09-07 19:49:56 +00:00
|
|
|
/** @var string|null */
|
2019-09-30 06:15:30 +00:00
|
|
|
private $eTag;
|
2024-09-07 19:49:56 +00:00
|
|
|
/** @var int|null */
|
2019-09-30 06:15:30 +00:00
|
|
|
private $lastModified;
|
2024-09-07 19:49:56 +00:00
|
|
|
/** @var bool */
|
2019-09-30 06:15:30 +00:00
|
|
|
private $hasRepresentation;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the object with information about the requested resource.
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $eTag The entity-tag (including quotes), or null if
|
|
|
|
|
* it is unknown.
|
|
|
|
|
* @param string|int|null $lastModified The Last-Modified date in a format
|
|
|
|
|
* accepted by ConvertibleTimestamp, or null if it is unknown.
|
|
|
|
|
* @param bool|null $hasRepresentation Whether the server has a current
|
|
|
|
|
* representation of the target resource. This should be true if the
|
|
|
|
|
* resource exists, and false if it does not exist. It is used for
|
|
|
|
|
* wildcard validators -- the intended use case is to abort a PUT if the
|
|
|
|
|
* resource does (or does not) exist. If null is passed, we assume that
|
|
|
|
|
* the resource exists if an ETag was specified for it.
|
|
|
|
|
*/
|
|
|
|
|
public function setValidators( $eTag, $lastModified, $hasRepresentation ) {
|
|
|
|
|
$this->eTag = $eTag;
|
|
|
|
|
if ( $lastModified === null ) {
|
|
|
|
|
$this->lastModified = null;
|
|
|
|
|
} else {
|
2022-02-26 16:28:48 +00:00
|
|
|
$this->lastModified = (int)ConvertibleTimestamp::convert( TS_UNIX, $lastModified );
|
2019-09-30 06:15:30 +00:00
|
|
|
}
|
2022-12-22 07:17:13 +00:00
|
|
|
$this->hasRepresentation = $hasRepresentation ?? ( $eTag !== null );
|
2019-09-30 06:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
2022-06-22 12:44:20 +00:00
|
|
|
/**
|
|
|
|
|
* If the Varnish ETag hack is disabled by calling this method,
|
|
|
|
|
* strong ETag comparison will follow RFC 7232, rejecting all weak
|
|
|
|
|
* ETags for If-Match comparison.
|
|
|
|
|
*
|
|
|
|
|
* @param bool $hack
|
|
|
|
|
*/
|
|
|
|
|
public function setVarnishETagHack( $hack ) {
|
|
|
|
|
$this->varnishETagHack = $hack;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-30 06:15:30 +00:00
|
|
|
/**
|
|
|
|
|
* Check conditional request headers in the order required by RFC 7232 section 6.
|
|
|
|
|
*
|
|
|
|
|
* @param RequestInterface $request
|
|
|
|
|
* @return int|null The status code to immediately return, or null to
|
|
|
|
|
* continue processing the request.
|
|
|
|
|
*/
|
|
|
|
|
public function checkPreconditions( RequestInterface $request ) {
|
|
|
|
|
$parser = new IfNoneMatch;
|
|
|
|
|
if ( $this->eTag !== null ) {
|
|
|
|
|
$resourceTag = $parser->parseETag( $this->eTag );
|
|
|
|
|
if ( !$resourceTag ) {
|
2024-02-08 23:12:50 +00:00
|
|
|
throw new RuntimeException( 'Invalid ETag returned by handler: `' .
|
2024-01-30 16:25:01 +00:00
|
|
|
$parser->getLastError() . '`' );
|
2019-09-30 06:15:30 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$resourceTag = null;
|
|
|
|
|
}
|
|
|
|
|
$getOrHead = in_array( $request->getMethod(), [ 'GET', 'HEAD' ] );
|
|
|
|
|
if ( $request->hasHeader( 'If-Match' ) ) {
|
|
|
|
|
$im = $request->getHeader( 'If-Match' );
|
|
|
|
|
$match = false;
|
|
|
|
|
foreach ( $parser->parseHeaderList( $im ) as $tag ) {
|
2024-08-08 13:33:51 +00:00
|
|
|
if ( ( $tag['whole'] === '*' && $this->hasRepresentation ) ||
|
|
|
|
|
$this->strongCompare( $resourceTag, $tag )
|
|
|
|
|
) {
|
2019-09-30 06:15:30 +00:00
|
|
|
$match = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( !$match ) {
|
|
|
|
|
return 412;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $request->hasHeader( 'If-Unmodified-Since' ) ) {
|
|
|
|
|
$requestDate = HttpDate::parse( $request->getHeader( 'If-Unmodified-Since' )[0] );
|
|
|
|
|
if ( $requestDate !== null
|
|
|
|
|
&& ( $this->lastModified === null || $this->lastModified > $requestDate )
|
|
|
|
|
) {
|
|
|
|
|
return 412;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $request->hasHeader( 'If-None-Match' ) ) {
|
|
|
|
|
$inm = $request->getHeader( 'If-None-Match' );
|
|
|
|
|
foreach ( $parser->parseHeaderList( $inm ) as $tag ) {
|
2024-08-08 13:33:51 +00:00
|
|
|
if ( ( $tag['whole'] === '*' && $this->hasRepresentation ) ||
|
|
|
|
|
$this->weakCompare( $resourceTag, $tag )
|
|
|
|
|
) {
|
2022-10-06 15:05:44 +00:00
|
|
|
return $getOrHead ? 304 : 412;
|
|
|
|
|
}
|
2019-09-30 06:15:30 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( $getOrHead && $request->hasHeader( 'If-Modified-Since' ) ) {
|
|
|
|
|
$requestDate = HttpDate::parse( $request->getHeader( 'If-Modified-Since' )[0] );
|
|
|
|
|
if ( $requestDate !== null && $this->lastModified !== null
|
|
|
|
|
&& $this->lastModified <= $requestDate
|
|
|
|
|
) {
|
|
|
|
|
return 304;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// RFC 7232 states that If-Range should be evaluated here. However, the
|
|
|
|
|
// purpose of If-Range is to cause the Range request header to be
|
|
|
|
|
// conditionally ignored, not to immediately send a response, so it
|
|
|
|
|
// doesn't fit here. RFC 7232 only requires that If-Range be checked
|
|
|
|
|
// after the other conditional header fields, a requirement that is
|
|
|
|
|
// satisfied if it is processed in Handler::execute().
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set Last-Modified and ETag headers in the response according to the cached
|
|
|
|
|
* values set by setValidators(), which are also used for precondition checks.
|
|
|
|
|
*
|
|
|
|
|
* If the headers are already present in the response, the existing headers
|
|
|
|
|
* take precedence.
|
|
|
|
|
*
|
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
|
*/
|
|
|
|
|
public function applyResponseHeaders( ResponseInterface $response ) {
|
|
|
|
|
if ( $this->lastModified !== null && !$response->hasHeader( 'Last-Modified' ) ) {
|
|
|
|
|
$response->setHeader( 'Last-Modified', HttpDate::format( $this->lastModified ) );
|
|
|
|
|
}
|
|
|
|
|
if ( $this->eTag !== null && !$response->hasHeader( 'ETag' ) ) {
|
|
|
|
|
$response->setHeader( 'ETag', $this->eTag );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-22 12:44:20 +00:00
|
|
|
* The weak comparison function, per RFC 7232, section 2.3.2.
|
2019-09-30 06:15:30 +00:00
|
|
|
*
|
2022-06-22 12:44:20 +00:00
|
|
|
* @param array|null $resourceETag ETag generated by the handler, parsed tag info array
|
|
|
|
|
* @param array|null $headerETag ETag supplied by the client, parsed tag info array
|
2019-09-30 06:15:30 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2022-06-22 12:44:20 +00:00
|
|
|
private function weakCompare( $resourceETag, $headerETag ) {
|
|
|
|
|
if ( $resourceETag === null || $headerETag === null ) {
|
2022-03-09 19:29:02 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-06-22 12:44:20 +00:00
|
|
|
return $resourceETag['contents'] === $headerETag['contents'];
|
2019-09-30 06:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The strong comparison function
|
|
|
|
|
*
|
2022-06-22 12:44:20 +00:00
|
|
|
* A strong ETag returned by the server may have been "weakened" by Varnish when applying
|
|
|
|
|
* compression. So optionally ignore the weakness of the header.
|
|
|
|
|
* {@link https://varnish-cache.org/docs/6.0/users-guide/compression.html}.
|
|
|
|
|
* @see T238849 and T310710
|
|
|
|
|
*
|
|
|
|
|
* @param array|null $resourceETag ETag generated by the handler, parsed tag info array
|
|
|
|
|
* @param array|null $headerETag ETag supplied by the client, parsed tag info array
|
|
|
|
|
*
|
2019-09-30 06:15:30 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2022-06-22 12:44:20 +00:00
|
|
|
private function strongCompare( $resourceETag, $headerETag ) {
|
|
|
|
|
if ( $resourceETag === null || $headerETag === null ) {
|
2019-11-22 08:00:47 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2022-06-22 12:44:20 +00:00
|
|
|
|
|
|
|
|
return !$resourceETag['weak']
|
|
|
|
|
&& ( $this->varnishETagHack || !$headerETag['weak'] )
|
|
|
|
|
&& $resourceETag['contents'] === $headerETag['contents'];
|
2019-09-30 06:15:30 +00:00
|
|
|
}
|
2022-06-22 12:44:20 +00:00
|
|
|
|
2019-09-30 06:15:30 +00:00
|
|
|
}
|