Improve server request parsing.
This commit is contained in:
parent
dbf10374cb
commit
3524f2618c
3 changed files with 86 additions and 11 deletions
|
@ -29,7 +29,8 @@
|
|||
"psr/http-server-middleware": "^1.0",
|
||||
"psr/http-message": "^2.0",
|
||||
"psr/http-factory": "^1.1",
|
||||
"filp/whoops": "^2.16"
|
||||
"filp/whoops": "^2.16",
|
||||
"nyholm/psr7-server": "^1.1"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-dom": "*"
|
||||
|
|
68
composer.lock
generated
68
composer.lock
generated
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "2ddbf7190a614a3bab8ab92fd7179bbc",
|
||||
"content-hash": "90576cdd8504a5049249884ab3e1f7d2",
|
||||
"packages": [
|
||||
{
|
||||
"name": "filp/whoops",
|
||||
|
@ -188,6 +188,72 @@
|
|||
],
|
||||
"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",
|
||||
"version": "1.1.0",
|
||||
|
|
26
src/Http.php
26
src/Http.php
|
@ -4,12 +4,14 @@ namespace Nest\Http;
|
|||
|
||||
use Nest\Application;
|
||||
use Nest\Http\Exceptions\Renderer\ViewNotFoundException;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use Nyholm\Psr7Server\ServerRequestCreator;
|
||||
use Override;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use function Nest\Utils\array_first;
|
||||
use function Nest\Utils\path_join;
|
||||
|
||||
/**
|
||||
|
@ -78,14 +80,20 @@ class Http
|
|||
*/
|
||||
protected function parseRequest(): ServerRequestInterface
|
||||
{
|
||||
return $this->request = new ServerRequest(
|
||||
$_SERVER["REQUEST_METHOD"],
|
||||
$_SERVER["REQUEST_URI"],
|
||||
getallheaders(),
|
||||
fopen("php://input", "r"),
|
||||
"1.1",
|
||||
$_SERVER,
|
||||
);
|
||||
// Initialize server request.
|
||||
$psr17Factor = new Psr17Factory();
|
||||
$this->request = (new ServerRequestCreator(
|
||||
$psr17Factor,
|
||||
$psr17Factor,
|
||||
$psr17Factor,
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue