mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-17 20:54:52 +08:00
add static file server and fix root mapping bug
This commit is contained in:
parent
c632977ca1
commit
920680ee3d
@ -136,5 +136,6 @@
|
||||
"xsl": "text/xml",
|
||||
"xwd": "image/x-xwindowdump",
|
||||
"xyz": "chemical/x-xyz",
|
||||
"zip": "application/zip"
|
||||
}
|
||||
"zip": "application/zip",
|
||||
"": "application/octet-stream"
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user