改名
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -8,7 +8,6 @@ namespace Kiri\Jwt;
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Http\Route\MiddlewareAbstracts;
|
||||
use Server\RequestInterface;
|
||||
use Kiri\Kiri;
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Http\IInterface;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Server\RequestInterface;
|
||||
use Server\Constrict\RequestInterface;
|
||||
|
||||
/**
|
||||
* Interface IMiddleware
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace Http\Route;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Server\RequestInterface;
|
||||
use Server\Constrict\RequestInterface;
|
||||
|
||||
/**
|
||||
* Class CoreMiddleware
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
namespace Protocol\Message;
|
||||
|
||||
use Server\SInterface\DownloadInterface;
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
|
||||
namespace Protocol\Message;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Core\Help;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Server\SInterface\DownloadInterface;
|
||||
|
||||
|
||||
class Response implements ResponseInterface
|
||||
@@ -13,6 +16,12 @@ class Response implements ResponseInterface
|
||||
use Message;
|
||||
|
||||
|
||||
const CONTENT_TYPE_JSON = 'application/json;charset=utf-8';
|
||||
const CONTENT_TYPE_HTML = 'text/html;charset=utf-8';
|
||||
const CONTENT_TYPE_STREAM = 'octet-stream';
|
||||
const CONTENT_TYPE_XML = 'application/xml;charset=utf-8';
|
||||
|
||||
|
||||
protected int $statusCode = 200;
|
||||
|
||||
|
||||
@@ -75,4 +84,123 @@ class Response implements ResponseInterface
|
||||
{
|
||||
return $this->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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
namespace Protocol\Message;
|
||||
|
||||
class StatusCode
|
||||
{
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Server\ResponseInterface;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
|
||||
interface Emitter
|
||||
{
|
||||
|
||||
@@ -12,8 +12,6 @@ use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Server\Message\Response;
|
||||
use Server\RequestInterface;
|
||||
use Server\ResponseInterface;
|
||||
|
||||
|
||||
class Request implements RequestInterface
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
namespace Server\Constrict;
|
||||
|
||||
|
||||
use Http\IInterface\AuthIdentity;
|
||||
@@ -10,8 +10,6 @@ use Kiri\Kiri;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Server\Message\Request as RequestMessage;
|
||||
use Server\Message\Response as Psr7Response;
|
||||
use Server\RequestInterface;
|
||||
use Server\ResponseInterface;
|
||||
use Server\ServerManager;
|
||||
use Server\SInterface\DownloadInterface;
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Server\ResponseInterface;
|
||||
use Server\RequestInterface;
|
||||
use Server\SInterface\DownloadInterface;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
namespace Server\Constrict;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
@@ -4,7 +4,7 @@ namespace Server\Constrict;
|
||||
|
||||
use Exception;
|
||||
use Http\Context\Formatter\FileFormatter;
|
||||
use Server\ResponseInterface;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Server\ResponseInterface;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Server\Constrict;
|
||||
use Exception;
|
||||
use Kiri\Kiri;
|
||||
use Server\ServerManager;
|
||||
use Server\ResponseInterface;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
|
||||
class Headers
|
||||
{
|
||||
|
||||
|
||||
private array $headers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
* @return $this
|
||||
*/
|
||||
public function withHeader(array $headers): static
|
||||
{
|
||||
$class = clone $this;
|
||||
$class->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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,295 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Core\Xml;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Server\RequestInterface;
|
||||
use Server\ResponseInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
trait Message
|
||||
{
|
||||
|
||||
public string $version;
|
||||
|
||||
|
||||
public StreamInterface $stream;
|
||||
|
||||
|
||||
public array $headers = [];
|
||||
|
||||
|
||||
public array $servers = [];
|
||||
|
||||
|
||||
public array $cookies = [];
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getServers(): array
|
||||
{
|
||||
return $this->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);
|
||||
}
|
||||
}
|
||||
@@ -1,381 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Exception;
|
||||
use Http\IInterface\AuthIdentity;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Server\RequestInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Request implements RequestInterface
|
||||
{
|
||||
|
||||
use Message;
|
||||
|
||||
|
||||
public string $requestTarget;
|
||||
|
||||
|
||||
public string $method;
|
||||
|
||||
|
||||
/**
|
||||
* @var Uri|UriInterface
|
||||
*/
|
||||
private Uri|UriInterface $uri;
|
||||
|
||||
|
||||
private \Swoole\Http\Request $serverRequest;
|
||||
|
||||
|
||||
private array $parseBody;
|
||||
|
||||
private array $files = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var AuthIdentity|null
|
||||
*/
|
||||
public ?AuthIdentity $authority = null;
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getClientId(): int
|
||||
{
|
||||
return $this->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;
|
||||
}
|
||||
}
|
||||
@@ -1,208 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Core\Help;
|
||||
use Kiri\ToArray;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Server\SInterface\DownloadInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Response implements ResponseInterface, \Server\ResponseInterface
|
||||
{
|
||||
use Message;
|
||||
|
||||
public int $statusCode = 200;
|
||||
|
||||
|
||||
public string $reasonPhrase = '';
|
||||
|
||||
const CONTENT_TYPE_JSON = 'application/json;charset=utf-8';
|
||||
const CONTENT_TYPE_HTML = 'text/html;charset=utf-8';
|
||||
const CONTENT_TYPE_STREAM = 'octet-stream';
|
||||
const CONTENT_TYPE_XML = 'application/xml;charset=utf-8';
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
#[Pure] public function __construct()
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Stream implements StreamInterface
|
||||
{
|
||||
|
||||
|
||||
private string $body;
|
||||
|
||||
|
||||
private int $size;
|
||||
|
||||
|
||||
private int $offset = 0;
|
||||
|
||||
|
||||
private bool $writable = true;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $body
|
||||
*/
|
||||
public function __construct(string $body)
|
||||
{
|
||||
$this->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.');
|
||||
}
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Uploaded implements UploadedFileInterface
|
||||
{
|
||||
|
||||
public string $tmp_name;
|
||||
|
||||
|
||||
public string $name;
|
||||
|
||||
|
||||
public string $type;
|
||||
|
||||
|
||||
public string $size;
|
||||
|
||||
|
||||
public int $error;
|
||||
|
||||
const ERROR = [
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,287 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Message;
|
||||
|
||||
use Psr\Http\Message\UriInterface;
|
||||
|
||||
|
||||
class Uri implements UriInterface
|
||||
{
|
||||
|
||||
|
||||
public string $scheme = '';
|
||||
|
||||
|
||||
public string $host = '';
|
||||
|
||||
public int $port = 80;
|
||||
|
||||
|
||||
public string $path = '';
|
||||
|
||||
|
||||
public string $query = '';
|
||||
|
||||
|
||||
public string $fragment = '';
|
||||
|
||||
|
||||
public string $username = '';
|
||||
|
||||
|
||||
public string $password = '';
|
||||
|
||||
|
||||
private array $_explode = [];
|
||||
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getExplode(): array
|
||||
{
|
||||
if ($this->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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Http\IInterface\MiddlewareInterface;
|
||||
use Server\RequestInterface;
|
||||
use Server\Constrict\RequestInterface;
|
||||
|
||||
';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user