diff --git a/HttpServer/Events/OnHandshake.php b/HttpServer/Events/OnHandshake.php index f7a8412a..fa56861b 100644 --- a/HttpServer/Events/OnHandshake.php +++ b/HttpServer/Events/OnHandshake.php @@ -37,10 +37,7 @@ class OnHandshake extends Callback if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) { throw new Exception('protocol error.', 500); } - $key = base64_encode(sha1( - $request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', - TRUE - )); + $key = base64_encode(sha1($request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', TRUE)); $headers = [ 'Upgrade' => 'websocket', 'Connection' => 'Upgrade', @@ -69,11 +66,24 @@ class OnHandshake extends Callback } + /** + * @param $response + * @param int $code + * @return false + */ + private function connect($response, $code = 101): bool + { + $response->status($code); + $response->end(); + return false; + } + + /** * @param SRequest $request * @param SResponse $response * @return void - * @throws ComponentException + * @throws Exception */ public function onHandler(SRequest $request, SResponse $response): void { @@ -88,7 +98,6 @@ class OnHandshake extends Callback * @param SRequest $request * @param SResponse $response * @return mixed - * @throws ComponentException * @throws Exception */ private function execute(SRequest $request, SResponse $response): mixed @@ -96,11 +105,8 @@ class OnHandshake extends Callback try { $this->resolveParse($request, $response); if (isset($request->get['debug']) && $request->get['debug'] == 'test') { - $response->status(101); - $response->end(); - return true; + return $this->connect($response, 101); } - $router = Snowflake::app()->getRouter(); $node = $router->search('/' . Socket::HANDSHAKE . '::event', 'sw::socket'); if ($node === null) { @@ -110,25 +116,9 @@ class OnHandshake extends Callback } catch (\Throwable $exception) { $this->addError($exception->getMessage() . ' ' . $exception->getFile() . ' ' . $exception->getLine()); return $this->disconnect($response, 500); - } finally { - $this->eventTrigger($request, $response); } } - /** - * @param $request - * @param $response - * @throws ComponentException - * @throws Exception - */ - private function eventTrigger($request, $response) - { - go(function () use ($request, $response) { - $manager = Snowflake::app()->getEvent(); - $manager->trigger(Event::SERVER_HANDSHAKE, [$request, $response]); - }); - } - } diff --git a/System/Core/Help.php b/System/Core/Help.php index fb2a805d..ede32159 100644 --- a/System/Core/Help.php +++ b/System/Core/Help.php @@ -6,6 +6,7 @@ namespace Snowflake\Core; use Exception; +use JetBrains\PhpStorm\Pure; /** @@ -19,11 +20,13 @@ class Help * @param array $data * @return string */ - public static function toXml(array $data) + #[Pure] public static function toXml(array $data): string { $xml = ""; foreach ($data as $key => $val) { - if (is_numeric($val)) { + if (is_array($val)) { + $xml .= "<" . $key . ">" . static::xmlChild($val) . ""; + } else if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . ""; } else { $xml .= "<" . $key . ">"; @@ -34,6 +37,26 @@ class Help } + /** + * @param array $array + * @return string + */ + private static function xmlChild(array $array): string + { + $string = ''; + foreach ($array as $key => $value) { + if (is_array($value)) { + $string .= static::xmlChild($value); + } else if (is_numeric($value)) { + $string .= "<" . $key . ">" . $value . ""; + } else { + $string .= "<" . $key . ">"; + } + } + return $string; + } + + /** * @param $xml * @return mixed @@ -151,7 +174,7 @@ class Help * @param $type * @return string */ - public static function sign(array $array, $key, $type): string + public static function sign(array $array, $key, $type = 'MD5'): string { ksort($array, SORT_ASC); $string = []; diff --git a/System/Core/Json.php b/System/Core/Json.php index 040f6c05..6fb2049a 100644 --- a/System/Core/Json.php +++ b/System/Core/Json.php @@ -10,7 +10,9 @@ declare(strict_types=1); namespace Snowflake\Core; +use Error; use Exception; +use Throwable; /** * Class JSON @@ -19,10 +21,10 @@ use Exception; class Json { + /** * @param $data * @return false|string - * @throws Exception */ public static function encode($data): bool|string { @@ -49,6 +51,7 @@ class Json return json_decode($data, $asArray); } + /** * @param $code * @param string $message @@ -85,6 +88,24 @@ class Json return static::encode($params); } + + /** + * @param Throwable|Error $throwable + * @return bool|string + * @throws Exception + */ + public static function error(Throwable|Error $throwable): bool|string + { + $array['code'] = $throwable->getCode() == 0 ? 500 : $throwable->getCode(); + $array['message'] = $throwable->getMessage(); + $array['param'] = [ + 'file' => $throwable->getFile(), + 'line' => $throwable->getLine() + ]; + return Json::encode($array); + } + + /** * @param $state * @param $body @@ -98,5 +119,4 @@ class Json return static::encode($params); } - }