From d5f5fbd651f98fc9af2a0978ea346d393d1e731a Mon Sep 17 00:00:00 2001 From: Thinking Song <123wuming_123@163.com> Date: Fri, 2 Dec 2022 15:01:38 +0800 Subject: [PATCH] add Content-Range unit test --- README.md | 5 ++++ lib/XSendfile/XSendfile.php | 8 +++--- test/XSendfile/XSendfileTest.php | 42 ++++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ac08cc3..81355d9 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,11 @@ php send static files use web server, now support apache, nginx, Lighttpd +## Change Log + +[2.1.0] - 2022-12-02 +* Add Content-Range support, but not support Multipart ranges + ## require php >= 7.3 diff --git a/lib/XSendfile/XSendfile.php b/lib/XSendfile/XSendfile.php index e872821..90adcdd 100644 --- a/lib/XSendfile/XSendfile.php +++ b/lib/XSendfile/XSendfile.php @@ -170,9 +170,9 @@ public static function xSendfile( $file, $downFilename = null, $serverType = nul } } try { - $fp = fopen($path, 'rb'); + $fp = fopen($file, 'rb'); if ($fp === false) { - throw new \RuntimeException("Failed to open file " . $path . " for reading "); + throw new \RuntimeException("Failed to open file " . $file . " for reading "); } // using this php://output hack because fpassthru is unsuitable: https://github.com/php/php-src/issues/9673 $output = fopen('php://output', 'wb'); @@ -191,7 +191,7 @@ public static function xSendfile( $file, $downFilename = null, $serverType = nul throw new \RuntimeException("Failed to send file"); } return; - } catch (Throwable $ex) { + } catch (\Throwable $ex) { if(!headers_sent()) { http_response_code(500); header("Content-Length: ", true); @@ -210,5 +210,5 @@ public static function xSendfile( $file, $downFilename = null, $serverType = nul public static function pathToUri( $path ) { return '/' . ltrim( str_replace( [ $_SERVER['DOCUMENT_ROOT'], '\\' ], [ '', '/' ], $path ), '/' ); } - + } diff --git a/test/XSendfile/XSendfileTest.php b/test/XSendfile/XSendfileTest.php index 13eac7e..dea49e1 100644 --- a/test/XSendfile/XSendfileTest.php +++ b/test/XSendfile/XSendfileTest.php @@ -189,16 +189,42 @@ public function testLighttpd() * @runInSeparateProcess * @depends testChrome */ - // public function testImageFile() - // { + public function testFile() + { + $_SERVER["HTTP_USER_AGENT"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'; - // $_SERVER["HTTP_USER_AGENT"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'; + ob_start(); + XSendfile::xSendfile($this->file); + $output = ob_get_contents(); + ob_end_clean(); - // XSendfile::xSendfile($this->file); + $headers_list = xdebug_get_headers(); - // $headers_list = xdebug_get_headers(); + $this->assertNotEmpty($headers_list); + $this->assertContains("Content-type: text/plain;charset=UTF-8", $headers_list); + $this->assertEquals("hello\n", $output); + } - // $this->assertNotEmpty($headers_list); - // $this->assertContains("Content-type: text/plain;charset=UTF-8", $headers_list); - // } + /** + * @runInSeparateProcess + */ + public function testHttpRange() + { + $_SERVER['HTTP_RANGE'] = 'bytes=1-2'; + $_SERVER["HTTP_USER_AGENT"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'; + + ob_start(); + XSendfile::xSendfile($this->file); + $output = ob_get_contents(); + ob_end_clean(); + + $headers_list = xdebug_get_headers(); + + // var_dump($headers_list); + + $this->assertNotEmpty($headers_list); + $this->assertContains("Content-Length: 2", $headers_list); + $this->assertContains("Content-Range: bytes 1-2/6", $headers_list); + $this->assertEquals('el', $output); + } }