Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ public function set(array $route): void
*/
private function handleRequestUri(array $route): void
{
$this->handleRequestMethod();

$request = $this->rudra->request();
$server = $request->server();
$server = $request->server();

$this->spoofRequestMethod();

if ($route['method'] !== $server->get('REQUEST_METHOD')) {
return;
}

$this->parseRequestBody();

$uriRaw = $server->get('REQUEST_URI');
$parsed = parse_url($uriRaw);
$requestPath = $parsed && isset($parsed['path']) ? ltrim($parsed['path'], '/') : '';
Expand All @@ -67,24 +69,24 @@ private function handleRequestUri(array $route): void
}
}

/**
* Processes the HTTP request method, including spoofing via _method (for PUT/PATCH/DELETE)
*/
private function handleRequestMethod(): void
private function spoofRequestMethod(): void
{
$request = $this->rudra->request();
$requestMethod = $request->server()->get('REQUEST_METHOD');

// Spoofing the method via _method parameter in POST requests
if ($requestMethod === 'POST' && $request->post()->has('_method')) {
$spoofedMethod = strtoupper($request->post()->get('_method'));
if (in_array($spoofedMethod, ['PUT', 'PATCH', 'DELETE'])) {
$requestMethod = $spoofedMethod;
$request->server()->set(['REQUEST_METHOD' => $spoofedMethod]);
}
}
}

private function parseRequestBody(): void
{
$request = $this->rudra->request();
$requestMethod = $request->server()->get('REQUEST_METHOD');

// Handle PUT, PATCH, and DELETE requests by parsing raw input data
if (in_array($requestMethod, ['PUT', 'PATCH', 'DELETE'])) {
$rawInput = file_get_contents('php://input');
parse_str($rawInput, $data);
Expand Down Expand Up @@ -241,7 +243,7 @@ private function callActionThroughReflection(?array $params, string $action, obj
*/
private function callActionThroughException(?array $params, string $action, object $controller): void
{
if (isset($params) && in_array('', $params)) {
if (isset($params) && in_array('', $params, true)) {
throw new RouterException('Not Found', 404);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,19 @@ public function testRegex(): void
Router::get('test/:[\d]{1,3}', [MainController::class, 'actionRegexGet']);
$this->assertEquals('regex', Rudra::config()->get('regex'));
}

public function testRequestBodyNotParsedForWrongMethod(): void
{
$_SERVER['REQUEST_URI'] = 'test/page';
$_SERVER['REQUEST_METHOD'] = 'POST'; // POST request
$this->setContainer();

// Register only GET route (method doesn't match)
Router::get('test/page', function () {
Rudra::config()->set(['get_called' => true]);
});

// GET route should not be called - check that key was never set
$this->assertFalse(Rudra::config()->has('get_called'));
}
}
Loading