111
This commit is contained in:
+238
-191
@@ -3,7 +3,7 @@
|
||||
namespace Protocol\Message;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
|
||||
@@ -13,236 +13,283 @@ use Psr\Http\Message\StreamInterface;
|
||||
trait Message
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $version;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $version;
|
||||
|
||||
|
||||
/**
|
||||
* @var StreamInterface
|
||||
*/
|
||||
protected StreamInterface $stream;
|
||||
/**
|
||||
* @var StreamInterface
|
||||
*/
|
||||
protected StreamInterface $stream;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $headers = [];
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $headers = [];
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
protected ?array $cookieParams = [];
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
protected ?array $cookieParams = [];
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProtocolVersion(): string
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProtocolVersion(): string
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $version
|
||||
* @return static
|
||||
*/
|
||||
public function withProtocolVersion($version): static
|
||||
{
|
||||
$this->version = $version;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @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 getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasHeader($name): bool
|
||||
{
|
||||
return array_key_exists($name, $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|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
|
||||
*/
|
||||
public 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;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function parse_curl_header(): array
|
||||
{
|
||||
$_headers = [];
|
||||
foreach ($this->headers as $key => $val) {
|
||||
$_headers[] = $key . ': ' . implode(';', $val);
|
||||
}
|
||||
return $_headers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
private function addRequestHeader($key, $value)
|
||||
{
|
||||
$this->headers[$key] = [$value];
|
||||
}
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function withData(string $headerString): static
|
||||
{
|
||||
[$headers, $body] = explode("\r\n\r\n", $headerString);
|
||||
|
||||
$this->stream = new Stream($body);
|
||||
|
||||
return $this->slip_headers($headers);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getHeaderLine($name): string|null
|
||||
{
|
||||
if ($this->hasHeader($name)) {
|
||||
return implode(';', $this->headers[$name]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param $headers
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function slip_headers($headers): static
|
||||
{
|
||||
$headers = explode("\r\n", $headers);
|
||||
|
||||
$this->resolve_status(array_shift($headers));
|
||||
|
||||
foreach ($headers as $header) {
|
||||
[$key, $value] = explode(': ', $header);
|
||||
$this->withHeader($key, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getContentType(): ?string
|
||||
{
|
||||
return $this->getHeaderLine('Content-Type');
|
||||
}
|
||||
/**
|
||||
* @param string $protocol
|
||||
*/
|
||||
private function resolve_status(string $protocol)
|
||||
{
|
||||
if ($this instanceof ResponseInterface) {
|
||||
[$sch, $status, $message] = explode(' ', $protocol);
|
||||
[$sch, $protocolVersion] = explode('/', $sch);
|
||||
$this->withProtocolVersion($protocolVersion)
|
||||
->withStatus(intval($status));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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 $key
|
||||
* @param $value
|
||||
*/
|
||||
private function addRequestHeader($key, $value)
|
||||
{
|
||||
$this->headers[$key] = [$value];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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 string|null
|
||||
*/
|
||||
#[Pure] public function getHeaderLine($name): string|null
|
||||
{
|
||||
if ($this->hasHeader($name)) {
|
||||
return implode(';', $this->headers[$name]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return static
|
||||
*/
|
||||
public function withoutHeader($name): static
|
||||
{
|
||||
unset($this->headers[$name]);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getContentType(): ?string
|
||||
{
|
||||
return $this->getHeaderLine('Content-Type');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return null|array
|
||||
*/
|
||||
public function getCookieParams(): ?array
|
||||
{
|
||||
return $this->cookieParams;
|
||||
}
|
||||
/**
|
||||
* @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 array|null $cookies
|
||||
* @return static
|
||||
*/
|
||||
public function withCookieParams(?array $cookies): static
|
||||
{
|
||||
$this->cookieParams = $cookies;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return StreamInterface
|
||||
*/
|
||||
public function getBody(): StreamInterface
|
||||
{
|
||||
return $this->stream;
|
||||
}
|
||||
/**
|
||||
* @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 StreamInterface $body
|
||||
* @return static
|
||||
*/
|
||||
public function withBody(StreamInterface $body): static
|
||||
{
|
||||
$this->stream = $body;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @return static
|
||||
*/
|
||||
public function withoutHeader($name): static
|
||||
{
|
||||
unset($this->headers[$name]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowOrigin(): ?string
|
||||
{
|
||||
return $this->getHeaderLine('Access-Control-Allow-Origin');
|
||||
}
|
||||
/**
|
||||
* @return null|array
|
||||
*/
|
||||
public function getCookieParams(): ?array
|
||||
{
|
||||
return $this->cookieParams;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlAllowHeaders(): ?string
|
||||
{
|
||||
return $this->getHeaderLine('Access-Control-Allow-Headers');
|
||||
}
|
||||
/**
|
||||
* @param array|null $cookies
|
||||
* @return static
|
||||
*/
|
||||
public function withCookieParams(?array $cookies): static
|
||||
{
|
||||
$this->cookieParams = $cookies;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return StreamInterface
|
||||
*/
|
||||
public function getBody(): StreamInterface
|
||||
{
|
||||
return $this->stream;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
#[Pure] public function getAccessControlRequestMethod(): ?string
|
||||
{
|
||||
return $this->getHeaderLine('Access-Control-Request-Method');
|
||||
}
|
||||
/**
|
||||
* @param StreamInterface $body
|
||||
* @return static
|
||||
*/
|
||||
public function withBody(StreamInterface $body): static
|
||||
{
|
||||
$this->stream = $body;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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');
|
||||
}
|
||||
|
||||
|
||||
protected function setStore($key, callable $callback)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user