From b8cbe931eeaffc0a9c2e8ab30ddffe2c22b2aa31 Mon Sep 17 00:00:00 2001 From: Starfox64 <1530720+Starfox64@users.noreply.github.com> Date: Fri, 3 Mar 2023 16:40:10 +0100 Subject: [PATCH] Add support for custom HTTP headers on static files (#653) * Add static file headers * Add support for file patterns * formatting --------- Co-authored-by: Taylor Otwell --- src/Swoole/SwooleClient.php | 11 +++++++++++ tests/SwooleClientTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Swoole/SwooleClient.php b/src/Swoole/SwooleClient.php index 911c3c803..b6901340b 100644 --- a/src/Swoole/SwooleClient.php +++ b/src/Swoole/SwooleClient.php @@ -138,6 +138,17 @@ public function serveStaticFile(Request $request, RequestContext $context): void $swooleResponse = $context->swooleResponse; $publicPath = $context->publicPath; + $octaneConfig = $context->octaneConfig ?? []; + + if (! empty($octaneConfig['static_file_headers'] ?? [])) { + foreach ($octaneConfig['static_file_headers'] as $pattern => $headers) { + if ($request->is($pattern)) { + foreach ($headers as $name => $value) { + $swooleResponse->header($name, $value); + } + } + } + } $swooleResponse->status(200); $swooleResponse->header('Content-Type', MimeType::get(pathinfo($request->path(), PATHINFO_EXTENSION))); diff --git a/tests/SwooleClientTest.php b/tests/SwooleClientTest.php index 910124ea9..9adc8cd71 100644 --- a/tests/SwooleClientTest.php +++ b/tests/SwooleClientTest.php @@ -65,6 +65,7 @@ public function test_can_serve_static_files_if_configured_to_and_file_is_within_ $context = new RequestContext([ 'publicPath' => __DIR__.'/public', + 'octaneConfig' => [], ]); $this->assertTrue($client->canServeRequestAsStaticFile($request, $context)); @@ -78,6 +79,7 @@ public function test_cant_serve_static_files_if_file_is_outside_public_directory $context = new RequestContext([ 'publicPath' => __DIR__.'/public/files', + 'octaneConfig' => [], ]); $this->assertFalse($client->canServeRequestAsStaticFile($request, $context)); @@ -91,6 +93,7 @@ public function test_cant_serve_static_files_if_file_has_forbidden_extension() $context = new RequestContext([ 'publicPath' => __DIR__.'/public/files', + 'octaneConfig' => [], ]); $this->assertFalse($client->canServeRequestAsStaticFile($request, $context)); @@ -106,6 +109,7 @@ public function test_static_file_can_be_served() $context = new RequestContext([ 'swooleResponse' => $swooleResponse = Mockery::mock('stdClass'), 'publicPath' => __DIR__.'/public', + 'octaneConfig' => [], ]); $swooleResponse->shouldReceive('status')->once()->with(200); @@ -115,6 +119,32 @@ public function test_static_file_can_be_served() $client->serveStaticFile($request, $context); } + public function test_static_file_headers_can_be_sent() + { + $client = new SwooleClient; + + $request = Request::create('/foo.txt', 'GET'); + + $context = new RequestContext([ + 'swooleResponse' => $swooleResponse = Mockery::mock('stdClass'), + 'publicPath' => __DIR__.'/public', + 'octaneConfig' => [ + 'static_file_headers' => [ + 'foo.txt' => [ + 'X-Test-Header' => 'Valid', + ], + ], + ], + ]); + + $swooleResponse->shouldReceive('status')->once()->with(200); + $swooleResponse->shouldReceive('header')->once()->with('X-Test-Header', 'Valid'); + $swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/plain'); + $swooleResponse->shouldReceive('sendfile')->once()->with(realpath(__DIR__.'/public/foo.txt')); + + $client->serveStaticFile($request, $context); + } + public function test_can_serve_static_files_through_symlink() { $client = new SwooleClient; @@ -123,6 +153,7 @@ public function test_can_serve_static_files_through_symlink() $context = new RequestContext([ 'publicPath' => __DIR__.'/public/files', + 'octaneConfig' => [], ]); $this->assertTrue($client->canServeRequestAsStaticFile($request, $context)); @@ -136,6 +167,7 @@ public function test_cant_serve_static_files_through_symlink_using_directory_tra $context = new RequestContext([ 'publicPath' => __DIR__.'/public/files', + 'octaneConfig' => [], ]); $this->assertFalse($client->canServeRequestAsStaticFile($request, $context));