Improve server request parsing.

This commit is contained in:
Madeorsk 2024-11-08 22:38:22 +01:00
parent dbf10374cb
commit 3524f2618c
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
3 changed files with 86 additions and 11 deletions

View file

@ -29,7 +29,8 @@
"psr/http-server-middleware": "^1.0", "psr/http-server-middleware": "^1.0",
"psr/http-message": "^2.0", "psr/http-message": "^2.0",
"psr/http-factory": "^1.1", "psr/http-factory": "^1.1",
"filp/whoops": "^2.16" "filp/whoops": "^2.16",
"nyholm/psr7-server": "^1.1"
}, },
"suggest": { "suggest": {
"ext-dom": "*" "ext-dom": "*"

68
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "2ddbf7190a614a3bab8ab92fd7179bbc", "content-hash": "90576cdd8504a5049249884ab3e1f7d2",
"packages": [ "packages": [
{ {
"name": "filp/whoops", "name": "filp/whoops",
@ -188,6 +188,72 @@
], ],
"time": "2024-09-09T07:06:30+00:00" "time": "2024-09-09T07:06:30+00:00"
}, },
{
"name": "nyholm/psr7-server",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/Nyholm/psr7-server.git",
"reference": "4335801d851f554ca43fa6e7d2602141538854dc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Nyholm/psr7-server/zipball/4335801d851f554ca43fa6e7d2602141538854dc",
"reference": "4335801d851f554ca43fa6e7d2602141538854dc",
"shasum": ""
},
"require": {
"php": "^7.1 || ^8.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0 || ^2.0"
},
"require-dev": {
"nyholm/nsa": "^1.1",
"nyholm/psr7": "^1.3",
"phpunit/phpunit": "^7.0 || ^8.5 || ^9.3"
},
"type": "library",
"autoload": {
"psr-4": {
"Nyholm\\Psr7Server\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com"
},
{
"name": "Martijn van der Ven",
"email": "martijn@vanderven.se"
}
],
"description": "Helper classes to handle PSR-7 server requests",
"homepage": "http://tnyholm.se",
"keywords": [
"psr-17",
"psr-7"
],
"support": {
"issues": "https://github.com/Nyholm/psr7-server/issues",
"source": "https://github.com/Nyholm/psr7-server/tree/1.1.0"
},
"funding": [
{
"url": "https://github.com/Zegnat",
"type": "github"
},
{
"url": "https://github.com/nyholm",
"type": "github"
}
],
"time": "2023-11-08T09:30:43+00:00"
},
{ {
"name": "psr/http-factory", "name": "psr/http-factory",
"version": "1.1.0", "version": "1.1.0",

View file

@ -4,12 +4,14 @@ namespace Nest\Http;
use Nest\Application; use Nest\Application;
use Nest\Http\Exceptions\Renderer\ViewNotFoundException; use Nest\Http\Exceptions\Renderer\ViewNotFoundException;
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;
use Override; use Override;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use function Nest\Utils\array_first;
use function Nest\Utils\path_join; use function Nest\Utils\path_join;
/** /**
@ -78,14 +80,20 @@ class Http
*/ */
protected function parseRequest(): ServerRequestInterface protected function parseRequest(): ServerRequestInterface
{ {
return $this->request = new ServerRequest( // Initialize server request.
$_SERVER["REQUEST_METHOD"], $psr17Factor = new Psr17Factory();
$_SERVER["REQUEST_URI"], $this->request = (new ServerRequestCreator(
getallheaders(), $psr17Factor,
fopen("php://input", "r"), $psr17Factor,
"1.1", $psr17Factor,
$_SERVER, $psr17Factor,
); ))->fromGlobals();
if (str_starts_with(array_first($this->request->getHeader("ContentType")), "application/json"))
// If body is supposed to be JSON-encoded, decode JSON.
$this->request = $this->request->withParsedBody(json_decode($this->request->getBody()->getContents()));
return $this->request;
} }
/** /**