refactor:使用更合适的办法设置跨域相关的响应头

This commit is contained in:
妙码生花 2024-03-11 13:48:26 +08:00
parent 2e1aedf57d
commit 3cc8d983f3
2 changed files with 12 additions and 5 deletions

View File

@ -43,7 +43,7 @@ class AllowCrossDomain
$header = !empty($header) ? array_merge($this->header, $header) : $this->header; $header = !empty($header) ? array_merge($this->header, $header) : $this->header;
$origin = $request->header('origin'); $origin = $request->header('origin');
if ($origin) { if ($origin && !isset($header['Access-Control-Allow-Origin'])) {
$info = parse_url($origin); $info = parse_url($origin);
// 获取跨域配置 // 获取跨域配置
@ -51,10 +51,12 @@ class AllowCrossDomain
$corsDomain[] = $request->host(true); $corsDomain[] = $request->host(true);
if (in_array("*", $corsDomain) || in_array($origin, $corsDomain) || (isset($info['host']) && in_array($info['host'], $corsDomain))) { 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); return $next($request)->header($header);
} }
} }

View File

@ -170,9 +170,14 @@ class Terminal
*/ */
public function exec(bool $authentication = true): void public function exec(bool $authentication = true): void
{ {
header('X-Accel-Buffering: no'); $headers = request()->allowCrossDomainHeaders ?? [];
header('Content-Type: text/event-stream'); $headers['X-Accel-Buffering'] = 'no';
header('Cache-Control: no-cache'); $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()) { while (ob_get_level()) {
ob_end_clean(); ob_end_clean();