Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/ConfiguratorServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Http;

use function time;
use function microtime;

class ConfiguratorServer
Comment thread
root-aza marked this conversation as resolved.
Outdated
{
/**
* Returns altered copy of _SERVER variable. Sets ip-address,
* request-time and other values.
*
* @return non-empty-array<array-key|string, mixed|string>
*/
public function configure(Request $request): array
Comment thread
root-aza marked this conversation as resolved.
Outdated
{
$_SERVER['REQUEST_URI'] = $request->uri;
$_SERVER['REQUEST_TIME'] = time();
$_SERVER['REQUEST_TIME_FLOAT'] = microtime(true);
$_SERVER['REMOTE_ADDR'] = $request->getRemoteAddr();
$_SERVER['REQUEST_METHOD'] = $request->method;
$_SERVER['HTTP_USER_AGENT'] = '';

foreach ($request->headers as $key => $value) {
$key = \strtoupper(\str_replace('-', '_', $key));

if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
Comment thread
root-aza marked this conversation as resolved.
Outdated
$_SERVER[$key] = \implode(', ', $value);

continue;
}

$_SERVER['HTTP_' . $key] = \implode(', ', $value);
}

return $_SERVER;
Comment thread
roxblnfk marked this conversation as resolved.
Outdated
}
}
45 changes: 2 additions & 43 deletions src/PSR7Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class PSR7Worker implements PSR7WorkerInterface

private readonly HttpWorker $httpWorker;

private readonly array $originalServer;

/**
* @var string[] Valid values for HTTP protocol version
*/
Expand All @@ -44,9 +42,9 @@ public function __construct(
private readonly ServerRequestFactoryInterface $requestFactory,
private readonly StreamFactoryInterface $streamFactory,
private readonly UploadedFileFactoryInterface $uploadsFactory,
private readonly ConfiguratorServer $configuratorServer = new ConfiguratorServer(),
) {
$this->httpWorker = new HttpWorker($worker);
$this->originalServer = $_SERVER;
}

public function getWorker(): WorkerInterface
Expand All @@ -69,7 +67,7 @@ public function waitRequest(): ?ServerRequestInterface
return null;
}

$_SERVER = $this->configureServer($httpRequest);
$_SERVER = $this->configuratorServer->configure($httpRequest);
Comment thread
roxblnfk marked this conversation as resolved.
Outdated

return $this->mapRequest($httpRequest, $_SERVER);
}
Expand Down Expand Up @@ -117,45 +115,6 @@ private function streamToGenerator(StreamInterface $stream): Generator
}
}

/**
* Returns altered copy of _SERVER variable. Sets ip-address,
* request-time and other values.
*
* @return non-empty-array<array-key|string, mixed|string>
*/
protected function configureServer(Request $request): array
Comment thread
root-aza marked this conversation as resolved.
{
$server = $this->originalServer;

$server['REQUEST_URI'] = $request->uri;
$server['REQUEST_TIME'] = $this->timeInt();
$server['REQUEST_TIME_FLOAT'] = $this->timeFloat();
$server['REMOTE_ADDR'] = $request->getRemoteAddr();
$server['REQUEST_METHOD'] = $request->method;

$server['HTTP_USER_AGENT'] = '';
foreach ($request->headers as $key => $value) {
$key = \strtoupper(\str_replace('-', '_', $key));
if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
$server[$key] = \implode(', ', $value);
} else {
$server['HTTP_' . $key] = \implode(', ', $value);
}
}

return $server;
}

protected function timeInt(): int
{
return \time();
}

protected function timeFloat(): float
{
return \microtime(true);
}

/**
* @throws \JsonException
*/
Expand Down