Skip to content

Commit a17291d

Browse files
committed
fixed minor bug in Request
1 parent 41f1092 commit a17291d

File tree

5 files changed

+71
-20
lines changed

5 files changed

+71
-20
lines changed

example.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require "vendor/autoload.php";
4+
5+
use flight\Flight;
6+
7+
$app = Flight::app( 'test', __DIR__ );
8+
9+
$router = $app->router();
10+
$router->get( '/', function ($request) use ($app) {
11+
error_log( json_encode( $_SERVER ) );
12+
13+
if ( isset( $_SERVER[ 'HTTP_ORIGIN' ] ) || isset( $_SERVER[ 'HTTP_SEC_FETCH_SITE' ] ) ) {
14+
return $app->allow_cross_origin( $request );
15+
}
16+
echo 'hello world!';
17+
}, [ 'share' => '*' ] );
18+
19+
$app->start();

public/index.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
include "../example.php";

src/flight/Engine.php

+31-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function ($view) use ($self) {
134134
// Register framework methods
135135
$methods = array(
136136
'start', 'stop', 'route', 'halt', 'error', 'notFound',
137-
'render', 'redirect', 'etag', 'lastModified', 'json',
137+
'render', 'redirect', 'allow_cross_origin', 'etag', 'lastModified', 'json',
138138
'jsonp', 'dispatchRoute'
139139
);
140140

@@ -435,8 +435,7 @@ public function _error($e) {
435435
$this->response()
436436
->clear()
437437
->status( 500 )
438-
->write( $msg )
439-
->send();
438+
->write( $msg );
440439
} catch ( \Throwable $t ) {
441440
exit( $msg );
442441
}
@@ -480,6 +479,35 @@ public function _redirect($url, $code = 303): http\Response {
480479
->header( 'Location', $url );
481480
}
482481

482+
public function _allow_cross_origin(http\Request $request): http\Response {
483+
$response = $this->response();
484+
485+
if ( isset( $_SERVER[ 'HTTP_ORIGIN' ] ) ) {
486+
// Allow source
487+
$response->header( 'Access-Control-Allow-Origin', $_SERVER[ 'HTTP_ORIGIN' ] )
488+
->header( 'Access-Control-Allow-Credentials', true )
489+
->header( 'Access-Control-Max-Age', 86400 );
490+
} else if ( isset( $_SERVER[ 'HTTP_SEC_FETCH_SITE' ] ) ) {
491+
$response->header( 'Access-Control-Allow-Origin', '*' )
492+
->header( 'Access-Control-Allow-Credentials', true )
493+
->header( 'Access-Control-Max-Age', 86400 );
494+
}
495+
496+
// Access-Control headers are received during OPTIONS requests
497+
if ( $request->method === 'OPTIONS' ) {
498+
if ( isset( $_SERVER[ 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' ] ) ) {
499+
$response->header( 'Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS' );
500+
}
501+
502+
if ( isset( $_SERVER[ 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' ] ) ) {
503+
$response->header( 'Access-Control-Allow-Methods', $_SERVER[ 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' ] );
504+
}
505+
$response->status( 200 );
506+
}
507+
508+
return $response;
509+
}
510+
483511
/**
484512
* Renders a template.
485513
*

src/flight/http/Response.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,14 @@ public function sendHeaders() {
176176
if ( strpos( php_sapi_name(), 'cgi' ) !== false ) {
177177
header(
178178
sprintf(
179-
'Status: %d %s', $this->status, self::$codes[ $this->status ]
179+
'Status: %d %s', $this->status, HTTPStatus::CODES[ $this->status ]
180180
), true
181181
);
182182
} else {
183183
header(
184184
sprintf(
185185
'%s %d %s', (isset( $_SERVER[ 'SERVER_PROTOCOL' ] ) ? $_SERVER[ 'SERVER_PROTOCOL' ] : 'HTTP/1.1' ),
186-
$this->status, self::$codes[ $this->status ]
186+
$this->status, HTTPStatus::CODES[ $this->status ]
187187
), true, $this->status
188188
);
189189
}

src/flight/http/Router.php

+16-15
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct() {
5656
*
5757
* @return array Array of routes
5858
*/
59-
public function getRoutes() {
59+
public function routes() {
6060
return $this->routes;
6161
}
6262

@@ -74,7 +74,7 @@ public function clear() {
7474
* @param callback $callback Callback function
7575
* @param array $config Pass the matching route object to the callback
7676
*/
77-
public function map( $pattern, $callback, array $config = [] ): Route {
77+
public function map($pattern, $callback, array $config = []): Route {
7878
$url = trim( $pattern );
7979
$methods = [ 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD' ];
8080

@@ -86,9 +86,9 @@ public function map( $pattern, $callback, array $config = [] ): Route {
8686

8787
$route = new Route( $url, $callback, $methods, $config );
8888
foreach ( $methods as $method ) {
89-
$this->routes[$method][] = $route;
89+
$this->routes[ $method ][] = $route;
9090
}
91-
91+
9292
return $route;
9393
}
9494

@@ -99,51 +99,51 @@ public function map( $pattern, $callback, array $config = [] ): Route {
9999
* @param callback $callback Callback function
100100
* @param array $config Pass the matching route object to the callback
101101
*/
102-
public function head( $pattern, $callback, array $config = [] ): Route {
102+
public function head($pattern, $callback, array $config = []): Route {
103103
return $this->map( 'HEAD ' . $pattern, $callback, $config );
104104
}
105-
105+
106106
/**
107107
* Maps a GET URL pattern to a callback function.
108108
*
109109
* @param string $pattern URL pattern to match
110110
* @param callback $callback Callback function
111111
* @param array $config Pass the matching route object to the callback
112112
*/
113-
public function get( $pattern, $callback, array $config = [] ): Route {
113+
public function get($pattern, $callback, array $config = []): Route {
114114
return $this->map( 'GET ' . $pattern, $callback, $config );
115115
}
116-
116+
117117
/**
118118
* Maps a POST URL pattern to a callback function.
119119
*
120120
* @param string $pattern URL pattern to match
121121
* @param callback $callback Callback function
122122
* @param array $config Pass the matching route object to the callback
123123
*/
124-
public function post( $pattern, $callback, array $config = [] ): Route {
124+
public function post($pattern, $callback, array $config = []): Route {
125125
return $this->map( 'POST ' . $pattern, $callback, $config );
126126
}
127-
127+
128128
/**
129129
* Maps a PUT URL pattern to a callback function.
130130
*
131131
* @param string $pattern URL pattern to match
132132
* @param callback $callback Callback function
133133
* @param array $config Pass the matching route object to the callback
134134
*/
135-
public function put( $pattern, $callback, array $config = [] ): Route {
135+
public function put($pattern, $callback, array $config = []): Route {
136136
return $this->map( 'PUT ' . $pattern, $callback, $config );
137137
}
138-
138+
139139
/**
140140
* Maps a DELETE URL pattern to a callback function.
141141
*
142142
* @param string $pattern URL pattern to match
143143
* @param callback $callback Callback function
144144
* @param array $config Pass the matching route object to the callback
145145
*/
146-
public function delete( $pattern, $callback, array $config = [] ): Route {
146+
public function delete($pattern, $callback, array $config = []): Route {
147147
return $this->map( 'DELETE ' . $pattern, $callback, $config );
148148
}
149149

@@ -153,10 +153,10 @@ public function delete( $pattern, $callback, array $config = [] ): Route {
153153
* @param Request $request Request object
154154
* @return Route|bool Matching route or false if no match
155155
*/
156-
public function route( Request $request ) {
156+
public function route(Request $request) {
157157
$url_decoded = urldecode( $request->url );
158158

159-
$bucket = $this->routes[$request->method];
159+
$bucket = $this->routes[ $request->method ];
160160

161161
foreach ( $bucket as $route ) {
162162
if ( $route->matchUrl( $url_decoded, $this->case_sensitive ) ) {
@@ -166,4 +166,5 @@ public function route( Request $request ) {
166166

167167
return false;
168168
}
169+
169170
}

0 commit comments

Comments
 (0)