Skip to content

Commit 534869d

Browse files
committed
Add support for custom server header in HTTP responses
1 parent a4438ef commit 534869d

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

examples/app-api/config/common.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'class' => \Dacheng\Yii2\Swoole\Server\HttpServer::class,
5151
'host' => '127.0.0.1',
5252
'port' => 9501,
53+
'serverHeader' => 'yii2-swoole',
5354
'documentRoot' => dirname(__DIR__) . '/web',
5455
'settings' => [
5556
'open_tcp_nodelay' => true,

examples/app-basic/config/common.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'class' => \Dacheng\Yii2\Swoole\Server\HttpServer::class,
5151
'host' => '127.0.0.1',
5252
'port' => 9501,
53+
'serverHeader' => 'yii2-swoole',
5354
'documentRoot' => dirname(__DIR__) . '/web',
5455
'settings' => [
5556
'open_tcp_nodelay' => true,

src/Server/HttpServer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class HttpServer extends Component
6666
'eot' => 'application/vnd.ms-fontobject',
6767
];
6868

69+
/**
70+
* @var string|null Custom server header value (default: null to use Swoole's default)
71+
*/
72+
public ?string $serverHeader = null;
73+
6974
private ?SwooleCoroutineHttpServer $server = null;
7075

7176
private bool $isRunning = false;
@@ -169,6 +174,11 @@ public function start(): void
169174
$afterStartEvent();
170175

171176
$this->server->handle('/', function (Request $request, Response $response) use ($dispatcher, $server): void {
177+
// Set custom server header if configured
178+
if ($this->serverHeader !== null) {
179+
$response->header('Server', $this->serverHeader);
180+
}
181+
172182
// Check if shutdown is requested
173183
if ($this->signalHandler && $this->signalHandler->isShutdownRequested()) {
174184
$response->status(503);

0 commit comments

Comments
 (0)