This commit is contained in:
2021-02-08 16:52:27 +08:00
parent da35f038c5
commit 0e31371651
3 changed files with 64 additions and 31 deletions
+16 -26
View File
@@ -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]);
});
}
}
+26 -3
View File
@@ -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 = "<xml>";
foreach ($data as $key => $val) {
if (is_numeric($val)) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . static::xmlChild($val) . "</" . $key . ">";
} else if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $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 . "</" . $key . ">";
} else {
$string .= "<" . $key . "><![CDATA[" . $value . "]]></" . $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 = [];
+22 -2
View File
@@ -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);
}
}