Skip to content

Commit 2b7da31

Browse files
committed
Add server-side parameters to implementation
1 parent a3b1a84 commit 2b7da31

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

src/RequestHeaderParser.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ class RequestHeaderParser extends EventEmitter
1818
private $maxSize = 4096;
1919

2020
private $uri;
21+
private $remoteAddress;
2122

22-
public function __construct($localSocketUri = '')
23+
public function __construct($localSocketUri = '', $remoteAddress = '')
2324
{
2425
$this->uri = $localSocketUri;
26+
$this->remoteAddress = $remoteAddress;
2527
}
2628

2729
public function feed($data)
@@ -85,13 +87,34 @@ private function parseRequest($data)
8587

8688
// create new obj implementing ServerRequestInterface by preserving all
8789
// previous properties and restoring original request-target
90+
$serverParams = array(
91+
'REQUEST_TIME' => time(),
92+
'REQUEST_TIME_FLOAT' => microtime(true)
93+
);
94+
95+
if (!empty($this->remoteAddress)) {
96+
$remoteAddress = parse_url($this->remoteAddress);
97+
$serverParams['REMOTE_ADDR'] = $remoteAddress['host'];
98+
$serverParams['REMOTE_PORT'] = $remoteAddress['port'];
99+
}
100+
101+
if (!empty($this->uri)) {
102+
$localAddress = parse_url($this->uri);
103+
$serverParams['SERVER_ADDR'] = $localAddress['host'];
104+
$serverParams['SERVER_PORT'] = $localAddress['port'];
105+
if (isset($localAddress['scheme']) && $localAddress['scheme'] === 'https') {
106+
$serverParams['HTTPS'] = 'on';
107+
}
108+
}
109+
88110
$target = $request->getRequestTarget();
89111
$request = new ServerRequest(
90112
$request->getMethod(),
91113
$request->getUri(),
92114
$request->getHeaders(),
93115
$request->getBody(),
94-
$request->getProtocolVersion()
116+
$request->getProtocolVersion(),
117+
$serverParams
95118
);
96119
$request = $request->withRequestTarget($target);
97120

src/Server.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ public function handleConnection(ConnectionInterface $conn)
146146
{
147147
$that = $this;
148148
$parser = new RequestHeaderParser(
149-
($this->isConnectionEncrypted($conn) ? 'https://' : 'http://') . $conn->getLocalAddress()
149+
($this->isConnectionEncrypted($conn) ? 'https://' : 'http://') . $conn->getLocalAddress(),
150+
$conn->getRemoteAddress()
150151
);
151152

152153
$listener = array($parser, 'feed');
@@ -174,7 +175,7 @@ public function handleConnection(ConnectionInterface $conn)
174175
}
175176

176177
/** @internal */
177-
public function handleRequest(ConnectionInterface $conn, ServerRequestInterface $request)
178+
public function handleRequest(ConnectionInterface $conn, ServerRequest $request)
178179
{
179180
$contentLength = 0;
180181
$stream = new CloseProtectionStream($conn);
@@ -227,12 +228,6 @@ public function handleRequest(ConnectionInterface $conn, ServerRequestInterface
227228
$conn->write("HTTP/1.1 100 Continue\r\n\r\n");
228229
}
229230

230-
// attach remote ip to the request as metadata
231-
$request->remoteAddress = trim(
232-
parse_url('tcp://' . $conn->getRemoteAddress(), PHP_URL_HOST),
233-
'[]'
234-
);
235-
236231
$callback = $this->callback;
237232
$promise = new Promise(function ($resolve, $reject) use ($callback, $request) {
238233
$resolve($callback($request));

src/ServerRequest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ class ServerRequest extends Request implements ServerRequestInterface
1616
private $queryParams = array();
1717
private $parsedBody = null;
1818

19+
/**
20+
* @param null|string $method HTTP method for the request.
21+
* @param null|string|UriInterface $uri URI for the request.
22+
* @param array $headers Headers for the message.
23+
* @param string|resource|StreamInterface $body Message body.
24+
* @param string $protocolVersion HTTP protocol version.
25+
*
26+
* @throws InvalidArgumentException for an invalid URI
27+
*/
28+
public function __construct(
29+
$method,
30+
$uri,
31+
array $headers = array(),
32+
$body = null,
33+
$protocolVersion = '1.1',
34+
$serverParams = array()
35+
) {
36+
$this->serverParams = $serverParams;
37+
parent::__construct($method, $uri, $headers, $body, $protocolVersion);
38+
}
39+
1940
public function getServerParams()
2041
{
2142
return $this->serverParams;

tests/ServerTest.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,30 @@ public function testRequestEvent()
6969
$server = new Server($this->socket, function (ServerRequestInterface $request) use (&$i, &$requestAssertion) {
7070
$i++;
7171
$requestAssertion = $request;
72+
7273
return \React\Promise\resolve(new Response());
7374
});
7475

7576
$this->connection
76-
->expects($this->once())
77+
->expects($this->any())
7778
->method('getRemoteAddress')
78-
->willReturn('127.0.0.1');
79+
->willReturn('127.0.0.1:8080');
7980

8081
$this->socket->emit('connection', array($this->connection));
8182

8283
$data = $this->createGetRequest();
8384
$this->connection->emit('data', array($data));
8485

86+
$serverParams = $requestAssertion->getServerParams();
87+
8588
$this->assertSame(1, $i);
8689
$this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion);
8790
$this->assertSame('GET', $requestAssertion->getMethod());
8891
$this->assertSame('/', $requestAssertion->getRequestTarget());
8992
$this->assertSame('/', $requestAssertion->getUri()->getPath());
9093
$this->assertSame('http://example.com/', (string)$requestAssertion->getUri());
9194
$this->assertSame('example.com', $requestAssertion->getHeaderLine('Host'));
92-
$this->assertSame('127.0.0.1', $requestAssertion->remoteAddress);
95+
$this->assertSame('127.0.0.1', $serverParams['REMOTE_ADDR']);
9396
}
9497

9598
public function testRequestGetWithHostAndCustomPort()
@@ -288,7 +291,7 @@ public function testRequestWithoutHostEventUsesSocketAddress()
288291
});
289292

290293
$this->connection
291-
->expects($this->once())
294+
->expects($this->any())
292295
->method('getLocalAddress')
293296
->willReturn('127.0.0.1:80');
294297

@@ -2332,6 +2335,40 @@ function ($data) use (&$buffer) {
23322335
$this->assertInstanceOf('RuntimeException', $exception);
23332336
}
23342337

2338+
public function testServerRequestParams()
2339+
{
2340+
$requestValidation = null;
2341+
$server = new Server($this->socket, function (ServerRequestInterface $request) use (&$requestValidation) {
2342+
$requestValidation = $request;
2343+
return new Response();
2344+
});
2345+
2346+
$this->connection
2347+
->expects($this->any())
2348+
->method('getRemoteAddress')
2349+
->willReturn('192.168.1.2:80');
2350+
2351+
$this->connection
2352+
->expects($this->any())
2353+
->method('getLocalAddress')
2354+
->willReturn('127.0.0.1:8080');
2355+
2356+
$this->socket->emit('connection', array($this->connection));
2357+
2358+
$data = $this->createGetRequest();
2359+
2360+
$this->connection->emit('data', array($data));
2361+
2362+
$serverParams = $requestValidation->getServerParams();
2363+
2364+
$this->assertEquals('127.0.0.1', $serverParams['SERVER_ADDR']);
2365+
$this->assertEquals('8080', $serverParams['SERVER_PORT']);
2366+
$this->assertEquals('192.168.1.2', $serverParams['REMOTE_ADDR']);
2367+
$this->assertEquals('80', $serverParams['REMOTE_PORT']);
2368+
$this->assertNotNull($serverParams['REQUEST_TIME']);
2369+
$this->assertNotNull($serverParams['REQUEST_TIME_FLOAT']);
2370+
}
2371+
23352372
private function createGetRequest()
23362373
{
23372374
$data = "GET / HTTP/1.1\r\n";

0 commit comments

Comments
 (0)