Skip to content

Commit 87b4217

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: remove no longer needed PHP version requirements from tests remove no longer needed PHP version requirements from tests [Validator] [Security] Add Norwegian translations add tests covering union types in MessengerPass [Messenger] Support for custom handler method containing a Union type tagged with #[AsMessageHandler] [HttpFoundation] Prevent BinaryFileResponse::prepare from adding content type if no content is sent
2 parents 52010f0 + 90f5d97 commit 87b4217

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
@@ -179,22 +179,25 @@ public function setContentDisposition(string $disposition, string $filename = ''
179179

180180
public function prepare(Request $request): static
181181
{
182-
if (!$this->headers->has('Content-Type')) {
183-
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
184-
}
182+
parent::prepare($request);
185183

186-
if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
187-
$this->setProtocolVersion('1.1');
184+
if ($this->isInformational() || $this->isEmpty()) {
185+
$this->maxlen = 0;
186+
187+
return $this;
188188
}
189189

190-
$this->ensureIEOverSSLCompatibility($request);
190+
if (!$this->headers->has('Content-Type')) {
191+
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
192+
}
191193

192194
$this->offset = 0;
193195
$this->maxlen = -1;
194196

195197
if (false === $fileSize = $this->file->getSize()) {
196198
return $this;
197199
}
200+
$this->headers->remove('Transfer-Encoding');
198201
$this->headers->set('Content-Length', $fileSize);
199202

200203
if (!$this->headers->has('Accept-Ranges')) {
@@ -264,6 +267,10 @@ public function prepare(Request $request): static
264267
}
265268
}
266269

270+
if ($request->isMethod('HEAD')) {
271+
$this->maxlen = 0;
272+
}
273+
267274
return $this;
268275
}
269276

@@ -282,40 +289,42 @@ private function hasValidIfRangeHeader(?string $header): bool
282289

283290
public function sendContent(): static
284291
{
285-
if (!$this->isSuccessful()) {
286-
return parent::sendContent();
287-
}
292+
try {
293+
if (!$this->isSuccessful()) {
294+
return parent::sendContent();
295+
}
288296

289-
if (0 === $this->maxlen) {
290-
return $this;
291-
}
297+
if (0 === $this->maxlen) {
298+
return $this;
299+
}
292300

293-
$out = fopen('php://output', 'w');
294-
$file = fopen($this->file->getPathname(), 'r');
301+
$out = fopen('php://output', 'w');
302+
$file = fopen($this->file->getPathname(), 'r');
295303

296-
ignore_user_abort(true);
304+
ignore_user_abort(true);
297305

298-
if (0 !== $this->offset) {
299-
fseek($file, $this->offset);
300-
}
306+
if (0 !== $this->offset) {
307+
fseek($file, $this->offset);
308+
}
301309

302-
$length = $this->maxlen;
303-
while ($length && !feof($file)) {
304-
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
305-
$length -= $read;
310+
$length = $this->maxlen;
311+
while ($length && !feof($file)) {
312+
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
313+
$length -= $read;
306314

307-
stream_copy_to_stream($file, $out, $read);
315+
stream_copy_to_stream($file, $out, $read);
308316

309-
if (connection_aborted()) {
310-
break;
317+
if (connection_aborted()) {
318+
break;
319+
}
311320
}
312-
}
313321

314-
fclose($out);
315-
fclose($file);
316-
317-
if ($this->deleteFileAfterSend && is_file($this->file->getPathname())) {
318-
unlink($this->file->getPathname());
322+
fclose($out);
323+
fclose($file);
324+
} finally {
325+
if ($this->deleteFileAfterSend && is_file($this->file->getPathname())) {
326+
unlink($this->file->getPathname());
327+
}
319328
}
320329

321330
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)