This commit is contained in:
2021-08-27 16:49:17 +08:00
parent 5f199ccb19
commit 98b7048db7
6 changed files with 955 additions and 0 deletions
+182
View File
@@ -0,0 +1,182 @@
<?php
namespace Server\Message;
use JetBrains\PhpStorm\Pure;
use Kiri\Core\Xml;
use Psr\Http\Message\StreamInterface;
/**
*
*/
trait Message
{
public string $version;
public StreamInterface $stream;
public array $headers = [];
/**
* @return string
*/
public function getProtocolVersion(): string
{
return $this->version;
}
/**
* @param $version
* @return $this
*/
public function withProtocolVersion($version): static
{
$class = clone $this;
$class->version = $version;
return $class;
}
/**
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* @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 $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
{
$class = clone $this;
if (!is_array($value)) {
$value = [$value];
}
$class->headers[$name] = $value;
return $class;
}
/**
* @param $name
* @param $value
* @return static
* @throws
*/
public function withAddedHeader($name, $value): static
{
$class = clone $this;
if (!array_key_exists($name, $class->headers)) {
throw new \Exception('Headers `' . $name . '` not exists.');
}
$class->headers[$name][] = $value;
return $class;
}
/**
* @param $name
* @return $this
*/
public function withoutHeader($name): static
{
$class = clone $this;
unset($class->headers[$name]);
return $class;
}
/**
* @return string
*/
public function getBody(): string
{
return $this->stream;
}
/**
* @param StreamInterface $body
* @return static
*/
public function withBody(StreamInterface $body): static
{
$class = clone $this;
$class->stream = $body;
return $class;
}
/**
* @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;
}
}
+177
View File
@@ -0,0 +1,177 @@
<?php
namespace Server\Message;
use BadMethodCallException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
/**
*
*/
class Request implements RequestInterface
{
use Message;
public string $requestTarget;
public string $method;
public UriInterface $uri;
private \Swoole\Http\Request $serverRequest;
private array $parseBody;
private array $files = [];
/**
* @return int
*/
public function getClientId(): int
{
return $this->serverRequest->fd;
}
/**
* @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->headers = $request->header;
return $message;
}
/**
* @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 $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 string
*/
public function getRequestTarget(): string
{
throw new BadMethodCallException('Not Accomplish Method.');
}
/**
* @param mixed $requestTarget
* @return RequestInterface
*/
public function withRequestTarget($requestTarget): RequestInterface
{
$class = clone $this;
$class->requestTarget = $requestTarget;
return $class;
}
/**
* @return string
*/
public function getMethod(): string
{
return $this->method;
}
/**
* @param string $method
* @return RequestInterface
*/
public function withMethod($method): RequestInterface
{
$class = clone $this;
$class->method = $method;
return $class;
}
/**
* @return UriInterface
*/
public function getUri(): 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;
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace Server\Message;
use Psr\Http\Message\ResponseInterface;
/**
*
*/
class Response implements ResponseInterface
{
use Message;
public int $statusCode = 200;
public string $reasonPhrase = '';
/**
* @return int
*/
public function getStatusCode(): int
{
// TODO: Implement getStatusCode() method.
return $this->statusCode;
}
/**
* @param int $code
* @param string $reasonPhrase
* @return ResponseInterface
*/
public function withStatus($code, $reasonPhrase = ''): ResponseInterface
{
// TODO: Implement withStatus() method.
$class = clone $this;
$class->statusCode = $code;
$class->reasonPhrase = $reasonPhrase;
return $class;
}
/**
* @return string
*/
public function getReasonPhrase(): string
{
// TODO: Implement getReasonPhrase() method.
return $this->reasonPhrase;
}
}
+180
View File
@@ -0,0 +1,180 @@
<?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
{
// TODO: Implement write() method.
$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): string
{
// TODO: Implement read() method.
return substr($this->body, 0, $length);
}
/**
* @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.');
}
}
+103
View File
@@ -0,0 +1,103 @@
<?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
*/
public function moveTo($targetPath)
{
@move_uploaded_file($this->tmp_name, $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;
}
}
+260
View File
@@ -0,0 +1,260 @@
<?php
namespace Server\Message;
use JetBrains\PhpStorm\Pure;
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 = '';
/**
* @return string
*/
public function getScheme(): string
{
return $this->scheme;
}
public function getAuthority()
{
// TODO: Implement getAuthority() 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
{
$class = clone $this;
$class->scheme = $scheme;
return $class;
}
/**
* @param string $user
* @param null $password
* @return $this
*/
public function withUserInfo($user, $password = null): UriInterface
{
$class = clone $this;
$class->username = $user;
$class->password = $password;
return $class;
}
/**
* @param string $host
* @return UriInterface
*/
public function withHost($host): UriInterface
{
$class = clone $this;
$class->host = $host;
return $class;
}
/**
* @return int
*/
public function getDefaultPort(): int
{
return 80;
}
/**
* @param int|null $port
* @return UriInterface
*/
public function withPort($port): UriInterface
{
$class = clone $this;
$class->port = $port;
return $class;
}
/**
* @param string $path
* @return UriInterface
*/
public function withPath($path): UriInterface
{
$class = clone $this;
$class->path = $path;
return $class;
}
/**
* @param string $query
* @return UriInterface
*/
public function withQuery($query): UriInterface
{
$class = clone $this;
$class->query = $query;
return $class;
}
/**
* @param string $fragment
* @return UriInterface
*/
public function withFragment($fragment): UriInterface
{
$class = clone $this;
$class->fragment = $fragment;
return $class;
}
/**
* @return string
*/
public function __toString(): string
{
return sprintf('%s://%s:%d%s?%s#%s', $this->scheme, $this->host, $this->port,
$this->path, $this->query, $this->fragment);
}
/**
* @param \Swoole\Http\Request $request
* @return UriInterface
*/
#[Pure] 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');
$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;
}
}