Skip to content
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
13 changes: 8 additions & 5 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,19 +550,22 @@ public static function close(TcpConnection $connection, int $status = 204, mixed
*/
public static function close_http(TcpConnection $connection, int|Http\Response $status = 204, mixed $data = null): void
{
// Если статус является объектом ответа
$headers = [];
if ($status instanceof Http\Response) {
// Получаем сообщение об ошибке, если есть
$headers = $status->getHeaders();
foreach ($headers as $name => $_) {
if (in_array(strtolower((string)$name), ['content-length', 'transfer-encoding'], true)) {
unset($headers[$name]);
}
}
if ($status instanceof Response && ($e = $status->exception())) {
$data = $e->getMessage();
} else {
$data = $status->rawBody();
}
// Получаем статусный код из объекта ответа
$status = $status->getStatusCode();
}
// Закрываем соединение с отправкой HTTP-ответа
$connection->close((string)new Http\Response($status, [], static::encodeJsonPayload([
$connection->close((string)new Http\Response($status, $headers, static::encodeJsonPayload([
'status' => $status,
'data' => $data ?? Server\Protocols\Http\Response::PHRASES[$status],
])));
Expand Down
17 changes: 14 additions & 3 deletions tests/ResponseCompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,22 @@ public function testServerResponseSendsOnlyItsBodyAsAWebSocketMessage(): void
public function testServerResponseCanBeUsedForHttpHandshakeRejection(): void
{
$connection = new ResponseCompatibilityConnection();
$response = new ServerResponse(403, ['X-Adapter' => 'preserved'], 'access denied');
$response = new ServerResponse(401, [
'WWW-Authenticate' => 'Basic realm="websocket"',
'X-Adapter' => 'preserved',
'Content-Length' => '3',
'Transfer-Encoding' => 'chunked',
], 'access denied');

ResponseCompatibilityProbeApp::closeHttpForTest($connection, $response);

self::assertStringStartsWith('HTTP/1.1 403', $connection->closedData);
self::assertStringContainsString('"data": "access denied"', $connection->closedData);
self::assertIsString($connection->closedData);
[$headers, $body] = explode("\r\n\r\n", $connection->closedData, 2);
self::assertStringStartsWith('HTTP/1.1 401', $headers);
self::assertStringContainsString('WWW-Authenticate: Basic realm="websocket"', $headers);
self::assertStringContainsString('X-Adapter: preserved', $headers);
self::assertStringNotContainsString('Transfer-Encoding:', $headers);
self::assertMatchesRegularExpression('/\r\nContent-Length: ' . strlen($body) . '(?:\r\n|$)/', $headers);
self::assertStringContainsString('"data": "access denied"', $body);
}
}
2 changes: 1 addition & 1 deletion tools/test-server7-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ COMPOSER="$manifest" composer config minimum-stability dev
COMPOSER="$manifest" composer config prefer-stable true

COMPOSER="$manifest" composer config repositories.server7-server \
'{"type":"path","url":"../../localzet/Server","options":{"symlink":true,"versions":{"localzet/server":"7.1.0"}}}'
'{"type":"path","url":"../../localzet/Server","options":{"symlink":true,"versions":{"localzet/server":"7.2.2"}}}'
COMPOSER="$manifest" composer config repositories.server7-engine \
'{"type":"path","url":"../Engine","options":{"symlink":true,"versions":{"triangle/engine":"9.0.x-dev"}}}'
COMPOSER="$manifest" composer config repositories.server7-router \
Expand Down