Skip to content

Commit 54be067

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: [Validator] [Security] Add Norwegian translations add tests covering union types in MessengerPass [HttpFoundation] Prevent BinaryFileResponse::prepare from adding content type if no content is sent
2 parents 22d2bbe + 7acdc97 commit 54be067

File tree

2 files changed

+55
-31
lines changed

2 files changed

+55
-31
lines changed

BinaryFileResponse.php

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,25 @@ public function setContentDisposition(string $disposition, string $filename = ''
206206
*/
207207
public function prepare(Request $request)
208208
{
209-
if (!$this->headers->has('Content-Type')) {
210-
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
211-
}
209+
parent::prepare($request);
212210

213-
if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
214-
$this->setProtocolVersion('1.1');
211+
if ($this->isInformational() || $this->isEmpty()) {
212+
$this->maxlen = 0;
213+
214+
return $this;
215215
}
216216

217-
$this->ensureIEOverSSLCompatibility($request);
217+
if (!$this->headers->has('Content-Type')) {
218+
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
219+
}
218220

219221
$this->offset = 0;
220222
$this->maxlen = -1;
221223

222224
if (false === $fileSize = $this->file->getSize()) {
223225
return $this;
224226
}
227+
$this->headers->remove('Transfer-Encoding');
225228
$this->headers->set('Content-Length', $fileSize);
226229

227230
if (!$this->headers->has('Accept-Ranges')) {
@@ -291,6 +294,10 @@ public function prepare(Request $request)
291294
}
292295
}
293296

297+
if ($request->isMethod('HEAD')) {
298+
$this->maxlen = 0;
299+
}
300+
294301
return $this;
295302
}
296303

@@ -312,40 +319,42 @@ private function hasValidIfRangeHeader(?string $header): bool
312319
*/
313320
public function sendContent()
314321
{
315-
if (!$this->isSuccessful()) {
316-
return parent::sendContent();
317-
}
322+
try {
323+
if (!$this->isSuccessful()) {
324+
return parent::sendContent();
325+
}
318326

319-
if (0 === $this->maxlen) {
320-
return $this;
321-
}
327+
if (0 === $this->maxlen) {
328+
return $this;
329+
}
322330

323-
$out = fopen('php://output', 'w');
324-
$file = fopen($this->file->getPathname(), 'r');
331+
$out = fopen('php://output', 'w');
332+
$file = fopen($this->file->getPathname(), 'r');
325333

326-
ignore_user_abort(true);
334+
ignore_user_abort(true);
327335

328-
if (0 !== $this->offset) {
329-
fseek($file, $this->offset);
330-
}
336+
if (0 !== $this->offset) {
337+
fseek($file, $this->offset);
338+
}
331339

332-
$length = $this->maxlen;
333-
while ($length && !feof($file)) {
334-
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
335-
$length -= $read;
340+
$length = $this->maxlen;
341+
while ($length && !feof($file)) {
342+
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
343+
$length -= $read;
336344

337-
stream_copy_to_stream($file, $out, $read);
345+
stream_copy_to_stream($file, $out, $read);
338346

339-
if (connection_aborted()) {
340-
break;
347+
if (connection_aborted()) {
348+
break;
349+
}
341350
}
342-
}
343351

344-
fclose($out);
345-
fclose($file);
346-
347-
if ($this->deleteFileAfterSend && is_file($this->file->getPathname())) {
348-
unlink($this->file->getPathname());
352+
fclose($out);
353+
fclose($file);
354+
} finally {
355+
if ($this->deleteFileAfterSend && is_file($this->file->getPathname())) {
356+
unlink($this->file->getPathname());
357+
}
349358
}
350359

351360
return $this;

Tests/BinaryFileResponseTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,21 @@ public function testStream()
396396
$this->assertNull($response->headers->get('Content-Length'));
397397
}
398398

399+
public function testPrepareNotAddingContentTypeHeaderIfNoContentResponse()
400+
{
401+
$request = Request::create('/');
402+
$request->headers->set('If-Modified-Since', date('D, d M Y H:i:s').' GMT');
403+
404+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
405+
$response->setLastModified(new \DateTimeImmutable('-1 day'));
406+
$response->isNotModified($request);
407+
408+
$response->prepare($request);
409+
410+
$this->assertSame(BinaryFileResponse::HTTP_NOT_MODIFIED, $response->getStatusCode());
411+
$this->assertFalse($response->headers->has('Content-Type'));
412+
}
413+
399414
protected function provideResponse()
400415
{
401416
return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);

0 commit comments

Comments
 (0)