diff --git a/src/App.php b/src/App.php index c42ef12..c967128 100644 --- a/src/App.php +++ b/src/App.php @@ -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], ]))); diff --git a/tests/ResponseCompatibilityTest.php b/tests/ResponseCompatibilityTest.php index 2f187e4..726820e 100644 --- a/tests/ResponseCompatibilityTest.php +++ b/tests/ResponseCompatibilityTest.php @@ -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); } } diff --git a/tools/test-server7-local.sh b/tools/test-server7-local.sh index c89967b..f304d92 100644 --- a/tools/test-server7-local.sh +++ b/tools/test-server7-local.sh @@ -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 \