-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Integrate Laravel CORS into framework (#41137)
* Add HandleCors middleware * Port tests * Apply fixes from StyleCI * wip * wip * wip * wip * Update HandleCors.php Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
bb9de44
commit fc186a6
Showing
5 changed files
with
408 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Illuminate\Http\Middleware; | ||
|
||
use Closure; | ||
use Fruitcake\Cors\CorsService; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Http\Request; | ||
|
||
class HandleCors | ||
{ | ||
/** | ||
* The container instance. | ||
* | ||
* @var \Illuminate\Contracts\Container\Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* The CORS service instance. | ||
* | ||
* @var \Fruitcake\Cors\CorsService | ||
*/ | ||
protected $cors; | ||
|
||
/** | ||
* Create a new middleware instance. | ||
* | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @param \Fruitcake\Cors\CorsService $cors | ||
* @return void | ||
*/ | ||
public function __construct(Container $container, CorsService $cors) | ||
{ | ||
$this->container = $container; | ||
$this->cors = $cors; | ||
} | ||
|
||
/** | ||
* Handle the incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if (! $this->hasMatchingPath($request)) { | ||
return $next($request); | ||
} | ||
|
||
$this->cors->setOptions($this->container['config']->get('cors', [])); | ||
|
||
if ($this->cors->isPreflightRequest($request)) { | ||
$response = $this->cors->handlePreflightRequest($request); | ||
|
||
$this->cors->varyHeader($response, 'Access-Control-Request-Method'); | ||
|
||
return $response; | ||
} | ||
|
||
$response = $next($request); | ||
|
||
if ($request->getMethod() === 'OPTIONS') { | ||
$this->cors->varyHeader($response, 'Access-Control-Request-Method'); | ||
} | ||
|
||
return $this->cors->addActualRequestHeaders($response, $request); | ||
} | ||
|
||
/** | ||
* Get the path from the configuration to determine if the CORS service should run. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return bool | ||
*/ | ||
protected function hasMatchingPath(Request $request): bool | ||
{ | ||
$paths = $this->getPathsByHost($request->getHost()); | ||
|
||
foreach ($paths as $path) { | ||
if ($path !== '/') { | ||
$path = trim($path, '/'); | ||
} | ||
|
||
if ($request->fullUrlIs($path) || $request->is($path)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Get the CORS paths for the given host. | ||
* | ||
* @param string $host | ||
* @return array | ||
*/ | ||
protected function getPathsByHost(string $host) | ||
{ | ||
$paths = $this->container['config']->get('cors.paths', []); | ||
|
||
if (isset($paths[$host])) { | ||
return $paths[$host]; | ||
} | ||
|
||
return array_filter($paths, function ($path) { | ||
return is_string($path); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.