Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Http\Middleware;
4
 
5
use Closure;
6
use Fruitcake\Cors\CorsService;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Http\Request;
9
 
10
class HandleCors
11
{
12
    /**
13
     * The container instance.
14
     *
15
     * @var \Illuminate\Contracts\Container\Container
16
     */
17
    protected $container;
18
 
19
    /**
20
     * The CORS service instance.
21
     *
22
     * @var \Fruitcake\Cors\CorsService
23
     */
24
    protected $cors;
25
 
26
    /**
27
     * Create a new middleware instance.
28
     *
29
     * @param  \Illuminate\Contracts\Container\Container  $container
30
     * @param  \Fruitcake\Cors\CorsService  $cors
31
     * @return void
32
     */
33
    public function __construct(Container $container, CorsService $cors)
34
    {
35
        $this->container = $container;
36
        $this->cors = $cors;
37
    }
38
 
39
    /**
40
     * Handle the incoming request.
41
     *
42
     * @param  \Illuminate\Http\Request  $request
43
     * @param  \Closure  $next
44
     * @return \Illuminate\Http\Response
45
     */
46
    public function handle($request, Closure $next)
47
    {
48
        if (! $this->hasMatchingPath($request)) {
49
            return $next($request);
50
        }
51
 
52
        $this->cors->setOptions($this->container['config']->get('cors', []));
53
 
54
        if ($this->cors->isPreflightRequest($request)) {
55
            $response = $this->cors->handlePreflightRequest($request);
56
 
57
            $this->cors->varyHeader($response, 'Access-Control-Request-Method');
58
 
59
            return $response;
60
        }
61
 
62
        $response = $next($request);
63
 
64
        if ($request->getMethod() === 'OPTIONS') {
65
            $this->cors->varyHeader($response, 'Access-Control-Request-Method');
66
        }
67
 
68
        return $this->cors->addActualRequestHeaders($response, $request);
69
    }
70
 
71
    /**
72
     * Get the path from the configuration to determine if the CORS service should run.
73
     *
74
     * @param  \Illuminate\Http\Request  $request
75
     * @return bool
76
     */
77
    protected function hasMatchingPath(Request $request): bool
78
    {
79
        $paths = $this->getPathsByHost($request->getHost());
80
 
81
        foreach ($paths as $path) {
82
            if ($path !== '/') {
83
                $path = trim($path, '/');
84
            }
85
 
86
            if ($request->fullUrlIs($path) || $request->is($path)) {
87
                return true;
88
            }
89
        }
90
 
91
        return false;
92
    }
93
 
94
    /**
95
     * Get the CORS paths for the given host.
96
     *
97
     * @param  string  $host
98
     * @return array
99
     */
100
    protected function getPathsByHost(string $host)
101
    {
102
        $paths = $this->container['config']->get('cors.paths', []);
103
 
104
        if (isset($paths[$host])) {
105
            return $paths[$host];
106
        }
107
 
108
        return array_filter($paths, function ($path) {
109
            return is_string($path);
110
        });
111
    }
112
}