Skip to content

Commit

Permalink
Server parameters are now filled in ServerRequest. Removed envs from it.
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Feb 14, 2024
1 parent 77d9d4d commit 34e1c80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/Server/Http/Psr7/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function __construct(HttpRequestStream $requestStream, string $method, st
$this->method = $method;
$this->uri = new Uri($uri);
$this->protocol = $protocol;
$this->serverParams = $serverParams;
$this->setHeaders($requestStream->getHeaders());
\parse_str($this->uri->getQuery(), $this->queryParams);
$this->cookieParams = $requestStream->getHeaderOptions('Cookie');
Expand All @@ -39,6 +38,22 @@ public function __construct(HttpRequestStream $requestStream, string $method, st
if (!$this->hasHeader('Host')) {
$this->updateHostFromUri();
}

$this->serverParams = [...$serverParams, ...[
'SERVER_NAME' => $this->uri->getHost(),
'SERVER_PROTOCOL' => 'HTTP/' . $this->getProtocolVersion(),
'REQUEST_URI' => $this->uri->getPath(),
'QUERY_STRING' => $this->uri->getQuery(),
'REQUEST_METHOD' => $this->getMethod(),
]];

if ($this->serverParams['QUERY_STRING'] !== '') {
$this->serverParams['REQUEST_URI'] .= '?' . $this->serverParams['QUERY_STRING'];
}

if ($this->uri->getScheme() === 'https') {
$this->serverParams['HTTPS'] = 'on';
}
}

public function getServerParams(): array
Expand Down
12 changes: 7 additions & 5 deletions src/Server/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Luzrain\PhpRunner\Server\Http;

use Luzrain\PhpRunner\Exception\HttpException;
use Luzrain\PhpRunner\PhpRunner;
use Luzrain\PhpRunner\Server\Http\Psr7\HttpRequestStream;
use Luzrain\PhpRunner\Server\Http\Psr7\ServerRequest;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function isCompleted(): bool
return $this->isCompleted;
}

public function getPsrServerRequest(string $serverAddr, string $serverPort, string $remoteAddr, string $remotePort): ServerRequestInterface
public function getPsrServerRequest(string $serverAddr, int $serverPort, string $remoteAddr, int $remotePort): ServerRequestInterface
{
if (!$this->isCompleted()) {
throw new \LogicException('ServerRequest cannot be created until request is complete');
Expand All @@ -106,12 +107,13 @@ public function getPsrServerRequest(string $serverAddr, string $serverPort, stri
method: $this->method,
uri: $this->uri,
protocol: $this->version,
serverParams: [...$_SERVER, ...[
'SERVER_ADDR' => $serverAddr,
'SERVER_PORT' => $serverPort,
serverParams: [
'REMOTE_ADDR' => $remoteAddr,
'REMOTE_PORT' => $remotePort,
]],
'SERVER_ADDR' => $serverAddr,
'SERVER_PORT' => $serverPort,
'SERVER_SOFTWARE' => PhpRunner::VERSION_STRING,
],
);
}
}
4 changes: 2 additions & 2 deletions src/Server/Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function decode(ConnectionInterface $connection, string $buffer): ServerR
if ($this->request->isCompleted()) {
$this->psrRequest = $this->request->getPsrServerRequest(
serverAddr: $connection->getLocalIp(),
serverPort: (string) $connection->getLocalPort(),
serverPort: $connection->getLocalPort(),
remoteAddr: $connection->getRemoteIp(),
remotePort: (string) $connection->getRemotePort(),
remotePort: $connection->getRemotePort(),
);
return $this->psrRequest;
}
Expand Down

0 comments on commit 34e1c80

Please sign in to comment.