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