Skip to content

Commit

Permalink
fix: add missing server variables
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
carlalexander committed Apr 6, 2021
1 parent b0165a5 commit 46a2c31
Show file tree
Hide file tree
Showing 2 changed files with 275 additions and 46 deletions.
16 changes: 11 additions & 5 deletions src/FastCgi/FastCgiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,37 @@ public function __construct(string $content = '', array $parameters = [])
public static function createFromInvocationEvent(HttpRequestEvent $event, string $scriptFilename): self
{
$content = $event->getBody();
$documentRoot = (string) getcwd();
$headers = $event->getHeaders();
$host = $headers['x-forwarded-host'][0] ?? $headers['host'][0] ?? 'localhost';
$method = strtoupper($event->getMethod());
$path = $uri = $event->getPath();
$port = $headers['x-forwarded-port'][0] ?? 80;
$queryString = $event->getQueryString();

if (!empty($queryString)) {
$uri = $uri.'?'.$queryString;
}
$scriptName = str_replace($documentRoot, '', $scriptFilename);

$parameters = [
'DOCUMENT_ROOT' => $documentRoot,
'GATEWAY_INTERFACE' => 'FastCGI/1.0',
'PATH_INFO' => $path,
'PHP_SELF' => '/'.trim($scriptName.$uri, '/'),
'QUERY_STRING' => $queryString,
'REMOTE_ADDR' => '127.0.0.1',
'REMOTE_PORT' => $port,
'REQUEST_METHOD' => $method,
'REQUEST_URI' => $uri,
'REQUEST_TIME' => time(),
'REQUEST_TIME_FLOAT' => microtime(true),
'SCRIPT_FILENAME' => $scriptFilename,
'SCRIPT_NAME' => $scriptName,
'SERVER_ADDR' => '127.0.0.1',
'SERVER_NAME' => $host,
'SERVER_PORT' => $port,
'SERVER_PROTOCOL' => $event->getProtocol(),
'SERVER_SOFTWARE' => 'ymir',
];

$parameters['REQUEST_URI'] = empty($queryString) ? $uri : $uri.'?'.$queryString;

if (isset($headers['x-forwarded-proto'][0]) && 'https' == strtolower($headers['x-forwarded-proto'][0])) {
$parameters['HTTPS'] = 'on';
}
Expand All @@ -100,6 +104,8 @@ public static function createFromInvocationEvent(HttpRequestEvent $event, string
// Force "HTTP_HOST" and "SERVER_NAME" to match because of the "X_FORWARDED_HOST" header.
$parameters['HTTP_HOST'] = $parameters['SERVER_NAME'];

ksort($parameters);

return new self($content, $parameters);
}

Expand Down
Loading

0 comments on commit 46a2c31

Please sign in to comment.