Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.55% covered (success)
96.55%
56 / 58
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConditionalHeaderUtil
96.55% covered (success)
96.55%
56 / 58
66.67% covered (warning)
66.67%
4 / 6
42
0.00% covered (danger)
0.00%
0 / 1
 setValidators
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setVarnishETagHack
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 checkPreconditions
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
1 / 1
25
 applyResponseHeaders
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
5
 weakCompare
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 strongCompare
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3namespace MediaWiki\Rest;
4
5use MediaWiki\Rest\HeaderParser\HttpDate;
6use MediaWiki\Rest\HeaderParser\IfNoneMatch;
7use RuntimeException;
8use Wikimedia\Timestamp\ConvertibleTimestamp;
9
10class ConditionalHeaderUtil {
11    private $varnishETagHack = true;
12    private $eTag;
13    private $lastModified;
14    private $hasRepresentation;
15
16    /**
17     * Initialize the object with information about the requested resource.
18     *
19     * @param string|null $eTag The entity-tag (including quotes), or null if
20     *   it is unknown.
21     * @param string|int|null $lastModified The Last-Modified date in a format
22     *   accepted by ConvertibleTimestamp, or null if it is unknown.
23     * @param bool|null $hasRepresentation Whether the server has a current
24     *   representation of the target resource. This should be true if the
25     *   resource exists, and false if it does not exist. It is used for
26     *   wildcard validators -- the intended use case is to abort a PUT if the
27     *   resource does (or does not) exist. If null is passed, we assume that
28     *   the resource exists if an ETag was specified for it.
29     */
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
40    /**
41     * If the Varnish ETag hack is disabled by calling this method,
42     * strong ETag comparison will follow RFC 7232, rejecting all weak
43     * ETags for If-Match comparison.
44     *
45     * @param bool $hack
46     */
47    public function setVarnishETagHack( $hack ) {
48        $this->varnishETagHack = $hack;
49    }
50
51    /**
52     * Check conditional request headers in the order required by RFC 7232 section 6.
53     *
54     * @param RequestInterface $request
55     * @return int|null The status code to immediately return, or null to
56     *   continue processing the request.
57     */
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
126    /**
127     * Set Last-Modified and ETag headers in the response according to the cached
128     * values set by setValidators(), which are also used for precondition checks.
129     *
130     * If the headers are already present in the response, the existing headers
131     * take precedence.
132     *
133     * @param ResponseInterface $response
134     */
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
144    /**
145     * The weak comparison function, per RFC 7232, section 2.3.2.
146     *
147     * @param array|null $resourceETag ETag generated by the handler, parsed tag info array
148     * @param array|null $headerETag ETag supplied by the client, parsed tag info array
149     * @return bool
150     */
151    private function weakCompare( $resourceETag, $headerETag ) {
152        if ( $resourceETag === null || $headerETag === null ) {
153            return false;
154        }
155        return $resourceETag['contents'] === $headerETag['contents'];
156    }
157
158    /**
159     * The strong comparison function
160     *
161     * A strong ETag returned by the server may have been "weakened" by Varnish when applying
162     * compression. So optionally ignore the weakness of the header.
163     * {@link https://varnish-cache.org/docs/6.0/users-guide/compression.html}.
164     * @see T238849 and T310710
165     *
166     * @param array|null $resourceETag ETag generated by the handler, parsed tag info array
167     * @param array|null $headerETag ETag supplied by the client, parsed tag info array
168     *
169     * @return bool
170     */
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}