10000 Some further improvements to BinaryFileResponse::prepare · symfony/symfony@e3e952b · GitHub
[go: up one dir, main page]

Skip to content

Commit e3e952b

Browse files
author
naitsirch
committed
Some further improvements to BinaryFileResponse::prepare
1 parent 4c65afe commit e3e952b

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -201,26 +201,25 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
201201
*/
202202
public function prepare(Request $request)
203203
{
204-
if (\in_array($this->getStatusCode(), [self::HTTP_NO_CONTENT, self::HTTP_NOT_MODIFIED])) {
204+
parent::prepare($request);
205+
206+
if ($this->isInformational() || $this->isEmpty()) {
207+
$this->maxlen = 0;
208+
205209
return $this;
206210
}
207211

208212
if (!$this->headers->has('Content-Type')) {
209213
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
210214
}
211215

212-
if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
213-
$this->setProtocolVersion('1.1');
214-
}
215-
216-
$this->ensureIEOverSSLCompatibility($request);
217-
218216
$this->offset = 0;
219217
$this->maxlen = -1;
220218

221219
if (false === $fileSize = $this->file->getSize()) {
222220
return $this;
223221
}
222+
$this->headers->remove('Transfer-Encoding');
224223
$this->headers->set('Content-Length', $fileSize);
225224

226225
if (!$this->headers->has('Accept-Ranges')) {
@@ -290,6 +289,10 @@ public function prepare(Request $request)
290289
}
291290
}
292291

292+
if ($request->isMethod('HEAD')) {
293+
$this->maxlen = 0;
294+
}
295+
293296
return $this;
294297
}
295298

@@ -313,40 +316,42 @@ private function hasValidIfRangeHeader(?string $header): bool
313316
*/
314317
public function sendContent()
315318
{
316-
if (!$this->isSuccessful()) {
317-
return parent::sendContent();
318-
}
319+
try {
320+
if (!$this->isSuccessful()) {
321+
return parent::sendContent();
322+
}
319323

320-
if (0 === $this->maxlen) {
321-
return $this;
322-
}
324+
if (0 === $this->maxlen) {
325+
return $this;
326+
}
323327

324-
$out = fopen('php://output', 'w');
325-
$file = fopen($this->file->getPathname(), 'r');
328+
$out = fopen('php://output', 'w');
329+
$file = fopen($this->file->getPathname(), 'r');
326330

327-
ignore_user_abort(true);
331+
ignore_user_abort(true);
328332

329-
if (0 !== $this->offset) {
330-
fseek($file, $this->offset);
331-
}
333+
if (0 !== $this->offset) {
334+
fseek($file, $this->offset);
335+
}
332336

333-
$length = $this->maxlen;
334-
while ($length && !feof($file)) {
335-
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
336-
$length -= $read;
337+
$length = $this->maxlen;
338+
while ($length && !feof($file)) {
339+
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
340+
$length -= $read;
337341

338-
stream_copy_to_stream($file, $out, $read);
342+
stream_copy_to_stream($file, $out, $read);
339343

340-
if (connection_aborted()) {
341-
break;
344+
if (connection_aborted()) {
345+
break;
346+
}
342347
}
343-
}
344348

345-
fclose($out);
346-
fclose($file);
347-
348-
if ($this->deleteFileAfterSend && file_exists($this->file->getPathname())) {
349-
unlink($this->file->getPathname());
349+
fclose($out);
350+
fclose($file);
351+
} finally {
352+
if ($this->deleteFileAfterSend && file_exists($this->file->getPathname())) {
353+
unlink($this->file->getPathname());
354+
}
350355
}
351356

352357
return $this;

0 commit comments

Comments
 (0)
0