modify
This commit is contained in:
+445
-445
@@ -37,451 +37,451 @@ defined('REQUEST_FAIL') or define('REQUEST_FAIL', 500);
|
|||||||
class Request extends HttpService
|
class Request extends HttpService
|
||||||
{
|
{
|
||||||
|
|
||||||
public int $fd = 0;
|
public int $fd = 0;
|
||||||
|
|
||||||
public ?HttpParams $params;
|
public ?HttpParams $params;
|
||||||
|
|
||||||
public ?HttpHeaders $headers;
|
public ?HttpHeaders $headers;
|
||||||
|
|
||||||
public bool $isCli = FALSE;
|
public bool $isCli = FALSE;
|
||||||
|
|
||||||
public float $startTime;
|
public float $startTime;
|
||||||
|
|
||||||
public string $uri = '';
|
public string $uri = '';
|
||||||
|
|
||||||
public int $statusCode = 200;
|
public int $statusCode = 200;
|
||||||
|
|
||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
private array $explode = [];
|
private array $explode = [];
|
||||||
|
|
||||||
const PLATFORM_MAC_OX = 'mac';
|
const PLATFORM_MAC_OX = 'mac';
|
||||||
const PLATFORM_IPHONE = 'iphone';
|
const PLATFORM_IPHONE = 'iphone';
|
||||||
const PLATFORM_ANDROID = 'android';
|
const PLATFORM_ANDROID = 'android';
|
||||||
const PLATFORM_WINDOWS = 'windows';
|
const PLATFORM_WINDOWS = 'windows';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var AuthIdentity|null
|
* @var AuthIdentity|null
|
||||||
*/
|
*/
|
||||||
private ?AuthIdentity $_grant = null;
|
private ?AuthIdentity $_grant = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $fd
|
* @param $fd
|
||||||
*/
|
*/
|
||||||
public function setFd($fd)
|
public function setFd($fd)
|
||||||
{
|
{
|
||||||
$this->fd = $fd;
|
$this->fd = $fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|null
|
* @return array|null
|
||||||
* @throws ComponentException
|
* @throws ComponentException
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function getConnectInfo(): array|null
|
public function getConnectInfo(): array|null
|
||||||
{
|
{
|
||||||
if (empty($this->fd)) {
|
if (empty($this->fd)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$server = Snowflake::app()->getSwoole();
|
$server = Snowflake::app()->getSwoole();
|
||||||
|
|
||||||
return $server->getClientInfo($this->fd);
|
return $server->getClientInfo($this->fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isFavicon(): bool
|
public function isFavicon(): bool
|
||||||
{
|
{
|
||||||
return $this->getUri() === 'favicon.ico';
|
return $this->getUri() === 'favicon.ico';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getIdentity(): mixed
|
public function getIdentity(): mixed
|
||||||
{
|
{
|
||||||
return $this->_grant;
|
return $this->_grant;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isHead(): bool
|
public function isHead(): bool
|
||||||
{
|
{
|
||||||
$result = $this->headers->getHeader('request_method') == 'head';
|
$result = $this->headers->getHeader('request_method') == 'head';
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$this->setStatus(101);
|
$this->setStatus(101);
|
||||||
} else {
|
} else {
|
||||||
$this->setStatus(200);
|
$this->setStatus(200);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $status
|
* @param $status
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function setStatus($status): mixed
|
public function setStatus($status): mixed
|
||||||
{
|
{
|
||||||
return $this->statusCode = $status;
|
return $this->statusCode = $status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getStatus(): int
|
public function getStatus(): int
|
||||||
{
|
{
|
||||||
return $this->statusCode;
|
return $this->statusCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getIsPackage(): bool
|
public function getIsPackage(): bool
|
||||||
{
|
{
|
||||||
return $this->headers->getHeader('request_method') == 'package';
|
return $this->headers->getHeader('request_method') == 'package';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getIsReceive(): bool
|
public function getIsReceive(): bool
|
||||||
{
|
{
|
||||||
return $this->headers->getHeader('request_method') == 'receive';
|
return $this->headers->getHeader('request_method') == 'receive';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
*/
|
*/
|
||||||
public function setGrantAuthorization($value)
|
public function setGrantAuthorization($value)
|
||||||
{
|
{
|
||||||
$this->_grant = $value;
|
$this->_grant = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasGrant(): bool
|
public function hasGrant(): bool
|
||||||
{
|
{
|
||||||
return $this->_grant !== null;
|
return $this->_grant !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function parseUri(): string
|
public function parseUri(): string
|
||||||
{
|
{
|
||||||
$array = [];
|
$array = [];
|
||||||
$explode = explode('/', $this->headers->getHeader('request_uri'));
|
$explode = explode('/', $this->headers->getHeader('request_uri'));
|
||||||
foreach ($explode as $item) {
|
foreach ($explode as $item) {
|
||||||
if (empty($item)) {
|
if (empty($item)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$array[] = $item;
|
$array[] = $item;
|
||||||
}
|
}
|
||||||
return $this->uri = implode('/', ($this->explode = $array));
|
return $this->uri = implode('/', ($this->explode = $array));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public function getExplode(): array
|
public function getExplode(): array
|
||||||
{
|
{
|
||||||
return $this->explode;
|
return $this->explode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] public function getCurrent(): string
|
#[Pure] public function getCurrent(): string
|
||||||
{
|
{
|
||||||
return current($this->explode);
|
return current($this->explode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getUri(): string
|
public function getUri(): string
|
||||||
{
|
{
|
||||||
if (!$this->headers) {
|
if (!$this->headers) {
|
||||||
return 'command exec.';
|
return 'command exec.';
|
||||||
}
|
}
|
||||||
if (!empty($this->uri)) {
|
if (!empty($this->uri)) {
|
||||||
return $this->uri;
|
return $this->uri;
|
||||||
}
|
}
|
||||||
$uri = $this->headers->getHeader('request_uri');
|
$uri = $this->headers->getHeader('request_uri');
|
||||||
$uri = ltrim($uri, '/');
|
$uri = ltrim($uri, '/');
|
||||||
if (empty($uri)) return '/';
|
if (empty($uri)) return '/';
|
||||||
return $uri;
|
return $uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ComponentException
|
* @throws ComponentException
|
||||||
*/
|
*/
|
||||||
public function adapter(): mixed
|
public function adapter(): mixed
|
||||||
{
|
{
|
||||||
if (!$this->isHead()) {
|
if (!$this->isHead()) {
|
||||||
return router()->dispatch();
|
return router()->dispatch();
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getPlatform(): ?string
|
public function getPlatform(): ?string
|
||||||
{
|
{
|
||||||
$user = $this->headers->getHeader('user-agent');
|
$user = $this->headers->getHeader('user-agent');
|
||||||
$match = preg_match('/\(.*\)?/', $user, $output);
|
$match = preg_match('/\(.*\)?/', $user, $output);
|
||||||
if (!$match || count($output) < 1) {
|
if (!$match || count($output) < 1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$output = strtolower(array_shift($output));
|
$output = strtolower(array_shift($output));
|
||||||
if (strpos('mac', $output)) {
|
if (strpos('mac', $output)) {
|
||||||
return 'mac';
|
return 'mac';
|
||||||
} else if (strpos('iphone', $output)) {
|
} else if (strpos('iphone', $output)) {
|
||||||
return 'iphone';
|
return 'iphone';
|
||||||
} else if (strpos('android', $output)) {
|
} else if (strpos('android', $output)) {
|
||||||
return 'android';
|
return 'android';
|
||||||
} else if (strpos('windows', $output)) {
|
} else if (strpos('windows', $output)) {
|
||||||
return 'windows';
|
return 'windows';
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isIos(): bool
|
public function isIos(): bool
|
||||||
{
|
{
|
||||||
return $this->getPlatform() == static::PLATFORM_IPHONE;
|
return $this->getPlatform() == static::PLATFORM_IPHONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isAndroid(): bool
|
public function isAndroid(): bool
|
||||||
{
|
{
|
||||||
return $this->getPlatform() == static::PLATFORM_ANDROID;
|
return $this->getPlatform() == static::PLATFORM_ANDROID;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isMacOs(): bool
|
public function isMacOs(): bool
|
||||||
{
|
{
|
||||||
return $this->getPlatform() == static::PLATFORM_MAC_OX;
|
return $this->getPlatform() == static::PLATFORM_MAC_OX;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isWindows(): bool
|
public function isWindows(): bool
|
||||||
{
|
{
|
||||||
return $this->getPlatform() == static::PLATFORM_WINDOWS;
|
return $this->getPlatform() == static::PLATFORM_WINDOWS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getIsPost(): bool
|
public function getIsPost(): bool
|
||||||
{
|
{
|
||||||
return $this->getMethod() == 'post';
|
return $this->getMethod() == 'post';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getIsHttp(): bool
|
public function getIsHttp(): bool
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getIsOption(): bool
|
public function getIsOption(): bool
|
||||||
{
|
{
|
||||||
return $this->getMethod() == 'options';
|
return $this->getMethod() == 'options';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getIsGet(): bool
|
public function getIsGet(): bool
|
||||||
{
|
{
|
||||||
return $this->getMethod() == 'get';
|
return $this->getMethod() == 'get';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getIsDelete(): bool
|
public function getIsDelete(): bool
|
||||||
{
|
{
|
||||||
return $this->getMethod() == 'delete';
|
return $this->getMethod() == 'delete';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*
|
*
|
||||||
* 获取请求类型
|
* 获取请求类型
|
||||||
*/
|
*/
|
||||||
public function getMethod(): string
|
public function getMethod(): string
|
||||||
{
|
{
|
||||||
$method = $this->headers->get('request_method');
|
$method = $this->headers->get('request_method');
|
||||||
if (empty($method)) {
|
if (empty($method)) {
|
||||||
return 'get';
|
return 'get';
|
||||||
}
|
}
|
||||||
return strtolower($method);
|
return strtolower($method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getIsCli(): bool
|
public function getIsCli(): bool
|
||||||
{
|
{
|
||||||
return $this->isCli === TRUE;
|
return $this->isCli === TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $value
|
* @param $value
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __set($name, $value)
|
public function __set($name, $value)
|
||||||
{
|
{
|
||||||
$method = 'set' . ucfirst($name);
|
$method = 'set' . ucfirst($name);
|
||||||
if (method_exists($this, $method)) {
|
if (method_exists($this, $method)) {
|
||||||
$this->$method($value);
|
$this->$method($value);
|
||||||
} else {
|
} else {
|
||||||
parent::__set($name, $value); // TODO: Change the autogenerated stub
|
parent::__set($name, $value); // TODO: Change the autogenerated stub
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed|null
|
* @return mixed|null
|
||||||
*/
|
*/
|
||||||
public function getIp(): string|null
|
public function getIp(): string|null
|
||||||
{
|
{
|
||||||
$headers = $this->headers->getHeaders();
|
$headers = $this->headers->getHeaders();
|
||||||
if (!empty($headers['x-forwarded-for'])) return $headers['x-forwarded-for'];
|
if (!empty($headers['x-forwarded-for'])) return $headers['x-forwarded-for'];
|
||||||
if (!empty($headers['request-ip'])) return $headers['request-ip'];
|
if (!empty($headers['request-ip'])) return $headers['request-ip'];
|
||||||
if (!empty($headers['remote_addr'])) return $headers['remote_addr'];
|
if (!empty($headers['remote_addr'])) return $headers['remote_addr'];
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getRuntime(): string
|
public function getRuntime(): string
|
||||||
{
|
{
|
||||||
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
|
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getDebug(): string
|
public function getDebug(): string
|
||||||
{
|
{
|
||||||
$mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳
|
$mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳
|
||||||
|
|
||||||
$timestamp = floor($mainstay); // 时间戳
|
$timestamp = floor($mainstay); // 时间戳
|
||||||
$milliseconds = round(($mainstay - $timestamp) * 1000); // 毫秒
|
$milliseconds = round(($mainstay - $timestamp) * 1000); // 毫秒
|
||||||
|
|
||||||
$datetime = date("Y-m-d H:i:s", $timestamp) . '.' . $milliseconds;
|
$datetime = date("Y-m-d H:i:s", $timestamp) . '.' . $milliseconds;
|
||||||
|
|
||||||
$tmp = [
|
$tmp = [
|
||||||
'[Debug ' . $datetime . '] ',
|
'[Debug ' . $datetime . '] ',
|
||||||
$this->getIp(),
|
$this->getIp(),
|
||||||
$this->getUri(),
|
$this->getUri(),
|
||||||
'`' . $this->headers->getHeader('user-agent') . '`',
|
'`' . $this->headers->getHeader('user-agent') . '`',
|
||||||
$this->getRuntime()
|
$this->getRuntime()
|
||||||
];
|
];
|
||||||
|
|
||||||
return implode(' ', $tmp);
|
return implode(' ', $tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $router
|
* @param $router
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function is($router): bool
|
public function is($router): bool
|
||||||
{
|
{
|
||||||
return $this->getUri() == trim($router, '/');
|
return $this->getUri() == trim($router, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isNotFound(): bool
|
public function isNotFound(): bool
|
||||||
{
|
{
|
||||||
return Json::to(404, 'Page ' . $this->getUri() . ' not found.');
|
return Json::to(404, 'Page ' . $this->getUri() . ' not found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $request
|
* @param $request
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
*/
|
*/
|
||||||
public static function create($request): Request
|
public static function create($request): Request
|
||||||
{
|
{
|
||||||
/** @var Request $sRequest */
|
/** @var Request $sRequest */
|
||||||
$sRequest = Context::setContext('request', Snowflake::createObject(Request::class));
|
$sRequest = Context::setContext('request', Snowflake::createObject(Request::class));
|
||||||
$sRequest->fd = $request->fd;
|
$sRequest->fd = $request->fd;
|
||||||
$sRequest->startTime = microtime(true);
|
$sRequest->startTime = microtime(true);
|
||||||
|
|
||||||
$sRequest->params = new HttpParams($request->rawContent(), $request->get, $request->files);
|
$sRequest->params = new HttpParams($request->rawContent(), $request->get, $request->files);
|
||||||
if (!empty($request->post)) {
|
if (!empty($request->post)) {
|
||||||
$sRequest->params->setPosts($request->post ?? []);
|
$sRequest->params->setPosts($request->post ?? []);
|
||||||
}
|
}
|
||||||
$sRequest->headers = Snowflake::createObject(HttpHeaders::class, [merge($request->server, $request->header)]);
|
$sRequest->headers = new HttpHeaders(merge($request->server, $request->header));
|
||||||
$sRequest->uri = $sRequest->headers->get('request_uri');
|
$sRequest->uri = $sRequest->headers->get('request_uri');
|
||||||
|
|
||||||
$sRequest->parseUri();
|
$sRequest->parseUri();
|
||||||
return $sRequest;
|
return $sRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $frame
|
* @param $frame
|
||||||
* @param $route
|
* @param $route
|
||||||
* @param string $event
|
* @param string $event
|
||||||
* @return Request
|
* @return Request
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public static function socketQuery($frame, $event = Socket::MESSAGE, $route = 'event'): Request
|
public static function socketQuery($frame, $event = Socket::MESSAGE, $route = 'event'): Request
|
||||||
{
|
{
|
||||||
/** @var Request $sRequest */
|
/** @var Request $sRequest */
|
||||||
$sRequest = Snowflake::createObject(Request::class);
|
$sRequest = Snowflake::createObject(Request::class);
|
||||||
$sRequest->fd = $frame->fd;
|
$sRequest->fd = $frame->fd;
|
||||||
$sRequest->startTime = microtime(true);
|
$sRequest->startTime = microtime(true);
|
||||||
|
|
||||||
$sRequest->params = new HttpParams([], [], []);
|
$sRequest->params = new HttpParams([], [], []);
|
||||||
$sRequest->headers = new HttpHeaders([]);
|
$sRequest->headers = new HttpHeaders([]);
|
||||||
$sRequest->headers->replace('request_method', 'sw::socket');
|
$sRequest->headers->replace('request_method', 'sw::socket');
|
||||||
$sRequest->headers->replace('request_uri', $event . '::' . $route);
|
$sRequest->headers->replace('request_uri', $event . '::' . $route);
|
||||||
$sRequest->parseUri();
|
$sRequest->parseUri();
|
||||||
|
|
||||||
return $sRequest;
|
return $sRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user