Skip to content

Commit 34e1c80

Browse files
committed
Server parameters are now filled in ServerRequest. Removed envs from it.
1 parent 77d9d4d commit 34e1c80

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/Server/Http/Psr7/ServerRequest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function __construct(HttpRequestStream $requestStream, string $method, st
3030
$this->method = $method;
3131
$this->uri = new Uri($uri);
3232
$this->protocol = $protocol;
33-
$this->serverParams = $serverParams;
3433
$this->setHeaders($requestStream->getHeaders());
3534
\parse_str($this->uri->getQuery(), $this->queryParams);
3635
$this->cookieParams = $requestStream->getHeaderOptions('Cookie');
@@ -39,6 +38,22 @@ public function __construct(HttpRequestStream $requestStream, string $method, st
3938
if (!$this->hasHeader('Host')) {
4039
$this->updateHostFromUri();
4140
}
41+
42+
$this->serverParams = [...$serverParams, ...[
43+
'SERVER_NAME' => $this->uri->getHost(),
44+
'SERVER_PROTOCOL' => 'HTTP/' . $this->getProtocolVersion(),
45+
'REQUEST_URI' => $this->uri->getPath(),
46+
'QUERY_STRING' => $this->uri->getQuery(),
47+
'REQUEST_METHOD' => $this->getMethod(),
48+
]];
49+
50+
if ($this->serverParams['QUERY_STRING'] !== '') {
51+
$this->serverParams['REQUEST_URI'] .= '?' . $this->serverParams['QUERY_STRING'];
52+
}
53+
54+
if ($this->uri->getScheme() === 'https') {
55+
$this->serverParams['HTTPS'] = 'on';
56+
}
4257
}
4358

4459
public function getServerParams(): array

src/Server/Http/Request.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Luzrain\PhpRunner\Server\Http;
66

77
use Luzrain\PhpRunner\Exception\HttpException;
8+
use Luzrain\PhpRunner\PhpRunner;
89
use Luzrain\PhpRunner\Server\Http\Psr7\HttpRequestStream;
910
use Luzrain\PhpRunner\Server\Http\Psr7\ServerRequest;
1011
use Psr\Http\Message\ServerRequestInterface;
@@ -95,7 +96,7 @@ public function isCompleted(): bool
9596
return $this->isCompleted;
9697
}
9798

98-
public function getPsrServerRequest(string $serverAddr, string $serverPort, string $remoteAddr, string $remotePort): ServerRequestInterface
99+
public function getPsrServerRequest(string $serverAddr, int $serverPort, string $remoteAddr, int $remotePort): ServerRequestInterface
99100
{
100101
if (!$this->isCompleted()) {
101102
throw new \LogicException('ServerRequest cannot be created until request is complete');
@@ -106,12 +107,13 @@ public function getPsrServerRequest(string $serverAddr, string $serverPort, stri
106107
method: $this->method,
107108
uri: $this->uri,
108109
protocol: $this->version,
109-
serverParams: [...$_SERVER, ...[
110-
'SERVER_ADDR' => $serverAddr,
111-
'SERVER_PORT' => $serverPort,
110+
serverParams: [
112111
'REMOTE_ADDR' => $remoteAddr,
113112
'REMOTE_PORT' => $remotePort,
114-
]],
113+
'SERVER_ADDR' => $serverAddr,
114+
'SERVER_PORT' => $serverPort,
115+
'SERVER_SOFTWARE' => PhpRunner::VERSION_STRING,
116+
],
115117
);
116118
}
117119
}

src/Server/Protocols/Http.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function decode(ConnectionInterface $connection, string $buffer): ServerR
4848
if ($this->request->isCompleted()) {
4949
$this->psrRequest = $this->request->getPsrServerRequest(
5050
serverAddr: $connection->getLocalIp(),
51-
serverPort: (string) $connection->getLocalPort(),
51+
serverPort: $connection->getLocalPort(),
5252
remoteAddr: $connection->getRemoteIp(),
53-
remotePort: (string) $connection->getRemotePort(),
53+
remotePort: $connection->getRemotePort(),
5454
);
5555
return $this->psrRequest;
5656
}

0 commit comments

Comments
 (0)