diff --git a/core/Di/Container.php b/core/Di/Container.php index f80561c5..d7a02e1d 100644 --- a/core/Di/Container.php +++ b/core/Di/Container.php @@ -24,8 +24,8 @@ use ReflectionMethod; use ReflectionProperty; use Server\Constrict\Request; use Server\Constrict\Response; -use Server\RequestInterface; -use Server\ResponseInterface; +use Server\Constrict\RequestInterface; +use Server\Constrict\ResponseInterface; /** * Class Container diff --git a/core/Jwt/JWTAuthMiddleware.php b/core/Jwt/JWTAuthMiddleware.php index a1e7f141..a8437b10 100644 --- a/core/Jwt/JWTAuthMiddleware.php +++ b/core/Jwt/JWTAuthMiddleware.php @@ -8,7 +8,6 @@ namespace Kiri\Jwt; use Closure; use Exception; use Http\Route\MiddlewareAbstracts; -use Server\RequestInterface; use Kiri\Kiri; /** diff --git a/function.php b/function.php index 81dfc90c..3c6539a6 100644 --- a/function.php +++ b/function.php @@ -21,7 +21,7 @@ use Kiri\Kiri; use Psr\Log\LoggerInterface; use Server\Constrict\Request; use Server\Constrict\Response; -use Server\ResponseInterface; +use Server\Constrict\ResponseInterface; use Server\ServerManager; use Swoole\WebSocket\Server; use Server\Message\Response as Par7Response; diff --git a/http-helper/Controller.php b/http-helper/Controller.php index 69233dd6..4702735d 100644 --- a/http-helper/Controller.php +++ b/http-helper/Controller.php @@ -9,8 +9,8 @@ use JetBrains\PhpStorm\Pure; use Kiri\Application; use Kiri\Di\ContainerInterface; use Psr\Log\LoggerInterface; -use Server\RequestInterface; -use Server\ResponseInterface; +use Server\Constrict\RequestInterface; +use Server\Constrict\ResponseInterface; /** * Class WebController diff --git a/http-helper/IInterface/MiddlewareInterface.php b/http-helper/IInterface/MiddlewareInterface.php index 15c25797..85bdeb72 100644 --- a/http-helper/IInterface/MiddlewareInterface.php +++ b/http-helper/IInterface/MiddlewareInterface.php @@ -6,7 +6,7 @@ namespace Http\IInterface; use Closure; -use Server\RequestInterface; +use Server\Constrict\RequestInterface; /** * Interface IMiddleware diff --git a/http-helper/Route/CoreMiddleware.php b/http-helper/Route/CoreMiddleware.php index e4daa1ac..97cde717 100644 --- a/http-helper/Route/CoreMiddleware.php +++ b/http-helper/Route/CoreMiddleware.php @@ -7,7 +7,7 @@ namespace Http\Route; use Closure; use Exception; -use Server\RequestInterface; +use Server\Constrict\RequestInterface; /** * Class CoreMiddleware diff --git a/http-helper/Route/Node.php b/http-helper/Route/Node.php index d83ce5b0..cc173afa 100644 --- a/http-helper/Route/Node.php +++ b/http-helper/Route/Node.php @@ -5,20 +5,17 @@ declare(strict_types=1); namespace Http\Route; -use Annotation\Aspect; use Closure; use Exception; use Http\Exception\RequestException; use JetBrains\PhpStorm\Pure; -use Kiri\Di\NoteManager; use Kiri\Events\EventProvider; use Kiri\Exception\NotFindClassException; -use Kiri\IAspect; use Kiri\Kiri; use ReflectionException; use Server\Constant; use Server\Events\OnAfterWorkerStart; -use Server\RequestInterface; +use Server\Constrict\RequestInterface; /** * Class Node diff --git a/http-helper/Route/Router.php b/http-helper/Route/Router.php index 1d3fb719..95547893 100644 --- a/http-helper/Route/Router.php +++ b/http-helper/Route/Router.php @@ -16,7 +16,7 @@ use Kiri\Exception\ConfigException; use Kiri\Exception\NotFindClassException; use Kiri\Kiri; use ReflectionException; -use Server\RequestInterface; +use Server\Constrict\RequestInterface; use Throwable; defined('ROUTER_TREE') or define('ROUTER_TREE', 1); diff --git a/http-server/Message/Download.php b/http-message/Download.php similarity index 98% rename from http-server/Message/Download.php rename to http-message/Download.php index e2ae8ba8..b48e9ac0 100644 --- a/http-server/Message/Download.php +++ b/http-message/Download.php @@ -1,6 +1,6 @@ getHeaderLine('Access-Control-Request-Method'); } + + + /** + * @param string $type + * @return Response + */ + public function withContentType(string $type): static + { + return $this->withHeader('Content-Type', $type); + } + + + /** + * @return bool + */ + #[Pure] public function hasContentType(): bool + { + return $this->hasHeader('Content-Type'); + } + + + /** + * @return string + */ + #[Pure] public function getContentType(): string + { + return $this->getHeaderLine('Content-Type'); + } + + /** + * @param string|null $value + * @return Response + */ + public function withAccessControlAllowHeaders(?string $value): static + { + return $this->withHeader('Access-Control-Allow-Headers', $value); + } + + + /** + * @param string|null $value + * @return Response + */ + public function withAccessControlRequestMethod(?string $value): static + { + return $this->withHeader('Access-Control-Request-Method', $value); + } + + + /** + * @param string|null $value + * @return Response + */ + public function withAccessControlAllowOrigin(?string $value): static + { + return $this->withHeader('Access-Control-Allow-Origin', $value); + } + + + /** + * @param $data + * @return static + * @throws Exception + */ + public function json($data): static + { + $this->stream->write(json_encode($data)); + + return $this->withContentType(self::CONTENT_TYPE_JSON); + } + + + /** + * @param $data + * @return static + * @throws Exception + */ + public function html($data): static + { + if (!is_string($data)) { + $data = json_encode($data); + } + + $this->stream->write((string)$data); + + return $this->withContentType(self::CONTENT_TYPE_HTML); + } + + + /** + * @param $data + * @return static + * @throws Exception + */ + public function xml($data): static + { + + $this->stream->write(Help::toXml($data)); + + return $this->withContentType(self::CONTENT_TYPE_XML); + } + + + /** + * @param $path + * @param bool $isChunk + * @param int $size + * @param int $offset + * @return DownloadInterface + * @throws Exception + */ + public function file($path, bool $isChunk = false, int $size = -1, int $offset = 0): DownloadInterface + { + $path = realpath($path); + if (!file_exists($path) || !is_readable($path)) { + throw new Exception('Cannot read file "' . $path . '", no permission'); + } + return (new Download())->path($path, $isChunk, $size, $offset); + } } diff --git a/http-server/Message/StatusCode.php b/http-message/StatusCode.php similarity index 99% rename from http-server/Message/StatusCode.php rename to http-message/StatusCode.php index ea42d137..cef3f808 100644 --- a/http-server/Message/StatusCode.php +++ b/http-message/StatusCode.php @@ -1,6 +1,6 @@ headers = $headers; - return $class; - } - - - /** - * @param $name - * @param null $default - * @return string|array|float|int|null - */ - public function get($name, $default = null): string|array|float|int|null - { - return $this->headers[$name] ?? $default; - } - - - /** - * @return array - */ - public function toArray(): array - { - return $this->headers; - } - - -} diff --git a/http-server/Message/Message.php b/http-server/Message/Message.php deleted file mode 100644 index e2bf69d4..00000000 --- a/http-server/Message/Message.php +++ /dev/null @@ -1,295 +0,0 @@ -servers; - } - - - /** - * @return string - */ - public function getProtocolVersion(): string - { - return $this->version; - } - - - /** - * @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 static - */ - public function withCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): static - { - $this->cookies[$name] = [$value, $expires, $path, $domain, $secure, $httponly, $samesite, $priority]; - return $this; - } - - - /** - * @return array - */ - public function getCookie(): array - { - return $this->cookies; - } - - - /** - * @return string|null - */ - #[Pure] public function getAccessControlAllowOrigin(): ?string - { - return $this->getHeaderLine('Access-Control-Allow-Origin'); - } - - - /** - * @return string|null - */ - #[Pure] public function getAccessControlAllowHeaders(): ?string - { - return $this->getHeaderLine('Access-Control-Allow-Headers'); - } - - - /** - * @return string|null - */ - #[Pure] public function getAccessControlRequestMethod(): ?string - { - return $this->getHeaderLine('Access-Control-Request-Method'); - } - - - /** - * @param $version - * @return static - */ - public function withProtocolVersion($version): static - { - $this->version = $version; - return $this; - } - - /** - * @return array - */ - public function getHeaders(): array - { - return $this->headers; - } - - - /** - * @return array - */ - public function getCookies(): array - { - return $this->cookies; - } - - /** - * @param $name - * @return bool - */ - public function hasHeader($name): bool - { - return array_key_exists($name, $this->headers); - } - - - /** - * @param $name - * @return string|array|null - */ - #[Pure] public function getHeader($name): string|null|array - { - if (!$this->hasHeader($name)) { - return null; - } - return $this->headers[$name]; - } - - - /** - * @param \Swoole\Http\Request $request - * @return static - */ - private function parseRequestHeaders(\Swoole\Http\Request $request): static - { - $index = strpos($request->getData(), "\r\n\r\n"); - $headers = explode("\r\n", substr($request->getData(), 0, $index)); - array_shift($headers); - foreach ($headers as $header) { - [$key, $value] = explode(': ', $header); - $this->addRequestHeader($key, $value); - } - return $this; - } - - - /** - * @param $key - * @param $value - */ - private function addRequestHeader($key, $value) - { - $this->headers[$key] = [$value]; - } - - - /** - * @param $name - * @return string|null - */ - #[Pure] public function getHeaderLine($name): string|null - { - if ($this->hasHeader($name)) { - return implode(';', $this->headers[$name]); - } - return null; - } - - - /** - * @param $name - * @param $value - * @return static - */ - public function withHeader($name, $value): static - { - if (!is_array($value)) { - $value = [$value]; - } - $this->headers[$name] = $value; - return $this; - } - - - /** - * @param $name - * @param $value - * @return static - * @throws - */ - public function withAddedHeader($name, $value): static - { - if (!array_key_exists($name, $this->headers)) { - throw new \Exception('Headers `' . $name . '` not exists.'); - } - $this->headers[$name][] = $value; - return $this; - } - - - /** - * @param $name - * @return static - */ - public function withoutHeader($name): static - { - unset($this->headers[$name]); - return $this; - } - - - /** - * @return string - */ - #[Pure] public function getBody(): string - { - return $this->stream->getContents(); - } - - - /** - * @param StreamInterface $body - * @return static - */ - public function withBody(StreamInterface $body): static - { - $this->stream = $body; - return $this; - } - - - /** - * @param StreamInterface $stream - * @return mixed - */ - public function parseBody(StreamInterface $stream): mixed - { - $content = $stream->getContents(); - if (empty($content)) { - return $content; - } - $contentType = $this->getHeaderLine('content-type'); - if (str_contains($contentType, 'json')) { - return json_encode($contentType); - } - if (str_contains($contentType, 'xml')) { - return Xml::toArray($contentType); - } - if (str_contains($contentType, 'x-www-form-urlencoded')) { - parse_str($content, $array); - return $array; - } - if (str_contains($contentType, 'serialize')) { - return unserialize($content); - } - return $content; - } - - - /** - * @param $host - * @return static - */ - public function redirectTo($host): static - { - return $this->withHeader('Location', $host) - ->withStatus(302); - } -} diff --git a/http-server/Message/Request.php b/http-server/Message/Request.php deleted file mode 100644 index 06fb0c48..00000000 --- a/http-server/Message/Request.php +++ /dev/null @@ -1,381 +0,0 @@ -serverRequest->fd; - } - - - /** - * @param AuthIdentity $authority - */ - public function setAuthority(AuthIdentity $authority): void - { - $this->authority = $authority; - } - - - /** - * @param \Swoole\Http\Request $request - * @return RequestInterface - */ - public static function parseRequest(\Swoole\Http\Request $request): RequestInterface - { - $message = new Request(); - $message->uri = Uri::parseUri($request); - $message->method = $request->getMethod(); - $message->requestTarget = ''; - $message->serverRequest = $request; - $message->version = $request->server['server_protocol']; - $message->stream = new Stream($request->getContent()); - $message->servers = $request->server; - $message->parseRequestHeaders($request); - return $message; - } - - - /** - * @return float - */ - #[Pure] public function getStartTime(): float - { - return $this->servers['request_time_float']; - } - - - /** - * @param $name - * @param null $default - * @return mixed - */ - public function input($name, $default = null): mixed - { - if (empty($this->parseBody)) { - $this->parseBody = $this->parseBody($this->stream); - } - if (!is_array($this->parseBody)) { - return $default; - } - return $this->parseBody[$name] ?? $default; - } - - - /** - * @param $name - * @param null $default - * @return mixed - */ - public function post($name, $default = null): mixed - { - return $this->serverRequest->post[$name] ?? $default; - } - - - /** - * @param string $field - * @param int $max - * @return int - */ - public function size(string $field = 'size', int $max = 100): int - { - $size = (int)$this->query($field); - if ($size < 1) { - $size = 1; - } else if ($size > $max) { - $size = $max; - } - return $size; - } - - - /** - * @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 = (int)$this->query($field); - if ($page < 1) { - $page = 1; - } - return ($page - 1) * $this->size($sizeField, $max); - } - - - /** - * @param string $name - * @param bool $required - * @return string|null - * @throws Exception - */ - public function string(string $name, bool $required = false): ?string - { - if (is_null($data = $this->post($name))) { - if ($required) { - throw new Exception('Parameter is required and cannot be empty.'); - } - return null; - } - return (string)$data; - } - - - /** - * @param string $name - * @param bool $required - * @return int|null - * @throws Exception - */ - public function int(string $name, bool $required = false): ?int - { - if (is_null($data = $this->post($name))) { - if ($required) { - throw new Exception('Parameter is required and cannot be empty.'); - } - return null; - } - return (string)$data; - } - - - /** - * @param string $name - * @param bool $required - * @return float|null - * @throws Exception - */ - public function float(string $name, bool $required = false): ?float - { - if (is_null($data = $this->post($name))) { - if ($required) { - throw new Exception('Parameter is required and cannot be empty.'); - } - return null; - } - return (float)$data; - } - - - /** - * @param string $name - * @param array $default - * @return mixed - */ - public function array(string $name, array $default = []): array - { - $data = $this->post($name); - if (!is_array($data)) { - return $default; - } - return $data; - } - - - /** - * @return array|null - */ - public function gets(): ?array - { - return $this->serverRequest->get; - } - - - /** - * @param $name - * @param null $default - * @return mixed - */ - public function query($name, $default = null): mixed - { - return $this->serverRequest->get[$name] ?? $default; - } - - - /** - * @param $name - * @return Uploaded|null - */ - public function file($name): ?Uploaded - { - if (isset($this->serverRequest->files[$name])) { - return new Uploaded($this->serverRequest->files[$name]); - } - return null; - } - - - /** - * @return array - */ - public function all(): array - { - if (empty($this->parseBody)) { - $this->parseBody = $this->parseBody($this->stream); - } - return array_merge($this->serverRequest->post ?? [], - $this->serverRequest->get ?? [], - is_array($this->parseBody) ? $this->parseBody : [] - ); - } - - - /** - * @return string - */ - public function getRequestTarget(): string - { - throw new BadMethodCallException('Not Accomplish Method.'); - } - - - /** - * @param mixed $requestTarget - * @return RequestInterface - */ - public function withRequestTarget($requestTarget): RequestInterface - { - $this->requestTarget = $requestTarget; - return $this; - } - - - /** - * @return string - */ - public function getMethod(): string - { - return $this->method; - } - - - /** - * @param string $method - * @return bool - */ - public function isMethod(string $method): bool - { - return $this->method === $method; - } - - - /** - * @param string $method - * @return RequestInterface - */ - public function withMethod($method): RequestInterface - { - $class = clone $this; - $class->method = $method; - return $class; - } - - - /** - * @return Uri|UriInterface - */ - public function getUri(): Uri|UriInterface - { - return $this->uri; - } - - - /** - * @param UriInterface $uri - * @param false $preserveHost - * @return RequestInterface - */ - public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface - { - $class = clone $this; - $class->uri = $uri; - return $class; - } - - - /** - * @param string $name - * @param bool $required - * @return string|null - * @throws Exception - */ - public function date(string $name, bool $required = false): ?string - { - $param = $this->post($name, null); - if (empty($param)) { - if ($required) { - throw new Exception('Required ' . $name . ' is must.'); - } - return $param; - } - return $param; - } - - - /** - * @param string $name - * @param bool $required - * @return int|null - * @throws Exception - */ - public function timestamp(string $name, bool $required = false): ?int - { - $param = $this->post($name, null); - if (empty($param)) { - if ($required) { - throw new Exception('Required ' . $name . ' is must.'); - } - return $param; - } - return (int)$param; - } -} diff --git a/http-server/Message/Response.php b/http-server/Message/Response.php deleted file mode 100644 index 04a2619d..00000000 --- a/http-server/Message/Response.php +++ /dev/null @@ -1,208 +0,0 @@ -stream = new Stream(''); - } - - - /** - * @param string $type - * @return $this - * @throws Exception - */ - public function withContentType(string $type): static - { - return $this->withHeader('Content-Type', $type); - } - - - /** - * @return bool - */ - #[Pure] public function hasContentType(): bool - { - return $this->hasHeader('Content-Type'); - } - - - /** - * @return string - */ - #[Pure] public function getContentType(): string - { - return $this->getHeaderLine('Content-Type'); - } - - - /** - * @return int - */ - public function getStatusCode(): int - { - // TODO: Implement getStatusCode() method. - return $this->statusCode; - } - - /** - * @param int $code - * @param string $reasonPhrase - * @return static - */ - public function withStatus($code, $reasonPhrase = ''): static - { - $this->statusCode = $code; - $this->reasonPhrase = $reasonPhrase; - return $this; - } - - - /** - * @return string - */ - public function getReasonPhrase(): string - { - // TODO: Implement getReasonPhrase() method. - return $this->reasonPhrase; - } - - - /** - * @param string|null $value - * @return Response - */ - public function withAccessControlAllowHeaders(?string $value): static - { - return $this->withHeader('Access-Control-Allow-Headers', $value); - } - - - /** - * @param string|null $value - * @return Response - */ - public function withAccessControlRequestMethod(?string $value): static - { - return $this->withHeader('Access-Control-Request-Method', $value); - } - - - /** - * @param string|null $value - * @return Response - */ - public function withAccessControlAllowOrigin(?string $value): static - { - return $this->withHeader('Access-Control-Allow-Origin', $value); - } - - - /** - * @param $data - * @return \Server\ResponseInterface - * @throws Exception - */ - public function json($data): \Server\ResponseInterface - { - if (!is_array($data = $this->_toArray($data))) { - throw new Exception('Json data format error.'); - } - - $this->stream->write(json_encode($this->_toArray($data))); - - return $this->withContentType(self::CONTENT_TYPE_JSON); - } - - - /** - * @param $data - * @return \Server\ResponseInterface - * @throws Exception - */ - public function html($data): \Server\ResponseInterface - { - $this->stream->write((string)$this->_toArray($data)); - - return $this->withContentType(self::CONTENT_TYPE_HTML); - } - - - /** - * @param $data - * @return \Server\ResponseInterface - * @throws Exception - */ - public function xml($data): \Server\ResponseInterface - { - if (!is_array($data = $this->_toArray($data))) { - throw new Exception('Xml data format error.'); - } - - $this->stream->write(Help::toXml($data)); - - return $this->withContentType(self::CONTENT_TYPE_XML); - } - - - /** - * @param $path - * @param bool $isChunk - * @param int $size - * @param int $offset - * @return DownloadInterface - * @throws Exception - */ - public function file($path, bool $isChunk = false, int $size = -1, int $offset = 0): DownloadInterface - { - $path = realpath($path); - if (!file_exists($path) || !is_readable($path)) { - throw new Exception('Cannot read file "' . $path . '", no permission'); - } - return (new Download())->path($path, $isChunk, $size, $offset); - } - - - /** - * @param $responseData - * @return string|array|bool|int|null - */ - public function _toArray($responseData): string|array|null|bool|int - { - if (is_object($responseData)) { - $responseData = $responseData instanceof ToArray ? $responseData->toArray() : get_object_vars($responseData); - } - return $responseData; - } - -} diff --git a/http-server/Message/Stream.php b/http-server/Message/Stream.php deleted file mode 100644 index c1ed7288..00000000 --- a/http-server/Message/Stream.php +++ /dev/null @@ -1,193 +0,0 @@ -body = $body; - } - - - /** - * @return string - */ - public function __toString(): string - { - return $this->body; - } - - - /** - * - */ - public function close(): void - { - $this->detach(); - } - - - /** - * - */ - public function detach(): void - { - $this->body = ''; - $this->size = 0; - $this->writable = false; - } - - - /** - * @return int - */ - public function getSize(): int - { - if ($this->size == 0) { - $this->size = strlen($this->body); - } - return $this->size; - } - - - /** - * @return int - */ - public function tell(): int - { - return $this->offset; - } - - - /** - * @return bool - */ - public function eof(): bool - { - return true; - } - - /** - * @return bool - */ - public function isSeekable(): bool - { - return $this->offset == 0; - } - - - /** - * @param int $offset - * @param int $whence - */ - public function seek($offset, $whence = SEEK_SET) - { - $this->offset = $offset; - } - - - /** - * - */ - public function rewind(): void - { - $this->offset = 0; - } - - /** - * @return bool - */ - public function isWritable(): bool - { - return $this->writable; - } - - - /** - * @param string $string - * @return int - */ - public function write($string): int - { - $this->body = $string; - $this->size = strlen($this->body); - return $this->size; - } - - - /** - * @param string $string - * @return int - */ - public function append(string $string): int - { - $this->body .= $string; - $this->size = strlen($this->body); - return $this->size; - } - - - /** - * @return bool - */ - public function isReadable(): bool - { - return true; - } - - - /** - * @param int $length - * @return string - */ - public function read($length = -1): string - { - if ($length > 0) { - return substr($this->body, 0, $length); - } - return $this->body; - } - - - /** - * @return string - */ - public function getContents(): string - { - return $this->body; - } - - - /** - * @param null $key - * @return mixed - */ - public function getMetadata($key = null): mixed - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } -} diff --git a/http-server/Message/Uploaded.php b/http-server/Message/Uploaded.php deleted file mode 100644 index 0e7ef0ad..00000000 --- a/http-server/Message/Uploaded.php +++ /dev/null @@ -1,106 +0,0 @@ - "There is no error, the file uploaded with success", - 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini", - 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", - 3 => "The uploaded file was only partially uploaded", - 4 => "No file was uploaded", - 6 => "Missing a temporary folder" - ]; - - - /** - * @param mixed $file - */ - public function __construct(array $file) - { - $this->tmp_name = $file['tmp_name']; - $this->name = $file['name']; - $this->type = $file['type']; - $this->size = $file['size']; - $this->error = $file['error']; - } - - - /** - * @return StreamInterface - */ - public function getStream(): StreamInterface - { - return new Stream(file_get_contents($this->tmp_name)); - } - - - /** - * @param string $targetPath - * @return bool - */ - public function moveTo($targetPath): bool - { - @move_uploaded_file($this->tmp_name, $targetPath); - - return file_exists($targetPath); - } - - /** - * @return int - */ - public function getSize(): int - { - return $this->size; - } - - /** - * @return string - */ - public function getError(): string - { - return Uploaded::ERROR[$this->error] ?? 'unknown error'; - } - - - /** - * @return string|null - */ - public function getClientFilename(): string|null - { - return $this->name; - } - - - /** - * @return string|null - */ - public function getClientMediaType(): string|null - { - return $this->type; - } -} diff --git a/http-server/Message/Uri.php b/http-server/Message/Uri.php deleted file mode 100644 index 1b87868e..00000000 --- a/http-server/Message/Uri.php +++ /dev/null @@ -1,287 +0,0 @@ -path == '/' || $this->path == '') { - return ['/']; - } - if (empty($this->_explode)) { - $this->_explode = array_filter(explode('/', $this->path)); - } - return $this->_explode; - } - - - /** - * @return string - */ - public function getScheme(): string - { - return $this->scheme; - } - - - /** - * @return mixed - */ - public function getAuthority(): string - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } - - - - - public function getUserInfo() - { - // TODO: Implement getUserInfo() method. - } - - - /** - * @return string - */ - public function getHost(): string - { - return $this->host; - } - - - /** - * @return int - */ - public function getPort(): int - { - return $this->port; - } - - - /** - * @return string - */ - public function getPath(): string - { - return $this->path; - } - - /** - * @return string - */ - public function getQuery(): string - { - return $this->query; - } - - - /** - * @return string - */ - public function getFragment(): string - { - return $this->fragment; - } - - - /** - * @param string $scheme - * @return UriInterface - */ - public function withScheme($scheme): UriInterface - { - $this->scheme = $scheme; - return $this; - } - - /** - * @param string $user - * @param null $password - * @return $this - */ - public function withUserInfo($user, $password = null): UriInterface - { - $this->username = $user; - $this->password = $password; - return $this; - } - - - /** - * @param string $host - * @return UriInterface - */ - public function withHost($host): UriInterface - { - $this->host = $host; - return $this; - } - - - /** - * @return int - */ - public function getDefaultPort(): int - { - return $this->scheme == 'https' ? 443 : 80; - } - - - /** - * @param int|null $port - * @return UriInterface - */ - public function withPort($port): UriInterface - { - $this->port = $port; - return $this; - } - - - /** - * @param string $path - * @return UriInterface - */ - public function withPath($path): UriInterface - { - $this->path = $path; - return $this; - } - - - /** - * @param string $query - * @return UriInterface - */ - public function withQuery($query): UriInterface - { - $this->query = $query; - return $this; - } - - - /** - * @param string $fragment - * @return UriInterface - */ - public function withFragment($fragment): UriInterface - { - $this->fragment = $fragment; - return $this; - } - - - /** - * @return string - */ - public function __toString(): string - { - $domain = sprintf('%s://%s', $this->scheme, $this->host); - if (!in_array($this->port, [80, 443])) { - $domain .= ':' . $this->port; - } - if (empty($this->query) && empty($this->fragment)) { - return $domain . $this->path; - } - return sprintf('%s?%s#%s', $domain . $this->path, - $this->query, $this->fragment); - } - - - /** - * @param \Swoole\Http\Request $request - * @return UriInterface - */ - public static function parseUri(\Swoole\Http\Request $request): UriInterface - { - $server = $request->server; - $header = $request->header; - $uri = new Uri(); - $uri = $uri->withScheme(!empty($server['https']) && $server['https'] !== 'off' ? 'https' : 'http'); - if (isset($request->header['x-forwarded-proto'])) { - $uri->withScheme($request->header['x-forwarded-proto'])->withPort(443); - } - - $hasPort = false; - if (isset($server['http_host'])) { - $hostHeaderParts = explode(':', $server['http_host']); - $uri = $uri->withHost($hostHeaderParts[0]); - if (isset($hostHeaderParts[1])) { - $hasPort = true; - $uri = $uri->withPort($hostHeaderParts[1]); - } - } elseif (isset($server['server_name'])) { - $uri = $uri->withHost($server['server_name']); - } elseif (isset($server['server_addr'])) { - $uri = $uri->withHost($server['server_addr']); - } elseif (isset($header['host'])) { - $hasPort = true; - if (strpos($header['host'], ':')) { - [$host, $port] = explode(':', $header['host'], 2); - if ($port != $uri->getDefaultPort()) { - $uri = $uri->withPort($port); - } - } else { - $host = $header['host']; - } - - $uri = $uri->withHost($host); - } - - if (!$hasPort && isset($server['server_port'])) { - $uri = $uri->withPort($server['server_port']); - } - - $hasQuery = false; - if (isset($server['request_uri'])) { - $requestUriParts = explode('?', $server['request_uri']); - $uri = $uri->withPath($requestUriParts[0]); - if (isset($requestUriParts[1])) { - $hasQuery = true; - $uri = $uri->withQuery($requestUriParts[1]); - } - } - - if (!$hasQuery && isset($server['query_string'])) { - $uri = $uri->withQuery($server['query_string']); - } - - return $uri; - } -} diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index 542cc15e..5eb4892a 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -10,8 +10,8 @@ use Kiri\Core\Help; use Server\Constant; use Server\Events\OnAfterRequest; use Server\Message\Response as MsgResponse; -use Server\RequestInterface; -use Server\ResponseInterface; +use Server\Constrict\RequestInterface; +use Server\Constrict\ResponseInterface; use Server\SInterface\OnClose; use Server\SInterface\OnConnect; use Swoole\Error; diff --git a/kiri-gii/GiiMiddleware.php b/kiri-gii/GiiMiddleware.php index bd65a57e..9080aef3 100644 --- a/kiri-gii/GiiMiddleware.php +++ b/kiri-gii/GiiMiddleware.php @@ -34,7 +34,7 @@ namespace App\Http\Middleware; use Closure; use Http\IInterface\MiddlewareInterface; -use Server\RequestInterface; +use Server\Constrict\RequestInterface; ';