Skip to content

Commit

Permalink
fix: DownloadResponse cache headers
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Oct 25, 2024
1 parent 11d9721 commit d3e1e51
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
2 changes: 2 additions & 0 deletions system/Exceptions/DownloadException.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static function forNotFoundDownloadSource()
}

/**
* @deprecated Since v4.5.6
*
* @return static
*/
public static function forCannotSetCache()
Expand Down
12 changes: 0 additions & 12 deletions system/HTTP/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,6 @@ public function noCache(): self
return $this;
}

/**
* Disables cache configuration.
*
* @throws DownloadException
*/
public function setCache(array $options = [])
{
throw DownloadException::forCannotSetCache();
}

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -290,10 +280,8 @@ public function buildHeaders()
$this->setHeader('Content-Disposition', $this->getContentDisposition());
}

$this->setHeader('Expires-Disposition', '0');
$this->setHeader('Content-Transfer-Encoding', 'binary');
$this->setHeader('Content-Length', (string) $this->getContentLength());
$this->noCache();
}

/**
Expand Down
50 changes: 43 additions & 7 deletions tests/system/HTTP/DownloadResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,25 @@ public function testNoCache(): void
$this->assertSame('private, no-transform, no-store, must-revalidate', $response->getHeaderLine('Cache-control'));
}

public function testCantSetCache(): void
public function testSetCache(): void
{
$response = new DownloadResponse('unit-test.txt', true);

$this->expectException(DownloadException::class);
$response->setCache();
$date = date('r');

$options = [
'etag' => '12345678',
'last-modified' => $date,
'max-age' => 300,
'must-revalidate',
];

$response->setCache($options);
$response->buildHeaders();

$this->assertSame('12345678', $response->getHeaderLine('ETag'));
$this->assertSame($date, $response->getHeaderLine('Last-Modified'));
$this->assertSame('max-age=300, must-revalidate', $response->getHeaderLine('Cache-Control'));
}

public function testWhenFilepathIsSetBinaryCanNotBeSet(): void
Expand Down Expand Up @@ -200,30 +213,53 @@ public function testCanGetContentLength(): void
$this->assertSame($size, $response->getContentLength());
}

public function testIsSetDownloadableHeadlersFromBinary(): void
public function testIsSetDownloadableHeadersFromBinary(): void
{
$response = new DownloadResponse('unit test.txt', false);

$response->setBinary('test');
$response->buildHeaders();

$this->assertSame('private, no-transform, no-store, must-revalidate', $response->getHeaderLine('Cache-Control'));
$this->assertSame('application/octet-stream', $response->getHeaderLine('Content-Type'));
$this->assertSame('attachment; filename="unit test.txt"; filename*=UTF-8\'\'unit%20test.txt', $response->getHeaderLine('Content-Disposition'));
$this->assertSame('0', $response->getHeaderLine('Expires-Disposition'));
$this->assertSame('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
$this->assertSame('4', $response->getHeaderLine('Content-Length'));
}

public function testIsSetDownloadableHeadlersFromFile(): void
public function testIsSetDownloadableHeadersFromFile(): void
{
$response = new DownloadResponse('unit-test.php', false);

$response->setFilePath(__FILE__);
$response->buildHeaders();

$this->assertSame('private, no-transform, no-store, must-revalidate', $response->getHeaderLine('Cache-Control'));
$this->assertSame('application/octet-stream', $response->getHeaderLine('Content-Type'));
$this->assertSame('attachment; filename="unit-test.php"; filename*=UTF-8\'\'unit-test.php', $response->getHeaderLine('Content-Disposition'));
$this->assertSame('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
$this->assertSame(filesize(__FILE__), (int) $response->getHeaderLine('Content-Length'));
}

public function testCustomHeaders(): void
{
$response = new DownloadResponse('unit-test.php', false);

$response->setFilePath(__FILE__);

$response->setHeader('Last-Modified', 'Fri, 18 Oct 2024 13:17:37 GMT');
$response->setHeader('Expires', 'Sun, 17 Nov 2024 14:17:37 GMT');
$response->setHeader('Pragma', 'public');
$response->removeHeader('Cache-Control');
$response->setHeader('Cache-Control', 'public');
$response->buildHeaders();

$this->assertSame('Fri, 18 Oct 2024 13:17:37 GMT', $response->getHeaderLine('Last-Modified'));
$this->assertSame('Sun, 17 Nov 2024 14:17:37 GMT', $response->getHeaderLine('Expires'));
$this->assertSame('public', $response->getHeaderLine('Pragma'));
$this->assertSame('public', $response->getHeaderLine('Cache-Control'));
$this->assertSame('application/octet-stream', $response->getHeaderLine('Content-Type'));
$this->assertSame('attachment; filename="unit-test.php"; filename*=UTF-8\'\'unit-test.php', $response->getHeaderLine('Content-Disposition'));
$this->assertSame('0', $response->getHeaderLine('Expires-Disposition'));
$this->assertSame('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
$this->assertSame(filesize(__FILE__), (int) $response->getHeaderLine('Content-Length'));
}
Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/changelogs/v4.5.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ Deprecations
Bugs Fixed
**********
- **Session Library:** The session initialization debug message now uses the correct log type "debug" instead of "info".

- **Validation:** Fixed the `getValidated()` method that did not return valid data when validation rules used multiple asterisks.
- **DownloadResponse:** Fixed a bug that prevented setting custom cache headers. We can now also use the `setCache()` method.
- **DownloadResponse:** Fixed a bug involving sending a custom "Expires-Disposition" header.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down

0 comments on commit d3e1e51

Please sign in to comment.