Skip to content

Commit a734671

Browse files
committed
Refactor GlobalState and PSR7Worker to cache and enrich $_SERVER variables
1 parent b439591 commit a734671

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"jetbrains/phpstorm-attributes": "^1.0",
5151
"nyholm/psr7": "^1.3",
5252
"phpunit/phpunit": "^10.0",
53+
"spiral/dumper": "^3.3",
5354
"symfony/process": "^6.2 || ^7.0",
5455
"vimeo/psalm": "^5.9"
5556
},

src/GlobalState.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,48 @@
1313
final class GlobalState
1414
{
1515
/**
16-
* Sets ip-address, request-time and other values.
17-
*
18-
* @return non-empty-array<array-key|string, mixed|string>
16+
* @var array<array-key, mixed> Cached state of the $_SERVER superglobal.
1917
*/
20-
public static function populateServer(Request $request): array
21-
{
22-
/** @var non-empty-array<array-key|string, mixed|string>|null $originalServer */
23-
static $originalServer = null;
18+
private static array $cachedServer = [];
2419

25-
if ($originalServer == null) {
26-
$originalServer = $_SERVER;
27-
}
20+
/**
21+
* Cache superglobal $_SERVER to avoid state leaks between requests.
22+
*/
23+
public static function cacheServerVars(): void
24+
{
25+
self::$cachedServer = $_SERVER;
26+
}
2827

29-
$newServer = $originalServer;
28+
/**
29+
* Enrich cached $_SERVER with data from the request.
30+
*
31+
* @return non-empty-array<array-key, mixed> Cached $_SERVER data enriched with request data.
32+
*/
33+
public static function enrichServerVars(Request $request): array
34+
{
35+
$server = self::$cachedServer;
3036

31-
$newServer['REQUEST_URI'] = $request->uri;
32-
$newServer['REQUEST_TIME'] = time();
33-
$newServer['REQUEST_TIME_FLOAT'] = microtime(true);
34-
$newServer['REMOTE_ADDR'] = $request->getRemoteAddr();
35-
$newServer['REQUEST_METHOD'] = $request->method;
36-
$newServer['HTTP_USER_AGENT'] = '';
37+
$server['REQUEST_URI'] = $request->uri;
38+
$server['REQUEST_TIME'] = time();
39+
$server['REQUEST_TIME_FLOAT'] = microtime(true);
40+
$server['REMOTE_ADDR'] = $request->getRemoteAddr();
41+
$server['REQUEST_METHOD'] = $request->method;
42+
$server['HTTP_USER_AGENT'] = '';
3743

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

4147
if ($key == 'CONTENT_TYPE' || $key == 'CONTENT_LENGTH') {
42-
$newServer[$key] = implode(', ', $value);
48+
$server[$key] = implode(', ', $value);
4349

4450
continue;
4551
}
4652

47-
$newServer['HTTP_' . $key] = implode(', ', $value);
53+
$server['HTTP_' . $key] = implode(', ', $value);
4854
}
4955

50-
return $newServer;
56+
return $server;
5157
}
5258
}
59+
60+
GlobalState::cacheServerVars();

src/PSR7Worker.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,24 @@ public function getHttpWorker(): HttpWorker
6060
/**
6161
* @psalm-suppress DeprecatedMethod
6262
*
63+
* @param bool $populateServer Whether to populate $_SERVER superglobal.
64+
*
6365
* @throws \JsonException
6466
*/
65-
public function waitRequest(): ?ServerRequestInterface
67+
public function waitRequest(bool $populateServer = true): ?ServerRequestInterface
6668
{
6769
$httpRequest = $this->httpWorker->waitRequest();
6870
if ($httpRequest === null) {
6971
return null;
7072
}
7173

72-
$_SERVER = $this->configureServer($httpRequest);
74+
$vars = $this->configureServer($httpRequest);
75+
76+
if ($populateServer) {
77+
$_SERVER = $vars;
78+
}
7379

74-
return $this->mapRequest($httpRequest, $_SERVER);
80+
return $this->mapRequest($httpRequest, $vars);
7581
}
7682

7783
/**
@@ -92,7 +98,7 @@ public function respond(ResponseInterface $response): void
9298

9399
/**
94100
* @return Generator<mixed, scalar|Stringable, mixed, Stringable|scalar|null> Compatible
95-
* with {@see \Spiral\RoadRunner\Http\HttpWorker::respondStream()}.
101+
* with {@see HttpWorker::respondStream}.
96102
*/
97103
private function streamToGenerator(StreamInterface $stream): Generator
98104
{
@@ -121,16 +127,13 @@ private function streamToGenerator(StreamInterface $stream): Generator
121127
* Returns altered copy of _SERVER variable. Sets ip-address,
122128
* request-time and other values.
123129
*
124-
* @deprecated
125-
*
126130
* @return non-empty-array<array-key|string, mixed|string>
127131
*/
128132
protected function configureServer(Request $request): array
129133
{
130-
return GlobalState::populateServer($request);
134+
return GlobalState::enrichServerVars($request);
131135
}
132136

133-
134137
/**
135138
* @deprecated
136139
*/
@@ -139,7 +142,6 @@ protected function timeInt(): int
139142
return \time();
140143
}
141144

142-
143145
/**
144146
* @deprecated
145147
*/

0 commit comments

Comments
 (0)