Skip to content

Commit 9229264

Browse files
committed
add trust hosts middleware
1 parent 4830831 commit 9229264

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Illuminate\Http\Middleware;
4+
5+
use Illuminate\Contracts\Foundation\Application;
6+
use Illuminate\Http\Request;
7+
8+
abstract class TrustHosts
9+
{
10+
/**
11+
* The application instance.
12+
*
13+
* @var \Illuminate\Contracts\Foundation\Application
14+
*/
15+
protected $app;
16+
17+
/**
18+
* Create a new middleware instance.
19+
*
20+
* @param \Illuminate\Contracts\Foundation\Application $app
21+
* @return void
22+
*/
23+
public function __construct(Application $app)
24+
{
25+
$this->app = $app;
26+
}
27+
28+
/**
29+
* Get the host patterns that should be trusted.
30+
*
31+
* @return array
32+
*/
33+
abstract public function hosts();
34+
35+
/**
36+
* Handle the incoming request.
37+
*
38+
* @param \Illuminate\Http\Request $request
39+
* @param callable $next
40+
* @return \Illuminate\Http\Response
41+
*/
42+
public function handle(Request $request, $next)
43+
{
44+
if ($this->shouldSpecifyTrustedHosts()) {
45+
Request::setTrustedHosts(array_filter($this->hosts()));
46+
}
47+
48+
return $next($request);
49+
}
50+
51+
/**
52+
* Determine if the application should specify trusted hosts.
53+
*
54+
* @return bool
55+
*/
56+
protected function shouldSpecifyTrustedHosts()
57+
{
58+
return config('app.env') !== 'local' &&
59+
! $this->app->runningUnitTests();
60+
}
61+
62+
/**
63+
* Get a regular expression matching the application URL and all of its subdomains.
64+
*
65+
* @return string|null
66+
*/
67+
protected function allSubdomainsOfApplicationUrl()
68+
{
69+
if ($host = parse_url($this->app['config']->get('app.url'), PHP_URL_HOST)) {
70+
return '^(.+\.)?'.preg_quote($host).'$';
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)