mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-24 00:55:35 +08:00
add static file server and fix root mapping bug
This commit is contained in:
@@ -136,5 +136,6 @@
|
|||||||
"xsl": "text/xml",
|
"xsl": "text/xml",
|
||||||
"xwd": "image/x-xwindowdump",
|
"xwd": "image/x-xwindowdump",
|
||||||
"xyz": "chemical/x-xyz",
|
"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 */
|
/** 上下文接口类 implemented from ContextInterface */
|
||||||
$config['context_class'] = \ZM\Context\Context::class;
|
$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;
|
return $config;
|
||||||
|
|||||||
@@ -3,29 +3,17 @@
|
|||||||
|
|
||||||
namespace ZM\Annotation;
|
namespace ZM\Annotation;
|
||||||
|
|
||||||
use Doctrine\Common\Annotations\AnnotationException;
|
use Doctrine\Common\Annotations\{AnnotationException, AnnotationReader};
|
||||||
use Doctrine\Common\Annotations\AnnotationReader;
|
use Framework\{Console, ZMBuf};
|
||||||
use Framework\Console;
|
|
||||||
use Framework\ZMBuf;
|
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use ReflectionMethod;
|
use ReflectionMethod;
|
||||||
use ZM\Annotation\CQ\{CQAfter, CQBefore, CQCommand, CQMessage, CQMetaEvent, CQNotice, CQRequest};
|
use ZM\Annotation\CQ\{CQAfter, CQBefore, CQCommand, CQMessage, CQMetaEvent, CQNotice, CQRequest};
|
||||||
use ZM\Annotation\Http\After;
|
use ZM\Annotation\Http\{After, Before, Controller, HandleException, Middleware, MiddlewareClass, RequestMapping};
|
||||||
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\Interfaces\CustomAnnotation;
|
use ZM\Annotation\Interfaces\CustomAnnotation;
|
||||||
use ZM\Annotation\Interfaces\Level;
|
use ZM\Annotation\Interfaces\Level;
|
||||||
use ZM\Annotation\Module\Closed;
|
use ZM\Annotation\Module\{Closed, InitBuffer, SaveBuffer};
|
||||||
use ZM\Annotation\Module\InitBuffer;
|
use ZM\Annotation\Swoole\{OnStart, SwooleEventAfter, SwooleEventAt};
|
||||||
use ZM\Annotation\Module\SaveBuffer;
|
|
||||||
use ZM\Annotation\Swoole\OnStart;
|
|
||||||
use ZM\Annotation\Swoole\SwooleEventAfter;
|
|
||||||
use ZM\Annotation\Swoole\SwooleEventAt;
|
|
||||||
use ZM\Annotation\Interfaces\Rule;
|
use ZM\Annotation\Interfaces\Rule;
|
||||||
use ZM\Connection\WSConnection;
|
use ZM\Connection\WSConnection;
|
||||||
use ZM\Http\MiddlewareInterface;
|
use ZM\Http\MiddlewareInterface;
|
||||||
@@ -244,9 +232,9 @@ class AnnotationParser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($prefix_exp == [] && $route_exp == []) {
|
if ($prefix_exp == [] && $route_exp == []) {
|
||||||
$array[$uid - 1]['method'] = $method->getName();
|
$array[0]['method'] = $method->getName();
|
||||||
$array[$uid - 1]['class'] = $class->getName();
|
$array[0]['class'] = $class->getName();
|
||||||
$array[$uid - 1]['request_method'] = $vss->request_method;
|
$array[0]['request_method'] = $vss->request_method;
|
||||||
ZMBuf::$req_mapping = $array;
|
ZMBuf::$req_mapping = $array;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ namespace ZM\Event\Swoole;
|
|||||||
use Closure;
|
use Closure;
|
||||||
use Doctrine\Common\Annotations\AnnotationException;
|
use Doctrine\Common\Annotations\AnnotationException;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Framework\Console;
|
||||||
use Framework\ZMBuf;
|
use Framework\ZMBuf;
|
||||||
use Swoole\Http\Request;
|
use Swoole\Http\Request;
|
||||||
use ZM\Annotation\Http\MiddlewareClass;
|
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);
|
$this->responseStatus(404);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -94,7 +126,7 @@ class RequestEvent implements SwooleEvent
|
|||||||
$c_name = $node["class"];
|
$c_name = $node["class"];
|
||||||
if (isset(ZMBuf::$events[MiddlewareInterface::class][$c_name][$node["method"]])) {
|
if (isset(ZMBuf::$events[MiddlewareInterface::class][$c_name][$node["method"]])) {
|
||||||
$middleware = 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];
|
$middleware = ZMBuf::$events[MiddlewareClass::class][$middleware];
|
||||||
$before = $middleware["class"];
|
$before = $middleware["class"];
|
||||||
$r = new $before();
|
$r = new $before();
|
||||||
@@ -112,8 +144,8 @@ class RequestEvent implements SwooleEvent
|
|||||||
if (!$this->response->isEnd()) goto eventCall;
|
if (!$this->response->isEnd()) goto eventCall;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
if (!isset($middleware["exceptions"])) throw $e;
|
if (!isset($middleware["exceptions"])) throw $e;
|
||||||
foreach($middleware["exceptions"] as $name => $method) {
|
foreach ($middleware["exceptions"] as $name => $method) {
|
||||||
if($e instanceof $name) {
|
if ($e instanceof $name) {
|
||||||
call_user_func([$r, $method], $e, $this->request, $this->response, $params);
|
call_user_func([$r, $method], $e, $this->request, $this->response, $params);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user