Skip to content

Commit 51f2214

Browse files
authored
Merge pull request #307 from clue-labs/unix
Fix Server to skip SERVER_ADDR params for Unix domain sockets (UDS)
2 parents 4a80825 + c90efb6 commit 51f2214

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ $server = new Server(function (ServerRequestInterface $request) {
270270

271271
See also [example #3](examples).
272272

273+
> Advanced: Note that address parameters will not be set if you're listening on
274+
a Unix domain socket (UDS) path as this protocol lacks the concept of
275+
host/port.
276+
273277
#### Query parameters
274278

275279
The `getQueryParams(): array` method can be used to get the query parameters

src/Io/RequestHeaderParser.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,23 @@ private function parseRequest($headers)
9797
'REQUEST_TIME_FLOAT' => microtime(true)
9898
);
9999

100+
// apply REMOTE_ADDR and REMOTE_PORT if source address is known
101+
// address should always be known, unless this is over Unix domain sockets (UDS)
100102
if ($this->remoteSocketUri !== null) {
101103
$remoteAddress = parse_url($this->remoteSocketUri);
102104
$serverParams['REMOTE_ADDR'] = $remoteAddress['host'];
103105
$serverParams['REMOTE_PORT'] = $remoteAddress['port'];
104106
}
105107

108+
// apply SERVER_ADDR and SERVER_PORT if server address is known
109+
// address should always be known, even for Unix domain sockets (UDS)
110+
// but skip UDS as it doesn't have a concept of host/port.s
106111
if ($this->localSocketUri !== null) {
107112
$localAddress = parse_url($this->localSocketUri);
108-
$serverParams['SERVER_ADDR'] = $localAddress['host'];
109-
$serverParams['SERVER_PORT'] = $localAddress['port'];
113+
if (isset($localAddress['host'], $localAddress['port'])) {
114+
$serverParams['SERVER_ADDR'] = $localAddress['host'];
115+
$serverParams['SERVER_PORT'] = $localAddress['port'];
116+
}
110117
if (isset($localAddress['scheme']) && $localAddress['scheme'] === 'https') {
111118
$serverParams['HTTPS'] = 'on';
112119
}

tests/Io/RequestHeaderParserTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,33 @@ public function testServerParamsWillBeSetOnHttpRequest()
408408
$this->assertEquals('8001', $serverParams['REMOTE_PORT']);
409409
}
410410

411+
public function testServerParamsWillNotSetRemoteAddressForUnixDomainSockets()
412+
{
413+
$request = null;
414+
415+
$parser = new RequestHeaderParser(
416+
'unix://./server.sock',
417+
null
418+
);
419+
420+
$parser->on('headers', function ($parsedRequest) use (&$request) {
421+
$request = $parsedRequest;
422+
});
423+
424+
$parser->feed("GET /foo HTTP/1.0\r\nHost: example.com\r\n\r\n");
425+
$serverParams = $request->getServerParams();
426+
427+
$this->assertArrayNotHasKey('HTTPS', $serverParams);
428+
$this->assertNotEmpty($serverParams['REQUEST_TIME']);
429+
$this->assertNotEmpty($serverParams['REQUEST_TIME_FLOAT']);
430+
431+
$this->assertArrayNotHasKey('SERVER_ADDR', $serverParams);
432+
$this->assertArrayNotHasKey('SERVER_PORT', $serverParams);
433+
434+
$this->assertArrayNotHasKey('REMOTE_ADDR', $serverParams);
435+
$this->assertArrayNotHasKey('REMOTE_PORT', $serverParams);
436+
}
437+
411438
public function testServerParamsWontBeSetOnMissingUrls()
412439
{
413440
$request = null;

0 commit comments

Comments
 (0)