From 3cc8d983f3b15c79e7e76ddcade37180a1a2e81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=99=E7=A0=81=E7=94=9F=E8=8A=B1?= <18523774412@qq.com> Date: Mon, 11 Mar 2024 13:48:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E4=BD=BF=E7=94=A8=E6=9B=B4=E5=90=88?= =?UTF-8?q?=E9=80=82=E7=9A=84=E5=8A=9E=E6=B3=95=E8=AE=BE=E7=BD=AE=E8=B7=A8?= =?UTF-8?q?=E5=9F=9F=E7=9B=B8=E5=85=B3=E7=9A=84=E5=93=8D=E5=BA=94=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/middleware/AllowCrossDomain.php | 6 ++++-- extend/ba/Terminal.php | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/common/middleware/AllowCrossDomain.php b/app/common/middleware/AllowCrossDomain.php index 48dea529..4709cbb2 100644 --- a/app/common/middleware/AllowCrossDomain.php +++ b/app/common/middleware/AllowCrossDomain.php @@ -43,7 +43,7 @@ class AllowCrossDomain $header = !empty($header) ? array_merge($this->header, $header) : $this->header; $origin = $request->header('origin'); - if ($origin) { + if ($origin && !isset($header['Access-Control-Allow-Origin'])) { $info = parse_url($origin); // 获取跨域配置 @@ -51,10 +51,12 @@ class AllowCrossDomain $corsDomain[] = $request->host(true); if (in_array("*", $corsDomain) || in_array($origin, $corsDomain) || (isset($info['host']) && in_array($info['host'], $corsDomain))) { - header("Access-Control-Allow-Origin: " . $origin); + $header['Access-Control-Allow-Origin'] = $origin; } } + $request->allowCrossDomainHeaders = $header; + return $next($request)->header($header); } } diff --git a/extend/ba/Terminal.php b/extend/ba/Terminal.php index a0e839c7..d0a831af 100644 --- a/extend/ba/Terminal.php +++ b/extend/ba/Terminal.php @@ -170,9 +170,14 @@ class Terminal */ public function exec(bool $authentication = true): void { - header('X-Accel-Buffering: no'); - header('Content-Type: text/event-stream'); - header('Cache-Control: no-cache'); + $headers = request()->allowCrossDomainHeaders ?? []; + $headers['X-Accel-Buffering'] = 'no'; + $headers['Content-Type'] = 'text/event-stream'; + $headers['Cache-Control'] = 'no-cache'; + + foreach ($headers as $name => $val) { + header($name . (!is_null($val) ? ':' . $val : '')); + } while (ob_get_level()) { ob_end_clean();