Files
kiri-core/http-server/Service/Http.php
T

106 lines
2.4 KiB
PHP
Raw Normal View History

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-08-12 12:40:06 +08:00
use Server\Events\OnAfterRequest;
2021-08-28 01:21:23 +08:00
use Server\Message\Stream;
2021-08-12 12:40:06 +08:00
use Server\ResponseInterface;
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-31 14:01:18 +08:00
use Server\Message\Response as MsgResponse;
2021-08-12 12:40:06 +08:00
/**
*
*/
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
{
// TODO: Implement onRequest() method.
try {
$node = $this->router->Branch_search(\Server\Constrict\Request::create($request));
if (!($node instanceof Node)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
}
if (!(($responseData = $node->dispatch()) instanceof ResponseInterface)) {
$responseData = $this->transferToResponse($responseData);
}
} catch (Error | \Throwable $exception) {
$responseData = $this->exceptionHandler->emit($exception, $this->response);
} finally {
$this->responseEmitter->sender($response, $responseData);
$this->eventDispatch->dispatch(new OnAfterRequest());
}
}
2021-08-12 12:40:06 +08:00
2021-08-31 13:51:21 +08:00
/**
* @param $responseData
* @return ResponseInterface
* @throws Exception
*/
2021-08-31 14:01:18 +08:00
private function transferToResponse($responseData): ResponseInterface
{
$interface = $this->response->withStatus(200);
if (!$interface->hasHeader('Content-Type')) {
$interface->withContentType(MsgResponse::CONTENT_TYPE_JSON);
}
$responseData = $interface->_toArray($responseData);
if ($interface->getHeader('Content-Type') == MsgResponse::CONTENT_TYPE_XML) {
$responseData = Help::toXml($responseData);
}
if (is_array($responseData)) {
2021-08-31 14:03:26 +08:00
$interface->stream->write(json_encode($responseData, JSON_UNESCAPED_UNICODE));
2021-08-31 14:01:18 +08:00
} else {
2021-08-31 14:03:26 +08:00
$interface->stream->write((string)$responseData);
2021-08-31 14:01:18 +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
}