Skip to content

Commit 8b50d1e

Browse files
committed
Change order of appending files
1 parent f4addec commit 8b50d1e

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace Com\Tqdev\CrudApi\Router;
3+
4+
use Com\Tqdev\CrudApi\Controller\Responder;
5+
use Com\Tqdev\CrudApi\Request;
6+
use Com\Tqdev\CrudApi\Response;
7+
8+
class SecurityHeaders extends Middleware
9+
{
10+
private $allowedOrigins;
11+
12+
public function __construct(Router $router, Responder $responder, String $allowedOrigins)
13+
{
14+
$router->load($this);
15+
$this->allowedOrigins = $allowedOrigins;
16+
}
17+
18+
private function isOriginAllowed(String $origin, String $allowedOrigins): bool
19+
{
20+
$found = false;
21+
foreach (explode(',', $allowedOrigins) as $allowedOrigin) {
22+
$hostname = preg_quote(strtolower(trim($allowedOrigin)));
23+
$regex = '/^' . str_replace('\*', '.*', $hostname) . '$/';
24+
if (preg_match($regex, $origin)) {
25+
$found = true;
26+
break;
27+
}
28+
}
29+
return $found;
30+
}
31+
32+
public function handle(Request $request): Response
33+
{
34+
$origin = $request->getHeader('Origin');
35+
if ($origin) {
36+
$allowedOrigins = $this->allowedOrigins;
37+
if (!$this->isOriginAllowed($origin, $allowedOrigins)) {
38+
return $this->responder->error(ErrorCode::ORIGIN_FORBIDDEN, $origin);
39+
}
40+
}
41+
$method = $request->getMethod();
42+
if ($method == 'OPTIONS') {
43+
$response = new Response(Response::OK, '');
44+
$response->addHeader('Access-Control-Allow-Headers', 'Content-Type, X-XSRF-TOKEN');
45+
$response->addHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, PUT, POST, DELETE, PATCH');
46+
$response->addHeader('Access-Control-Allow-Credentials', 'true');
47+
$response->addHeader('Access-Control-Max-Age', '1728000');
48+
} else {
49+
$response = $this->next->handle($request);
50+
}
51+
if ($origin) {
52+
$response->addHeader('Access-Control-Allow-Credentials', 'true');
53+
$response->addHeader('Access-Control-Allow-Origin', $origin);
54+
}
55+
return $response;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
namespace Com\Tqdev\CrudApi\Router;
3+
4+
use Com\Tqdev\CrudApi\Data\ErrorCode;
5+
use Com\Tqdev\CrudApi\Data\PathTree;
6+
use Com\Tqdev\CrudApi\Controller\Responder;
7+
use Com\Tqdev\CrudApi\Request;
8+
use Com\Tqdev\CrudApi\Response;
9+
10+
class SimpleRouter implements Router
11+
{
12+
private $responder;
13+
private $routes;
14+
private $midlewares;
15+
16+
public function __construct(Responder $responder)
17+
{
18+
$this->responder = $responder;
19+
$this->routes = new PathTree();
20+
$this->middlewares = array();
21+
}
22+
23+
public function register(String $method, String $path, array $handler)
24+
{
25+
$parts = explode('/', trim($path, '/'));
26+
array_unshift($parts, $method);
27+
$this->routes->put($parts, $handler);
28+
}
29+
30+
public function load(Middleware $middleware)/*: void*/
31+
{
32+
if (count($this->middlewares) > 0) {
33+
$next = $this->middlewares[0];
34+
} else {
35+
$next = $this;
36+
}
37+
$middleware->setNext($next);
38+
array_unshift($this->middlewares, $middleware);
39+
}
40+
41+
public function route(Request $request): Response
42+
{
43+
$obj = $this;
44+
if (count($this->middlewares) > 0) {
45+
$obj = $this->middlewares[0];
46+
}
47+
return $obj->handle($request);
48+
}
49+
50+
public function handle(Request $request): Response
51+
{
52+
$method = strtoupper($request->getMethod());
53+
$path = explode('/', trim($request->getPath(0), '/'));
54+
array_unshift($path, $method);
55+
56+
$functions = $this->matchPath($path, $this->routes);
57+
if (count($functions) == 0) {
58+
return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getPath());
59+
}
60+
return call_user_func($functions[0], $request);
61+
}
62+
63+
private function matchPath(array $path, PathTree $tree): array
64+
{
65+
$values = array();
66+
while (count($path) > 0) {
67+
$key = array_shift($path);
68+
if ($tree->has($key)) {
69+
$tree = $tree->get($key);
70+
} else if ($tree->has('*')) {
71+
$tree = $tree->get('*');
72+
} else {
73+
$tree = null;
74+
break;
75+
}
76+
}
77+
if ($tree !== null) {
78+
$values = $tree->getValues();
79+
}
80+
return $values;
81+
}
82+
}

0 commit comments

Comments
 (0)