This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php namespace Barryvdh\Cors\Middleware; | ||
|
||
use Closure; | ||
use Asm89\Stack\CorsService; | ||
use Illuminate\Contracts\Routing\Middleware; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class HandleCors implements Middleware { | ||
|
||
/** | ||
* @param CorsService $cors | ||
*/ | ||
public function __construct(CorsService $cors) | ||
{ | ||
$this->cors = $cors; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. Based on Asm89\Stack\Cors by asm89 | ||
* @see https://github.com/asm89/stack-cors/blob/master/src/Asm89/Stack/Cors.php | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if ( | ||
! $this->cors->isCorsRequest($request) | ||
|| $request->headers->get('Origin') == $request->getSchemeAndHttpHost() | ||
) { | ||
return $next($request); | ||
} | ||
|
||
if ($this->cors->isPreflightRequest($request)) { | ||
return $this->cors->handlePreflightRequest($request); | ||
} | ||
|
||
if ( ! $this->cors->isActualRequestAllowed($request)) { | ||
return new Response('Not allowed.', 403); | ||
} | ||
|
||
$response = $next($request); | ||
|
||
return $this->cors->addActualRequestHeaders($response, $request); | ||
} | ||
} |