From 920680ee3d6835356de10fc071fc62e7fb14ee03 Mon Sep 17 00:00:00 2001 From: whale Date: Sun, 26 Apr 2020 17:15:27 +0800 Subject: [PATCH] add static file server and fix root mapping bug --- config/file_header.json | 5 ++-- config/global.php | 9 ++++++ src/ZM/Annotation/AnnotationParser.php | 28 ++++++------------- src/ZM/Event/Swoole/RequestEvent.php | 38 ++++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/config/file_header.json b/config/file_header.json index 8c9caf4b..5feb6fe8 100644 --- a/config/file_header.json +++ b/config/file_header.json @@ -136,5 +136,6 @@ "xsl": "text/xml", "xwd": "image/x-xwindowdump", "xyz": "chemical/x-xyz", - "zip": "application/zip" -} \ No newline at end of file + "zip": "application/zip", + "": "application/octet-stream" +} diff --git a/config/global.php b/config/global.php index b64ef93d..883a6e59 100644 --- a/config/global.php +++ b/config/global.php @@ -67,4 +67,13 @@ $config['auto_save_interval'] = 900; /** 上下文接口类 implemented from ContextInterface */ $config['context_class'] = \ZM\Context\Context::class; +/** 静态文件访问 */ +$config['static_file_server'] = [ + 'status' => false, + 'document_root' => WORKING_DIR . '/resources/html', + 'document_index' => [ + 'index.html' + ] +]; + return $config; diff --git a/src/ZM/Annotation/AnnotationParser.php b/src/ZM/Annotation/AnnotationParser.php index a5d71da6..1471508b 100644 --- a/src/ZM/Annotation/AnnotationParser.php +++ b/src/ZM/Annotation/AnnotationParser.php @@ -3,29 +3,17 @@ namespace ZM\Annotation; -use Doctrine\Common\Annotations\AnnotationException; -use Doctrine\Common\Annotations\AnnotationReader; -use Framework\Console; -use Framework\ZMBuf; +use Doctrine\Common\Annotations\{AnnotationException, AnnotationReader}; +use Framework\{Console, ZMBuf}; use ReflectionClass; use ReflectionException; use ReflectionMethod; use ZM\Annotation\CQ\{CQAfter, CQBefore, CQCommand, CQMessage, CQMetaEvent, CQNotice, CQRequest}; -use ZM\Annotation\Http\After; -use ZM\Annotation\Http\Before; -use ZM\Annotation\Http\Controller; -use ZM\Annotation\Http\HandleException; -use ZM\Annotation\Http\Middleware; -use ZM\Annotation\Http\MiddlewareClass; -use ZM\Annotation\Http\RequestMapping; +use ZM\Annotation\Http\{After, Before, Controller, HandleException, Middleware, MiddlewareClass, RequestMapping}; use ZM\Annotation\Interfaces\CustomAnnotation; use ZM\Annotation\Interfaces\Level; -use ZM\Annotation\Module\Closed; -use ZM\Annotation\Module\InitBuffer; -use ZM\Annotation\Module\SaveBuffer; -use ZM\Annotation\Swoole\OnStart; -use ZM\Annotation\Swoole\SwooleEventAfter; -use ZM\Annotation\Swoole\SwooleEventAt; +use ZM\Annotation\Module\{Closed, InitBuffer, SaveBuffer}; +use ZM\Annotation\Swoole\{OnStart, SwooleEventAfter, SwooleEventAt}; use ZM\Annotation\Interfaces\Rule; use ZM\Connection\WSConnection; use ZM\Http\MiddlewareInterface; @@ -244,9 +232,9 @@ class AnnotationParser } } if ($prefix_exp == [] && $route_exp == []) { - $array[$uid - 1]['method'] = $method->getName(); - $array[$uid - 1]['class'] = $class->getName(); - $array[$uid - 1]['request_method'] = $vss->request_method; + $array[0]['method'] = $method->getName(); + $array[0]['class'] = $class->getName(); + $array[0]['request_method'] = $vss->request_method; ZMBuf::$req_mapping = $array; return; } diff --git a/src/ZM/Event/Swoole/RequestEvent.php b/src/ZM/Event/Swoole/RequestEvent.php index 6e56be3a..23fe9da8 100644 --- a/src/ZM/Event/Swoole/RequestEvent.php +++ b/src/ZM/Event/Swoole/RequestEvent.php @@ -7,6 +7,7 @@ namespace ZM\Event\Swoole; use Closure; use Doctrine\Common\Annotations\AnnotationException; use Exception; +use Framework\Console; use Framework\ZMBuf; use Swoole\Http\Request; use ZM\Annotation\Http\MiddlewareClass; @@ -84,6 +85,37 @@ class RequestEvent implements SwooleEvent } } } + if (ZMBuf::globals("static_file_server")["status"]) { + $base_dir = ZMBuf::globals("static_file_server")["document_root"]; + $base_index = ZMBuf::globals("static_file_server")["document_index"]; + $uri = $this->request->server["request_uri"]; + $path = realpath($base_dir . urldecode($uri)); + if ($path !== false) { + if (is_dir($path)) $path = $path . '/'; + $work = realpath(WORKING_DIR) . '/'; + if (strpos($path, $work) !== 0) { + $this->responseStatus(403); + return $this; + } + if (is_dir($path)) { + foreach ($base_index as $vp) { + if (is_file($path . $vp)) { + Console::info("[200] " . $uri . " (static)"); + $exp = strtolower(pathinfo($path . $vp)['extension'] ?? "unknown"); + $this->response->setHeader("Content-Type", ZMBuf::config("file_header")[$exp] ?? "application/octet-stream"); + $this->response->end(file_get_contents($path . $vp)); + return $this; + } + } + } elseif (is_file($path)) { + Console::info("[200] " . $uri . " (static)"); + $exp = strtolower(pathinfo($path)['extension'] ?? "unknown"); + $this->response->setHeader("Content-Type", ZMBuf::config("file_header")[$exp] ?? "application/octet-stream"); + $this->response->end(file_get_contents($path)); + return $this; + } + } + } $this->responseStatus(404); return $this; } @@ -94,7 +126,7 @@ class RequestEvent implements SwooleEvent $c_name = $node["class"]; if (isset(ZMBuf::$events[MiddlewareInterface::class][$c_name][$node["method"]])) { $middleware = ZMBuf::$events[MiddlewareInterface::class][$c_name][$node["method"]]; - if(!isset(ZMBuf::$events[MiddlewareClass::class][$middleware])) throw new AnnotationException("Annotation parse error: Unknown MiddlewareClass named \"{$middleware}\"!"); + if (!isset(ZMBuf::$events[MiddlewareClass::class][$middleware])) throw new AnnotationException("Annotation parse error: Unknown MiddlewareClass named \"{$middleware}\"!"); $middleware = ZMBuf::$events[MiddlewareClass::class][$middleware]; $before = $middleware["class"]; $r = new $before(); @@ -112,8 +144,8 @@ class RequestEvent implements SwooleEvent if (!$this->response->isEnd()) goto eventCall; } catch (Exception $e) { if (!isset($middleware["exceptions"])) throw $e; - foreach($middleware["exceptions"] as $name => $method) { - if($e instanceof $name) { + foreach ($middleware["exceptions"] as $name => $method) { + if ($e instanceof $name) { call_user_func([$r, $method], $e, $this->request, $this->response, $params); return $this; }