Skip to content

Commit

Permalink
refactor:使用更合理的方案处理 OPTIONS 请求
Browse files Browse the repository at this point in the history
  • Loading branch information
build-admin committed Jul 3, 2024
1 parent cf7b998 commit e4948d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
4 changes: 4 additions & 0 deletions app/common/middleware/AllowCrossDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public function handle(Request $request, Closure $next, ?array $header = []): Re
}
}

if ($request->isOptions()) {
return response('', 204, $header);
}

$request->allowCrossDomainHeaders = $header;

return $next($request)->header($header);
Expand Down
27 changes: 7 additions & 20 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@
// [ 应用入口文件 ]
namespace think;

/*
* 不在tp加载后判断,为了安全的使用exit()
* 使用绝对路径,确保花里胡哨的url均能正确判定和跳转
*/
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 86400");
header("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: " . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
header("Access-Control-Allow-Origin: *");
exit();
}

$rootPath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR;
$server = isset($_REQUEST['server']) || isset($_SERVER['HTTP_SERVER']) || substr($_SERVER['REQUEST_URI'], 1, 9) == 'index.php';
$server = isset($_REQUEST['server']) || isset($_SERVER['HTTP_SERVER']) || substr($_SERVER['REQUEST_URI'], 1, 9) == 'index.php' || $_SERVER['REQUEST_METHOD'] == 'OPTIONS';
if (!$server) {
// 用户访问前端
/*
* 用户访问前端
* 不在tp加载后判断,为了安全的使用 exit()(常驻内存运行时不走本文件)
*/
$rootPath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR;

// 安装检测-s
if (!is_file($rootPath . 'install.lock') && is_file($rootPath . 'install' . DIRECTORY_SEPARATOR . 'index.html')) {
Expand All @@ -37,10 +27,7 @@
}
// 安装检测-e

/*
* 检测是否已编译前端-s
* 如果存在 index.html 则访问 index.html
*/
// 检测是否已编译前端(如果存在 index.html,则访问)-s
if (is_file($rootPath . 'index.html')) {
header("location:" . DIRECTORY_SEPARATOR . 'index.html');
exit();
Expand Down

0 comments on commit e4948d8

Please sign in to comment.