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;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use HttpServer\Application;
|
|
|
|
|
use HttpServer\IInterface\AuthIdentity;
|
2020-12-17 14:09:14 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
|
|
|
|
use ReflectionException;
|
2020-09-02 18:43:23 +08:00
|
|
|
use Snowflake\Core\JSON;
|
2020-12-17 14:09:14 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
2020-09-16 10:41:52 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2020-08-31 12:38:32 +08:00
|
|
|
use Snowflake\Snowflake;
|
2020-10-29 18:17:25 +08:00
|
|
|
use function router;
|
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
|
|
|
|
|
*/
|
|
|
|
|
class Request extends Application
|
|
|
|
|
{
|
|
|
|
|
|
2020-09-02 18:43:23 +08:00
|
|
|
/** @var int $fd */
|
2020-10-29 18:17:25 +08:00
|
|
|
public int $fd = 0;
|
2020-09-02 18:43:23 +08:00
|
|
|
|
|
|
|
|
/** @var HttpParams */
|
2020-10-29 18:17:25 +08:00
|
|
|
public HttpParams $params;
|
2020-09-02 18:43:23 +08:00
|
|
|
|
|
|
|
|
/** @var HttpHeaders */
|
2020-10-29 18:17:25 +08:00
|
|
|
public HttpHeaders $headers;
|
2020-09-02 18:43:23 +08:00
|
|
|
|
|
|
|
|
/** @var bool */
|
2020-10-29 18:17:25 +08:00
|
|
|
public bool $isCli = FALSE;
|
2020-09-02 18:43:23 +08:00
|
|
|
|
|
|
|
|
/** @var float */
|
2020-10-29 18:17:25 +08:00
|
|
|
public float $startTime;
|
2020-09-02 18:43:23 +08:00
|
|
|
|
2020-10-29 18:17:25 +08:00
|
|
|
public string $uri = '';
|
2020-09-02 18:43:23 +08:00
|
|
|
|
2020-10-29 18:17:25 +08:00
|
|
|
public int $statusCode = 200;
|
2020-09-02 18:43:23 +08:00
|
|
|
|
|
|
|
|
/** @var string[] */
|
2020-10-29 18:17:25 +08:00
|
|
|
private array $explode = [];
|
2020-09-02 18:43:23 +08:00
|
|
|
|
|
|
|
|
const PLATFORM_MAC_OX = 'mac';
|
|
|
|
|
const PLATFORM_IPHONE = 'iphone';
|
|
|
|
|
const PLATFORM_ANDROID = 'android';
|
|
|
|
|
const PLATFORM_WINDOWS = 'windows';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var AuthIdentity|null
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
private ?AuthIdentity $_grant = null;
|
2020-09-02 18:43:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $fd
|
|
|
|
|
*/
|
|
|
|
|
public function setFd($fd)
|
|
|
|
|
{
|
|
|
|
|
$this->fd = $fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function isFavicon(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getUri() === 'favicon.ico';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIdentity(): mixed
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->_grant;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function isHead(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
$result = $this->headers->getHeader('request_method') == 'head';
|
|
|
|
|
if ($result) {
|
|
|
|
|
$this->setStatus(101);
|
|
|
|
|
} else {
|
|
|
|
|
$this->setStatus(200);
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $status
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function setStatus($status): mixed
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->statusCode = $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getStatus(): int
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->statusCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIsPackage(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->headers->getHeader('request_method') == 'package';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIsReceive(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->headers->getHeader('request_method') == 'receive';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $value
|
|
|
|
|
*/
|
|
|
|
|
public function setGrantAuthorization($value)
|
|
|
|
|
{
|
|
|
|
|
$this->_grant = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function hasGrant(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->_grant !== null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function parseUri(): string
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
$array = [];
|
|
|
|
|
$explode = explode('/', $this->headers->getHeader('request_uri'));
|
|
|
|
|
foreach ($explode as $item) {
|
|
|
|
|
if (empty($item)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$array[] = $item;
|
|
|
|
|
}
|
|
|
|
|
return $this->uri = implode('/', ($this->explode = $array));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getExplode(): array
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->explode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return string
|
2020-09-02 18:43:23 +08:00
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
#[Pure] public function getCurrent(): string
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return current($this->explode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getUri(): string
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
if (!$this->headers) {
|
|
|
|
|
return 'command exec.';
|
|
|
|
|
}
|
|
|
|
|
if (!empty($this->uri)) {
|
|
|
|
|
return $this->uri;
|
|
|
|
|
}
|
|
|
|
|
$uri = $this->headers->getHeader('request_uri');
|
|
|
|
|
$uri = ltrim($uri, '/');
|
|
|
|
|
if (empty($uri)) return '/';
|
|
|
|
|
return $uri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return mixed
|
|
|
|
|
* @throws ComponentException
|
2020-09-02 18:43:23 +08:00
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function adapter(): mixed
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
if (!$this->isHead()) {
|
2020-10-29 18:17:25 +08:00
|
|
|
return router()->dispatch();
|
2020-09-02 18:43:23 +08:00
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getPlatform(): ?string
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
$user = $this->headers->getHeader('user-agent');
|
|
|
|
|
$match = preg_match('/\(.*\)?/', $user, $output);
|
|
|
|
|
if (!$match || count($output) < 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$output = strtolower(array_shift($output));
|
|
|
|
|
if (strpos('mac', $output)) {
|
|
|
|
|
return 'mac';
|
|
|
|
|
} else if (strpos('iphone', $output)) {
|
|
|
|
|
return 'iphone';
|
|
|
|
|
} else if (strpos('android', $output)) {
|
|
|
|
|
return 'android';
|
|
|
|
|
} else if (strpos('windows', $output)) {
|
|
|
|
|
return 'windows';
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function isIos(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getPlatform() == static::PLATFORM_IPHONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function isAndroid(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getPlatform() == static::PLATFORM_ANDROID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function isMacOs(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getPlatform() == static::PLATFORM_MAC_OX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function isWindows(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getPlatform() == static::PLATFORM_WINDOWS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIsPost(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getMethod() == 'post';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIsHttp(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIsOption(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getMethod() == 'options';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIsGet(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getMethod() == 'get';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIsDelete(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getMethod() == 'delete';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*
|
|
|
|
|
* 获取请求类型
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getMethod(): string
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
2020-09-21 17:30:13 +08:00
|
|
|
$method = $this->headers->get('request_method');
|
2020-09-17 19:10:38 +08:00
|
|
|
if (empty($method)) {
|
|
|
|
|
return 'get';
|
|
|
|
|
}
|
|
|
|
|
return strtolower($method);
|
2020-09-02 18:43:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getIsCli(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
#[Pure] public function getIp()
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
$headers = $this->headers->getHeaders();
|
|
|
|
|
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
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
#[Pure] public function getRuntime(): string
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getDebug(): string
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
$mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳
|
|
|
|
|
|
|
|
|
|
$timestamp = floor($mainstay); // 时间戳
|
|
|
|
|
$milliseconds = round(($mainstay - $timestamp) * 1000); // 毫秒
|
|
|
|
|
|
|
|
|
|
$datetime = date("Y-m-d H:i:s", $timestamp) . '.' . $milliseconds;
|
|
|
|
|
|
|
|
|
|
$tmp = [
|
|
|
|
|
'[Debug ' . $datetime . '] ',
|
|
|
|
|
$this->getIp(),
|
|
|
|
|
$this->getUri(),
|
|
|
|
|
'`' . $this->headers->getHeader('user-agent') . '`',
|
|
|
|
|
$this->getRuntime()
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return implode(' ', $tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $router
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function is($router): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return $this->getUri() == trim($router, '/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function isNotFound(): bool
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
|
|
|
|
return JSON::to(404, 'Page ' . $this->getUri() . ' not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $request
|
2020-09-16 10:41:52 +08:00
|
|
|
* @return mixed
|
2020-12-17 14:09:14 +08:00
|
|
|
* @throws ReflectionException
|
2020-09-16 10:41:52 +08:00
|
|
|
* @throws NotFindClassException
|
2020-09-02 18:43:23 +08:00
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public static function create($request): Request
|
2020-09-02 18:43:23 +08:00
|
|
|
{
|
2020-09-16 10:41:52 +08:00
|
|
|
$sRequest = Context::setContext('request', Snowflake::createObject(Request::class));
|
2020-09-02 18:43:23 +08:00
|
|
|
$sRequest->fd = $request->fd;
|
|
|
|
|
$sRequest->startTime = microtime(true);
|
2020-09-09 11:50:58 +08:00
|
|
|
$sRequest->uri = $request->server['request_uri'] ?? $request->header['request_uri'];
|
2020-09-16 10:41:52 +08:00
|
|
|
|
2020-09-17 14:12:14 +08:00
|
|
|
$sRequest->params = new HttpParams($request->rawContent(), $request->get, $request->files);
|
2020-09-02 18:43:23 +08:00
|
|
|
if (!empty($request->post)) {
|
|
|
|
|
$sRequest->params->setPosts($request->post ?? []);
|
|
|
|
|
}
|
2020-09-17 19:10:38 +08:00
|
|
|
$sRequest->headers = Snowflake::createObject(HttpHeaders::class, [array_merge($request->server, $request->header ?? [])]);
|
2020-09-02 18:43:23 +08:00
|
|
|
return $sRequest;
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|