Skip to content

Commit b8cbe93

Browse files
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 <[email protected]>
1 parent 51825dd commit b8cbe93

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Swoole/SwooleClient.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ public function serveStaticFile(Request $request, RequestContext $context): void
138138
$swooleResponse = $context->swooleResponse;
139139

140140
$publicPath = $context->publicPath;
141+
$octaneConfig = $context->octaneConfig ?? [];
142+
143+
if (! empty($octaneConfig['static_file_headers'] ?? [])) {
144+
foreach ($octaneConfig['static_file_headers'] as $pattern => $headers) {
145+
if ($request->is($pattern)) {
146+
foreach ($headers as $name => $value) {
147+
$swooleResponse->header($name, $value);
148+
}
149+
}
150+
}
151+
}
141152

142153
$swooleResponse->status(200);
143154
$swooleResponse->header('Content-Type', MimeType::get(pathinfo($request->path(), PATHINFO_EXTENSION)));

tests/SwooleClientTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function test_can_serve_static_files_if_configured_to_and_file_is_within_
6565

6666
$context = new RequestContext([
6767
'publicPath' => __DIR__.'/public',
68+
'octaneConfig' => [],
6869
]);
6970

7071
$this->assertTrue($client->canServeRequestAsStaticFile($request, $context));
@@ -78,6 +79,7 @@ public function test_cant_serve_static_files_if_file_is_outside_public_directory
7879

7980
$context = new RequestContext([
8081
'publicPath' => __DIR__.'/public/files',
82+
'octaneConfig' => [],
8183
]);
8284

8385
$this->assertFalse($client->canServeRequestAsStaticFile($request, $context));
@@ -91,6 +93,7 @@ public function test_cant_serve_static_files_if_file_has_forbidden_extension()
9193

9294
$context = new RequestContext([
9395
'publicPath' => __DIR__.'/public/files',
96+
'octaneConfig' => [],
9497
]);
9598

9699
$this->assertFalse($client->canServeRequestAsStaticFile($request, $context));
@@ -106,6 +109,7 @@ public function test_static_file_can_be_served()
106109
$context = new RequestContext([
107110
'swooleResponse' => $swooleResponse = Mockery::mock('stdClass'),
108111
'publicPath' => __DIR__.'/public',
112+
'octaneConfig' => [],
109113
]);
110114

111115
$swooleResponse->shouldReceive('status')->once()->with(200);
@@ -115,6 +119,32 @@ public function test_static_file_can_be_served()
115119
$client->serveStaticFile($request, $context);
116120
}
117121

122+
public function test_static_file_headers_can_be_sent()
123+
{
124+
$client = new SwooleClient;
125+
126+
$request = Request::create('/foo.txt', 'GET');
127+
128+
$context = new RequestContext([
129+
'swooleResponse' => $swooleResponse = Mockery::mock('stdClass'),
130+
'publicPath' => __DIR__.'/public',
131+
'octaneConfig' => [
132+
'static_file_headers' => [
133+
'foo.txt' => [
134+
'X-Test-Header' => 'Valid',
135+
],
136+
],
137+
],
138+
]);
139+
140+
$swooleResponse->shouldReceive('status')->once()->with(200);
141+
$swooleResponse->shouldReceive('header')->once()->with('X-Test-Header', 'Valid');
142+
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/plain');
143+
$swooleResponse->shouldReceive('sendfile')->once()->with(realpath(__DIR__.'/public/foo.txt'));
144+
145+
$client->serveStaticFile($request, $context);
146+
}
147+
118148
public function test_can_serve_static_files_through_symlink()
119149
{
120150
$client = new SwooleClient;
@@ -123,6 +153,7 @@ public function test_can_serve_static_files_through_symlink()
123153

124154
$context = new RequestContext([
125155
'publicPath' => __DIR__.'/public/files',
156+
'octaneConfig' => [],
126157
]);
127158

128159
$this->assertTrue($client->canServeRequestAsStaticFile($request, $context));
@@ -136,6 +167,7 @@ public function test_cant_serve_static_files_through_symlink_using_directory_tra
136167

137168
$context = new RequestContext([
138169
'publicPath' => __DIR__.'/public/files',
170+
'octaneConfig' => [],
139171
]);
140172

141173
$this->assertFalse($client->canServeRequestAsStaticFile($request, $context));

0 commit comments

Comments
 (0)