BinaryFileResponseTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
  12. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  13. use Symfony\Component\HttpFoundation\File\File;
  14. use Symfony\Component\HttpFoundation\File\Stream;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  17. use Symfony\Component\HttpFoundation\Tests\File\FakeFile;
  18. class BinaryFileResponseTest extends ResponseTestCase
  19. {
  20. use ExpectDeprecationTrait;
  21. public function testConstruction()
  22. {
  23. $file = __DIR__.'/../README.md';
  24. $response = new BinaryFileResponse($file, 404, ['X-Header' => 'Foo'], true, null, true, true);
  25. $this->assertEquals(404, $response->getStatusCode());
  26. $this->assertEquals('Foo', $response->headers->get('X-Header'));
  27. $this->assertTrue($response->headers->has('ETag'));
  28. $this->assertTrue($response->headers->has('Last-Modified'));
  29. $this->assertFalse($response->headers->has('Content-Disposition'));
  30. $response = new BinaryFileResponse($file, 404, [], true, ResponseHeaderBag::DISPOSITION_INLINE);
  31. $this->assertEquals(404, $response->getStatusCode());
  32. $this->assertFalse($response->headers->has('ETag'));
  33. $this->assertEquals('inline; filename=README.md', $response->headers->get('Content-Disposition'));
  34. }
  35. /**
  36. * @group legacy
  37. */
  38. public function testConstructionLegacy()
  39. {
  40. $file = __DIR__.'/../README.md';
  41. $this->expectDeprecation('Since symfony/http-foundation 5.2: The "Symfony\Component\HttpFoundation\BinaryFileResponse::create()" method is deprecated, use "new Symfony\Component\HttpFoundation\BinaryFileResponse()" instead.');
  42. $response = BinaryFileResponse::create($file, 404, ['X-Header' => 'Foo'], true, null, true, true);
  43. $this->assertEquals(404, $response->getStatusCode());
  44. $this->assertEquals('Foo', $response->headers->get('X-Header'));
  45. $this->assertTrue($response->headers->has('ETag'));
  46. $this->assertTrue($response->headers->has('Last-Modified'));
  47. $this->assertFalse($response->headers->has('Content-Disposition'));
  48. $response = BinaryFileResponse::create($file, 404, [], true, ResponseHeaderBag::DISPOSITION_INLINE);
  49. $this->assertEquals(404, $response->getStatusCode());
  50. $this->assertFalse($response->headers->has('ETag'));
  51. $this->assertEquals('inline; filename=README.md', $response->headers->get('Content-Disposition'));
  52. }
  53. public function testConstructWithNonAsciiFilename()
  54. {
  55. touch(sys_get_temp_dir().'/fööö.html');
  56. $response = new BinaryFileResponse(sys_get_temp_dir().'/fööö.html', 200, [], true, 'attachment');
  57. @unlink(sys_get_temp_dir().'/fööö.html');
  58. $this->assertSame('fööö.html', $response->getFile()->getFilename());
  59. }
  60. public function testSetContent()
  61. {
  62. $this->expectException(\LogicException::class);
  63. $response = new BinaryFileResponse(__FILE__);
  64. $response->setContent('foo');
  65. }
  66. public function testGetContent()
  67. {
  68. $response = new BinaryFileResponse(__FILE__);
  69. $this->assertFalse($response->getContent());
  70. }
  71. public function testSetContentDispositionGeneratesSafeFallbackFilename()
  72. {
  73. $response = new BinaryFileResponse(__FILE__);
  74. $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
  75. $this->assertSame('attachment; filename=f__.html; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
  76. }
  77. public function testSetContentDispositionGeneratesSafeFallbackFilenameForWronglyEncodedFilename()
  78. {
  79. $response = new BinaryFileResponse(__FILE__);
  80. $iso88591EncodedFilename = mb_convert_encoding('föö.html', 'ISO-8859-1', 'UTF-8');
  81. $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $iso88591EncodedFilename);
  82. // the parameter filename* is invalid in this case (rawurldecode('f%F6%F6') does not provide a UTF-8 string but an ISO-8859-1 encoded one)
  83. $this->assertSame('attachment; filename=f__.html; filename*=utf-8\'\'f%F6%F6.html', $response->headers->get('Content-Disposition'));
  84. }
  85. /**
  86. * @dataProvider provideRanges
  87. */
  88. public function testRequests($requestRange, $offset, $length, $responseRange)
  89. {
  90. $response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();
  91. // do a request to get the ETag
  92. $request = Request::create('/');
  93. $response->prepare($request);
  94. $etag = $response->headers->get('ETag');
  95. // prepare a request for a range of the testing file
  96. $request = Request::create('/');
  97. $request->headers->set('If-Range', $etag);
  98. $request->headers->set('Range', $requestRange);
  99. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  100. fseek($file, $offset);
  101. $data = fread($file, $length);
  102. fclose($file);
  103. $this->expectOutputString($data);
  104. $response = clone $response;
  105. $response->prepare($request);
  106. $response->sendContent();
  107. $this->assertEquals(206, $response->getStatusCode());
  108. $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
  109. $this->assertSame((string) $length, $response->headers->get('Content-Length'));
  110. }
  111. /**
  112. * @dataProvider provideRanges
  113. */
  114. public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
  115. {
  116. $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  117. // do a request to get the LastModified
  118. $request = Request::create('/');
  119. $response->prepare($request);
  120. $lastModified = $response->headers->get('Last-Modified');
  121. // prepare a request for a range of the testing file
  122. $request = Request::create('/');
  123. $request->headers->set('If-Range', $lastModified);
  124. $request->headers->set('Range', $requestRange);
  125. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  126. fseek($file, $offset);
  127. $data = fread($file, $length);
  128. fclose($file);
  129. $this->expectOutputString($data);
  130. $response = clone $response;
  131. $response->prepare($request);
  132. $response->sendContent();
  133. $this->assertEquals(206, $response->getStatusCode());
  134. $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
  135. }
  136. public static function provideRanges()
  137. {
  138. return [
  139. ['bytes=1-4', 1, 4, 'bytes 1-4/35'],
  140. ['bytes=-5', 30, 5, 'bytes 30-34/35'],
  141. ['bytes=30-', 30, 5, 'bytes 30-34/35'],
  142. ['bytes=30-30', 30, 1, 'bytes 30-30/35'],
  143. ['bytes=30-34', 30, 5, 'bytes 30-34/35'],
  144. ['bytes=30-40', 30, 5, 'bytes 30-34/35'],
  145. ];
  146. }
  147. public function testRangeRequestsWithoutLastModifiedDate()
  148. {
  149. // prevent auto last modified
  150. $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false);
  151. // prepare a request for a range of the testing file
  152. $request = Request::create('/');
  153. $request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
  154. $request->headers->set('Range', 'bytes=1-4');
  155. $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
  156. $response = clone $response;
  157. $response->prepare($request);
  158. $response->sendContent();
  159. $this->assertEquals(200, $response->getStatusCode());
  160. $this->assertNull($response->headers->get('Content-Range'));
  161. }
  162. /**
  163. * @dataProvider provideFullFileRanges
  164. */
  165. public function testFullFileRequests($requestRange)
  166. {
  167. $response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();
  168. // prepare a request for a range of the testing file
  169. $request = Request::create('/');
  170. $request->headers->set('Range', $requestRange);
  171. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  172. $data = fread($file, 35);
  173. fclose($file);
  174. $this->expectOutputString($data);
  175. $response = clone $response;
  176. $response->prepare($request);
  177. $response->sendContent();
  178. $this->assertEquals(200, $response->getStatusCode());
  179. }
  180. public static function provideFullFileRanges()
  181. {
  182. return [
  183. ['bytes=0-'],
  184. ['bytes=0-34'],
  185. ['bytes=-35'],
  186. // Syntactical invalid range-request should also return the full resource
  187. ['bytes=20-10'],
  188. ['bytes=50-40'],
  189. // range units other than bytes must be ignored
  190. ['unknown=10-20'],
  191. ];
  192. }
  193. public function testRangeOnPostMethod()
  194. {
  195. $request = Request::create('/', 'POST');
  196. $request->headers->set('Range', 'bytes=10-20');
  197. $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  198. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  199. $data = fread($file, 35);
  200. fclose($file);
  201. $this->expectOutputString($data);
  202. $response = clone $response;
  203. $response->prepare($request);
  204. $response->sendContent();
  205. $this->assertSame(200, $response->getStatusCode());
  206. $this->assertSame('35', $response->headers->get('Content-Length'));
  207. $this->assertNull($response->headers->get('Content-Range'));
  208. }
  209. public function testUnpreparedResponseSendsFullFile()
  210. {
  211. $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200);
  212. $data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');
  213. $this->expectOutputString($data);
  214. $response = clone $response;
  215. $response->sendContent();
  216. $this->assertEquals(200, $response->getStatusCode());
  217. }
  218. /**
  219. * @dataProvider provideInvalidRanges
  220. */
  221. public function testInvalidRequests($requestRange)
  222. {
  223. $response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();
  224. // prepare a request for a range of the testing file
  225. $request = Request::create('/');
  226. $request->headers->set('Range', $requestRange);
  227. $response = clone $response;
  228. $response->prepare($request);
  229. $response->sendContent();
  230. $this->assertEquals(416, $response->getStatusCode());
  231. $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
  232. }
  233. public static function provideInvalidRanges()
  234. {
  235. return [
  236. ['bytes=-40'],
  237. ['bytes=40-50'],
  238. ];
  239. }
  240. /**
  241. * @dataProvider provideXSendfileFiles
  242. */
  243. public function testXSendfile($file)
  244. {
  245. $request = Request::create('/');
  246. $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
  247. BinaryFileResponse::trustXSendfileTypeHeader();
  248. $response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']);
  249. $response->prepare($request);
  250. $this->expectOutputString('');
  251. $response->sendContent();
  252. $this->assertStringContainsString('README.md', $response->headers->get('X-Sendfile'));
  253. }
  254. public static function provideXSendfileFiles()
  255. {
  256. return [
  257. [__DIR__.'/../README.md'],
  258. ['file://'.__DIR__.'/../README.md'],
  259. ];
  260. }
  261. /**
  262. * @dataProvider getSampleXAccelMappings
  263. */
  264. public function testXAccelMapping($realpath, $mapping, $virtual)
  265. {
  266. $request = Request::create('/');
  267. $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
  268. $request->headers->set('X-Accel-Mapping', $mapping);
  269. $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
  270. BinaryFileResponse::trustXSendfileTypeHeader();
  271. $response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']);
  272. $reflection = new \ReflectionObject($response);
  273. $property = $reflection->getProperty('file');
  274. $property->setAccessible(true);
  275. $property->setValue($response, $file);
  276. $response->prepare($request);
  277. $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
  278. }
  279. public function testDeleteFileAfterSend()
  280. {
  281. $request = Request::create('/');
  282. $path = __DIR__.'/File/Fixtures/to_delete';
  283. touch($path);
  284. $realPath = realpath($path);
  285. $this->assertFileExists($realPath);
  286. $response = new BinaryFileResponse($realPath, 200, ['Content-Type' => 'application/octet-stream']);
  287. $response->deleteFileAfterSend(true);
  288. $response->prepare($request);
  289. $response->sendContent();
  290. $this->assertFileDoesNotExist($path);
  291. }
  292. public function testAcceptRangeOnUnsafeMethods()
  293. {
  294. $request = Request::create('/', 'POST');
  295. $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  296. $response->prepare($request);
  297. $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
  298. }
  299. public function testAcceptRangeNotOverriden()
  300. {
  301. $request = Request::create('/', 'POST');
  302. $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  303. $response->headers->set('Accept-Ranges', 'foo');
  304. $response->prepare($request);
  305. $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
  306. }
  307. public static function getSampleXAccelMappings()
  308. {
  309. return [
  310. ['/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'],
  311. ['/home/Foo/bar.txt', '/var/www/=/files/,/home/Foo/=/baz/', '/baz/bar.txt'],
  312. ['/home/Foo/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', '/baz/bar.txt'],
  313. ['/tmp/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', null],
  314. ];
  315. }
  316. public function testStream()
  317. {
  318. $request = Request::create('/');
  319. $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, ['Content-Type' => 'text/plain']);
  320. $response->prepare($request);
  321. $this->assertNull($response->headers->get('Content-Length'));
  322. }
  323. public function testPrepareNotAddingContentTypeHeaderIfNoContentResponse()
  324. {
  325. $request = Request::create('/');
  326. $request->headers->set('If-Modified-Since', date('D, d M Y H:i:s').' GMT');
  327. $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  328. $response->setLastModified(new \DateTimeImmutable('-1 day'));
  329. $response->isNotModified($request);
  330. $response->prepare($request);
  331. $this->assertSame(BinaryFileResponse::HTTP_NOT_MODIFIED, $response->getStatusCode());
  332. $this->assertFalse($response->headers->has('Content-Type'));
  333. }
  334. public function testContentTypeIsCorrectlyDetected()
  335. {
  336. $file = new File(__DIR__.'/File/Fixtures/test.gif');
  337. try {
  338. $file->getMimeType();
  339. } catch (\LogicException $e) {
  340. $this->markTestSkipped('Guessing the mime type is not possible');
  341. }
  342. $response = new BinaryFileResponse($file);
  343. $request = Request::create('/');
  344. $response->prepare($request);
  345. $this->assertSame(200, $response->getStatusCode());
  346. $this->assertSame('image/gif', $response->headers->get('Content-Type'));
  347. }
  348. public function testContentTypeIsNotGuessedWhenTheFileWasNotModified()
  349. {
  350. $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif');
  351. $response->setAutoLastModified();
  352. $request = Request::create('/');
  353. $request->headers->set('If-Modified-Since', $response->getLastModified()->format('D, d M Y H:i:s').' GMT');
  354. $isNotModified = $response->isNotModified($request);
  355. $this->assertTrue($isNotModified);
  356. $response->prepare($request);
  357. $this->assertSame(304, $response->getStatusCode());
  358. $this->assertFalse($response->headers->has('Content-Type'));
  359. }
  360. protected function provideResponse()
  361. {
  362. return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);
  363. }
  364. public static function tearDownAfterClass(): void
  365. {
  366. $path = __DIR__.'/../Fixtures/to_delete';
  367. if (file_exists($path)) {
  368. @unlink($path);
  369. }
  370. }
  371. }