This commit is contained in:
2021-08-12 15:07:12 +08:00
parent 6ea406251c
commit 0d6176e4e2
13 changed files with 141 additions and 302 deletions
-274
View File
@@ -1,274 +0,0 @@
<?php
namespace HttpServer\Http;
use Exception;
use HttpServer\Abstracts\HttpService;
use HttpServer\Http\Formatter\HtmlFormatter;
use HttpServer\Http\Formatter\JsonFormatter;
use HttpServer\Http\Formatter\XmlFormatter;
use HttpServer\IInterface\IFormatter;
use Kiri\Core\Help;
use Kiri\Kiri;
use Swoole\Http\Response as SResponse;
/**
* Class CoroutineResponse
* @package HttpServer\Http
*/
class CoroutineResponse extends HttpService
{
const JSON = 'json';
const XML = 'xml';
const HTML = 'html';
/** @var ?string */
public ?string $format = null;
/** @var int */
public int $statusCode = 200;
public array $headers = [];
public array $cookies = [];
private float $startTime = 0;
private array $_format_maps = [
self::JSON => JsonFormatter::class,
self::XML => XmlFormatter::class,
self::HTML => HtmlFormatter::class
];
public int $fd = 0;
/**
* @param $format
* @return $this
*/
public function setFormat($format): static
{
$this->format = $format;
return $this;
}
/**
* @return string
*/
public function getFormat(): 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 $content
* @return string
*/
public function toHtml($content): string
{
$this->format = self::HTML;
return (string)$content;
}
/**
* @param $content
* @return string|bool
*/
public function toJson($content): string|bool
{
$this->format = self::JSON;
return json_encode($content, JSON_UNESCAPED_UNICODE);
}
/**
* @param $content
* @return mixed
*/
public function toXml($content): mixed
{
$this->format = self::XML;
return $content;
}
/**
* @param $key
* @param $value
* @return Response
*/
public function addHeader($key, $value): static
{
$this->headers[$key] = $value;
return $this;
}
/**
* @param $name
* @param null $value
* @param null $expires
* @param null $path
* @param null $domain
* @param null $secure
* @param null $httponly
* @param null $samesite
* @param null $priority
* @return Response
*/
public function addCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): static
{
$this->cookies[] = func_get_args();
return $this;
}
/**
* @param $statusCode
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
}
/**
* @param mixed $context
* @param int $statusCode
* @return bool
* @throws Exception
*/
public function send(mixed $context, SResponse $response): mixed
{
$sendData = $this->parseData($context);
$response->setStatusCode($this->statusCode);
if (!isset($response->header['Content-Type'])) {
$response->header('Content-Type', $this->getFormat());
}
$response->header('Run-Time', $this->getRuntime());
$response->end($sendData);
Context::remove(SResponse::class);
return $sendData;
}
/**
* @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 = Kiri::getDi()->get($class);
$context = $di->send($context)->getData();
}
return $context;
}
/**
* @param $result
* @return void
* @throws Exception
*/
private function printResult($result): mixed
{
$result = Help::toString($result);
fire('CONSOLE_END');
if (str_contains((string)$result, 'Event::rshutdown(): Event::wait()')) {
return $result;
}
$string = 'Command Result: ' . PHP_EOL . PHP_EOL;
if (empty($result)) {
$string .= 'success!' . PHP_EOL . PHP_EOL;
} else {
$string .= $result . PHP_EOL . PHP_EOL;
}
echo $string . 'Command End!' . PHP_EOL;
return $result;
}
/**
* @param $url
* @param array $param
* @return int
*/
public function redirect($url, array $param = []): mixed
{
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;
}
/**
* @param string $path
* @param int $offset
* @param int $limit
* @param int $sleep
* @return string
*/
public function sendFile(string $path, int $offset = 0, int $limit = 1024000, int $sleep = 0): string
{
$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());
}
}
+17 -3
View File
@@ -46,6 +46,8 @@ class Response extends HttpService implements ResponseInterface
private mixed $endData;
private array $_clientInfo = [];
const FORMAT_MAPS = [
self::JSON => JsonFormatter::class,
self::XML => XmlFormatter::class,
@@ -69,11 +71,23 @@ class Response extends HttpService implements ResponseInterface
}
/**
* @param array $clientInfo
*/
public function setClientInfo(array $clientInfo)
{
$this->_clientInfo = $clientInfo;
}
/**
* @return mixed
*/
public function getClientInfo(): mixed
{
if (!empty($this->_clientInfo)) {
return $this->_clientInfo;
}
$server = ServerManager::getContext()->getServer();
return $server->getClientInfo($this->clientId, $this->reactorId);
}
@@ -152,17 +166,17 @@ class Response extends HttpService implements ResponseInterface
* @param bool $isChunk
* @param int $offset
* @param int $limit
* @return $this|Response
* @return FileResponse
* @throws Exception
*/
public function sendFile(string $path, bool $isChunk = false, int $offset = 0, int $limit = 10240): static
public function sendFile(string $path, bool $isChunk = false, int $offset = 0, int $limit = 10240): FileResponse
{
$this->format = self::FILE;
if (!file_exists($path)) {
throw new Exception('File `' . $path . '` not exists.');
}
$this->endData = ['path' => $path, 'isChunk' => $isChunk, 'limit' => $limit, 'offset' => $offset];
return $this;
return new FileResponse($this);
}
+3
View File
@@ -9,6 +9,8 @@ use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\ResponseEmitter;
use Server\Constrict\TcpEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnRequest;
@@ -47,6 +49,7 @@ abstract class Http extends Server implements OnRequest
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
}
}
+2 -3
View File
@@ -3,6 +3,7 @@
namespace Server\Abstracts;
use Annotation\Inject;
use Server\Constrict\Emitter;
use Server\Constrict\Response as CResponse;
use Server\Constrict\ResponseEmitter;
@@ -18,9 +19,7 @@ trait ResponseHelper
public CResponse $response;
/** @var ResponseEmitter */
#[Inject(ResponseEmitter::class)]
public ResponseEmitter $responseEmitter;
public Emitter $responseEmitter;
}
+3
View File
@@ -7,6 +7,8 @@ use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\TcpEmitter;
use Server\Constrict\UdpEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnReceive;
@@ -37,6 +39,7 @@ abstract class Tcp extends Server implements OnReceive
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(TcpEmitter::class);
}
}
+2
View File
@@ -7,6 +7,7 @@ use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\UdpEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnPacket;
@@ -42,6 +43,7 @@ abstract class Udp extends Server implements OnPacket
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(UdpEmitter::class);
}
}
+3
View File
@@ -8,6 +8,8 @@ use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\ResponseEmitter;
use Server\Constrict\WebSocketEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnClose;
@@ -45,6 +47,7 @@ abstract class Websocket extends Server implements OnHandshake, OnMessage, OnClo
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(WebSocketEmitter::class);
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Constrict;
use Server\ResponseInterface;
interface Emitter
{
/**
* @param mixed $response
* @param ResponseInterface $emitter
* @return mixed
*/
public function sender(mixed $response, ResponseInterface $emitter): void;
}
-20
View File
@@ -28,10 +28,6 @@ class ResponseEmitter
public function sender(\Swoole\Http\Response|\Swoole\Http2\Response|Server $response, ResponseInterface $emitter)
{
$content = $emitter->configure($response)->getContent();
if ($response instanceof Server) {
$this->sendTcpData($response, $emitter, $content);
return;
}
if ($content instanceof FileFormatter) {
$this->download($content->getData(), $response);
} else {
@@ -40,22 +36,6 @@ class ResponseEmitter
}
}
/**
* @param Server $response
* @param ResponseInterface $emitter
* @param IFormatter $formatter
*/
private function sendTcpData(Server $response, ResponseInterface $emitter, IFormatter $formatter)
{
if ($formatter instanceof FileFormatter) {
$response->sendfile($emitter->getClientId(), $formatter->getData());
} else {
$response->send($emitter->getClientId(), $formatter->getData());
}
}
const IMAGES = [
'png' => 'image/png',
'jpeg' => 'image/jpeg',
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace Server\Constrict;
use HttpServer\Http\Formatter\FileFormatter;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
use Swoole\Server;
/**
*
*/
class TcpEmitter implements Emitter
{
/**
* @param Server $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(Server $response, ResponseInterface $emitter)
{
$formatter = $emitter->getContent();
if ($formatter instanceof FileFormatter) {
$response->sendfile($emitter->getClientId(), $formatter->getData());
} else {
$response->send($emitter->getClientId(), $formatter->getData());
}
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace Server\Constrict;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
use Swoole\Server;
/**
*
*/
class UdpEmitter implements Emitter
{
/**
* @param Server $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(Server $response, ResponseInterface $emitter)
{
$clientInfo = $emitter->getClientInfo();
$response->sendto($clientInfo['host'], $clientInfo['port'],
$emitter->getContent()->getData()
);
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Server\Constrict;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
use Swoole\WebSocket\Server;
/**
*
*/
class WebSocketEmitter implements Emitter
{
/**
* @param Server $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(Server $response, ResponseInterface $emitter)
{
$response->push($emitter->getClientId(), $emitter->getContent()->getData());
}
}
-2
View File
@@ -37,8 +37,6 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
/**
* @param Request $request
* @param Response $response
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onRequest(Request $request, Response $response): void
{