Request Body encoding + better method handling

This commit is contained in:
Roman 2024-05-04 22:18:04 +02:00
parent a5e4cf6a74
commit 623e5dbbc8

@ -215,15 +215,38 @@ abstract class Request {
return false; return false;
} }
$values = $_REQUEST; if ($this->isMethodAllowed("GET") && $this->isMethodAllowed("POST")) {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && in_array("application/json", explode(";", $_SERVER["CONTENT_TYPE"] ?? ""))) { $values = $_REQUEST;
$jsonData = json_decode(file_get_contents('php://input'), true); } else if ($this->isMethodAllowed("POST")) {
if ($jsonData !== null) { $values = $_POST;
$values = array_merge($values, $jsonData); } else if ($this->isMethodAllowed("GET")) {
} else { $values = $_GET;
$this->lastError = 'Invalid request body.'; }
http_response_code(400);
return false; if (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PUT', 'PATCH'])) {
$contentTypeData = explode(";", $_SERVER["CONTENT_TYPE"] ?? "");
$charset = "utf-8";
if ($contentTypeData[0] === "application/json") {
for ($i = 1; $i < count($contentTypeData); $i++) {
if (preg_match("/charset=(.*)/", $contentTypeData[$i], $match)) {
$charset = $match[1];
}
}
$body = file_get_contents('php://input');
if (strcasecmp($charset, "utf-8") !== 0) {
$body = iconv($charset, 'utf-8', $body);
}
$jsonData = json_decode($body, true);
if ($jsonData !== null) {
$values = array_merge($values, $jsonData);
} else {
$this->lastError = "Invalid request body.";
http_response_code(400);
return false;
}
} }
} }
@ -339,8 +362,7 @@ abstract class Request {
$obj = $this->params; $obj = $this->params;
} }
// I don't know why phpstorm return $obj[$name]?->value;
return (isset($obj[$name]) ? $obj[$name]->value : NULL);
} }
public function isMethodAllowed(string $method): bool { public function isMethodAllowed(string $method): bool {