Skip to content

Commit

Permalink
Improve the eTag handling of file downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
dtdesign committed May 25, 2024
1 parent e8dfe19 commit 588d2a1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
13 changes: 4 additions & 9 deletions wcfsetup/install/files/lib/action/FileDownloadAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,21 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}

$eTag = \sprintf(
'"W/%d-%s"',
$file->fileID,
\substr($file->fileHash, 0, 8),
);
$nonWeakETag = \sprintf(
'"%d-%s"',
$file->fileID,
\substr($file->fileHash, 0, 8),
);

$httpIfNoneMatch = \array_map(
static fn ($tag) => \preg_replace('~^"W/~', '"', $tag),
static fn ($tag) => \preg_replace('~^W/~', '', $tag),
Header::splitList($request->getHeaderLine('if-none-match'))
);
if (\in_array($nonWeakETag, $httpIfNoneMatch, true)) {
if (\in_array($eTag, $httpIfNoneMatch, true)) {
$emptyResponse = new EmptyResponse(304);
if ($response->getHeader('expires')) {
if ($response->hasHeader('expires')) {
$emptyResponse = $emptyResponse->withHeader('expires', $response->getHeader('expires'));
}
if ($response->getHeader('cache-control')) {
if ($response->hasHeader('cache-control')) {
$emptyResponse = $emptyResponse->withHeader('cache-control', $response->getHeader('cache-control'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function adopt(File $file, array $context): void
{
$attachmentHandler = $this->getAttachmentHandlerFromContext($context);
if ($attachmentHandler === null) {
// This can only happen whent he associated object has vanished
// This can only happen when the associated object has vanished
// while the file was being processed. There is nothing we can
// meaningfully do here.
return;
Expand Down

0 comments on commit 588d2a1

Please sign in to comment.