改名
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class PageNotFoundException extends \Exception
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
#[Pure] public function __construct(int $code)
|
||||
{
|
||||
parent::__construct('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', $code);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,6 @@ namespace Server\Abstracts;
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts\Utility;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
|
||||
trait EventDispatchHelper
|
||||
{
|
||||
|
||||
/** @var EventDispatch */
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts\Utility;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Server\Constrict\Emitter;
|
||||
use Server\Constrict\Response as CResponse;
|
||||
use Server\Constrict\ResponseEmitter;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
trait ResponseHelper
|
||||
{
|
||||
|
||||
/** @var CResponse|mixed */
|
||||
#[Inject(CResponse::class)]
|
||||
public CResponse $response;
|
||||
|
||||
|
||||
public Emitter $responseEmitter;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Server\Constrict\ResponseInterface;
|
||||
|
||||
interface Emitter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $response
|
||||
* @param ResponseInterface $emitter
|
||||
* @return mixed
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface $emitter): void;
|
||||
|
||||
}
|
||||
@@ -1,509 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Http\Handler\Context;
|
||||
use Http\Handler\AuthIdentity;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Kiri;
|
||||
use Http\Message\Response;
|
||||
use Http\Message\ServerRequest;
|
||||
use Http\Message\Uploaded;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
|
||||
|
||||
class Request implements RequestInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return ServerRequest
|
||||
*/
|
||||
private function __call__(): ServerRequest
|
||||
{
|
||||
return Context::getContext(RequestInterface::class, new ServerRequest());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call(string $name, array $arguments)
|
||||
{
|
||||
return $this->__call__()->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
// TODO: Change the autogenerated stub
|
||||
return $this->__call__()->{$name};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Http\Request $request
|
||||
* @return Request
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function create(\Swoole\Http\Request $request): Request
|
||||
{
|
||||
$serverRequest = ServerRequest::createServerRequest($request);
|
||||
|
||||
Context::setContext(ResponseInterface::class, new Response());
|
||||
|
||||
Context::setContext(RequestInterface::class, $serverRequest);
|
||||
|
||||
return Kiri::getDi()->get(Request::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProtocolVersion(): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $version
|
||||
* @return Request
|
||||
*/
|
||||
public function withProtocolVersion($version): RequestInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($version);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \string[][]
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasHeader($name): bool
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return string[]
|
||||
*/
|
||||
public function getHeader($name): array
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
public function getHeaderLine($name): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string|string[] $value
|
||||
* @return Request
|
||||
*/
|
||||
public function withHeader($name, $value): RequestInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string|string[] $value
|
||||
* @return Request
|
||||
*/
|
||||
public function withAddedHeader($name, $value): RequestInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Request
|
||||
*/
|
||||
public function withoutHeader($name): RequestInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return StreamInterface
|
||||
*/
|
||||
public function getBody(): StreamInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param StreamInterface $body
|
||||
* @return Request
|
||||
*/
|
||||
public function withBody(StreamInterface $body): RequestInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRequestTarget(): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $requestTarget
|
||||
* @return Request
|
||||
*/
|
||||
public function withRequestTarget($requestTarget): RequestInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($requestTarget);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod(): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @return bool
|
||||
*/
|
||||
public function isMethod(string $method): bool
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($method);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @return Request
|
||||
*/
|
||||
public function withMethod($method): RequestInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($method);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return UriInterface
|
||||
*/
|
||||
public function getUri(): UriInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param UriInterface $uri
|
||||
* @param false $preserveHost
|
||||
* @return Request
|
||||
*/
|
||||
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($uri, $preserveHost);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return UploadedFileInterface|null
|
||||
*/
|
||||
public function file(string $name): ?UploadedFileInterface
|
||||
{
|
||||
$files = $this->__call__()->getUploadedFiles();
|
||||
if (empty($files) || !isset($files[$name])) {
|
||||
return null;
|
||||
}
|
||||
return new Uploaded($files[$name]['tmp_name'], $files[$name]['name'], $files[$name]['type'],
|
||||
$files[$name]['size'], $files[$name]['error']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|null $name
|
||||
* @param mixed|null $default
|
||||
* @return mixed
|
||||
*/
|
||||
private function _getParsedBody(string|null $name = null, mixed $default = null): mixed
|
||||
{
|
||||
$body = $this->__call__()->getParsedBody();
|
||||
if (empty($name)) {
|
||||
return $body;
|
||||
}
|
||||
return $body[$name] ?? $default;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function all(): array
|
||||
{
|
||||
return $this->_getParsedBody();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool|int|string|null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function query(string $name, bool|int|string|null $default = null): mixed
|
||||
{
|
||||
$files = $this->__call__()->getQueryParams();
|
||||
|
||||
return $files[$name] ?? $default;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param int|bool|array|string|null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function post(string $name, int|bool|array|string|null $default = null): mixed
|
||||
{
|
||||
return $this->_getParsedBody($name, $default);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return int|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function int(string $name, bool $required = false): ?int
|
||||
{
|
||||
$int = $this->_getParsedBody($name);
|
||||
if (is_null($int) && $required) {
|
||||
throw new \Exception('Required param "' . $name . '"');
|
||||
}
|
||||
return (int)$int;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return float|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function float(string $name, bool $required = false): ?float
|
||||
{
|
||||
$int = $this->_getParsedBody($name);
|
||||
if (is_null($int) && $required) {
|
||||
throw new \Exception('Required param "' . $name . '"');
|
||||
}
|
||||
return (float)$int;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return string|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function date(string $name, bool $required = false): ?string
|
||||
{
|
||||
$int = $this->_getParsedBody($name);
|
||||
if (is_null($int) && $required) {
|
||||
throw new \Exception('Required param "' . $name . '"');
|
||||
}
|
||||
return (string)$int;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return int|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function timestamp(string $name, bool $required = false): ?int
|
||||
{
|
||||
$int = $this->_getParsedBody($name);
|
||||
if (is_null($int) && $required) {
|
||||
throw new \Exception('Required param "' . $name . '"');
|
||||
}
|
||||
return (int)$int;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return string|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function string(string $name, bool $required = false): ?string
|
||||
{
|
||||
$int = $this->_getParsedBody($name);
|
||||
if (is_null($int) && $required) {
|
||||
throw new \Exception('Required param "' . $name . '"');
|
||||
}
|
||||
return (string)$int;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $default
|
||||
* @return array|null
|
||||
*/
|
||||
public function array(string $name, array $default = []): ?array
|
||||
{
|
||||
$int = $this->_getParsedBody($name);
|
||||
if (is_null($int)) {
|
||||
return $default;
|
||||
}
|
||||
return $int;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function gets(): ?array
|
||||
{
|
||||
return $this->__call__()->getQueryParams();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string $sizeField
|
||||
* @param int $max
|
||||
* @return float|int
|
||||
*/
|
||||
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int
|
||||
{
|
||||
$page = $this->query($field);
|
||||
$size = $this->size($sizeField, $max);
|
||||
$offset = ($page - 1) * $size;
|
||||
if ($offset < 0) {
|
||||
$offset = 0;
|
||||
}
|
||||
return $offset;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param int $max
|
||||
* @return int
|
||||
*/
|
||||
public function size(string $field = 'size', int $max = 100): int
|
||||
{
|
||||
$size = $this->query($field);
|
||||
if ($size > $max) {
|
||||
$size = $max;
|
||||
}
|
||||
return $size;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function input($name, $default = null): mixed
|
||||
{
|
||||
return $this->_getParsedBody($name, $default);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
#[Pure] public function getStartTime(): float
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param AuthIdentity $authority
|
||||
*/
|
||||
public function setAuthority(AuthIdentity $authority): void
|
||||
{
|
||||
$this->__call__()->{__FUNCTION__}($authority);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getClientId(): int
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowOrigin(): ?string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowHeaders(): ?string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlRequestMethod(): ?string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
|
||||
use Http\Handler\AuthIdentity;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Http\Message\ServerRequest;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* @mixin ServerRequest
|
||||
*/
|
||||
interface RequestInterface extends \Psr\Http\Message\RequestInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @return bool
|
||||
*/
|
||||
public function isMethod(string $method): bool;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return UploadedFileInterface|null
|
||||
*/
|
||||
public function file(string $name): ?UploadedFileInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function all(): array;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param int|string|bool|null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function query(string $name, int|string|bool|null $default = null): mixed;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param int|string|bool|array|null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function post(string $name, int|string|bool|null|array $default = null): mixed;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return int|null
|
||||
*/
|
||||
public function int(string $name, bool $required = false): ?int;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return float|null
|
||||
*/
|
||||
public function float(string $name, bool $required = false): ?float;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return string|null
|
||||
*/
|
||||
public function date(string $name, bool $required = false): ?string;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return int|null
|
||||
*/
|
||||
public function timestamp(string $name, bool $required = false): ?int;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param bool $required
|
||||
* @return string|null
|
||||
*/
|
||||
public function string(string $name, bool $required = false): ?string;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $default
|
||||
* @return array|null
|
||||
*/
|
||||
public function array(string $name, array $default = []): ?array;
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function gets(): ?array;
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string $sizeField
|
||||
* @param int $max
|
||||
* @return float|int
|
||||
*/
|
||||
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int;
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param int $max
|
||||
* @return int
|
||||
*/
|
||||
public function size(string $field = 'size', int $max = 100): int;
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function input($name, $default = null): mixed;
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
#[Pure] public function getStartTime(): float;
|
||||
|
||||
/**
|
||||
* @param AuthIdentity $authority
|
||||
*/
|
||||
public function setAuthority(AuthIdentity $authority): void;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getClientId(): int;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowOrigin(): ?string;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowHeaders(): ?string;
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlRequestMethod(): ?string;
|
||||
}
|
||||
@@ -1,361 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
|
||||
use Http\Handler\Context;
|
||||
use Http\Message\ContentType;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Http\Message\ServerRequest as RequestMessage;
|
||||
use Http\Message\Response as Psr7Response;
|
||||
use Server\ServerManager;
|
||||
use Server\SInterface\OnDownloadInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
* @package Server
|
||||
*/
|
||||
class Response implements ResponseInterface
|
||||
{
|
||||
|
||||
const JSON = 'json';
|
||||
const XML = 'xml';
|
||||
const HTML = 'html';
|
||||
const FILE = 'file';
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$contentType = Config::get('response',[
|
||||
'format' => ContentType::JSON,
|
||||
'charset' => 'utf-8'
|
||||
]);
|
||||
$this->withContentType($contentType['format'] ?? ContentType::JSON)
|
||||
->withCharset($contentType['charset'] ?? 'utf-8');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $name)
|
||||
{
|
||||
return $this->__call__()->{$name};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Psr7Response
|
||||
*/
|
||||
public function __call__(): Psr7Response
|
||||
{
|
||||
return Context::getContext(ResponseInterface::class, new Psr7Response());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProtocolVersion(): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $version
|
||||
* @return ResponseInterface|Psr7Response
|
||||
*/
|
||||
public function withProtocolVersion($version): ResponseInterface|Psr7Response
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($version);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasHeader($name): bool
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
public function getHeader($name): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
public function getHeaderLine($name): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string|string[] $value
|
||||
* @return ResponseInterface|Psr7Response
|
||||
*/
|
||||
public function withHeader($name, $value): ResponseInterface|Psr7Response
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string|string[] $value
|
||||
* @return ResponseInterface|Psr7Response
|
||||
*/
|
||||
public function withAddedHeader($name, $value): ResponseInterface|Psr7Response
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return ResponseInterface|Psr7Response
|
||||
*/
|
||||
public function withoutHeader($name): ResponseInterface|Psr7Response
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return StreamInterface
|
||||
*/
|
||||
public function getBody(): StreamInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param StreamInterface $body
|
||||
* @return ResponseInterface|Psr7Response
|
||||
*/
|
||||
public function withBody(StreamInterface $body): ResponseInterface|Psr7Response
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $code
|
||||
* @param string $reasonPhrase
|
||||
* @return ResponseInterface|Psr7Response
|
||||
*/
|
||||
public function withStatus($code, $reasonPhrase = ''): ResponseInterface|Psr7Response
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($code, $reasonPhrase);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getReasonPhrase(): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return OnDownloadInterface
|
||||
*/
|
||||
public function file(string $path): OnDownloadInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $responseData
|
||||
* @return string|array|bool|int|null
|
||||
*/
|
||||
public function _toArray($responseData): string|array|null|bool|int
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function xml($data): ResponseInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function html($data): ResponseInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function json($data): ResponseInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContentType(): string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasContentType(): bool
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function withContentType(string $type): ResponseInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|null $value
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function withAccessControlAllowOrigin(?string $value): ResponseInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|null $value
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function withAccessControlRequestMethod(?string $value): ResponseInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|null $value
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function withAccessControlAllowHeaders(?string $value): ResponseInterface
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowOrigin(): ?string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowHeaders(): ?string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlRequestMethod(): ?string
|
||||
{
|
||||
return $this->__call__()->{__FUNCTION__}();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getClientId(): int
|
||||
{
|
||||
if (!Context::hasContext('client.id.property')) {
|
||||
$request = Context::getContext(RequestInterface::class, new RequestMessage());
|
||||
return Context::setContext('client.id.property', $request->getClientId());
|
||||
}
|
||||
return (int)Context::getContext('client.id.property');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getClientInfo(): array
|
||||
{
|
||||
if (!Context::hasContext('client.info.property')) {
|
||||
$request = Context::getContext(RequestInterface::class, new RequestMessage());
|
||||
|
||||
$server = Kiri::getDi()->get(ServerManager::class)->getServer();
|
||||
|
||||
$clientInfo = $server->getClientInfo($request->getClientId());
|
||||
|
||||
return Context::setContext('client.info.property', $clientInfo);
|
||||
}
|
||||
return Context::getContext('client.info.property');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Server\SInterface\OnDownloadInterface;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class ResponseEmitter implements Emitter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
#[Inject(RequestInterface::class)]
|
||||
public RequestInterface $request;
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Http\Response $response
|
||||
* @param \Http\Message\Response|ResponseInterface $emitter
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface|\Http\Message\Response $emitter): void
|
||||
{
|
||||
if (is_array($emitter->getHeaders())) {
|
||||
foreach ($emitter->getHeaders() as $name => $values) {
|
||||
$response->header($name, implode(';', $values));
|
||||
}
|
||||
}
|
||||
if (is_array($emitter->getCookieParams())) {
|
||||
foreach ($emitter->getCookieParams() as $name => $cookie) {
|
||||
$response->cookie($name, ...$cookie);
|
||||
}
|
||||
}
|
||||
$response->setStatusCode($emitter->getStatusCode());
|
||||
$response->header('Server', 'swoole');
|
||||
$response->header('Swoole-Version', swoole_version());
|
||||
if (!($emitter instanceof OnDownloadInterface)) {
|
||||
$response->end($emitter->getBody()->getContents());
|
||||
} else {
|
||||
$emitter->dispatch($response);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Http\Message\Response;
|
||||
use Server\SInterface\OnDownloadInterface;
|
||||
|
||||
/**
|
||||
* @mixin Response
|
||||
*/
|
||||
interface ResponseInterface extends \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return OnDownloadInterface
|
||||
*/
|
||||
public function file(string $path): OnDownloadInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function xml($data): ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function html($data): ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function json($data): ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getContentType(): ?string;
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasContentType(): bool;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function withContentType(string $type): ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @param ?string $value
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function withAccessControlAllowOrigin(?string $value): ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @param ?string $value
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function withAccessControlRequestMethod(?string $value): ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
* @param ?string $value
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function withAccessControlAllowHeaders(?string $value): ResponseInterface;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowOrigin(): ?string;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowHeaders(): ?string;
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlRequestMethod(): ?string;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Exception;
|
||||
use Http\Handler\Formatter\FileFormatter;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class TcpEmitter implements Emitter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $response
|
||||
* @param ResponseInterface $emitter
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface $emitter): void
|
||||
{
|
||||
$formatter = $emitter->stream->getContents();
|
||||
if ($formatter instanceof FileFormatter) {
|
||||
$response->sendfile($emitter->getClientId(), $formatter);
|
||||
} else {
|
||||
$response->send($emitter->getClientId(), $formatter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class UdpEmitter implements Emitter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $response
|
||||
* @param ResponseInterface $emitter
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface $emitter): void
|
||||
{
|
||||
$clientInfo = $emitter->getClientInfo();
|
||||
$response->sendto($clientInfo['host'], $clientInfo['port'],
|
||||
$emitter->stream->getContents()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Kiri;
|
||||
use Server\ServerManager;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class WebSocketEmitter implements Emitter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $response
|
||||
* @param ResponseInterface|\Http\Message\Response $emitter
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface|\Http\Message\Response $emitter): void
|
||||
{
|
||||
$server = Kiri::getDi()->get(ServerManager::class)->getServer();
|
||||
|
||||
$server->push($response->fd, $emitter->getBody()->getContents());
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
class OnAfterRequest
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
class OnBeforeRequest
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
|
||||
|
||||
use Http\Message\ContentType;
|
||||
use Http\Message\Stream;
|
||||
use Server\Constrict\Response;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class ExceptionHandlerDispatcher implements ExceptionHandlerInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Throwable $exception
|
||||
* @param Response $response
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function emit(Throwable $exception, Response $response): ResponseInterface
|
||||
{
|
||||
$response->withContentType(ContentType::HTML)->withCharset('utf-8');
|
||||
if ($exception->getCode() == 404) {
|
||||
return $response->withBody(new Stream($exception->getMessage()))
|
||||
->withStatus(404);
|
||||
}
|
||||
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
|
||||
return $response->withBody(new Stream(jTraceEx($exception, null, true)))
|
||||
->withStatus($code);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Server\Constrict\Response;
|
||||
use Throwable;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
interface ExceptionHandlerInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Throwable $exception
|
||||
* @param Response $response
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function emit(Throwable $exception, Response $response): ResponseInterface;
|
||||
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Manager;
|
||||
namespace Server\Handler;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Server\Abstracts\Server;
|
||||
use Exception;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Server\SInterface\OnPipeMessageInterface;
|
||||
use Kiri\Events\EventDispatch;
|
||||
|
||||
@@ -32,9 +31,7 @@ class OnPipeMessage extends Server
|
||||
if (!is_object($message) || !($message instanceof OnPipeMessageInterface)) {
|
||||
return;
|
||||
}
|
||||
|
||||
call_user_func([$message, 'process'], $server, $src_worker_id);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Manager;
|
||||
namespace Server\Handler;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Manager;
|
||||
namespace Server\Handler;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Manager;
|
||||
namespace Server\Handler;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
@@ -1,17 +1,14 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Server\Task;
|
||||
namespace Server\Handler;
|
||||
|
||||
|
||||
use Kiri\Abstracts\Config;
|
||||
use Annotation\Inject;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
use Server\Constrict\Response;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnTaskInterface;
|
||||
use Swoole\Server;
|
||||
|
||||
@@ -24,26 +21,8 @@ class OnServerTask
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface|null
|
||||
*/
|
||||
public ?ExceptionHandlerInterface $handler = null;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function emit(\Throwable $exception, Response $response): ResponseInterface
|
||||
{
|
||||
if ($this->handler == null) {
|
||||
$exceptionHandler = Config::get('exception.task', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->handler = Kiri::getDi()->get($exceptionHandler);
|
||||
}
|
||||
return $this->handler->emit($exception, $response);
|
||||
}
|
||||
#[Inject(Logger::class)]
|
||||
public Logger $logger;
|
||||
|
||||
|
||||
/**
|
||||
@@ -58,7 +37,9 @@ class OnServerTask
|
||||
try {
|
||||
$data = $this->resolve($data);
|
||||
} catch (\Throwable $exception) {
|
||||
$data = $this->emit($exception, new Response());
|
||||
$data = jTraceEx($exception);
|
||||
|
||||
$this->logger->error('task', [$data]);
|
||||
} finally {
|
||||
$server->finish($data);
|
||||
}
|
||||
@@ -75,7 +56,9 @@ class OnServerTask
|
||||
try {
|
||||
$data = $this->resolve($task->data);
|
||||
} catch (\Throwable $exception) {
|
||||
$data = $this->emit($exception, new Response());
|
||||
$data = jTraceEx($exception);
|
||||
|
||||
$this->logger->error('task', [$data]);
|
||||
} finally {
|
||||
$server->finish($data);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Worker;
|
||||
namespace Server\Handler;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Server\Protocol;
|
||||
|
||||
|
||||
abstract class Protocol
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function resolveProtocol($data): array
|
||||
{
|
||||
$explode = explode("\r\n\r\n", $data);
|
||||
|
||||
$http_protocol = [];
|
||||
foreach (explode("\r\n", $explode[0]) as $key => $datum) {
|
||||
if (empty($datum) || $key == 0) {
|
||||
continue;
|
||||
}
|
||||
[$key, $value] = explode(': ', $datum);
|
||||
|
||||
$http_protocol[trim($key)] = trim($value);
|
||||
}
|
||||
return [$http_protocol, $explode[1]];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Protocol;
|
||||
|
||||
|
||||
class WebSocket extends Protocol
|
||||
{
|
||||
|
||||
|
||||
//
|
||||
public function decode($received): ?string
|
||||
{
|
||||
$decoded = null;
|
||||
$buffer = $received;
|
||||
$len = ord($buffer[1]) & 127;
|
||||
if ($len === 126) {
|
||||
$masks = substr($buffer, 4, 4);
|
||||
$data = substr($buffer, 8);
|
||||
} else {
|
||||
if ($len === 127) {
|
||||
$masks = substr($buffer, 10, 4);
|
||||
$data = substr($buffer, 14);
|
||||
} else {
|
||||
$masks = substr($buffer, 2, 4);
|
||||
$data = substr($buffer, 6);
|
||||
}
|
||||
}
|
||||
for ($index = 0; $index < strlen($data); $index++) {
|
||||
$decoded .= $data[$index] ^ $masks[$index % 4];
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
const BINARY_TYPE_BLOB = "\x81";
|
||||
|
||||
|
||||
public function encode($buffer): string
|
||||
{
|
||||
$len = strlen($buffer);
|
||||
|
||||
$first_byte = self::BINARY_TYPE_BLOB;
|
||||
|
||||
if ($len <= 125) {
|
||||
$encode_buffer = $first_byte . chr($len) . $buffer;
|
||||
} else {
|
||||
if ($len <= 65535) {
|
||||
$encode_buffer = $first_byte . chr(126) . pack("n", $len) . $buffer;
|
||||
} else {
|
||||
//pack("xxxN", $len)pack函数只处理2的32次方大小的文件,实际上2的32次方已经4G了。
|
||||
$encode_buffer = $first_byte . chr(127) . pack("xxxxN", $len) . $buffer;
|
||||
}
|
||||
}
|
||||
|
||||
return $encode_buffer;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function getWebSocketProtocol($data): string
|
||||
{
|
||||
[$http_protocol, $body] = $this->resolveProtocol($data);
|
||||
$key = base64_encode(sha1($http_protocol['Sec-WebSocket-Key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', TRUE));
|
||||
$headers = [
|
||||
'HTTP/1.1 101 Switching Protocols',
|
||||
'Upgrade: websocket',
|
||||
'Connection: Upgrade',
|
||||
'Sec-WebSocket-Accept: ' . $key,
|
||||
'Sec-WebSocket-Version: 13',
|
||||
];
|
||||
if (isset($http_protocol['Sec-WebSocket-Protocol'])) {
|
||||
$headers[] = 'Sec-WebSocket-Protocol: ' . $http_protocol['Sec-WebSocket-Protocol'];
|
||||
}
|
||||
return implode("\r\n", $headers) . "\r\n\r\n";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Server\Constrict\Response;
|
||||
use Http\Message\Stream;
|
||||
|
||||
class Sender
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var Response
|
||||
*/
|
||||
#[Inject(Response::class)]
|
||||
public Response $response;
|
||||
|
||||
|
||||
/**
|
||||
* @var ServerManager
|
||||
*/
|
||||
#[Inject(ServerManager::class)]
|
||||
public ServerManager $manager;
|
||||
|
||||
|
||||
/**
|
||||
* @param $fd
|
||||
* @param $data
|
||||
*/
|
||||
public function send($fd, $data)
|
||||
{
|
||||
$body = $this->response->withBody(new Stream($data));
|
||||
$server = $this->manager->getServer();
|
||||
if (!$server->isEstablished($fd)) {
|
||||
return;
|
||||
}
|
||||
$server->push($fd, $body->getBody()->getContents());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $fd
|
||||
* @param int $code
|
||||
* @param string $message
|
||||
*/
|
||||
public function close($fd, int $code = 401, string $message = '')
|
||||
{
|
||||
$server = $this->manager->getServer();
|
||||
if (!$server->isEstablished($fd)) {
|
||||
return;
|
||||
}
|
||||
$server->close($code, $message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,7 +15,7 @@ use Server\Abstracts\OnWorkerStart as WorkerDispatch;
|
||||
use Server\Events\OnBeforeWorkerStart;
|
||||
use Server\Events\OnTaskerStart;
|
||||
use Server\Events\OnWorkerStart;
|
||||
use Server\Worker\OnServerWorker;
|
||||
use Server\Handler\OnServerWorker;
|
||||
use Swoole\Coroutine;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
|
||||
@@ -10,10 +10,14 @@ use Kiri\Di\ContainerInterface;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
use Server\Manager\OnPipeMessage;
|
||||
use Server\Manager\OnServer;
|
||||
use Server\Manager\OnServerManager;
|
||||
use Server\Manager\OnServerReload;
|
||||
|
||||
use Server\Handler\OnPipeMessage;
|
||||
use Server\Handler\OnServer;
|
||||
use Server\Handler\OnServerManager;
|
||||
use Server\Handler\OnServerReload;
|
||||
use Server\Handler\OnServerWorker;
|
||||
use Server\Handler\OnServerTask;
|
||||
|
||||
use Server\SInterface\OnCloseInterface;
|
||||
use Server\SInterface\OnConnectInterface;
|
||||
use Server\SInterface\OnDisconnectInterface;
|
||||
@@ -23,8 +27,6 @@ use Server\SInterface\OnPacketInterface;
|
||||
use Server\SInterface\OnProcessInterface;
|
||||
use Server\SInterface\OnReceiveInterface;
|
||||
use Server\SInterface\OnTaskInterface;
|
||||
use Server\Task\OnServerTask;
|
||||
use Server\Worker\OnServerWorker;
|
||||
use Swoole\Http\Server as HServer;
|
||||
use Swoole\Process;
|
||||
use Swoole\Server;
|
||||
|
||||
@@ -1,154 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Service;
|
||||
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Http\Handler\Abstracts\HandlerManager;
|
||||
use Http\Handler\Context;
|
||||
use Http\Handler\Dispatcher;
|
||||
use Http\Handler\Handler;
|
||||
use Http\Handler\Router;
|
||||
use Http\Message\ServerRequest;
|
||||
use Http\Message\Stream;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Server\Abstracts\Utility\EventDispatchHelper;
|
||||
use Server\Abstracts\Utility\ResponseHelper;
|
||||
use Server\Constrict\RequestInterface;
|
||||
use Server\Constrict\ResponseEmitter;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnCloseInterface;
|
||||
use Server\SInterface\OnConnectInterface;
|
||||
use Server\SInterface\OnRequestInterface;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
/** @var Router|mixed */
|
||||
#[Inject(Router::class)]
|
||||
public Router $router;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.http', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd): void
|
||||
{
|
||||
// TODO: Implement onConnect() method.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onRequest(Request $request, Response $response): void
|
||||
{
|
||||
try {
|
||||
[$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);
|
||||
}
|
||||
} catch (\Throwable $throwable) {
|
||||
$PsrResponse = $this->exceptionHandler->emit($throwable, $this->response);
|
||||
} finally {
|
||||
$this->responseEmitter->sender($response, $PsrResponse);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Handler $handler
|
||||
* @param $PsrRequest
|
||||
* @return ResponseInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$dispatcher = new Dispatcher($handler, $handler->_middlewares);
|
||||
return $dispatcher->handle($PsrRequest);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array<ServerRequestInterface, ResponseInterface>
|
||||
* @throws Exception
|
||||
*/
|
||||
private function initRequestResponse(Request $request): array
|
||||
{
|
||||
$PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response());
|
||||
|
||||
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
|
||||
if ($PsrRequest->isMethod('OPTIONS')) {
|
||||
$request->server['request_uri'] = '/*';
|
||||
}
|
||||
return [$PsrRequest, $PsrResponse];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onDisconnect(Server $server, int $fd): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onClose(Server $server, int $fd): void
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Service;
|
||||
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Abstracts\Utility\EventDispatchHelper;
|
||||
use Server\Abstracts\Utility\ResponseHelper;
|
||||
use Server\Constrict\TcpEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnCloseInterface;
|
||||
use Server\SInterface\OnConnectInterface;
|
||||
use Server\SInterface\OnReceiveInterface;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Tcp implements OnConnectInterface, OnCloseInterface, OnReceiveInterface
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.tcp', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(TcpEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd): void
|
||||
{
|
||||
// TODO: Implement onConnect() method.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @param int $reactor_id
|
||||
* @param string $data
|
||||
*/
|
||||
public function onReceive(Server $server, int $fd, int $reactor_id, string $data): void
|
||||
{
|
||||
// TODO: Implement onReceive() method.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onClose(Server $server, int $fd): void
|
||||
{
|
||||
// TODO: Implement onClose() method.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onDisconnect(Server $server, int $fd): void
|
||||
{
|
||||
// TODO: Implement onDisconnect() method.
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Service;
|
||||
|
||||
|
||||
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Abstracts\Server;
|
||||
use Server\Abstracts\Utility\EventDispatchHelper;
|
||||
use Server\Abstracts\Utility\ResponseHelper;
|
||||
use Server\Constrict\UdpEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnPacketInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Udp implements OnPacketInterface
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.udp', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(UdpEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param string $data
|
||||
* @param array $clientInfo
|
||||
*/
|
||||
public function onPacket(Server $server, string $data, array $clientInfo): void
|
||||
{
|
||||
// TODO: Implement onPacket() method.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Service;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Abstracts\Utility\EventDispatchHelper;
|
||||
use Server\Abstracts\Utility\ResponseHelper;
|
||||
use Server\Constrict\WebSocketEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnCloseInterface;
|
||||
use Server\SInterface\OnHandshakeInterface;
|
||||
use Server\SInterface\OnMessageInterface;
|
||||
use Server\SInterface\OnRequestInterface;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\Server;
|
||||
use Swoole\WebSocket\Frame;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class WebSocket implements OnHandshakeInterface, OnMessageInterface, OnCloseInterface
|
||||
{
|
||||
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.websocket', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(WebSocketEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandshake(Request $request, Response $response): void
|
||||
{
|
||||
// TODO: Implement OnHandshakeInterface() method.
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
||||
throw new Exception('protocol error.', 500);
|
||||
}
|
||||
$key = base64_encode(sha1($request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', TRUE));
|
||||
$headers = [
|
||||
'Upgrade' => 'websocket',
|
||||
'Connection' => 'Upgrade',
|
||||
'Sec-websocket-Accept' => $key,
|
||||
'Sec-websocket-Version' => '13',
|
||||
];
|
||||
if (isset($request->header['sec-websocket-protocol'])) {
|
||||
$explode = explode(',',$request->header['sec-websocket-protocol']);
|
||||
$headers['Sec-websocket-Protocol'] = $explode[0];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$response->setHeader($key, $val);
|
||||
}
|
||||
$response->status(101);
|
||||
$response->end();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Frame $frame
|
||||
*/
|
||||
public function onMessage(Server $server, Frame $frame): void
|
||||
{
|
||||
// TODO: Implement OnMessageInterface() method.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onClose(Server $server, int $fd): void
|
||||
{
|
||||
// TODO: Implement OnCloseInterface() method.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onDisconnect(Server $server, int $fd): void
|
||||
{
|
||||
// TODO: Implement OnDisconnectInterface() method.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user