Files
kiri-core/HttpServer/Http/Request.php
T

436 lines
7.5 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Http;
2021-08-02 11:07:15 +08:00
use Annotation\Inject;
2020-08-31 01:27:08 +08:00
use Exception;
2021-02-20 17:33:28 +08:00
use HttpServer\Abstracts\HttpService;
2020-08-31 01:27:08 +08:00
use HttpServer\IInterface\AuthIdentity;
2021-02-20 16:15:01 +08:00
use JetBrains\PhpStorm\Pure;
2021-08-01 15:03:49 +08:00
use Server\ServerManager;
2020-12-24 11:12:23 +08:00
use Snowflake\Core\Json;
2020-08-31 12:38:32 +08:00
use Snowflake\Snowflake;
2020-08-31 01:27:08 +08:00
defined('REQUEST_OK') or define('REQUEST_OK', 0);
defined('REQUEST_FAIL') or define('REQUEST_FAIL', 500);
/**
* Class HttpRequest
*
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\HttpRequest
2020-08-31 01:27:08 +08:00
*
* @property-read $isPost
* @property-read $isGet
* @property-read $isOption
* @property-read $isDelete
* @property-read $isHttp
* @property-read $method
* @property-read $identity
* @property-read $isPackage
* @property-read $isReceive
*/
2021-02-20 17:33:28 +08:00
class Request extends HttpService
2020-08-31 01:27:08 +08:00
{
2021-07-27 16:08:16 +08:00
public int $fd = 0;
2021-07-28 14:09:39 +08:00
2021-08-02 11:07:15 +08:00
/**
* @var HttpParams|null
*/
2021-08-02 11:10:07 +08:00
#[Inject(HttpParams::class)]
2021-07-27 18:25:48 +08:00
public ?HttpParams $params = null;
2021-07-27 16:08:16 +08:00
2021-07-28 14:09:39 +08:00
2021-08-02 11:07:15 +08:00
/**
* @var HttpHeaders|null
*/
#[Inject(HttpHeaders::class)]
2021-07-27 18:25:48 +08:00
public ?HttpHeaders $headers = null;
2021-07-27 16:08:16 +08:00
2021-08-02 11:09:39 +08:00
2021-07-27 16:08:16 +08:00
public bool $isCli = FALSE;
public float $startTime;
2021-08-01 15:03:49 +08:00
2021-07-27 16:08:16 +08:00
public int $statusCode = 200;
/** @var string[] */
private array $explode = [];
const PLATFORM_MAC_OX = 'mac';
const PLATFORM_IPHONE = 'iphone';
const PLATFORM_ANDROID = 'android';
const PLATFORM_WINDOWS = 'windows';
2021-08-01 15:03:49 +08:00
private string $_platform = '';
2021-07-27 16:08:16 +08:00
/**
* @var AuthIdentity|null
*/
private ?AuthIdentity $_grant = null;
/**
* @return array|null
* @throws Exception
*/
public function getConnectInfo(): array|null
{
2021-08-01 16:31:46 +08:00
$server = ServerManager::getContext()->getServer();
2021-07-27 16:08:16 +08:00
2021-08-02 11:09:39 +08:00
return $server->getClientInfo($this->getClientId());
2021-07-27 16:08:16 +08:00
}
2021-08-02 11:15:06 +08:00
/**
* @return mixed
*/
2021-08-02 11:18:03 +08:00
public function getStartTime(): mixed
2021-08-02 11:15:06 +08:00
{
return $this->headers->get('request_time_float');
}
2021-07-27 16:08:16 +08:00
/**
* @return int
*/
public function getClientId(): int
{
2021-08-02 11:07:15 +08:00
$request = Context::getContext(\Swoole\Http\Request::class);
2021-08-02 11:09:39 +08:00
return $request->fd ?? 0;
2021-07-27 16:08:16 +08:00
}
/**
* @return bool
*/
2021-08-02 11:07:15 +08:00
public function isFavicon(): bool
2021-07-27 16:08:16 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestUri() === 'favicon.ico';
2021-07-27 16:08:16 +08:00
}
/**
* @return AuthIdentity|null
*/
public function getIdentity(): ?AuthIdentity
{
return $this->_grant;
}
/**
* @return bool
*/
public function isHead(): bool
{
2021-08-02 11:07:15 +08:00
$result = $this->headers->getRequestMethod() == 'HEAD';
2021-07-27 16:08:16 +08:00
if ($result) {
$this->setStatus(101);
} else {
$this->setStatus(200);
}
return $result;
}
/**
* @param $status
* @return mixed
*/
public function setStatus($status): mixed
{
return $this->statusCode = $status;
}
/**
* @return int
*/
public function getStatus(): int
{
return $this->statusCode;
}
/**
* @return bool
*/
public function getIsPackage(): bool
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestMethod() == 'package';
2021-07-27 16:08:16 +08:00
}
/**
* @return bool
*/
public function getIsReceive(): bool
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestMethod() == 'receive';
2021-07-27 16:08:16 +08:00
}
/**
* @param $value
*/
public function setGrantAuthorization($value)
{
$this->_grant = $value;
}
/**
* @return bool
*/
public function hasGrant(): bool
{
return $this->_grant !== null;
}
/**
* @return string[]
*/
public function getExplode(): array
{
2021-08-03 11:57:27 +08:00
return explode('/', $this->getUri());
2021-07-27 16:08:16 +08:00
}
/**
* @return string
*/
#[Pure] public function getCurrent(): string
{
return current($this->explode);
}
2021-08-01 14:08:41 +08:00
2021-07-27 16:08:16 +08:00
/**
* @return string
*/
public function getUri(): string
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestUri();
2021-07-27 16:08:16 +08:00
}
/**
* @return string|null
*/
public function getPlatform(): ?string
{
2021-08-01 15:03:49 +08:00
if (!empty($this->_platform)) {
return $this->_platform;
}
2021-08-02 11:07:15 +08:00
$user = $this->headers->getAgent();
2021-07-27 16:08:16 +08:00
$match = preg_match('/\(.*\)?/', $user, $output);
if (!$match || count($output) < 1) {
2021-08-01 15:03:49 +08:00
return $this->_platform = 'unknown';
2021-07-27 16:08:16 +08:00
}
$output = strtolower(array_shift($output));
if (strpos('mac', $output)) {
2021-08-01 15:03:49 +08:00
return $this->_platform = 'mac';
2021-07-27 16:08:16 +08:00
} else if (strpos('iphone', $output)) {
2021-08-01 15:03:49 +08:00
return $this->_platform = 'iphone';
2021-07-27 16:08:16 +08:00
} else if (strpos('android', $output)) {
2021-08-01 15:03:49 +08:00
return $this->_platform = 'android';
2021-07-27 16:08:16 +08:00
} else if (strpos('windows', $output)) {
2021-08-01 15:03:49 +08:00
return $this->_platform = 'windows';
} else {
return $this->_platform = 'unknown';
2021-07-27 16:08:16 +08:00
}
}
/**
* @return bool
*/
public function isIos(): bool
{
return $this->getPlatform() == static::PLATFORM_IPHONE;
}
/**
* @return bool
*/
public function isAndroid(): bool
{
return $this->getPlatform() == static::PLATFORM_ANDROID;
}
/**
* @return bool
*/
public function isMacOs(): bool
{
return $this->getPlatform() == static::PLATFORM_MAC_OX;
}
/**
* @return bool
*/
public function isWindows(): bool
{
return $this->getPlatform() == static::PLATFORM_WINDOWS;
}
/**
* @return bool
*/
public function getIsPost(): bool
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestMethod() == 'POST';
2021-07-27 16:08:16 +08:00
}
/**
* @return bool
* @throws Exception
*/
public function getIsHttp(): bool
{
return true;
}
/**
* @return bool
*/
public function getIsOption(): bool
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestMethod() == 'OPTIONS';
2021-07-27 16:08:16 +08:00
}
/**
* @return bool
*/
public function getIsGet(): bool
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestMethod() == 'GET';
2021-07-27 16:08:16 +08:00
}
/**
* @return bool
*/
public function getIsDelete(): bool
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestMethod() == 'DELETE';
2021-07-27 16:08:16 +08:00
}
/**
* @return string
*
* 获取请求类型
*/
public function getMethod(): string
{
2021-08-02 11:07:15 +08:00
$method = $this->headers->getRequestMethod();
2021-07-27 16:08:16 +08:00
if (empty($method)) {
2021-07-27 18:25:48 +08:00
return 'GET';
2021-07-27 16:08:16 +08:00
}
2021-07-27 18:25:48 +08:00
return $method;
2021-07-27 16:08:16 +08:00
}
/**
* @return bool
*/
public function getIsCli(): bool
{
return $this->isCli === TRUE;
}
/**
* @param $name
* @param $value
*
* @throws Exception
*/
public function __set($name, $value)
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->$method($value);
} else {
parent::__set($name, $value); // TODO: Change the autogenerated stub
}
}
/**
* @return mixed|null
*/
#[Pure] public function getIp(): string|null
{
$headers = $this->headers->getHeaders();
if (!empty($headers['remoteip'])) return $headers['remoteip'];
if (!empty($headers['x-forwarded-for'])) return $headers['x-forwarded-for'];
if (!empty($headers['request-ip'])) return $headers['request-ip'];
if (!empty($headers['remote_addr'])) return $headers['remote_addr'];
return NULL;
}
/**
* @return string
*/
#[Pure] public function getRuntime(): string
{
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
}
/**
* @return string
*/
public function getDebug(): string
{
$mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳
$timestamp = floatval($mainstay); // 时间戳
$milliseconds = round(($mainstay - $timestamp) * 1000); // 毫秒
$datetime = date("Y-m-d H:i:s", (int)$timestamp) . '.' . $milliseconds;
$tmp = [
'[Debug ' . $datetime . '] ',
$this->getIp(),
$this->getUri(),
2021-08-02 11:07:15 +08:00
'`' . $this->headers->getAgent() . '`',
2021-07-27 16:08:16 +08:00
$this->getRuntime()
];
return implode(' ', $tmp);
}
/**
* @param $router
* @return bool
*/
2021-08-02 11:07:15 +08:00
public function is($router): bool
2021-07-27 16:08:16 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->headers->getRequestUri() == $router;
2021-07-27 16:08:16 +08:00
}
/**
* @return bool
*/
public function isNotFound(): bool
{
2021-08-02 11:09:39 +08:00
return Json::to(404, 'Page ' . $this->headers->getRequestUri() . ' not found.');
2021-07-27 16:08:16 +08:00
}
/**
* @param \Swoole\Http\Request $request
2021-07-27 18:21:30 +08:00
* @return Request
2021-08-02 11:07:15 +08:00
* @throws Exception
2021-07-27 16:08:16 +08:00
*/
public static function create(\Swoole\Http\Request $request): Request
{
2021-08-02 11:07:15 +08:00
$request->header = array_merge($request->header, $request->server);
2021-08-01 15:03:49 +08:00
2021-08-02 11:07:15 +08:00
Context::setContext(\Swoole\Http\Request::class, $request);
2021-08-01 15:03:49 +08:00
2021-08-04 02:02:21 +08:00
Context::setContext(Response::class, new CoroutineResponse());
2021-08-04 01:47:26 +08:00
2021-08-02 18:18:22 +08:00
return Snowflake::getDi()->get(Request::class);
2021-07-27 16:08:16 +08:00
}
2020-08-31 01:27:08 +08:00
}