Skip to content

Commit 90f5d97

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: remove no longer needed PHP version requirements from tests [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 34f8b99 + 294208f commit 90f5d97

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
@@ -182,22 +182,25 @@ public function setContentDisposition(string $disposition, string $filename = ''
182182
*/
183183
public function prepare(Request $request): static
184184
{
185-
if (!$this->headers->has('Content-Type')) {
186-
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
187-
}
185+
parent::prepare($request);
188186

189-
if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
190-
$this->setProtocolVersion('1.1');
187+
if ($this->isInformational() || $this->isEmpty()) {
188+
$this->maxlen = 0;
189+
190+
return $this;
191191
}
192192

193-
$this->ensureIEOverSSLCompatibility($request);
193+
if (!$this->headers->has('Content-Type')) {
194+
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
195+
}
194196

195197
$this->offset = 0;
196198
$this->maxlen = -1;
197199

198200
if (false === $fileSize = $this->file->getSize()) {
199201
return $this;
200202
}
203+
$this->headers->remove('Transfer-Encoding');
201204
$this->headers->set('Content-Length', $fileSize);
202205

203206
if (!$this->headers->has('Accept-Ranges')) {
@@ -267,6 +270,10 @@ public function prepare(Request $request): static
267270
}
268271
}
269272

273+
if ($request->isMethod('HEAD')) {
274+
$this->maxlen = 0;
275+
}
276+
270277
return $this;
271278
}
272279

@@ -288,40 +295,42 @@ private function hasValidIfRangeHeader(?string $header): bool
288295
*/
289296
public function sendContent(): static
290297
{
291-
if (!$this->isSuccessful()) {
292-
return parent::sendContent();
293-
}
298+
try {
299+
if (!$this->isSuccessful()) {
300+
return parent::sendContent();
301+
}
294302

295-
if (0 === $this->maxlen) {
296-
return $this;
297-
}
303+
if (0 === $this->maxlen) {
304+
return $this;
305+
}
298306

299-
$out = fopen('php://output', 'w');
300-
$file = fopen($this->file->getPathname(), 'r');
307+
$out = fopen('php://output', 'w');
308+
$file = fopen($this->file->getPathname(), 'r');
301309

302-
ignore_user_abort(true);
310+
ignore_user_abort(true);
303311

304-
if (0 !== $this->offset) {
305-
fseek($file, $this->offset);
306-
}
312+
if (0 !== $this->offset) {
313+
fseek($file, $this->offset);
314+
}
307315

308-
$length = $this->maxlen;
309-
while ($length && !feof($file)) {
310-
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
311-
$length -= $read;
316+
$length = $this->maxlen;
317+
while ($length && !feof($file)) {
318+
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
319+
$length -= $read;
312320

313-
stream_copy_to_stream($file, $out, $read);
321+
stream_copy_to_stream($file, $out, $read);
314322

315-
if (connection_aborted()) {
316-
break;
323+
if (connection_aborted()) {
324+
break;
325+
}
317326
}
318-
}
319327

320-
fclose($out);
321-
fclose($file);
322-
323-
if ($this->deleteFileAfterSend && is_file($this->file->getPathname())) {
324-
unlink($this->file->getPathname());
328+
fclose($out);
329+
fclose($file);
330+
} finally {
331+
if ($this->deleteFileAfterSend && is_file($this->file->getPathname())) {
332+
unlink($this->file->getPathname());
333+
}
325334
}
326335

327336
return $this;

Tests/BinaryFileResponseTest.php

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

375+
public function testPrepareNotAddingContentTypeHeaderIfNoContentResponse()
376+
{
377+
$request = Request::create('/');
378+
$request->headers->set('If-Modified-Since', date('D, d M Y H:i:s').' GMT');
379+
380+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
381+
$response->setLastModified(new \DateTimeImmutable('-1 day'));
382+
$response->isNotModified($request);
383+
384+
$response->prepare($request);
385+
386+
$this->assertSame(BinaryFileResponse::HTTP_NOT_MODIFIED, $response->getStatusCode());
387+
$this->assertFalse($response->headers->has('Content-Type'));
388+
}
389+
375390
protected function provideResponse()
376391
{
377392
return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);

0 commit comments

Comments
 (0)