-
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpFoundation] Prevent BinaryFileResponse::prepare from adding cont…
…ent type if no content is sent
- Loading branch information
Showing
2 changed files
with
55 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,22 +201,25 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal | |
*/ | ||
public function prepare(Request $request) | ||
{ | ||
if (!$this->headers->has('Content-Type')) { | ||
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream'); | ||
} | ||
parent::prepare($request); | ||
|
||
if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) { | ||
$this->setProtocolVersion('1.1'); | ||
if ($this->isInformational() || $this->isEmpty()) { | ||
$this->maxlen = 0; | ||
|
||
return $this; | ||
} | ||
|
||
$this->ensureIEOverSSLCompatibility($request); | ||
if (!$this->headers->has('Content-Type')) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
adrienprioul
|
||
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream'); | ||
} | ||
|
||
$this->offset = 0; | ||
$this->maxlen = -1; | ||
|
||
if (false === $fileSize = $this->file->getSize()) { | ||
return $this; | ||
} | ||
$this->headers->remove('Transfer-Encoding'); | ||
$this->headers->set('Content-Length', $fileSize); | ||
|
||
if (!$this->headers->has('Accept-Ranges')) { | ||
|
@@ -286,6 +289,10 @@ public function prepare(Request $request) | |
} | ||
} | ||
|
||
if ($request->isMethod('HEAD')) { | ||
$this->maxlen = 0; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
|
@@ -309,40 +316,42 @@ private function hasValidIfRangeHeader(?string $header): bool | |
*/ | ||
public function sendContent() | ||
{ | ||
if (!$this->isSuccessful()) { | ||
return parent::sendContent(); | ||
} | ||
try { | ||
if (!$this->isSuccessful()) { | ||
return parent::sendContent(); | ||
} | ||
|
||
if (0 === $this->maxlen) { | ||
return $this; | ||
} | ||
if (0 === $this->maxlen) { | ||
return $this; | ||
} | ||
|
||
$out = fopen('php://output', 'w'); | ||
$file = fopen($this->file->getPathname(), 'r'); | ||
$out = fopen('php://output', 'w'); | ||
$file = fopen($this->file->getPathname(), 'r'); | ||
|
||
ignore_user_abort(true); | ||
ignore_user_abort(true); | ||
|
||
if (0 !== $this->offset) { | ||
fseek($file, $this->offset); | ||
} | ||
if (0 !== $this->offset) { | ||
fseek($file, $this->offset); | ||
} | ||
|
||
$length = $this->maxlen; | ||
while ($length && !feof($file)) { | ||
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length; | ||
$length -= $read; | ||
$length = $this->maxlen; | ||
while ($length && !feof($file)) { | ||
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length; | ||
$length -= $read; | ||
|
||
stream_copy_to_stream($file, $out, $read); | ||
stream_copy_to_stream($file, $out, $read); | ||
|
||
if (connection_aborted()) { | ||
break; | ||
if (connection_aborted()) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
fclose($out); | ||
fclose($file); | ||
|
||
if ($this->deleteFileAfterSend && file_exists($this->file->getPathname())) { | ||
unlink($this->file->getPathname()); | ||
fclose($out); | ||
fclose($file); | ||
} finally { | ||
if ($this->deleteFileAfterSend && file_exists($this->file->getPathname())) { | ||
unlink($this->file->getPathname()); | ||
} | ||
} | ||
|
||
return $this; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Since
parent::prepare()
always setsContent-Type
header, this check always returnsfalse
.