Skip to content

Commit

Permalink
Include HTTP method DELETE to a body parsing list; parse arguments on…
Browse files Browse the repository at this point in the history
… HTTP calls from the Frontend
  • Loading branch information
roxblnfk committed Dec 17, 2023
1 parent ea323b3 commit 2a8b6d9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Info
{
public const NAME = 'Buggregator Trap';
public const VERSION = '1.3.0';
public const VERSION = '1.3.1';
public const LOGO_CLI_COLOR = <<<CONSOLE
\e[44;97;1m \e[0m
\e[44;97;1m ▄█▀ ▀█▄ \e[0m
Expand Down
19 changes: 16 additions & 3 deletions src/Sender/Frontend/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,29 @@ public function handle(ServerRequestInterface $request, callable $next): Respons
{
try {
$path = \trim($request->getUri()->getPath(), '/');
$method = $request->getMethod();
$method = Method::fromString($request->getMethod());

$handler = $this->router->match(Method::fromString($method), $path);
$handler = $this->router->match($method, $path);

if ($handler === null) {
return new Response(404);
}

try {
// Params
if ($method === Method::Get) {
$params = $request->getQueryParams();
} else {
/** @var mixed $params */
$params = Json::decode((string)$request->getBody());
\is_array($params) or $params = [];
}
} catch (\Throwable) {
$params = [];
}

/** @var mixed $message */
$message = $handler($request);
$message = $handler(...$params);

return new Response(
200,
Expand Down
2 changes: 1 addition & 1 deletion src/Traffic/Dispatcher/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class Http implements Dispatcher
public function __construct(
iterable $middlewares = [],
array $handlers = [],
private bool $silentMode = false,
private readonly bool $silentMode = false,
) {
// Init HTTP parser.
$this->parser = new Parser\Http();
Expand Down
2 changes: 1 addition & 1 deletion src/Traffic/Parser/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static function getBlock(StreamClient $stream): string
private function parseBody(StreamClient $stream, ServerRequestInterface $request): ServerRequestInterface
{
// Methods have body
if (!\in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
if (!\in_array($request->getMethod(), ['POST', 'PUT', 'PATCH', 'DELETE'], true)) {
return $request;
}

Expand Down

0 comments on commit 2a8b6d9

Please sign in to comment.