This commit is contained in:
2021-08-04 15:00:49 +08:00
parent 6850b3af2e
commit 55213b7624
2 changed files with 84 additions and 89 deletions
+1 -5
View File
@@ -194,15 +194,11 @@ class Response extends HttpService
/**
* @param mixed $content
* @param int $statusCode
* @param null $format
* @return Response
*/
public function setContent(mixed $content, int $statusCode = 200, $format = null): static
public function setContent(mixed $content): static
{
$this->endData = $content;
$this->setStatusCode($statusCode);
$this->setFormat($format);
return $this;
}
+83 -84
View File
@@ -6,9 +6,9 @@ use Annotation\Inject;
use Exception;
use HttpServer\Exception\RequestException;
use HttpServer\Http\Request as HSRequest;
use Server\Constrict\Response as CResponse;
use HttpServer\Route\Node;
use HttpServer\Route\Router;
use Server\Constrict\Response as CResponse;
use Server\Events\OnAfterRequest;
use Snowflake\Events\EventDispatch;
use Swoole\Error;
@@ -26,106 +26,105 @@ use Throwable;
class HTTPServerListener extends Abstracts\Server
{
protected static bool|Port $_http;
protected static bool|Port $_http;
use ListenerHelper;
use ListenerHelper;
/** @var Router|mixed */
#[Inject('router')]
public Router $router;
/** @var Router|mixed */
#[Inject('router')]
public Router $router;
/** @var CResponse|mixed */
#[Inject(CResponse::class)]
public CResponse $response;
/** @var CResponse|mixed */
#[Inject(CResponse::class)]
public CResponse $response;
/** @var EventDispatch */
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
/** @var EventDispatch */
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
/**
* UDPServerListener constructor.
* @param Server|Port $server
* @param array|null $settings
* @return Server|Port
* @throws Exception
*/
public function bindCallback(Server|Port $server, ?array $settings = []): Server|Port
{
$this->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
/**
* UDPServerListener constructor.
* @param Server|Port $server
* @param array|null $settings
* @return Server|Port
* @throws Exception
*/
public function bindCallback(Server|Port $server, ?array $settings = []): Server|Port
{
$this->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$server->set(array_merge($settings['settings'] ?? [], ['enable_unsafe_event' => false]));
if (isset($settings['events'][Constant::REQUEST])) {
$event = $settings['events'][Constant::REQUEST];
if (is_array($event) && is_string($event[0])) {
$event[0] = di($event[0]);
}
$server->on('request', $event);
} else {
$server->on('request', [$this, 'onRequest']);
}
$server->on('connect', [$this, 'onConnect']);
$server->on('close', [$this, 'onClose']);
return $server;
}
$server->set(array_merge($settings['settings'] ?? [], ['enable_unsafe_event' => false]));
if (isset($settings['events'][Constant::REQUEST])) {
$event = $settings['events'][Constant::REQUEST];
if (is_array($event) && is_string($event[0])) {
$event[0] = di($event[0]);
}
$server->on('request', $event);
} else {
$server->on('request', [$this, 'onRequest']);
}
$server->on('connect', [$this, 'onConnect']);
$server->on('close', [$this, 'onClose']);
return $server;
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onConnect(Server $server, int $fd)
{
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onConnect(Server $server, int $fd)
{
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
}
/**
* @param Request $request
* @param Response $response
* @throws Exception
*/
public function onRequest(Request $request, Response $response)
{
try {
$node = $this->router->find_path(HSRequest::create($request));
if (!($node instanceof Node)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
}
$responseData = $this->response->setContent($node->dispatch(),200);
} catch (Error | Throwable $exception) {
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
$data = $code == 404 ? $exception->getMessage() : jTraceEx($exception);
/**
* @param Request $request
* @param Response $response
* @throws Exception
*/
public function onRequest(Request $request, Response $response)
{
try {
$node = $this->router->find_path(HSRequest::create($request));
if (!($node instanceof Node)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
}
$responseData = $this->response->setContent($node->dispatch())->setStatusCode(200);
} catch (Error | Throwable $exception) {
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
$data = $code == 404 ? $exception->getMessage() : jTraceEx($exception);
$responseData = $this->response->setContent($data)->setFormat(CResponse::HTML)->setStatusCode($code);
} finally {
$response->end($responseData->configure($response)->getContent());
$responseData = $this->response->setContent($data, $code, CResponse::HTML);
} finally {
$response->end($responseData->configure($response)->getContent());
$this->eventDispatch->dispatch(new OnAfterRequest());
}
}
$this->eventDispatch->dispatch(new OnAfterRequest());
}
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onDisconnect(Server $server, int $fd)
{
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onDisconnect(Server $server, int $fd)
{
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onClose(Server $server, int $fd)
{
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onClose(Server $server, int $fd)
{
}
}