This commit is contained in:
2020-09-02 18:43:23 +08:00
parent 0f63888022
commit 30f4a6a9a8
2 changed files with 355 additions and 330 deletions
+5
View File
@@ -28,12 +28,17 @@ class OnRequest extends Callback
/** /**
* @param Request $request * @param Request $request
* @param Response $response * @param Response $response
* @return void
* @throws Exception * @throws Exception
*/ */
public function onHandler(Request $request, Response $response) public function onHandler(Request $request, Response $response)
{ {
try { try {
/** @var HRequest $sRequest */
[$sRequest, $sResponse] = static::setContext($request, $response); [$sRequest, $sResponse] = static::setContext($request, $response);
if ($sRequest->is('favicon.ico')) {
return $sResponse->send($sRequest->isNotFound(), 200);
}
$sResponse->send(Snowflake::get()->router->dispatch(), 200); $sResponse->send(Snowflake::get()->router->dispatch(), 200);
} catch (Error | \Throwable $exception) { } catch (Error | \Throwable $exception) {
$this->sendErrorMessage($sResponse ?? null, $exception, $response); $this->sendErrorMessage($sResponse ?? null, $exception, $response);
+350 -330
View File
@@ -6,6 +6,7 @@ use Snowflake\Core\Help;
use Exception; use Exception;
use HttpServer\Application; use HttpServer\Application;
use HttpServer\IInterface\AuthIdentity; use HttpServer\IInterface\AuthIdentity;
use Snowflake\Core\JSON;
use Snowflake\Snowflake; use Snowflake\Snowflake;
defined('REQUEST_OK') or define('REQUEST_OK', 0); defined('REQUEST_OK') or define('REQUEST_OK', 0);
@@ -29,390 +30,409 @@ defined('REQUEST_FAIL') or define('REQUEST_FAIL', 500);
class Request extends Application class Request extends Application
{ {
/** @var int $fd */ /** @var int $fd */
public $fd = 0; public $fd = 0;
/** @var HttpParams */ /** @var HttpParams */
public $params; public $params;
/** @var HttpHeaders */ /** @var HttpHeaders */
public $headers; public $headers;
/** @var bool */ /** @var bool */
public $isCli = FALSE; public $isCli = FALSE;
/** @var float */ /** @var float */
public $startTime; public $startTime;
public $uri = ''; public $uri = '';
public $statusCode = 200; public $statusCode = 200;
/** @var string[] */ /** @var string[] */
private $explode = []; private $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 $_grant = null; private $_grant = null;
/** /**
* @param $fd * @param $fd
*/ */
public function setFd($fd) public function setFd($fd)
{ {
$this->fd = $fd; $this->fd = $fd;
} }
/** /**
* @return bool * @return bool
*/ */
public function isFavicon() public function isFavicon()
{ {
return $this->getUri() === 'favicon.ico'; return $this->getUri() === 'favicon.ico';
} }
/** /**
* @return mixed * @return mixed
*/ */
public function getIdentity() public function getIdentity()
{ {
return $this->_grant; return $this->_grant;
} }
/** /**
* @return bool * @return bool
*/ */
public function isHead() public function isHead()
{ {
$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) public function setStatus($status)
{ {
return $this->statusCode = $status; return $this->statusCode = $status;
} }
/** /**
* @return int * @return int
*/ */
public function getStatus() public function getStatus()
{ {
return $this->statusCode; return $this->statusCode;
} }
/** /**
* @return bool * @return bool
*/ */
public function getIsPackage() public function getIsPackage()
{ {
return $this->headers->getHeader('request_method') == 'package'; return $this->headers->getHeader('request_method') == 'package';
} }
/** /**
* @return bool * @return bool
*/ */
public function getIsReceive() public function getIsReceive()
{ {
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() public function hasGrant()
{ {
return $this->_grant !== null; return $this->_grant !== null;
} }
/** /**
* @return string * @return string
*/ */
public function parseUri() public function parseUri()
{ {
$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() public function getExplode()
{ {
return $this->explode; return $this->explode;
} }
/** /**
* @return mixed|string * @return mixed|string
*/ */
public function getCurrent() public function getCurrent()
{ {
return current($this->explode); return current($this->explode);
} }
/** /**
* @return string * @return string
*/ */
public function getUri() public function getUri()
{ {
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|string * @return mixed|string
* @throws Exception * @throws Exception
*/ */
public function adapter() public function adapter()
{ {
if (!$this->isHead()) { if (!$this->isHead()) {
return router()->runHandler(); return router()->runHandler();
} }
return ''; return '';
} }
/** /**
* @return string|null * @return string|null
*/ */
public function getPlatform() public function getPlatform()
{ {
$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() public function isIos()
{ {
return $this->getPlatform() == static::PLATFORM_IPHONE; return $this->getPlatform() == static::PLATFORM_IPHONE;
} }
/** /**
* @return bool * @return bool
*/ */
public function isAndroid() public function isAndroid()
{ {
return $this->getPlatform() == static::PLATFORM_ANDROID; return $this->getPlatform() == static::PLATFORM_ANDROID;
} }
/** /**
* @return bool * @return bool
*/ */
public function isMacOs() public function isMacOs()
{ {
return $this->getPlatform() == static::PLATFORM_MAC_OX; return $this->getPlatform() == static::PLATFORM_MAC_OX;
} }
/** /**
* @return bool * @return bool
*/ */
public function isWindows() public function isWindows()
{ {
return $this->getPlatform() == static::PLATFORM_WINDOWS; return $this->getPlatform() == static::PLATFORM_WINDOWS;
} }
/** /**
* @return bool * @return bool
*/ */
public function getIsPost() public function getIsPost()
{ {
return $this->getMethod() == 'post'; return $this->getMethod() == 'post';
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function getIsHttp() public function getIsHttp()
{ {
return true; return true;
} }
/** /**
* @return bool * @return bool
*/ */
public function getIsOption() public function getIsOption()
{ {
return $this->getMethod() == 'options'; return $this->getMethod() == 'options';
} }
/** /**
* @return bool * @return bool
*/ */
public function getIsGet() public function getIsGet()
{ {
return $this->getMethod() == 'get'; return $this->getMethod() == 'get';
} }
/** /**
* @return bool * @return bool
*/ */
public function getIsDelete() public function getIsDelete()
{ {
return $this->getMethod() == 'delete'; return $this->getMethod() == 'delete';
} }
/** /**
* @return string * @return string
* *
* 获取请求类型 * 获取请求类型
*/ */
public function getMethod() public function getMethod()
{ {
$head = $this->headers->getHeader('request_method'); $head = $this->headers->getHeader('request_method');
return strtolower($head); return strtolower($head);
} }
/** /**
* @return bool * @return bool
*/ */
public function getIsCli() public function getIsCli()
{ {
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() public function getIp()
{ {
$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() public function getRuntime()
{ {
return sprintf('%.5f', microtime(TRUE) - $this->startTime); return sprintf('%.5f', microtime(TRUE) - $this->startTime);
} }
/** /**
* @return string * @return string
*/ */
public function getDebug() public function getDebug()
{ {
$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 $request * @param $router
* @return Request * @return bool
*/ */
public static function create($request) public function is($router)
{ {
$sRequest = new Request(); return $this->getUri() == trim($router, '/');
$sRequest->fd = $request->fd; }
$sRequest->startTime = microtime(true);
$sRequest->params = new HttpParams(Help::toArray($request->rawContent()), $request->get, $request->files); /**
if (!empty($request->post)) { * @param $router
$sRequest->params->setPosts($request->post ?? []); * @return bool
} */
$headers = $request->server; public function isNotFound()
if (!empty($request->header)) { {
$headers = array_merge($headers, $request->header); return JSON::to(404, 'Page ' . $this->getUri() . ' not found.');
} }
$sRequest->headers = new HttpHeaders($headers);
$sRequest->parseUri();
return $sRequest; /**
} * @param $request
* @return Request
*/
public static function create($request)
{
$sRequest = new Request();
$sRequest->fd = $request->fd;
$sRequest->startTime = microtime(true);
$sRequest->params = new HttpParams(Help::toArray($request->rawContent()), $request->get, $request->files);
if (!empty($request->post)) {
$sRequest->params->setPosts($request->post ?? []);
}
$headers = $request->server;
if (!empty($request->header)) {
$headers = array_merge($headers, $request->header);
}
$sRequest->headers = new HttpHeaders($headers);
$sRequest->parseUri();
return $sRequest;
}
} }