改名
This commit is contained in:
@@ -16,74 +16,12 @@ namespace HttpServer\Http;
|
||||
class HttpHeaders
|
||||
{
|
||||
|
||||
|
||||
private array $_headers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
*/
|
||||
public function setRequestUri(string $uri)
|
||||
{
|
||||
$this->_headers['request_uri'] = $uri;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
*/
|
||||
public function setRequestMethod(string $method)
|
||||
{
|
||||
$this->_headers['request_method'] = $method;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function replace($name, $value)
|
||||
{
|
||||
$this->_headers[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function addHeader($name, $value)
|
||||
{
|
||||
$this->_headers[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
* @return $this
|
||||
*/
|
||||
public function addHeaders(array $headers): static
|
||||
{
|
||||
foreach ($headers as $key => $header) {
|
||||
$this->_headers[$key] = $header;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
*/
|
||||
public function setHeaders(array $headers)
|
||||
{
|
||||
$this->_headers = $headers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->_headers;
|
||||
return $this->__handler__();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,7 +30,7 @@ class HttpHeaders
|
||||
*/
|
||||
public function getHeader($name): ?string
|
||||
{
|
||||
return $this->_headers[$name] ?? null;
|
||||
return $this->__handler__($name);
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +41,7 @@ class HttpHeaders
|
||||
*/
|
||||
public function get($name, $default = null): mixed
|
||||
{
|
||||
return $this->_headers[$name] ?? $default;
|
||||
return $this->__handler__($name, $default);
|
||||
}
|
||||
|
||||
|
||||
@@ -112,7 +50,34 @@ class HttpHeaders
|
||||
*/
|
||||
public function getContentType(): string
|
||||
{
|
||||
return $this->getHeader('content-type');
|
||||
return $this->__handler__('content-type');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRequestUri(): ?string
|
||||
{
|
||||
return $this->__handler__('request_uri');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRequestMethod(): ?string
|
||||
{
|
||||
return $this->__handler__('request_method');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAgent(): mixed
|
||||
{
|
||||
return $this->__handler__('user-agent');
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +87,7 @@ class HttpHeaders
|
||||
*/
|
||||
public function exists($name): bool
|
||||
{
|
||||
return isset($this->_headers[$name]) && $this->_headers[$name] != null;
|
||||
return $this->__handler__($name) === null;
|
||||
}
|
||||
|
||||
|
||||
@@ -131,8 +96,23 @@ class HttpHeaders
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->_headers;
|
||||
return $this->__handler__();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $name
|
||||
* @param null $default
|
||||
* @return mixed
|
||||
*/
|
||||
private function __handler__($name = null, $default = null): mixed
|
||||
{
|
||||
/** @var \Swoole\Http\Request $context */
|
||||
$context = Context::getContext(\Swoole\Http\Request::class);
|
||||
if (!empty($name)) {
|
||||
return $context->header[$name] ?? $default;
|
||||
}
|
||||
return $context->header;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,18 +36,27 @@ class HttpParams
|
||||
/** @var array|null */
|
||||
private ?array $_files = [];
|
||||
|
||||
|
||||
/** @var mixed|array */
|
||||
private mixed $_rawContent = [];
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getRawContent(): mixed
|
||||
{
|
||||
return $this->_rawContent;
|
||||
if (Context::hasContext('rawContent')) {
|
||||
return Context::getContext('rawContent');
|
||||
}
|
||||
/** @var \Swoole\Http\Request $context */
|
||||
$content = Context::getContext(\Swoole\Http\Request::class);
|
||||
$context_type = request()->headers->getContentType();
|
||||
if (str_contains($context_type, 'json')) {
|
||||
return Context::setContext('rawContent', json_decode($content, true));
|
||||
} else if (str_contains($context_type, 'xml')) {
|
||||
return Context::setContext('rawContent', Xml::toArray($content));
|
||||
} else {
|
||||
return Context::setContext('rawContent', $content);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
@@ -57,74 +66,13 @@ class HttpParams
|
||||
return ($this->page() - 1) * $this->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* 批量添加数据
|
||||
*/
|
||||
public function setPosts(mixed $data): void
|
||||
{
|
||||
$this->_posts = $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
*/
|
||||
public function addPosts(mixed $data): void
|
||||
{
|
||||
if (is_object($data)) {
|
||||
$data = get_object_vars($data);
|
||||
}
|
||||
foreach ($data as $key => $datum) {
|
||||
$this->_posts[$key] = $datum;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array|null $files
|
||||
*/
|
||||
public function setFiles(?array $files): void
|
||||
{
|
||||
$this->_files = $files;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array|null $gets
|
||||
*/
|
||||
public function setGets(?array $gets): void
|
||||
{
|
||||
$this->_gets = $gets;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $content
|
||||
* @param Request $contextType
|
||||
*/
|
||||
public function setRawContent(mixed $content, Request $contextType)
|
||||
{
|
||||
if (empty($content)) {
|
||||
return;
|
||||
}
|
||||
$context_type = $contextType->headers->getContentType();
|
||||
if (str_contains($context_type, 'json')) {
|
||||
$this->addPosts(json_decode($content, true));
|
||||
} else if (str_contains($context_type, 'xml')) {
|
||||
$this->addPosts(Xml::toArray($content));
|
||||
} else {
|
||||
$this->_rawContent = $content;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getBody(): ?array
|
||||
{
|
||||
return $this->_posts;
|
||||
return $this->__posts__();
|
||||
}
|
||||
|
||||
|
||||
@@ -144,7 +92,7 @@ class HttpParams
|
||||
*/
|
||||
public function query($name, $default = null): mixed
|
||||
{
|
||||
return $this->_gets[$name] ?? $default;
|
||||
return $this->__gets__($name, $default);
|
||||
}
|
||||
|
||||
|
||||
@@ -160,27 +108,21 @@ class HttpParams
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $defaultValue
|
||||
* @param null $call
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $defaultValue = null, $call = null): mixed
|
||||
public function get($name, $defaultValue = null): mixed
|
||||
{
|
||||
return $this->_gets[$name] ?? $defaultValue;
|
||||
return $this->__gets__($name, $defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $defaultValue
|
||||
* @param null $call
|
||||
* @return mixed
|
||||
*/
|
||||
public function post($name, $defaultValue = null, $call = null): mixed
|
||||
public function post($name, $defaultValue = null): mixed
|
||||
{
|
||||
$data = $this->_posts[$name] ?? $defaultValue;
|
||||
if ($call !== null) {
|
||||
$data = call_user_func($call, $data);
|
||||
}
|
||||
return $data;
|
||||
return $this->__posts__($name, $defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,7 +146,7 @@ class HttpParams
|
||||
*/
|
||||
public function gets(): array
|
||||
{
|
||||
return $this->_gets;
|
||||
return $this->__gets__();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,7 +154,7 @@ class HttpParams
|
||||
*/
|
||||
public function params(): array
|
||||
{
|
||||
return array_merge($this->_posts ?? [], $this->_files ?? []);
|
||||
return array_merge($this->__posts__(), $this->__gets__());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,7 +162,7 @@ class HttpParams
|
||||
*/
|
||||
public function load(): array
|
||||
{
|
||||
return array_merge($this->_files ?? [], $this->_posts ?? [], $this->_gets ?? []);
|
||||
return array_merge($this->__posts__(), $this->__gets__(), $this->__files__());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,7 +172,7 @@ class HttpParams
|
||||
*/
|
||||
public function array($name, array $defaultValue = []): mixed
|
||||
{
|
||||
return $this->_posts[$name] ?? $defaultValue;
|
||||
return $this->__posts__($name, $defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,7 +183,7 @@ class HttpParams
|
||||
*/
|
||||
public function file($name): File|null
|
||||
{
|
||||
$param = $this->_files[$name] ?? null;
|
||||
$param = $this->__files__($name);
|
||||
if (!empty($param)) {
|
||||
$param['class'] = File::class;
|
||||
return Snowflake::createObject($param);
|
||||
@@ -257,7 +199,7 @@ class HttpParams
|
||||
*/
|
||||
private function required(string $name, bool $isNeed = false): mixed
|
||||
{
|
||||
$int = $this->_posts[$name] ?? NULL;
|
||||
$int = $this->__posts__($name);
|
||||
if (is_null($int) && $isNeed === true) {
|
||||
throw new RequestException("You need to add request parameter $name");
|
||||
}
|
||||
@@ -426,11 +368,58 @@ class HttpParams
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
#[Pure] public function __get($name): mixed
|
||||
public function __get($name): mixed
|
||||
{
|
||||
$load = $this->load();
|
||||
|
||||
return $load[$name] ?? null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $name
|
||||
* @param null $default
|
||||
* @return mixed
|
||||
*/
|
||||
private function __posts__($name = null, $default = null): mixed
|
||||
{
|
||||
/** @var \Swoole\Http\Request $content */
|
||||
$content = Context::getContext(\Swoole\Http\Request::class);
|
||||
if (!empty($name)) {
|
||||
return $content->post[$name] ?? $default;
|
||||
}
|
||||
return $content->post ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $name
|
||||
* @param null $default
|
||||
* @return mixed
|
||||
*/
|
||||
private function __gets__($name = null, $default = null): mixed
|
||||
{
|
||||
/** @var \Swoole\Http\Request $content */
|
||||
$content = Context::getContext(\Swoole\Http\Request::class);
|
||||
if (!empty($name)) {
|
||||
return $content->get[$name] ?? $default;
|
||||
}
|
||||
return $content->get ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $name
|
||||
* @return mixed
|
||||
*/
|
||||
private function __files__($name = null): mixed
|
||||
{
|
||||
/** @var \Swoole\Http\Request $content */
|
||||
$content = Context::getContext(\Swoole\Http\Request::class);
|
||||
if (!empty($name)) {
|
||||
return $content->files[$name] ?? null;
|
||||
}
|
||||
return $content->files ?? [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+35
-42
@@ -3,14 +3,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Http;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use HttpServer\IInterface\AuthIdentity;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Server\ServerManager;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
defined('REQUEST_OK') or define('REQUEST_OK', 0);
|
||||
@@ -37,9 +36,17 @@ class Request extends HttpService
|
||||
public int $fd = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @var HttpParams|null
|
||||
*/
|
||||
#[Inject(HttpHeaders::class)]
|
||||
public ?HttpParams $params = null;
|
||||
|
||||
|
||||
/**
|
||||
* @var HttpHeaders|null
|
||||
*/
|
||||
#[Inject(HttpHeaders::class)]
|
||||
public ?HttpHeaders $headers = null;
|
||||
|
||||
public bool $isCli = FALSE;
|
||||
@@ -64,14 +71,6 @@ class Request extends HttpService
|
||||
const PLATFORM_WINDOWS = 'windows';
|
||||
|
||||
|
||||
const HTTP_POST = 'post';
|
||||
const HTTP_GET = 'GET';
|
||||
const HTTP_CMD = 'rpc';
|
||||
const HTTP_LISTEN = 'listen';
|
||||
const HTTP_SOCKET = 'sw::socket';
|
||||
|
||||
|
||||
|
||||
private string $_platform = '';
|
||||
|
||||
|
||||
@@ -100,7 +99,9 @@ class Request extends HttpService
|
||||
}
|
||||
$server = ServerManager::getContext()->getServer();
|
||||
|
||||
return $server->getClientInfo($this->fd);
|
||||
$request = Context::getContext(\Swoole\Http\Request::class);
|
||||
|
||||
return $server->getClientInfo($request->fd);
|
||||
}
|
||||
|
||||
|
||||
@@ -109,16 +110,18 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getClientId(): int
|
||||
{
|
||||
return $this->fd;
|
||||
$request = Context::getContext(\Swoole\Http\Request::class);
|
||||
|
||||
return $request->fd;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function isFavicon(): bool
|
||||
public function isFavicon(): bool
|
||||
{
|
||||
return $this->_uri === 'favicon.ico';
|
||||
return $this->headers->getRequestUri() === 'favicon.ico';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,7 +137,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function isHead(): bool
|
||||
{
|
||||
$result = $this->_method == 'HEAD';
|
||||
$result = $this->headers->getRequestMethod() == 'HEAD';
|
||||
if ($result) {
|
||||
$this->setStatus(101);
|
||||
} else {
|
||||
@@ -165,7 +168,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getIsPackage(): bool
|
||||
{
|
||||
return $this->_method == 'package';
|
||||
return $this->headers->getRequestMethod() == 'package';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,7 +176,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getIsReceive(): bool
|
||||
{
|
||||
return $this->_method == 'receive';
|
||||
return $this->headers->getRequestMethod() == 'receive';
|
||||
}
|
||||
|
||||
|
||||
@@ -217,7 +220,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getUri(): string
|
||||
{
|
||||
return $this->_uri;
|
||||
return $this->headers->getRequestUri();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,7 +231,8 @@ class Request extends HttpService
|
||||
if (!empty($this->_platform)) {
|
||||
return $this->_platform;
|
||||
}
|
||||
$user = $this->headers->getHeader('user-agent');
|
||||
|
||||
$user = $this->headers->getAgent();
|
||||
$match = preg_match('/\(.*\)?/', $user, $output);
|
||||
if (!$match || count($output) < 1) {
|
||||
return $this->_platform = 'unknown';
|
||||
@@ -284,7 +288,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getIsPost(): bool
|
||||
{
|
||||
return $this->_method == 'POST';
|
||||
return $this->headers->getRequestMethod() == 'POST';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,7 +305,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getIsOption(): bool
|
||||
{
|
||||
return $this->_method == 'OPTIONS';
|
||||
return $this->headers->getRequestMethod() == 'OPTIONS';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -309,7 +313,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getIsGet(): bool
|
||||
{
|
||||
return $this->_method == 'GET';
|
||||
return $this->headers->getRequestMethod() == 'GET';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,7 +321,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getIsDelete(): bool
|
||||
{
|
||||
return $this->_method == 'DELETE';
|
||||
return $this->headers->getRequestMethod() == 'DELETE';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -327,7 +331,7 @@ class Request extends HttpService
|
||||
*/
|
||||
public function getMethod(): string
|
||||
{
|
||||
$method = $this->_method;
|
||||
$method = $this->headers->getRequestMethod();
|
||||
if (empty($method)) {
|
||||
return 'GET';
|
||||
}
|
||||
@@ -396,7 +400,7 @@ class Request extends HttpService
|
||||
'[Debug ' . $datetime . '] ',
|
||||
$this->getIp(),
|
||||
$this->getUri(),
|
||||
'`' . $this->headers->getHeader('user-agent') . '`',
|
||||
'`' . $this->headers->getAgent() . '`',
|
||||
$this->getRuntime()
|
||||
];
|
||||
return implode(' ', $tmp);
|
||||
@@ -407,9 +411,9 @@ class Request extends HttpService
|
||||
* @param $router
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function is($router): bool
|
||||
public function is($router): bool
|
||||
{
|
||||
return $this->_uri == $router;
|
||||
return $this->headers->getRequestUri() == $router;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -424,26 +428,15 @@ class Request extends HttpService
|
||||
/**
|
||||
* @param \Swoole\Http\Request $request
|
||||
* @return Request
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function create(\Swoole\Http\Request $request): Request
|
||||
{
|
||||
$httpRequest = new Request();
|
||||
$httpRequest->fd = $request->fd;
|
||||
$httpRequest->startTime = microtime(true);
|
||||
$request->header = array_merge($request->header, $request->server);
|
||||
|
||||
$httpRequest->headers = new HttpHeaders();
|
||||
$httpRequest->headers->setHeaders(array_merge($request->header, $request->server));
|
||||
Context::setContext(\Swoole\Http\Request::class, $request);
|
||||
|
||||
$httpRequest->_uri = $httpRequest->headers->get('request_uri');
|
||||
$httpRequest->_method = $httpRequest->headers->get('request_method');
|
||||
|
||||
$httpRequest->params = new HttpParams();
|
||||
$httpRequest->params->setPosts($request->post);
|
||||
$httpRequest->params->setFiles($request->files);
|
||||
$httpRequest->params->setGets($request->get);
|
||||
$httpRequest->params->setRawContent($request->rawContent(), $httpRequest);
|
||||
|
||||
return Context::setContext(Request::class, $httpRequest);
|
||||
return Snowflake::app()->get('request');
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -89,6 +89,7 @@ class Server extends HttpService
|
||||
* @param $rpcService
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function rpcListener($rpcService)
|
||||
{
|
||||
@@ -100,6 +101,19 @@ class Server extends HttpService
|
||||
$rpcService['events'][Constant::DISCONNECT] = [Service::class, 'onDisconnect'];
|
||||
$rpcService['events'][Constant::CLOSE] = [Service::class, 'onClose'];
|
||||
}
|
||||
$rpcService['settings']['enable_unsafe_event'] = true;
|
||||
$this->addRpcListener($rpcService);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $rpcService
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function addRpcListener($rpcService)
|
||||
{
|
||||
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -145,7 +145,7 @@ class Service extends \Server\Abstracts\Server
|
||||
|
||||
$sRequest->params->setPosts($data);
|
||||
$sRequest->headers->setRequestUri('rpc/p' . $server_port . '/' . ltrim($cmd, '/'));
|
||||
$sRequest->headers->setRequestMethod(Request::HTTP_CMD);
|
||||
$sRequest->headers->setRequestMethod('rpc');
|
||||
|
||||
return Context::setContext(Request::class, $sRequest);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user