Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom HTTP headers on static files #653

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Swoole/SwooleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
32 changes: 32 additions & 0 deletions tests/SwooleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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));
Expand Down