Skip to content

Commit 46a2c31

Browse files
committed
fix: add missing server variables
fixes #1
1 parent b0165a5 commit 46a2c31

File tree

2 files changed

+275
-46
lines changed

2 files changed

+275
-46
lines changed

src/FastCgi/FastCgiRequest.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,37 @@ public function __construct(string $content = '', array $parameters = [])
5050
public static function createFromInvocationEvent(HttpRequestEvent $event, string $scriptFilename): self
5151
{
5252
$content = $event->getBody();
53+
$documentRoot = (string) getcwd();
5354
$headers = $event->getHeaders();
5455
$host = $headers['x-forwarded-host'][0] ?? $headers['host'][0] ?? 'localhost';
5556
$method = strtoupper($event->getMethod());
5657
$path = $uri = $event->getPath();
5758
$port = $headers['x-forwarded-port'][0] ?? 80;
5859
$queryString = $event->getQueryString();
59-
60-
if (!empty($queryString)) {
61-
$uri = $uri.'?'.$queryString;
62-
}
60+
$scriptName = str_replace($documentRoot, '', $scriptFilename);
6361

6462
$parameters = [
63+
'DOCUMENT_ROOT' => $documentRoot,
6564
'GATEWAY_INTERFACE' => 'FastCGI/1.0',
6665
'PATH_INFO' => $path,
66+
'PHP_SELF' => '/'.trim($scriptName.$uri, '/'),
6767
'QUERY_STRING' => $queryString,
6868
'REMOTE_ADDR' => '127.0.0.1',
6969
'REMOTE_PORT' => $port,
7070
'REQUEST_METHOD' => $method,
71-
'REQUEST_URI' => $uri,
71+
'REQUEST_TIME' => time(),
72+
'REQUEST_TIME_FLOAT' => microtime(true),
7273
'SCRIPT_FILENAME' => $scriptFilename,
74+
'SCRIPT_NAME' => $scriptName,
7375
'SERVER_ADDR' => '127.0.0.1',
7476
'SERVER_NAME' => $host,
7577
'SERVER_PORT' => $port,
7678
'SERVER_PROTOCOL' => $event->getProtocol(),
7779
'SERVER_SOFTWARE' => 'ymir',
7880
];
7981

82+
$parameters['REQUEST_URI'] = empty($queryString) ? $uri : $uri.'?'.$queryString;
83+
8084
if (isset($headers['x-forwarded-proto'][0]) && 'https' == strtolower($headers['x-forwarded-proto'][0])) {
8185
$parameters['HTTPS'] = 'on';
8286
}
@@ -100,6 +104,8 @@ public static function createFromInvocationEvent(HttpRequestEvent $event, string
100104
// Force "HTTP_HOST" and "SERVER_NAME" to match because of the "X_FORWARDED_HOST" header.
101105
$parameters['HTTP_HOST'] = $parameters['SERVER_NAME'];
102106

107+
ksort($parameters);
108+
103109
return new self($content, $parameters);
104110
}
105111

0 commit comments

Comments
 (0)