2021-08-12 12:40:06 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Server\Service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2021-08-17 16:43:50 +08:00
|
|
|
use Http\Exception\RequestException;
|
|
|
|
|
use Http\Route\Node;
|
2021-08-31 14:01:18 +08:00
|
|
|
use Kiri\Core\Help;
|
2021-09-09 17:33:43 +08:00
|
|
|
use Server\Constant;
|
2021-09-10 11:35:23 +08:00
|
|
|
use Server\Constrict\Request as ScRequest;
|
2021-09-10 10:45:24 +08:00
|
|
|
use Server\Constrict\ResponseInterface;
|
2021-09-10 11:17:32 +08:00
|
|
|
use Server\Events\OnAfterRequest;
|
2021-08-12 12:40:06 +08:00
|
|
|
use Server\SInterface\OnClose;
|
2021-08-12 14:43:16 +08:00
|
|
|
use Server\SInterface\OnConnect;
|
2021-08-12 12:40:06 +08:00
|
|
|
use Swoole\Error;
|
|
|
|
|
use Swoole\Http\Request;
|
|
|
|
|
use Swoole\Http\Response;
|
|
|
|
|
use Swoole\Server;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-08-12 14:43:16 +08:00
|
|
|
class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
|
2021-08-12 12:40:06 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2021-08-31 14:01:18 +08:00
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
*/
|
|
|
|
|
public function onConnect(Server $server, int $fd): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement onConnect() method.
|
|
|
|
|
}
|
2021-08-12 14:43:16 +08:00
|
|
|
|
|
|
|
|
|
2021-08-31 14:01:18 +08:00
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param Response $response
|
|
|
|
|
*/
|
|
|
|
|
public function onRequest(Request $request, Response $response): void
|
|
|
|
|
{
|
|
|
|
|
try {
|
2021-09-12 03:46:24 +08:00
|
|
|
$node = $this->router->radix_tree($Psr7Request = ScRequest::create($request));
|
2021-09-06 18:54:14 +08:00
|
|
|
if (!($node instanceof Node)) {
|
2021-09-09 17:33:43 +08:00
|
|
|
throw new RequestException(Constant::STATUS_404_MESSAGE, 404);
|
2021-09-06 18:54:14 +08:00
|
|
|
}
|
2021-09-10 11:35:23 +08:00
|
|
|
if (!(($psr7Response = $node->dispatch($Psr7Request)) instanceof ResponseInterface)) {
|
2021-09-06 19:03:38 +08:00
|
|
|
$psr7Response = $this->transferToResponse($psr7Response);
|
2021-09-06 18:54:14 +08:00
|
|
|
}
|
2021-08-31 14:01:18 +08:00
|
|
|
} catch (Error | \Throwable $exception) {
|
2021-09-06 19:03:38 +08:00
|
|
|
$psr7Response = $this->exceptionHandler->emit($exception, $this->response);
|
2021-08-31 14:01:18 +08:00
|
|
|
} finally {
|
2021-09-06 19:03:38 +08:00
|
|
|
$this->responseEmitter->sender($response, $psr7Response);
|
2021-08-31 14:01:18 +08:00
|
|
|
$this->eventDispatch->dispatch(new OnAfterRequest());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-12 12:40:06 +08:00
|
|
|
|
|
|
|
|
|
2021-08-31 13:51:21 +08:00
|
|
|
/**
|
2021-09-06 18:46:28 +08:00
|
|
|
* @param mixed $responseData
|
2021-08-31 13:51:21 +08:00
|
|
|
* @return ResponseInterface
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-09-10 11:07:59 +08:00
|
|
|
private function transferToResponse(mixed $responseData): ResponseInterface
|
2021-08-31 14:01:18 +08:00
|
|
|
{
|
|
|
|
|
$interface = $this->response->withStatus(200);
|
2021-09-06 18:29:04 +08:00
|
|
|
if (!$interface->hasContentType()) {
|
2021-09-10 11:01:11 +08:00
|
|
|
$interface->withContentType('application/json;charset=utf-8');
|
2021-08-31 14:01:18 +08:00
|
|
|
}
|
2021-09-10 11:17:32 +08:00
|
|
|
if (is_object($responseData)) {
|
|
|
|
|
$responseData = get_object_vars($responseData);
|
|
|
|
|
}
|
2021-09-10 11:01:11 +08:00
|
|
|
if ($interface->getContentType() == 'application/xml;charset=utf-8') {
|
|
|
|
|
$interface->getBody()->write(Help::toXml($responseData));
|
2021-09-06 18:54:14 +08:00
|
|
|
} else if (is_array($responseData)) {
|
2021-09-10 11:01:11 +08:00
|
|
|
$interface->getBody()->write(json_encode($responseData));
|
2021-09-06 18:54:14 +08:00
|
|
|
} else {
|
2021-09-10 11:01:11 +08:00
|
|
|
$interface->getBody()->write((string)$responseData);
|
2021-09-06 18:54:14 +08:00
|
|
|
}
|
2021-08-31 14:03:26 +08:00
|
|
|
return $interface;
|
2021-08-31 14:01:18 +08:00
|
|
|
}
|
2021-08-12 12:40:06 +08:00
|
|
|
|
|
|
|
|
|
2021-08-31 14:01:18 +08:00
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onDisconnect(Server $server, int $fd): void
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-08-28 01:57:33 +08:00
|
|
|
|
|
|
|
|
|
2021-08-31 14:01:18 +08:00
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onClose(Server $server, int $fd): void
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-08-12 12:40:06 +08:00
|
|
|
|
|
|
|
|
}
|