Skip to content

Commit

Permalink
Fix pre-condition precedence
Browse files Browse the repository at this point in the history
According to RFC7232 (https://tools.ietf.org/html/rfc7232#section-6),
If-None-Match Must be checked before If-Modidied-Since
  • Loading branch information
adoy committed Feb 28, 2019
1 parent 1ebc898 commit e56d164
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,6 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
}
}

// Last-Modified header and conditional GET check
$lastModified = $response->getHeaderLine('Last-Modified');

if ($lastModified) {
if (!is_integer($lastModified)) {
$lastModified = strtotime($lastModified);
}

$ifModifiedSince = $request->getHeaderLine('If-Modified-Since');

if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) {
return $response->withStatus(304);
}
}

// ETag header and conditional GET check
$etag = $response->getHeader('ETag');
$etag = reset($etag);
Expand All @@ -102,6 +87,22 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
}
}


// Last-Modified header and conditional GET check
$lastModified = $response->getHeaderLine('Last-Modified');

if ($lastModified) {
if (!is_integer($lastModified)) {
$lastModified = strtotime($lastModified);
}

$ifModifiedSince = $request->getHeaderLine('If-Modified-Since');

if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) {
return $response->withStatus(304);
}
}

return $response;
}
}

0 comments on commit e56d164

Please sign in to comment.