From 0097725ef28df75318ca6abf51e207c9abdf87b7 Mon Sep 17 00:00:00 2001 From: Mauro Tschiedel Date: Wed, 1 Sep 2021 08:22:12 -0300 Subject: [PATCH 1/9] Update in error status code Add example If you will add specific status code for the browser --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 73deaad..2cb77d7 100644 --- a/README.md +++ b/README.md @@ -972,6 +972,12 @@ If you do not want a redirect, but want the error-page rendered on the current-u $request->setRewriteCallback('ErrorController@notFound'); ``` +If you will set the correct status for the browser error use: + +```php +SimpleRouter::response()->httpCode(404); +``` + ## Using custom exception handlers This is a basic example of an ExceptionHandler implementation (please see "[Easily overwrite route about to be loaded](#easily-overwrite-route-about-to-be-loaded)" for examples on how to change callback). @@ -2073,4 +2079,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. From 9fa7ad3e9186df276c5b8e246946db21212d22eb Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Mon, 4 Oct 2021 01:49:00 +0200 Subject: [PATCH 2/9] implement test Function to test output without reset --- tests/TestRouter.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/TestRouter.php b/tests/TestRouter.php index 1a4cfd7..636c836 100644 --- a/tests/TestRouter.php +++ b/tests/TestRouter.php @@ -48,4 +48,17 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter return $response; } + public static function debugOutputNoReset(string $testUrl, string $testMethod = 'get', bool $reset = true): string + { + $response = null; + + // Route request + ob_start(); + static::debugNoReset($testUrl, $testMethod, $reset); + $response = ob_get_clean(); + + // Return response + return $response; + } + } \ No newline at end of file From 5268a998fff86e20340161ad6117c237f365686d Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Mon, 4 Oct 2021 01:49:42 +0200 Subject: [PATCH 3/9] Extend domain filter to support fixed subdomain and domain parameter --- src/Pecee/SimpleRouter/Route/Route.php | 7 ++- tests/Pecee/SimpleRouter/RouterRouteTest.php | 64 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index a8a5c37..401b1d7 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -123,7 +123,8 @@ abstract class Route implements IRoute ); // Ensures that host names/domains will work with parameters - $url = '/' . ltrim($url, '/'); + + if($route[0] == '{') $url = '/' . ltrim($url, '/'); $urlRegex = ''; $parameters = []; @@ -131,7 +132,7 @@ abstract class Route implements IRoute $urlRegex = preg_quote($route, '/'); } else { - foreach (preg_split('/((-?\/?){[^}]+})/', $route) as $key => $t) { + foreach (preg_split('/((\.?-?\/?){[^}]+})/', $route) as $key => $t) { $regex = ''; @@ -146,7 +147,7 @@ abstract class Route implements IRoute $regex = $parameterRegex ?? $this->defaultParameterRegex ?? static::PARAMETERS_DEFAULT_REGEX; } - $regex = sprintf('((\/|-)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex); + $regex = sprintf('((\/|-|\.)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex); } $urlRegex .= preg_quote($t, '/') . $regex; diff --git a/tests/Pecee/SimpleRouter/RouterRouteTest.php b/tests/Pecee/SimpleRouter/RouterRouteTest.php index 971676a..da16c56 100644 --- a/tests/Pecee/SimpleRouter/RouterRouteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRouteTest.php @@ -175,6 +175,70 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase TestRouter::debug('/test', 'get'); + $this->assertFalse($result); + + } + + public function testFixedSubdomainDynamicDomain() + { + TestRouter::request()->setHost('other.world.com'); + + $result = false; + + TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) { + TestRouter::get('/test', function ($domain = null) use (&$result) { + + $result = true; + }); + }); + + TestRouter::debug('/test', 'get'); + + $this->assertTrue($result); + + } + + public function testFixedSubdomainDynamicDomainParameter() + { + TestRouter::request()->setHost('other.world.com'); + + $result = false; + + TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) { + TestRouter::get('/test', 'DummyController@param'); + TestRouter::get('/test/{key}', 'DummyController@param'); + }); + + $response = TestRouter::debugOutputnoReset('/test', 'get'); + + $this->assertEquals('world.com', $response); + + $response = TestRouter::debugOutput('/test/unittest', 'get'); + + $this->assertEquals('unittest, world.com', $response); + + } + + public function testWrongFixedSubdomainDynamicDomain() + { + TestRouter::request()->setHost('wrong.world.com'); + + $result = false; + + TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) { + TestRouter::get('/test', function ($domain = null) use (&$result) { + + $result = true; + }); + }); + + try { + TestRouter::debug('/test', 'get'); + } catch(\Exception $e) { + + } + + $this->assertFalse($result); } From 06a63eb0e700e551cdc065835f6f318a7edc26a9 Mon Sep 17 00:00:00 2001 From: Stefan Warnat Date: Mon, 4 Oct 2021 02:02:45 +0200 Subject: [PATCH 4/9] Fix Typo --- tests/Pecee/SimpleRouter/RouterRouteTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Pecee/SimpleRouter/RouterRouteTest.php b/tests/Pecee/SimpleRouter/RouterRouteTest.php index da16c56..18ab4d9 100644 --- a/tests/Pecee/SimpleRouter/RouterRouteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRouteTest.php @@ -209,7 +209,7 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase TestRouter::get('/test/{key}', 'DummyController@param'); }); - $response = TestRouter::debugOutputnoReset('/test', 'get'); + $response = TestRouter::debugOutputNoReset('/test', 'get'); $this->assertEquals('world.com', $response); From b5e42dbdfb8bf40b903e091efc43ce588605f4ea Mon Sep 17 00:00:00 2001 From: Hannes <31671206+xJuvi@users.noreply.github.com> Date: Sun, 2 Jan 2022 14:42:43 +0100 Subject: [PATCH 5/9] Make current processing rule accessible Adds an constant with the data from the current processing rule to make it globally accessible, f.e. in middleware objects --- src/Pecee/SimpleRouter/Router.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 76721d2..da9e06d 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -35,6 +35,12 @@ class Router * @var bool */ protected $isProcessingRoute; + + /** + * Defines all data from current processing route. + * @var array + */ + protected $currentProcessingRoute = []; /** * All added routes @@ -371,6 +377,9 @@ class Router foreach ($this->processedRoutes as $key => $route) { $this->debug('Matching route "%s"', get_class($route)); + + /* Add current processing route to constants */ + $this->currentProcessingRoute = $route; /* If the route matches */ if ($route->matchRoute($url, $this->request) === true) { @@ -429,6 +438,9 @@ class Router } } } + + /* Remove the current processing route after handle all possible routes */ + $this->currentProcessingRoute = []; } catch (Exception $e) { $this->handleException($e); @@ -950,4 +962,4 @@ class Router return $this; } -} \ No newline at end of file +} From a1d5f38af7c4fc5ea3411bb5ffef164d4110fe5f Mon Sep 17 00:00:00 2001 From: Hannes <31671206+xJuvi@users.noreply.github.com> Date: Sun, 2 Jan 2022 21:59:39 +0100 Subject: [PATCH 6/9] Add function to fetch currect processing route --- src/Pecee/SimpleRouter/Router.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index da9e06d..855f98b 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -939,6 +939,16 @@ class Router { return $this->debugList; } + + /** + * Get the current processing route details. + * + * @return array + */ + public function getCurrentProcessingRoute(): array + { + return $this->currentProcessingRoute; + } /** * Changes the rendering behavior of the router. From 01bad94af0bc362ae7c37b6e1793c87d55f3ad86 Mon Sep 17 00:00:00 2001 From: Hannes <31671206+xJuvi@users.noreply.github.com> Date: Wed, 2 Feb 2022 15:20:23 +0100 Subject: [PATCH 7/9] Update Router.php --- src/Pecee/SimpleRouter/Router.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 855f98b..509ca2a 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -38,9 +38,9 @@ class Router /** * Defines all data from current processing route. - * @var array + * @var ILoadableRoute */ - protected $currentProcessingRoute = []; + protected $currentProcessingRoute; /** * All added routes @@ -438,9 +438,6 @@ class Router } } } - - /* Remove the current processing route after handle all possible routes */ - $this->currentProcessingRoute = []; } catch (Exception $e) { $this->handleException($e); @@ -943,9 +940,9 @@ class Router /** * Get the current processing route details. * - * @return array + * @return ILoadableRoute */ - public function getCurrentProcessingRoute(): array + public function getCurrentProcessingRoute(): ILoadableRoute { return $this->currentProcessingRoute; } From 301c2cfe4a87945518d83ea00beeae9b6efe2760 Mon Sep 17 00:00:00 2001 From: DeveloperMarius Date: Tue, 1 Mar 2022 15:39:31 +0100 Subject: [PATCH 8/9] fixed urldecode request-uri header --- src/Pecee/Http/Request.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 12ede38..63bd5d6 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -129,7 +129,12 @@ class Request $this->setHost($this->getHeader('http-host')); // Check if special IIS header exist, otherwise use default. - $this->setUrl(new Url($this->getFirstHeader(['unencoded-url', 'request-uri']))); + $url = $this->getHeader('unencoded-url'); + if($url !== null){ + $this->setUrl(new Url($url)); + }else{ + $this->setUrl(new Url(urldecode($this->getHeader('request-uri')))); + } $this->setContentType((string)$this->getHeader('content-type')); $this->setMethod((string)($_POST[static::FORCE_METHOD_KEY] ?? $this->getHeader('request-method'))); $this->inputHandler = new InputHandler($this); From f0a4b6e46f27482a71cf4dccb927d80e754ec62b Mon Sep 17 00:00:00 2001 From: sessingo Date: Thu, 9 Feb 2023 03:01:17 +0100 Subject: [PATCH 9/9] Updated composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a8294cd..a6e56c8 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ } ], "require": { - "php": ">=7.1", + "php": ">=7.4", "ext-json": "*" }, "require-dev": {