Skip to content

Commit

Permalink
fix: handling bad urls, returning errors in json format
Browse files Browse the repository at this point in the history
Changes to be committed:
	modified:   composer.json
	modified:   src/DataGenerator.php
	modified:   src/Dispatcher.php
	modified:   src/Interfaces/DataGenerator.php
	modified:   src/Interfaces/RouteParser.php
	modified:   src/RouteCollector.php
	modified:   src/RouteParser.php
	modified:   src/Router.php
  • Loading branch information
IcyDrae committed Oct 10, 2020
1 parent 1d78e83 commit a737e74
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 22 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
}
},
"require": {
"ext-json": "*"
"ext-json": "*",
"ext-http": "*"
}
}
24 changes: 19 additions & 5 deletions src/DataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

class DataGenerator implements DataGeneratorInterface
{
private array $parsed;
private $parsed;

/**
* @param array $route
* @inheritDoc
*/
public function addRoute(array $route)
public function addRoute($route)
{
return $this->setParsed($route);
}
Expand All @@ -22,6 +22,12 @@ public function addRoute(array $route)
*/
public function getData()
{
if ($this->isBadUrl()) {
return json_encode(
["error" => "Not accepted characters in URL"]
);
}

if ($this->isDynamicRoute()) {
return $this->generateDynamic();
} elseif ($this->isStaticRoute()) {
Expand All @@ -40,13 +46,21 @@ public function getParsed(): array
}

/**
* @param array $parsed
* @param array|int $parsed
*/
public function setParsed(array $parsed)
public function setParsed($parsed)
{
$this->parsed = $parsed;
}

/**
* @return bool
*/
public function isBadUrl()
{
return is_int($this->parsed);
}

/**
* @return bool|void
*/
Expand Down
9 changes: 6 additions & 3 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ public function dispatch(array $routerInput)

return self::FOUND;
} else {
header('Content-type:application/json;charset=utf-8');
http_response_code(405);

return [
"exception" => new MethodNotAllowedException()
];
$e = new MethodNotAllowedException();
echo json_encode([
"exception" => $e->getMessage()
]);
exit();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/DataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
interface DataGenerator
{
/**
* @param array $route
* @param array|int $route
* @return mixed
*/
public function addRoute(array $route);
public function addRoute($route);

/**
* @return array|string
Expand Down
6 changes: 4 additions & 2 deletions src/Interfaces/RouteParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

interface RouteParser
{
public const NOT_ALLOWED_CHARS = 0;

/**
* @param string $route
* @param string $uri
* @return array
* @return array|int
*/
public function parse(string $route, string $uri): array;
public function parse(string $route, string $uri);
}
1 change: 0 additions & 1 deletion src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Gjoni\Router;

use Gjoni\Router\Exception\InvalidRouteException;

class RouteCollector
{
Expand Down
8 changes: 6 additions & 2 deletions src/RouteParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ class RouteParser implements RouteParserInterface
*
* @param string $route
* @param string $uri
* @return array
* @return array|int
*/
public function parse(string $route, string $uri): array
public function parse(string $route, string $uri)
{
# Base route parsing pattern
$allowedCharacters = "[a-zA-Z0-9\_]+";

if (preg_match("/[^-:\/a-zA-Z\d]/", $uri, $matches)) {
return self::NOT_ALLOWED_CHARS;
}

# Swap the route annotation with it's corresponding pattern so it can be verified against in the Router
$pattern = preg_replace("/{($allowedCharacters)}/",
"(?<$1>$allowedCharacters)", $route);
Expand Down
30 changes: 24 additions & 6 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class Router
{
private static string $map = "App\Controllers"; # Default mapping
private static array $parsed;
private static $parsed;
private static array $methods = [];
private static array $routes = [];
private static array $handlers = [];
Expand Down Expand Up @@ -73,6 +73,13 @@ private static function beforeHandle()
$collector = new RouteCollector;
self::$parsed = $collector->addRoute($route, self::$request["uri"]);

if (self::$parsed["error"]) {
header('Content-type:application/json;charset=utf-8');
http_response_code(400);
echo self::$parsed;
exit();
}

$handler = self::$handlers[$pos][1];
$input = [
"parsed" => self::$parsed,
Expand All @@ -91,10 +98,13 @@ private static function beforeHandle()

# If we're at the last position and still haven't found a route, it means this route is not defined in our application
if ($pos == array_key_last(self::$routes) && self::$dispatcherValue == 0) {
header('Content-type:application/json;charset=utf-8');
http_response_code(404);
return [
"exception" => new InvalidRouteException()
];
$e = new InvalidRouteException();
echo json_encode([
"error" => $e->getMessage()
]);
exit();
}

++$pos;
Expand All @@ -112,9 +122,17 @@ private static function handle($input)
try {
self::$dispatcherValue = $dispatcher->dispatch($input);
} catch (ClassNotFoundException $exception) {
return [$exception];
header('Content-type:application/json;charset=utf-8');
echo json_encode([
"error" => $exception->getMessage()
]);
exit();
} catch (MethodNotCalledException $exception) {
return [$exception];
header('Content-type:application/json;charset=utf-8');
echo json_encode([
"error" => $exception->getMessage()
]);
exit();
}
}

Expand Down

0 comments on commit a737e74

Please sign in to comment.