您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-靜態文件-靜態文件中間件
发布时间:2022-01-24 15:44:55编辑:雪饮阅读()
webman自带一个静态文件中间件,位置app/middleware/StaticFile.php。
有时我们需要对静态文件做一些处理,例如给静态文件增加跨域http头,禁止访问以点(.)开头的文件可以使用这个中间件。
靜態中間件:app/middleware/StaticFile.php中内容大概如:
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
/**
* Class StaticFile
* @package app\middleware
*/
class StaticFile implements MiddlewareInterface
{
public function process(Request $request, callable $next) : Response
{
// Access to files beginning with. Is prohibited
if (strpos($request->path(), '/.') !== false) {
return response('<h1>403 forbidden</h1>', 403);
}
/** @var Response $response */
$response = $next($request);
// Add cross domain HTTP header
/*$response->withHeaders([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => 'true',
]);*/
return $response;
}
}
那麽假如我要開啓這個靜態中間件,則config/static.php:
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Static file settings
*/
return [
'enable' => true,
'middleware' => [ // Static file Middleware
app\middleware\StaticFile::class,
],
];
此時若我在public2(我這裏自定義了public目錄)中建立一個.ini文件,則直接訪問是不可行的,因爲上面中間件process方法中的if語句地方就是判斷以.開頭的文件名的,會返回403
[root@localhost ~]# wget http://192.168.1.10:8787/.ini
--2022-01-24 15:38:06-- http://192.168.1.10:8787/.ini
Connecting to 192.168.1.10:8787... connected.
HTTP request sent, awaiting response... 403 Forbidden
2022-01-24 15:38:06 ERROR 403: Forbidden.
那麽此時恢復config/static.php的内容為其默認内容:
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Static file settings
*/
return [
'enable' => true,
'middleware' => [ // Static file Middleware
// app\middleware\StaticFile::class,
],
];
也就是不使用靜態中間件,則再次訪問public2目錄中的.ini文件:
[root@localhost ~]# wget http://192.168.1.10:8787/.ini
--2022-01-24 15:38:20-- http://192.168.1.10:8787/.ini
Connecting to 192.168.1.10:8787... connected.
HTTP request sent, awaiting response... 200 OK
Length: 0 [application/octet-stream]
Saving to: ‘.ini’
[ <=> ] 0 --.-K/s in 0s
2022-01-24 15:38:20 (0.00 B/s) - ‘.ini’ saved [0/0]
可以看到又可以訪問.開頭的文件名的文件了。
关键字词:webman,靜態,文件,中間件