upgraded dependencies
This commit is contained in:
115
vendor/fruitcake/laravel-cors/src/CorsServiceProvider.php
vendored
Normal file
115
vendor/fruitcake/laravel-cors/src/CorsServiceProvider.php
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Fruitcake\Cors;
|
||||
|
||||
use Asm89\Stack\CorsService;
|
||||
use Illuminate\Foundation\Application as LaravelApplication;
|
||||
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
|
||||
use Laravel\Lumen\Application as LumenApplication;
|
||||
use Illuminate\Foundation\Http\Events\RequestHandled;
|
||||
|
||||
class CorsServiceProvider extends BaseServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->mergeConfigFrom($this->configPath(), 'cors');
|
||||
|
||||
$this->app->singleton(CorsService::class, function ($app) {
|
||||
return new CorsService($this->corsOptions(), $app);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the config for publishing
|
||||
*
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
|
||||
$this->publishes([$this->configPath() => config_path('cors.php')], 'cors');
|
||||
} elseif ($this->app instanceof LumenApplication) {
|
||||
$this->app->configure('cors');
|
||||
}
|
||||
|
||||
// Add the headers on the Request Handled event as fallback in case of exceptions
|
||||
if (class_exists(RequestHandled::class) && $this->app->bound('events')) {
|
||||
$this->app->make('events')->listen(RequestHandled::class, function (RequestHandled $event) {
|
||||
$this->app->make(HandleCors::class)->onRequestHandled($event);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the config path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function configPath()
|
||||
{
|
||||
return __DIR__ . '/../config/cors.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get options for CorsService
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function corsOptions()
|
||||
{
|
||||
$config = $this->app['config']->get('cors');
|
||||
|
||||
if ($config['exposed_headers'] && !is_array($config['exposed_headers'])) {
|
||||
throw new \RuntimeException('CORS config `exposed_headers` should be `false` or an array');
|
||||
}
|
||||
|
||||
foreach (['allowed_origins', 'allowed_origins_patterns', 'allowed_headers', 'allowed_methods'] as $key) {
|
||||
if (!is_array($config[$key])) {
|
||||
throw new \RuntimeException('CORS config `' . $key . '` should be an array');
|
||||
}
|
||||
}
|
||||
|
||||
// Convert case to supported options
|
||||
$options = [
|
||||
'supportsCredentials' => $config['supports_credentials'],
|
||||
'allowedOrigins' => $config['allowed_origins'],
|
||||
'allowedOriginsPatterns' => $config['allowed_origins_patterns'],
|
||||
'allowedHeaders' => $config['allowed_headers'],
|
||||
'allowedMethods' => $config['allowed_methods'],
|
||||
'exposedHeaders' => $config['exposed_headers'],
|
||||
'maxAge' => $config['max_age'],
|
||||
];
|
||||
|
||||
// Transform wildcard pattern
|
||||
foreach ($options['allowedOrigins'] as $origin) {
|
||||
if (strpos($origin, '*') !== false) {
|
||||
$options['allowedOriginsPatterns'][] = $this->convertWildcardToPattern($origin);
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a pattern for a wildcard, based on Str::is() from Laravel
|
||||
*
|
||||
* @see https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Str.php
|
||||
* @param string $pattern
|
||||
* @return string
|
||||
*/
|
||||
protected function convertWildcardToPattern($pattern)
|
||||
{
|
||||
$pattern = preg_quote($pattern, '#');
|
||||
|
||||
// Asterisks are translated into zero-or-more regular expression wildcards
|
||||
// to make it convenient to check if the strings starts with the given
|
||||
// pattern such as "library/*", making any string check convenient.
|
||||
$pattern = str_replace('\*', '.*', $pattern);
|
||||
|
||||
return '#^' . $pattern . '\z#u';
|
||||
}
|
||||
}
|
||||
145
vendor/fruitcake/laravel-cors/src/HandleCors.php
vendored
Normal file
145
vendor/fruitcake/laravel-cors/src/HandleCors.php
vendored
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Fruitcake\Cors;
|
||||
|
||||
use Closure;
|
||||
use Asm89\Stack\CorsService;
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Foundation\Http\Events\RequestHandled;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class HandleCors
|
||||
{
|
||||
/** @var CorsService $cors */
|
||||
protected $cors;
|
||||
|
||||
/** @var \Illuminate\Contracts\Container\Container $container */
|
||||
protected $container;
|
||||
|
||||
public function __construct(CorsService $cors, Container $container)
|
||||
{
|
||||
$this->cors = $cors;
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request. Based on Asm89\Stack\Cors by asm89
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// Check if we're dealing with CORS and if we should handle it
|
||||
if (! $this->shouldRun($request)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// For Preflight, return the Preflight response
|
||||
if ($this->cors->isPreflightRequest($request)) {
|
||||
$response = $this->cors->handlePreflightRequest($request);
|
||||
|
||||
$this->cors->varyHeader($response, 'Access-Control-Request-Method');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
// Handle the request
|
||||
$response = $next($request);
|
||||
|
||||
if ($request->getMethod() === 'OPTIONS') {
|
||||
$this->cors->varyHeader($response, 'Access-Control-Request-Method');
|
||||
}
|
||||
|
||||
return $this->addHeaders($request, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the headers to the Response, if they don't exist yet.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @return Response
|
||||
*/
|
||||
protected function addHeaders(Request $request, Response $response): Response
|
||||
{
|
||||
if (! $response->headers->has('Access-Control-Allow-Origin')) {
|
||||
// Add the CORS headers to the Response
|
||||
$response = $this->cors->addActualRequestHeaders($response, $request);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the headers to the Response, if they don't exist yet.
|
||||
*
|
||||
* @param RequestHandled $event
|
||||
* @deprecated
|
||||
*/
|
||||
public function onRequestHandled(RequestHandled $event)
|
||||
{
|
||||
if ($this->shouldRun($event->request) && $this->container->make(Kernel::class)->hasMiddleware(static::class)) {
|
||||
$this->addHeaders($event->request, $event->response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the request has a URI that should pass through the CORS flow.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return bool
|
||||
*/
|
||||
protected function shouldRun(Request $request): bool
|
||||
{
|
||||
return $this->isMatchingPath($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* The the path from the config, to see if the CORS Service should run
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return bool
|
||||
*/
|
||||
protected function isMatchingPath(Request $request): bool
|
||||
{
|
||||
// Get the paths from the config or the middleware
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Paths by given host or string values in config by default
|
||||
*
|
||||
* @param string $host
|
||||
* @return array
|
||||
*/
|
||||
protected function getPathsByHost(string $host)
|
||||
{
|
||||
$paths = $this->container['config']->get('cors.paths', []);
|
||||
// If where are paths by given host
|
||||
if (isset($paths[$host])) {
|
||||
return $paths[$host];
|
||||
}
|
||||
// Defaults
|
||||
return array_filter($paths, function ($path) {
|
||||
return is_string($path);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user