Skip to content

Commit

Permalink
Add support for custom HTTP headers on static files (#653)
Browse files Browse the repository at this point in the history
* Add static file headers

* Add support for file patterns

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
Starfox64 and taylorotwell authored Mar 3, 2023
1 parent 51825dd commit b8cbe93
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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

0 comments on commit b8cbe93

Please sign in to comment.