From a98829f12c5cfb167b856c718b395e326c04bbd3 Mon Sep 17 00:00:00 2001 From: Starfox64 <1530720+Starfox64@users.noreply.github.com> Date: Thu, 2 Mar 2023 13:38:56 +0100 Subject: [PATCH 1/3] Add static file headers --- config/octane.php | 15 +++++++++++++++ src/Swoole/SwooleClient.php | 8 ++++++++ tests/SwooleClientTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/config/octane.php b/config/octane.php index 818ee7036..e6678533e 100644 --- a/config/octane.php +++ b/config/octane.php @@ -135,6 +135,21 @@ // ], + /* + |-------------------------------------------------------------------------- + | Static file headers + |-------------------------------------------------------------------------- + | + | While using Swoole, you may serve static files from the public directory. + | You may set the HTTP headers that should be returned along with those + | files here. + | + */ + + 'static_file_headers' => [ + // 'Cache-Control' => 'max-age=604800, must-revalidate, immutable, public', + ], + /* |-------------------------------------------------------------------------- | Octane Cache Table diff --git a/src/Swoole/SwooleClient.php b/src/Swoole/SwooleClient.php index 911c3c803..bb17f71c2 100644 --- a/src/Swoole/SwooleClient.php +++ b/src/Swoole/SwooleClient.php @@ -139,6 +139,14 @@ public function serveStaticFile(Request $request, RequestContext $context): void $publicPath = $context->publicPath; + if (! empty($context->octaneConfig['static_file_headers'])) { + $staticHeaders = $context->octaneConfig['static_file_headers']; + + foreach ($staticHeaders as $name => $value) { + $swooleResponse->header($name, $value); + } + } + $swooleResponse->status(200); $swooleResponse->header('Content-Type', MimeType::get(pathinfo($request->path(), PATHINFO_EXTENSION))); $swooleResponse->sendfile(realpath($publicPath.'/'.$request->path())); diff --git a/tests/SwooleClientTest.php b/tests/SwooleClientTest.php index 910124ea9..4841e0689 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,30 @@ 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' => [ + '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 +151,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 +165,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)); From 6683d08e2d766d0eec92741c3d752f730d042be0 Mon Sep 17 00:00:00 2001 From: Starfox64 <1530720+Starfox64@users.noreply.github.com> Date: Thu, 2 Mar 2023 18:07:54 +0100 Subject: [PATCH 2/3] Add support for file patterns --- config/octane.php | 6 ++++-- src/Swoole/SwooleClient.php | 13 +++++++++---- tests/SwooleClientTest.php | 4 +++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/config/octane.php b/config/octane.php index e6678533e..4f8ca1aee 100644 --- a/config/octane.php +++ b/config/octane.php @@ -142,12 +142,14 @@ | | While using Swoole, you may serve static files from the public directory. | You may set the HTTP headers that should be returned along with those - | files here. + | files here. Headers can be applied according to the request path pattern. | */ 'static_file_headers' => [ - // 'Cache-Control' => 'max-age=604800, must-revalidate, immutable, public', + // 'css/dist*' => [ + // 'Cache-Control' => 'max-age=604800, must-revalidate, immutable, public', + // ], ], /* diff --git a/src/Swoole/SwooleClient.php b/src/Swoole/SwooleClient.php index bb17f71c2..5dd89b2fd 100644 --- a/src/Swoole/SwooleClient.php +++ b/src/Swoole/SwooleClient.php @@ -138,12 +138,17 @@ public function serveStaticFile(Request $request, RequestContext $context): void $swooleResponse = $context->swooleResponse; $publicPath = $context->publicPath; + $octaneConfig = $context->octaneConfig ?? []; - if (! empty($context->octaneConfig['static_file_headers'])) { - $staticHeaders = $context->octaneConfig['static_file_headers']; + if (! empty($octaneConfig['static_file_headers'])) { + $staticHeaders = $octaneConfig['static_file_headers']; - foreach ($staticHeaders as $name => $value) { - $swooleResponse->header($name, $value); + foreach ($staticHeaders as $pattern => $headers) { + if ($request->is($pattern)) { + foreach ($headers as $name => $value) { + $swooleResponse->header($name, $value); + } + } } } diff --git a/tests/SwooleClientTest.php b/tests/SwooleClientTest.php index 4841e0689..9adc8cd71 100644 --- a/tests/SwooleClientTest.php +++ b/tests/SwooleClientTest.php @@ -130,7 +130,9 @@ public function test_static_file_headers_can_be_sent() 'publicPath' => __DIR__.'/public', 'octaneConfig' => [ 'static_file_headers' => [ - 'X-Test-Header' => 'Valid', + 'foo.txt' => [ + 'X-Test-Header' => 'Valid', + ], ], ], ]); From 13ba327469c384f4d2456eaf960459f1eb0d673c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 3 Mar 2023 09:39:12 -0600 Subject: [PATCH 3/3] formatting --- config/octane.php | 17 ----------------- src/Swoole/SwooleClient.php | 6 ++---- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/config/octane.php b/config/octane.php index 4f8ca1aee..818ee7036 100644 --- a/config/octane.php +++ b/config/octane.php @@ -135,23 +135,6 @@ // ], - /* - |-------------------------------------------------------------------------- - | Static file headers - |-------------------------------------------------------------------------- - | - | While using Swoole, you may serve static files from the public directory. - | You may set the HTTP headers that should be returned along with those - | files here. Headers can be applied according to the request path pattern. - | - */ - - 'static_file_headers' => [ - // 'css/dist*' => [ - // 'Cache-Control' => 'max-age=604800, must-revalidate, immutable, public', - // ], - ], - /* |-------------------------------------------------------------------------- | Octane Cache Table diff --git a/src/Swoole/SwooleClient.php b/src/Swoole/SwooleClient.php index 5dd89b2fd..b6901340b 100644 --- a/src/Swoole/SwooleClient.php +++ b/src/Swoole/SwooleClient.php @@ -140,10 +140,8 @@ public function serveStaticFile(Request $request, RequestContext $context): void $publicPath = $context->publicPath; $octaneConfig = $context->octaneConfig ?? []; - if (! empty($octaneConfig['static_file_headers'])) { - $staticHeaders = $octaneConfig['static_file_headers']; - - foreach ($staticHeaders as $pattern => $headers) { + 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);