update to 3.0.0-alpha2 (build 610): refactor driver

This commit is contained in:
crazywhalecc
2022-08-01 16:31:54 +08:00
committed by Jerry Ma
parent f2a12634a4
commit 41b058aeaf
186 changed files with 1922 additions and 15444 deletions

View File

@@ -1,110 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Utils;
use Swoole\Coroutine;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use ZM\Config\ZMConfig;
use ZM\Http\Response;
use ZM\Utils\Manager\RouteManager;
class HttpUtil
{
/** @noinspection PhpMissingReturnTypeInspection */
public static function parseUri($request, $response, $uri, &$node, &$params)
{
$context = new RequestContext();
$context->setMethod($request->server['request_method']);
try {
$matcher = new UrlMatcher(RouteManager::$routes ?? new RouteCollection(), $context);
$matched = $matcher->match($uri);
} catch (ResourceNotFoundException $e) {
if (ZMConfig::get('global', 'static_file_server')['status']) {
HttpUtil::handleStaticPage($request->server['request_uri'], $response);
return null;
}
$matched = null;
} catch (MethodNotAllowedException $e) {
$matched = null;
}
if ($matched !== null) {
$node = [
'route' => RouteManager::$routes->get($matched['_route'])->getPath(),
'class' => $matched['_class'],
'method' => $matched['_method'],
'request_method' => $request->server['request_method'],
];
unset($matched['_class'], $matched['_method']);
$params = $matched;
return true;
}
return false;
}
public static function getHttpCodePage(int $http_code)
{
if (isset(ZMConfig::get('global', 'http_default_code_page')[$http_code])) {
return Coroutine::readFile(DataProvider::getResourceFolder() . 'html/' . ZMConfig::get('global', 'http_default_code_page')[$http_code]);
}
return null;
}
/**
* @param Response|\Swoole\Http\Response $response
*/
public static function handleStaticPage(string $uri, $response, array $settings = []): bool
{
$base_dir = $settings['document_root'] ?? ZMConfig::get('global', 'static_file_server')['document_root'];
$base_index = $settings['document_index'] ?? ZMConfig::get('global', 'static_file_server')['document_index'];
$path = realpath($base_dir . urldecode($uri));
if ($path !== false) {
if (is_dir($path)) {
$path = $path . '/';
}
$work = realpath($base_dir) . '/';
if (strpos($path, $work) !== 0) {
logger()->info('[403] ' . $uri);
self::responseCodePage($response, 403);
return true;
}
if (is_dir($path)) {
if (mb_substr($uri, -1, 1) != '/') {
logger()->info('[302] ' . $uri);
$response->redirect($uri . '/', 302);
return true;
}
foreach ($base_index as $vp) {
if (is_file($path . '/' . $vp)) {
logger()->info('[200] ' . $uri);
$exp = strtolower(pathinfo($path . $vp)['extension'] ?? 'unknown');
$response->setHeader('Content-Type', ZMConfig::get('file_header')[$exp] ?? 'application/octet-stream');
$response->end(file_get_contents($path . $vp));
return true;
}
}
} elseif (is_file($path)) {
logger()->info('[200] ' . $uri);
$exp = strtolower(pathinfo($path)['extension'] ?? 'unknown');
$response->setHeader('Content-Type', ZMConfig::get('file_header')[$exp] ?? 'application/octet-stream');
$response->end(file_get_contents($path));
return true;
}
}
logger()->info('[404] ' . $uri);
self::responseCodePage($response, 404);
return true;
}
public static function responseCodePage($response, $code)
{
$response->status($code);
$response->end(self::getHttpCodePage($code));
}
}