Skip to content

Commit 35abcc2

Browse files
committed
Merge branch '4.4' into 5.1
2 parents e5439ca + c886528 commit 35abcc2

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
@@ -231,33 +231,36 @@ public function prepare(Request $request)
231231
$this->headers->set($type, $path);
232232
$this->maxlen = 0;
233233
}
234-
} elseif ($request->headers->has('Range')) {
234+
} elseif ($request->headers->has('Range') && $request->isMethod('GET')) {
235235
// Process the range headers.
236236
if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
237237
$range = $request->headers->get('Range');
238238

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

241-
$end = ('' === $end) ? $fileSize - 1 : (int) $end;
242+
$end = ('' === $end) ? $fileSize - 1 : (int) $end;
242243

243-
if ('' === $start) {
244-
$start = $fileSize - $end;
245-
$end = $fileSize - 1;
246-
} else {
247-
$start = (int) $start;
248-
}
244+
if ('' === $start) {
245+
$start = $fileSize - $end;
246+
$end = $fileSize - 1;
247+
} else {
248+
$start = (int) $start;
249+
}
249250

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

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)