Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: use correct format for expires, last-modified, and if-modified-s…
…ince headers

Before: Sat, 10 May 2025 18:17:41 +0000
After: Sat, 10 May 2025 18:17:41 GMT

RFC: https://httpwg.org/specs/rfc9110.html#http.date

Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Jun 10, 2025
commit be587def0e1cef7d0e6aa34b0406bea6825f6a25
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function afterController($controller, $methodName, Response $response) {
}

$modifiedSinceHeader = $this->request->getHeader('IF_MODIFIED_SINCE');
if ($modifiedSinceHeader !== '' && $response->getLastModified() !== null && trim($modifiedSinceHeader) === $response->getLastModified()->format(\DateTimeInterface::RFC2822)) {
if ($modifiedSinceHeader !== '' && $response->getLastModified() !== null && trim($modifiedSinceHeader) === $response->getLastModified()->format(\DateTimeInterface::RFC7231)) {
$response->setStatus(Http::STATUS_NOT_MODIFIED);
return $response;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/public/AppFramework/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function cacheFor(int $cacheSeconds, bool $public = false, bool $immutabl
$time = \OCP\Server::get(ITimeFactory::class);
$expires->setTimestamp($time->getTime());
$expires->add(new \DateInterval('PT' . $cacheSeconds . 'S'));
$this->addHeader('Expires', $expires->format(\DateTimeInterface::RFC2822));
$this->addHeader('Expires', $expires->format(\DateTimeInterface::RFC7231));
} else {
$this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
unset($this->headers['Expires']);
Expand Down Expand Up @@ -238,7 +238,7 @@ public function getHeaders() {
];

if ($this->lastModified) {
$mergeWith['Last-Modified'] = $this->lastModified->format(\DateTimeInterface::RFC2822);
$mergeWith['Last-Modified'] = $this->lastModified->format(\DateTimeInterface::RFC7231);
}

if ($this->ETag) {
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/AppFramework/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function testCacheSeconds(): void {

$headers = $this->childResponse->getHeaders();
$this->assertEquals('private, max-age=33, must-revalidate', $headers['Cache-Control']);
$this->assertEquals('Thu, 15 Jan 1970 06:56:40 +0000', $headers['Expires']);
$this->assertEquals('Thu, 15 Jan 1970 06:56:40 GMT', $headers['Expires']);
}


Expand All @@ -239,7 +239,7 @@ public function testEtagLastModifiedHeaders(): void {
$lastModified->setTimestamp(1);
$this->childResponse->setLastModified($lastModified);
$headers = $this->childResponse->getHeaders();
$this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
$this->assertEquals('Thu, 01 Jan 1970 00:00:01 GMT', $headers['Last-Modified']);
}

public function testChainability(): void {
Expand All @@ -257,7 +257,7 @@ public function testChainability(): void {
$this->assertEquals('world', $headers['hello']);
$this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
$this->assertEquals('hi', $this->childResponse->getEtag());
$this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
$this->assertEquals('Thu, 01 Jan 1970 00:00:01 GMT', $headers['Last-Modified']);
$this->assertEquals('private, max-age=33, must-revalidate',
$headers['Cache-Control']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public static function dataModified(): array {
[null, '"etag"', null, '', false],
['etag', '"etag"', null, '', true],

[null, '', $now, $now->format(\DateTimeInterface::RFC2822), true],
[null, '', $now, $now->format(\DateTimeInterface::RFC7231), true],
[null, '', $now, $now->format(\DateTimeInterface::ATOM), false],
[null, '', null, $now->format(\DateTimeInterface::RFC2822), false],
[null, '', null, $now->format(\DateTimeInterface::RFC7231), false],
[null, '', $now, '', false],

['etag', '"etag"', $now, $now->format(\DateTimeInterface::ATOM), true],
['etag', '"etag"', $now, $now->format(\DateTimeInterface::RFC2822), true],
['etag', '"etag"', $now, $now->format(\DateTimeInterface::RFC7231), true],
];
}

Expand Down