Skip to content

Commit 6b13708

Browse files
bug symfony#16971 [HttpFoundation] Added the ability of using BinaryFileResponse with stream wrappers (jakzal, Sander-Toonen)
This PR was merged into the 2.3 branch. Discussion ---------- [HttpFoundation] Added the ability of using BinaryFileResponse with stream wrappers | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#12990 symfony#13187 | License | MIT | Doc PR | ~ Commits ------- 1da3d61 [HttpFoundation] Added the ability of mapping stream wrapper protocols when using X-Sendfile dd129b7 [HttpFoundation] Add a test case for using BinaryFileResponse with stream wrappers
2 parents c2b7ccb + 1da3d61 commit 6b13708

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public function prepare(Request $request)
192192
// Use X-Sendfile, do not send any content.
193193
$type = $request->headers->get('X-Sendfile-Type');
194194
$path = $this->file->getRealPath();
195+
// Fall back to scheme://path for stream wrapped locations.
196+
if (false === $path) {
197+
$path = $this->file->getPathname();
198+
}
195199
if (strtolower($type) == 'x-accel-redirect') {
196200
// Do X-Accel-Mapping substitutions.
197201
// @link http://wiki.nginx.org/X-accel#X-Accel-Redirect

src/Symfony/Component/HttpFoundation/Tests/BinaryFileResponseTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,16 @@ public function provideInvalidRanges()
153153
);
154154
}
155155

156-
public function testXSendfile()
156+
/**
157+
* @dataProvider provideXSendfileFiles
158+
*/
159+
public function testXSendfile($file)
157160
{
158161
$request = Request::create('/');
159162
$request->headers->set('X-Sendfile-Type', 'X-Sendfile');
160163

161164
BinaryFileResponse::trustXSendfileTypeHeader();
162-
$response = BinaryFileResponse::create(__DIR__.'/../README.md', 200, array('Content-Type' => 'application/octet-stream'));
165+
$response = BinaryFileResponse::create($file, 200, array('Content-Type' => 'application/octet-stream'));
163166
$response->prepare($request);
164167

165168
$this->expectOutputString('');
@@ -168,6 +171,14 @@ public function testXSendfile()
168171
$this->assertContains('README.md', $response->headers->get('X-Sendfile'));
169172
}
170173

174+
public function provideXSendfileFiles()
175+
{
176+
return array(
177+
array(__DIR__.'/../README.md'),
178+
array('file://'.__DIR__.'/../README.md'),
179+
);
180+
}
181+
171182
/**
172183
* @dataProvider getSampleXAccelMappings
173184
*/

0 commit comments

Comments
 (0)