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

192 lines
4.8 KiB
PHP
Raw Normal View History

2021-08-12 12:40:06 +08:00
<?php
namespace Server\Service;
use Exception;
2021-09-18 11:44:42 +08:00
use Http\Context\Context;
2021-08-17 16:43:50 +08:00
use Http\Exception\RequestException;
2021-09-18 11:44:42 +08:00
use Http\Handler\Abstracts\HandlerManager;
use Http\Handler\Dispatcher;
use Http\Handler\Handler;
2021-09-18 10:38:38 +08:00
use Http\Handler\TestRequest;
2021-09-18 11:44:42 +08:00
use Http\Message\ServerRequest;
use Http\Message\Stream;
use Http\Route\MiddlewareManager;
2021-08-17 16:43:50 +08:00
use Http\Route\Node;
2021-08-31 14:01:18 +08:00
use Kiri\Core\Help;
2021-09-18 11:44:42 +08:00
use Psr\Http\Message\ServerRequestInterface;
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-18 11:44:42 +08:00
use Server\Constrict\RequestInterface;
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-09-18 10:38:38 +08:00
public TestRequest $request;
public function init()
{
$this->request = new TestRequest();
parent::init();
}
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
2021-09-18 10:38:38 +08:00
* @throws Exception
2021-08-31 14:01:18 +08:00
*/
public function onRequest(Request $request, Response $response): void
{
try {
2021-09-18 11:44:42 +08:00
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
/** @var Handler $handler */
$handler = HandlerManager::get($request->server['request_uri'], $request->getMethod());
if (is_integer($handler)) {
$PsrResponse->withStatus($handler)->withBody(new Stream('Allow Method[' . $request->getMethod() . '].'));
} else if (is_null($handler)) {
$PsrResponse->withStatus(404)->withBody(new Stream('Page not found.'));
} else {
$PsrResponse = $this->handler($handler, $PsrRequest);
2021-09-06 18:54:14 +08:00
}
2021-09-18 11:44:42 +08:00
} catch (\Throwable $throwable) {
$PsrResponse = \response()->withStatus($throwable->getCode())
->withContentType(\Http\Message\Response::CONTENT_TYPE_HTML)
->withBody(new Stream(jTraceEx($throwable, null, true)));
2021-08-31 14:01:18 +08:00
} finally {
2021-09-18 11:44:42 +08:00
$this->response->sender($response, $PsrResponse);
2021-08-31 14:01:18 +08:00
}
}
2021-08-12 12:40:06 +08:00
2021-09-18 11:44:42 +08:00
/**
* @param Handler $handler
* @param $PsrRequest
* @return ResponseInterface
* @throws Exception
*/
protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface
{
$middlewares = MiddlewareManager::get($handler->callback);
$dispatcher = new Dispatcher($handler, $middlewares);
return $dispatcher->handle($PsrRequest);
}
2021-09-16 14:45:59 +08:00
/**
* @param Request $request
2021-09-18 11:44:42 +08:00
* @return array<ServerRequestInterface, ResponseInterface>
* @throws Exception
2021-09-16 14:45:59 +08:00
*/
2021-09-18 11:44:42 +08:00
private function initRequestResponse(Request $request): array
2021-09-16 14:45:59 +08:00
{
2021-09-18 11:44:42 +08:00
$PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response());
2021-09-16 14:48:54 +08:00
2021-09-18 11:44:42 +08:00
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
2021-09-16 14:48:54 +08:00
2021-09-18 11:44:42 +08:00
return [$PsrRequest, $PsrResponse];
2021-09-16 14:45:59 +08:00
}
2021-09-18 11:44:42 +08:00
//
//
//
// /**
// * @param Request $request
// * @param Response $response
// * @throws Exception
// */
// public function onRequest(Request $request, Response $response): void
// {
// $this->request->onRequest($request, $response);
// return;
//
// try {
// if (!(($node = $this->router->radix_tree($Psr7Request = ScRequest::create($request))) instanceof Node)) {
// throw new RequestException(Constant::STATUS_404_MESSAGE, 404);
// }
// if (!(($psr7Response = $node->dispatch($Psr7Request)) instanceof ResponseInterface)) {
// $psr7Response = $this->transferToResponse($psr7Response);
// }
// $psr7Response->withHeader('Run-Time', $this->_runTime($request));
// } catch (Error | \Throwable $exception) {
// $psr7Response = $this->exceptionHandler->emit($exception, $this->response);
// } finally {
// $this->responseEmitter->sender($response, $psr7Response);
// $this->eventDispatch->dispatch(new OnAfterRequest());
// }
// }
2021-09-16 14:45:59 +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
}