Skip to content

Commit

Permalink
Fix: content.php: last-modified/if-unmodified-since (#3771) (#3772)
Browse files Browse the repository at this point in the history
* Fix: content.php: last-modified/if-unmodified-since (#3771)

Fix exception if server sent invalid Last-Modified header
Add support for Unix time instead of standard date string
Send back standard RFC7231 date string instead of Unix time

* Fix: content.php: if-unmodified-since: cURL API

Use getTimestamp() as cURL expects that and will format the If-Modified-Since header appropriately.
  • Loading branch information
ORelio authored Oct 20, 2023
1 parent 8ff39f6 commit 4f74518
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ function getContents(
if ($cachedResponse) {
$cachedLastModified = $cachedResponse->getHeader('last-modified');
if ($cachedLastModified) {
$cachedLastModified = new \DateTimeImmutable($cachedLastModified);
$config['if_not_modified_since'] = $cachedLastModified->getTimestamp();
try {
// Some servers send Unix timestamp instead of RFC7231 date. Prepend it with @ to allow parsing as DateTime
$cachedLastModified = new \DateTimeImmutable((is_numeric($cachedLastModified) ? '@' : '') . $cachedLastModified);
$config['if_not_modified_since'] = $cachedLastModified->getTimestamp();
} catch (Exception $dateTimeParseFailue) {
// Ignore invalid 'Last-Modified' HTTP header value
}
}
}

Expand Down

0 comments on commit 4f74518

Please sign in to comment.