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