modify
This commit is contained in:
@@ -427,7 +427,7 @@ class Request extends HttpService
|
|||||||
|
|
||||||
Context::setContext(\Swoole\Http\Request::class, $request);
|
Context::setContext(\Swoole\Http\Request::class, $request);
|
||||||
|
|
||||||
Context::setContext(CoroutineResponse::class, new CoroutineResponse());
|
Context::setContext(Response::class, new Response());
|
||||||
|
|
||||||
return Snowflake::getDi()->get(Request::class);
|
return Snowflake::getDi()->get(Request::class);
|
||||||
}
|
}
|
||||||
|
|||||||
+133
-25
@@ -42,6 +42,8 @@ class Response extends HttpService
|
|||||||
|
|
||||||
private float $startTime = 0;
|
private float $startTime = 0;
|
||||||
|
|
||||||
|
private mixed $endData;
|
||||||
|
|
||||||
private array $_format_maps = [
|
private array $_format_maps = [
|
||||||
self::JSON => JsonFormatter::class,
|
self::JSON => JsonFormatter::class,
|
||||||
self::XML => XmlFormatter::class,
|
self::XML => XmlFormatter::class,
|
||||||
@@ -56,7 +58,7 @@ class Response extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function setFormat($format): static
|
public function setFormat($format): static
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$this->format = $format;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +69,7 @@ class Response extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function toHtml($content): string
|
public function toHtml($content): string
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$this->format = self::HTML;
|
||||||
return (string)$content;
|
return (string)$content;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +80,7 @@ class Response extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function toJson($content): string|bool
|
public function toJson($content): string|bool
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$this->format = self::JSON;
|
||||||
return json_encode($content, JSON_UNESCAPED_UNICODE);
|
return json_encode($content, JSON_UNESCAPED_UNICODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +91,7 @@ class Response extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function toXml($content): mixed
|
public function toXml($content): mixed
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$this->format = self::XML;
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,23 +103,11 @@ class Response extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function addHeader($key, $value): static
|
public function addHeader($key, $value): static
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$this->headers[$key] = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @param $value
|
|
||||||
*/
|
|
||||||
private function __coroutine__call($name, $value)
|
|
||||||
{
|
|
||||||
/** @var \HttpServer\Http\CoroutineResponse $handler */
|
|
||||||
$handler = Context::getContext(CoroutineResponse::class);
|
|
||||||
call_user_func([$handler, $name], ...$value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param null $value
|
* @param null $value
|
||||||
@@ -132,7 +122,7 @@ class Response extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function addCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): static
|
public function addCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): static
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$this->cookies[] = func_get_args();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,18 +132,96 @@ class Response extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function setStatusCode($statusCode)
|
public function setStatusCode($statusCode)
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$this->statusCode = $statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getResponseFormat(): string
|
||||||
|
{
|
||||||
|
if ($this->format == self::HTML) {
|
||||||
|
return 'text/html;charset=utf-8';
|
||||||
|
} else if ($this->format == self::XML) {
|
||||||
|
return 'application/xml;charset=utf-8';
|
||||||
|
} else {
|
||||||
|
return 'application/json;charset=utf-8';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $context
|
* @param mixed $context
|
||||||
* @param int $statusCode
|
* @param int $statusCode
|
||||||
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function send(mixed $context, SResponse $response): void
|
public function getBuilder(mixed $data, SResponse $response): static
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$response->setStatusCode($this->statusCode);
|
||||||
|
if (!isset($response->header['Content-Type'])) {
|
||||||
|
$response->header('Content-Type', $this->getResponseFormat());
|
||||||
|
}
|
||||||
|
$response->header('Run-Time', $this->getRuntime());
|
||||||
|
if (!empty($this->headers)) {
|
||||||
|
foreach ($this->headers as $name => $header) {
|
||||||
|
$response->header($name, $header);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!empty($this->cookies)) {
|
||||||
|
foreach ($this->cookies as $header) {
|
||||||
|
$response->setCookie(...$header);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->setContent($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $context
|
||||||
|
* @return mixed
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function parseData($context): mixed
|
||||||
|
{
|
||||||
|
if (!empty($context) && !is_string($context)) {
|
||||||
|
/** @var IFormatter $class */
|
||||||
|
$class = $this->_format_maps[$this->format] ?? HtmlFormatter::class;
|
||||||
|
|
||||||
|
$di = Snowflake::getDi()->get($class);
|
||||||
|
$context = $di->send($context)->getData();
|
||||||
|
}
|
||||||
|
return $context;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $content
|
||||||
|
*/
|
||||||
|
public function setContent(mixed $content): static
|
||||||
|
{
|
||||||
|
$this->endData = $content;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws \ReflectionException
|
||||||
|
* @throws \Snowflake\Exception\NotFindClassException
|
||||||
|
*/
|
||||||
|
public function getContent(): string
|
||||||
|
{
|
||||||
|
if (empty($this->endData) || is_string($this->endData)) {
|
||||||
|
return $this->endData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var IFormatter $class */
|
||||||
|
$class = $this->_format_maps[$this->format] ?? HtmlFormatter::class;
|
||||||
|
|
||||||
|
$di = Snowflake::getDi()->get($class);
|
||||||
|
return $di->send($this->endData)->getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -162,9 +230,21 @@ class Response extends HttpService
|
|||||||
* @param array $param
|
* @param array $param
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function redirect($url, array $param = []): void
|
public function redirect($url, array $param = []): mixed
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
if (!empty($param)) {
|
||||||
|
$url .= '?' . http_build_query($param);
|
||||||
|
}
|
||||||
|
$url = ltrim($url, '/');
|
||||||
|
if (!preg_match('/^http/', $url)) {
|
||||||
|
$url = '/' . $url;
|
||||||
|
}
|
||||||
|
/** @var SResponse $response */
|
||||||
|
$response = Context::getContext('response');
|
||||||
|
if (!empty($response)) {
|
||||||
|
return $response->redirect($url);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -175,9 +255,37 @@ class Response extends HttpService
|
|||||||
* @param int $sleep
|
* @param int $sleep
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function sendFile(string $path, int $offset = 0, int $limit = 1024000, int $sleep = 0): void
|
public function sendFile(string $path, int $offset = 0, int $limit = 1024000, int $sleep = 0): string
|
||||||
{
|
{
|
||||||
$this->__coroutine__call(__METHOD__, func_get_args());
|
$open = fopen($path, 'r');
|
||||||
|
|
||||||
|
$stat = fstat($open);
|
||||||
|
|
||||||
|
|
||||||
|
/** @var SResponse $response */
|
||||||
|
$response = Context::getContext('response');
|
||||||
|
$response->header('Content-length', $stat['size']);
|
||||||
|
while ($file = fread($open, $limit)) {
|
||||||
|
$response->write($file);
|
||||||
|
fseek($open, $offset);
|
||||||
|
if ($sleep > 0) sleep($sleep);
|
||||||
|
if ($offset >= $stat['size']) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$offset += $limit;
|
||||||
|
}
|
||||||
|
$response->end();
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function getRuntime(): string
|
||||||
|
{
|
||||||
|
return sprintf('%.5f', microtime(TRUE) - request()->getStartTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Server;
|
|||||||
|
|
||||||
use Annotation\Inject;
|
use Annotation\Inject;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use HttpServer\Exception\RequestException;
|
||||||
use HttpServer\Http\Context;
|
use HttpServer\Http\Context;
|
||||||
use HttpServer\Http\Request as HSRequest;
|
use HttpServer\Http\Request as HSRequest;
|
||||||
use HttpServer\Route\Node;
|
use HttpServer\Route\Node;
|
||||||
@@ -40,7 +41,6 @@ class HTTPServerListener extends Abstracts\Server
|
|||||||
public \HttpServer\Http\Response $response;
|
public \HttpServer\Http\Response $response;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** @var EventDispatch */
|
/** @var EventDispatch */
|
||||||
#[Inject(EventDispatch::class)]
|
#[Inject(EventDispatch::class)]
|
||||||
public EventDispatch $eventDispatch;
|
public EventDispatch $eventDispatch;
|
||||||
@@ -94,23 +94,32 @@ class HTTPServerListener extends Abstracts\Server
|
|||||||
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)) {
|
||||||
$this->response->setStatusCode(404);
|
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
||||||
$this->response->setFormat(\HttpServer\Http\Response::HTML);
|
}
|
||||||
$data = '<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>';
|
|
||||||
} else {
|
|
||||||
$this->response->setStatusCode(200);
|
|
||||||
$data = $node->dispatch();
|
$data = $node->dispatch();
|
||||||
}
|
|
||||||
} catch (Error | Throwable $exception) {
|
} catch (Error | Throwable $exception) {
|
||||||
$this->response->setStatusCode(500);
|
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
|
||||||
$data = jTraceEx($exception);
|
$data = $code ? $exception->getMessage() : jTraceEx($exception);
|
||||||
} finally {
|
} finally {
|
||||||
$this->response->send($data, $response);
|
$this->requestEnd($data, $response);
|
||||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $data
|
||||||
|
* @param \Swoole\Http\Response $response
|
||||||
|
*/
|
||||||
|
protected function requestEnd(mixed $data, Response $response)
|
||||||
|
{
|
||||||
|
$sResponse = $this->response->getBuilder($data, $response);
|
||||||
|
|
||||||
|
$response->end($sResponse->getContent());
|
||||||
|
|
||||||
|
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
|
|||||||
Reference in New Issue
Block a user