This commit is contained in:
as2252258@163.com
2021-08-28 00:51:27 +08:00
parent c4144f2913
commit f052cf1fbf
13 changed files with 7 additions and 666 deletions
-2
View File
@@ -15,7 +15,6 @@ use Database\Connection;
use Exception;
use Http\Context\HttpHeaders;
use Http\Context\HttpParams;
use Http\Context\Request;
use Http\Context\Response;
use Http\Route\Router;
use Http\Server;
@@ -485,7 +484,6 @@ abstract class BaseApplication extends Component
'async' => ['class' => Async::class],
'kafka-container' => ['class' => KafkaProvider::class],
'response' => ['class' => Response::class],
'request' => ['class' => Request::class],
'shutdown' => ['class' => Shutdown::class],
]);
}
-2
View File
@@ -8,7 +8,6 @@ use Annotation\Annotation as SAnnotation;
use Database\DatabasesProviders;
use Http\Client\Client;
use Http\Client\Curl;
use Http\Context\Request;
use Http\Context\Response;
use Http\HttpFilter;
use Http\Route\Router;
@@ -27,7 +26,6 @@ use Kiri\Jwt\Jwt;
* @property \Redis|Redis $redis
* @property Server $server
* @property Response $response
* @property Request $request
* @property DatabasesProviders $db
* @property Async $async
* @property Logger $logger
+2 -3
View File
@@ -7,7 +7,6 @@ namespace Kiri\Jwt;
use Closure;
use Exception;
use Http\Context\Request;
use Http\Route\MiddlewareAbstracts;
use Server\RequestInterface;
use Kiri\Kiri;
@@ -26,14 +25,14 @@ class JWTAuthMiddleware extends MiddlewareAbstracts
/**
* @param Request $request
* @param RequestInterface $request
* @param Closure $next
* @return mixed
* @throws Exception
*/
public function onHandler(RequestInterface $request, Closure $next): mixed
{
$authorization = $request->header('Authorization');
$authorization = $request->getHeaderLine('Authorization');
if (empty($authorization)) {
throw new JWTAuthTokenException('JWT voucher cannot be empty.');
}
-413
View File
@@ -1,413 +0,0 @@
<?php
declare(strict_types=1);
namespace Http\Context;
use Annotation\Inject;
use Exception;
use Http\Abstracts\HttpService;
use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure;
use Server\RequestInterface;
use Server\ServerManager;
use Kiri\Core\Json;
defined('REQUEST_OK') or define('REQUEST_OK', 0);
defined('REQUEST_FAIL') or define('REQUEST_FAIL', 500);
/**
* Class HttpRequest
*
* @package Kiri\Kiri\HttpRequest
*
* @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 HttpService implements RequestInterface
{
use HttpHeaders, HttpParams;
private array $_explode = [];
private string $_uri = '';
private int $fd = 0;
private bool $isCli = FALSE;
private float $startTime;
private int $statusCode = 200;
private int $_clientId = 0;
/** @var string[] */
private array $explode = [];
const PLATFORM_MAC_OX = 'mac';
const PLATFORM_IPHONE = 'iphone';
const PLATFORM_ANDROID = 'android';
const PLATFORM_WINDOWS = 'windows';
private string $_platform = '';
/**
* @var AuthIdentity|null
*/
private ?AuthIdentity $_grant = null;
/**
* @param $id
*/
public function setClientId($id)
{
$this->_clientId = $id;
}
/**
* @param string $request_uri
*/
public function setUri(string $request_uri)
{
$request_uri = array_filter(explode('/', $request_uri));
$this->_explode = $request_uri;
$this->_uri = '/' . implode('/', $request_uri);
}
/**
* @return array|null
* @throws Exception
*/
public function getConnectInfo(): array|null
{
$server = di(ServerManager::class)->getServer();
return $server->getClientInfo($this->getClientId());
}
/**
* @return mixed
*/
public function getStartTime(): mixed
{
return $this->getHeader('request_time_float');
}
/**
* @return int
*/
public function getClientId(): int
{
return $this->_clientId ?? 0;
}
/**
* @return bool
*/
public function isFavicon(): bool
{
return $this->getRequestUri() === 'favicon.ico';
}
/**
* @return AuthIdentity|null
*/
public function getIdentity(): ?AuthIdentity
{
return $this->_grant;
}
/**
* @return bool
*/
public function isHead(): bool
{
$result = $this->getRequestMethod() == 'HEAD';
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
{
return $this->getRequestMethod() == 'package';
}
/**
* @return bool
*/
public function getIsReceive(): bool
{
return $this->getRequestMethod() == 'receive';
}
/**
* @param AuthIdentity $value
* @return AuthIdentity
*/
public function setGrantAuthorization(AuthIdentity $value): AuthIdentity
{
return $this->_grant = $value;
}
/**
* @return bool
*/
public function hasGrant(): bool
{
return $this->_grant !== null;
}
/**
* @return string[]
*/
public function getExplode(): array
{
return $this->_explode;
}
/**
* @return string
*/
#[Pure] public function getCurrent(): string
{
return current($this->explode);
}
/**
* @return string
*/
public function getUri(): string
{
return $this->_uri;
}
/**
* @return string|null
*/
public function getPlatform(): ?string
{
if (!empty($this->_platform)) {
return $this->_platform;
}
$user = $this->getAgent();
$match = preg_match('/\(.*\)?/', $user, $output);
if (!$match || count($output) < 1) {
return $this->_platform = 'unknown';
}
$output = strtolower(array_shift($output));
if (strpos('mac', $output)) {
return $this->_platform = 'mac';
} else if (strpos('iphone', $output)) {
return $this->_platform = 'iphone';
} else if (strpos('android', $output)) {
return $this->_platform = 'android';
} else if (strpos('windows', $output)) {
return $this->_platform = 'windows';
} else {
return $this->_platform = 'unknown';
}
}
/**
* @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
{
return $this->getRequestMethod() == 'POST';
}
/**
* @return bool
* @throws Exception
*/
public function getIsHttp(): bool
{
return true;
}
/**
* @return bool
*/
public function getIsOption(): bool
{
return $this->getRequestMethod() == 'OPTIONS';
}
/**
* @return bool
*/
public function getIsGet(): bool
{
return $this->getRequestMethod() == 'GET';
}
/**
* @return bool
*/
public function getIsDelete(): bool
{
return $this->getRequestMethod() == 'DELETE';
}
/**
* @return string
*
* 获取请求类型
*/
public function getMethod(): string
{
$method = $this->getRequestMethod();
if (empty($method)) {
return 'GET';
}
return $method;
}
/**
* @return bool
*/
public function getIsCli(): bool
{
return $this->isCli === TRUE;
}
/**
* @return mixed|null
*/
#[Pure] public function getIp(): string|null
{
$headers = $this->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(),
'`' . $this->getAgent() . '`',
$this->getRuntime()
];
return implode(' ', $tmp);
}
/**
* @param $router
* @return bool
*/
public function is($router): bool
{
return $this->getRequestUri() == $router;
}
/**
* @return bool
*/
public function isNotFound(): bool
{
return Json::to(404, 'Page ' . $this->getRequestUri() . ' not found.');
}
}
-22
View File
@@ -1,22 +0,0 @@
<?php
declare(strict_types=1);
namespace Http\IInterface;
use Closure;
use Http\Context\Request;
interface After
{
/**
* @param Request $request
* @param $params
* @return mixed
*/
public function onHandler(Request $request, $params): void;
}
-22
View File
@@ -1,22 +0,0 @@
<?php
declare(strict_types=1);
namespace Http\IInterface;
use Http\Context\Request;
use Closure;
interface Interceptor
{
/**
* @param Request $request
* @param Closure $closure
* @return mixed
*/
public function Interceptor(Request $request, Closure $closure): mixed;
}
-21
View File
@@ -1,21 +0,0 @@
<?php
declare(strict_types=1);
namespace Http\IInterface;
use Http\Context\Request;
use Closure;
interface Limits
{
/**
* @param Request $request
* @param Closure $closure
* @return mixed
*/
public function next(Request $request, Closure $closure): mixed;
}
-1
View File
@@ -7,7 +7,6 @@ namespace Http\Route;
use Closure;
use Exception;
use Http\Context\Request;
use Http\Context\Response;
use Server\RequestInterface;
+3 -3
View File
@@ -8,7 +8,6 @@ namespace Http\Route;
use Annotation\Aspect;
use Closure;
use Exception;
use Http\Context\Request;
use Http\Exception\RequestException;
use JetBrains\PhpStorm\Pure;
use Kiri\Events\EventProvider;
@@ -17,6 +16,7 @@ use Kiri\IAspect;
use Kiri\Kiri;
use ReflectionException;
use Server\Events\OnAfterWorkerStart;
use Server\RequestInterface;
/**
* Class Node
@@ -269,10 +269,10 @@ class Node
/**
* @param Request $request
* @param RequestInterface $request
* @return bool
*/
public function methodAllow(Request $request): bool
public function methodAllow(RequestInterface $request): bool
{
if ($this->method == $request->getMethod()) {
return true;
+2 -3
View File
@@ -7,7 +7,6 @@ use Annotation\Inject;
use Closure;
use Exception;
use Http\Abstracts\HttpService;
use Http\Context\Request;
use Http\Context\Response;
use Http\Controller;
use Http\IInterface\MiddlewareInterface;
@@ -494,12 +493,12 @@ class Router extends HttpService implements RouterInterface
/**
* @param Request $request
* @param RequestInterface $request
* @return Node|null 树干搜索
* 树干搜索
* @throws Exception
*/
public function find_path(Request $request): ?Node
public function find_path(RequestInterface $request): ?Node
{
$method = $request->getMethod();
-98
View File
@@ -1,98 +0,0 @@
<?php
declare(strict_types=1);
namespace Gii;
use Exception;
use Kiri\Kiri;
/**
* Class GiiInterceptor
* @package Gii
*/
class GiiInterceptor extends GiiBase
{
public ?string $tableName = null;
/**
* @return bool|array
* @throws Exception
*/
public function generate(): bool|array
{
$managerName = $this->input->get('name', null);
if (empty($managerName)) {
throw new Exception('文件名称不能为空~');
}
$html = '<?php
namespace App\Http\Interceptor;
';
$file = APP_PATH . 'app/Http/Interceptor/' . $managerName . 'Interceptor.php';
if (file_exists($file)) {
try {
$class = new \ReflectionClass('App\\Http\\Interceptor\\' . $managerName . 'Interceptor');
$html .= $this->getImports($file, $class);
} catch (\Throwable $exception) {
return logger()->addError($exception, 'throwable');
}
} else {
$html .= '
use Closure;
use Http\Context\Request;
use Http\IInterface\Interceptor;
';
}
$managerName = ucfirst($managerName);
$html .= '
/**
* Class ' . $managerName . 'Interceptor
* @package App\Http\Interceptor
*/
class ' . $managerName . 'Interceptor implements Interceptor
{';
if (isset($class)) {
$html .= $this->getClassProperty($class);
$html .= $this->getClassMethods($class);
$html .= '
}';
} else {
$html .= '
/**
* @param Request $request
* @param Closure $closure
* @return mixed
*/
public function Interceptor(Request $request, Closure $closure)
{
return $closure($request);
}
}';
}
if (file_exists($file)) {
throw new Exception('File exists.');
}
Kiri::writeFile($file, $html);
return [$managerName . 'Interceptor.php'];
}
}
-75
View File
@@ -1,75 +0,0 @@
<?php
declare(strict_types=1);
namespace Gii;
use Exception;
use Kiri\Kiri;
/**
* Class GiiLimits
* @package Gii
*/
class GiiLimits extends GiiBase
{
public ?string $tableName = '';
/**
* @return string[]
* @throws Exception
*/
public function generate(): array
{
$managerName = $this->input->get('name', null);
if (empty($managerName)) {
throw new Exception('文件名称不能为空~');
}
$html = '<?php
namespace App\Http\Limits;
use Closure;
use Http\Context\Request;
use Http\IInterface\Limits;
';
$managerName = ucfirst($managerName);
$html .= '
/**
* Class ' . $managerName . 'Limits
* @package App\Http\Limits
*/
class ' . $managerName . 'Limits implements Limits
{
/**
* @param Request $request
* @param Closure $closure
* @return mixed
*/
public function next(Request $request, Closure $closure)
{
return $closure($request);
}
}';
$file = APP_PATH . 'app/Http/Limits/' . $managerName . 'Limits.php';
if (file_exists($file)) {
throw new Exception('File exists.');
}
Kiri::writeFile($file, $html);
return [$managerName . 'Limits.php'];
}
}
-1
View File
@@ -33,7 +33,6 @@ class GiiMiddleware extends GiiBase
namespace App\Http\Middleware;
use Closure;
use Http\Context\Request;
use Http\IInterface\MiddlewareInterface;
use Server\RequestInterface;