Skip to content

Commit c886528

Browse files
Merge branch '3.4' into 4.4
* 3.4: [Form] Sync translations Added dutch translations for new invalid messages Don't skip Doctrine tests on php 8. Bump APCu to 5.1.19 on Travis. [WebProfilerBundle] Hide debug toolbar in print view indexBy does not refer to attributes, but to column names Fix Reflection file name with eval()\'d code [HttpFoundation] Fix Range Requests
2 parents 0c47226 + aacebf2 commit c886528

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

BinaryFileResponse.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,33 +234,36 @@ public function prepare(Request $request)
234234
$this->headers->set($type, $path);
235235
$this->maxlen = 0;
236236
}
237-
} elseif ($request->headers->has('Range')) {
237+
} elseif ($request->headers->has('Range') && $request->isMethod('GET')) {
238238
// Process the range headers.
239239
if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
240240
$range = $request->headers->get('Range');
241241

242-
list($start, $end) = explode('-', substr($range, 6), 2) + [0];
242+
if (0 === strpos($range, 'bytes=')) {
243+
list($start, $end) = explode('-', substr($range, 6), 2) + [0];
243244

244-
$end = ('' === $end) ? $fileSize - 1 : (int) $end;
245+
$end = ('' === $end) ? $fileSize - 1 : (int) $end;
245246

246-
if ('' === $start) {
247-
$start = $fileSize - $end;
248-
$end = $fileSize - 1;
249-
} else {
250-
$start = (int) $start;
251-
}
247+
if ('' === $start) {
248+
$start = $fileSize - $end;
249+
$end = $fileSize - 1;
250+
} else {
251+
$start = (int) $start;
252+
}
252253

253-
if ($start <= $end) {
254-
if ($start < 0 || $end > $fileSize - 1) {
255-
$this->setStatusCode(416);
256-
$this->headers->set('Content-Range', sprintf('bytes */%s', $fileSize));
257-
} elseif (0 !== $start || $end !== $fileSize - 1) {
258-
$this->maxlen = $end < $fileSize ? $end - $start + 1 : -1;
259-
$this->offset = $start;
260-
261-
$this->setStatusCode(206);
262-
$this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize));
263-
$this->headers->set('Content-Length', $end - $start + 1);
254+
if ($start <= $end) {
255+
$end = min($end, $fileSize - 1);
256+
if ($start < 0 || $start > $end) {
257+
$this->setStatusCode(416);
258+
$this->headers->set('Content-Range', sprintf('bytes */%s', $fileSize));
259+
} elseif ($end - $start < $fileSize - 1) {
260+
$this->maxlen = $end < $fileSize ? $end - $start + 1 : -1;
261+
$this->offset = $start;
262+
263+
$this->setStatusCode(206);
264+
$this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize));
265+
$this->headers->set('Content-Length', $end - $start + 1);
266+
}
264267
}
265268
}
266269
}

Tests/BinaryFileResponseTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function provideRanges()
149149
['bytes=30-', 30, 5, 'bytes 30-34/35'],
150150
['bytes=30-30', 30, 1, 'bytes 30-30/35'],
151151
['bytes=30-34', 30, 5, 'bytes 30-34/35'],
152+
['bytes=30-40', 30, 5, 'bytes 30-34/35'],
152153
];
153154
}
154155

@@ -203,9 +204,31 @@ public function provideFullFileRanges()
203204
// Syntactical invalid range-request should also return the full resource
204205
['bytes=20-10'],
205206
['bytes=50-40'],
207+
// range units other than bytes must be ignored
208+
['unknown=10-20'],
206209
];
207210
}
208211

212+
public function testRangeOnPostMethod()
213+
{
214+
$request = Request::create('/', 'POST');
215+
$request->headers->set('Range', 'bytes=10-20');
216+
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
217+
218+
$file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
219+
$data = fread($file, 35);
220+
fclose($file);
221+
222+
$this->expectOutputString($data);
223+
$response = clone $response;
224+
$response->prepare($request);
225+
$response->sendContent();
226+
227+
$this->assertSame(200, $response->getStatusCode());
228+
$this->assertSame('35', $response->headers->get('Content-Length'));
229+
$this->assertNull($response->headers->get('Content-Range'));
230+
}
231+
209232
public function testUnpreparedResponseSendsFullFile()
210233
{
211234
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);
@@ -242,7 +265,7 @@ public function provideInvalidRanges()
242265
{
243266
return [
244267
['bytes=-40'],
245-
['bytes=30-40'],
268+
['bytes=40-50'],
246269
];
247270
}
248271

0 commit comments

Comments
 (0)