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\Events;
|
|
|
|
|
|
|
|
|
|
|
2020-09-02 11:38:47 +08:00
|
|
|
use Exception;
|
2020-09-04 01:05:33 +08:00
|
|
|
use HttpServer\Abstracts\Callback;
|
2020-12-16 17:48:01 +08:00
|
|
|
use HttpServer\Exception\ExitException;
|
2020-08-31 01:27:08 +08:00
|
|
|
use HttpServer\Http\Request as HRequest;
|
|
|
|
|
use HttpServer\Http\Response as HResponse;
|
2020-12-17 10:12:54 +08:00
|
|
|
use ReflectionException;
|
2021-01-07 14:09:59 +08:00
|
|
|
use Snowflake\Core\Json;
|
2020-09-02 15:45:52 +08:00
|
|
|
use Snowflake\Event;
|
2020-11-27 14:57:58 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
2020-12-17 10:12:54 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
use Swoole\Error;
|
|
|
|
|
use Swoole\Http\Request;
|
|
|
|
|
use Swoole\Http\Response;
|
|
|
|
|
|
2020-09-02 11:38:47 +08:00
|
|
|
/**
|
|
|
|
|
* Class OnRequest
|
|
|
|
|
* @package HttpServer\Events
|
|
|
|
|
*/
|
|
|
|
|
class OnRequest extends Callback
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param Response $response
|
2020-09-02 18:43:23 +08:00
|
|
|
* @return void
|
2020-09-02 11:38:47 +08:00
|
|
|
* @throws Exception
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-01-07 14:09:59 +08:00
|
|
|
public function onHandler(Request $request, Response $response): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
try {
|
2021-01-04 18:21:37 +08:00
|
|
|
[$req, $rep] = static::create($request, $response);
|
|
|
|
|
if ($req->is('favicon.ico')) {
|
2021-01-07 14:09:59 +08:00
|
|
|
return \send(null, 404);
|
2021-01-04 18:21:37 +08:00
|
|
|
}
|
2021-01-07 15:01:06 +08:00
|
|
|
return \router()->dispatch();
|
2021-01-04 18:21:37 +08:00
|
|
|
} catch (ExitException | Error | \Throwable $exception) {
|
|
|
|
|
if ($exception instanceof ExitException) {
|
2021-01-07 14:09:59 +08:00
|
|
|
return \send($exception->getMessage(), $exception->getCode());
|
2020-09-02 18:43:23 +08:00
|
|
|
}
|
2021-01-07 15:01:06 +08:00
|
|
|
return $this->sendErrorMessage($exception);
|
2021-02-20 15:21:42 +08:00
|
|
|
} finally {
|
|
|
|
|
write(Json::encode(get_object_vars($request)), 'request');
|
|
|
|
|
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
2020-09-02 15:45:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 14:26:34 +08:00
|
|
|
|
2020-12-17 10:12:54 +08:00
|
|
|
/**
|
|
|
|
|
* @param $request
|
|
|
|
|
* @param $response
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
2021-01-04 18:21:37 +08:00
|
|
|
public static function create($request, $response): array
|
2020-12-17 10:12:54 +08:00
|
|
|
{
|
|
|
|
|
return [HRequest::create($request), HResponse::create($response)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-10 13:42:08 +08:00
|
|
|
/**
|
|
|
|
|
* @param $response
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static function shutdown($response)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2020-11-27 15:06:25 +08:00
|
|
|
$error = error_get_last();
|
|
|
|
|
if (!isset($error['type'])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$types = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR];
|
|
|
|
|
if (!in_array($error['type'], $types)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-10 13:42:08 +08:00
|
|
|
$message = $error['message'] . ':' . microtime(true);
|
|
|
|
|
if ($response instanceof Response) {
|
|
|
|
|
$response->status(500);
|
|
|
|
|
$response->end($message);
|
|
|
|
|
}
|
|
|
|
|
} catch (\ErrorException $exception) {
|
2021-02-20 13:08:54 +08:00
|
|
|
$logger = Snowflake::app()->getLogger();
|
2020-09-10 13:42:08 +08:00
|
|
|
$logger->write($exception->getMessage(), 'shutdown');
|
2020-11-27 15:06:25 +08:00
|
|
|
} finally {
|
|
|
|
|
unset($response);
|
2020-09-10 13:42:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-02 15:45:52 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $exception
|
2020-12-16 16:59:33 +08:00
|
|
|
* @return bool|string
|
2020-12-02 11:36:45 +08:00
|
|
|
* @throws ComponentException
|
2020-09-02 15:45:52 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-16 16:59:33 +08:00
|
|
|
protected function sendErrorMessage($exception): bool|string
|
2020-09-02 15:45:52 +08:00
|
|
|
{
|
2020-12-16 16:59:33 +08:00
|
|
|
|
|
|
|
|
$sRequest = \request();
|
|
|
|
|
$sResponse = \response();
|
|
|
|
|
|
|
|
|
|
$sResponse->addHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
|
$sResponse->addHeader('Access-Control-Allow-Headers', $sRequest->headers->get('access-control-request-headers'));
|
|
|
|
|
$sResponse->addHeader('Access-Control-Request-Method', $sRequest->headers->get('access-control-request-method'));
|
|
|
|
|
|
2020-09-09 12:08:07 +08:00
|
|
|
$params = Snowflake::app()->getLogger()->exception($exception);
|
2020-12-16 16:59:33 +08:00
|
|
|
return $sResponse->send($params, 200);
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|