@@ -23,7 +23,7 @@ This is an HTTP server which responds with `Hello World` to every request.
2323$loop = React\EventLoop\Factory::create();
2424$socket = new React\Socket\Server(8080, $loop);
2525
26- $http = new Server($socket, function (RequestInterface $request) {
26+ $http = new Server($socket, function (ServerRequestInterface $request) {
2727 return new Response(
2828 200,
2929 array('Content-Type' => 'text/plain'),
@@ -54,7 +54,7 @@ constructor with the respective [request](#request) and
5454``` php
5555$socket = new React\Socket\Server(8080, $loop);
5656
57- $http = new Server($socket, function (RequestInterface $request) {
57+ $http = new Server($socket, function (ServerRequestInterface $request) {
5858 return new Response(
5959 200,
6060 array('Content-Type' => 'text/plain'),
@@ -75,7 +75,7 @@ $socket = new React\Socket\SecureServer($socket, $loop, array(
7575 'local_cert' => __DIR__ . '/localhost.pem'
7676));
7777
78- $http = new Server($socket, function (RequestInterface $request) {
78+ $http = new Server($socket, function (ServerRequestInterface $request) {
7979 return new Response(
8080 200,
8181 array('Content-Type' => 'text/plain'),
@@ -137,11 +137,13 @@ connections and then processing each incoming HTTP request.
137137The request object will be processed once the request headers have
138138been received by the client.
139139This request object implements the
140+ [ PSR-7 ServerRequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface )
141+ which in turn extends the
140142[ PSR-7 RequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface )
141143and will be passed to the callback function like this.
142144
143- ``` php
144- $http = new Server($socket, function (RequestInterface $request) {
145+ ``` php
146+ $http = new Server($socket, function (ServerRequestInterface $request) {
145147 $body = "The method of the request is: " . $request->getMethod();
146148 $body .= "The requested path is: " . $request->getUri()->getPath();
147149
@@ -155,6 +157,8 @@ $http = new Server($socket, function (RequestInterface $request) {
155157
156158For more details about the request object, check out the documentation of
157159[ PSR-7 RequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface ) .
160+ and
161+ [ PSR-7 ServerRequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface )
158162
159163Note that the request object will be processed once the request headers have
160164been received.
@@ -184,7 +188,7 @@ Instead, you should use the `ReactPHP ReadableStreamInterface` which
184188gives you access to the incoming request body as the individual chunks arrive:
185189
186190``` php
187- $http = new Server($socket, function (RequestInterface $request) {
191+ $http = new Server($socket, function (ServerRequestInterface $request) {
188192 return new Promise(function ($resolve, $reject) use ($request) {
189193 $contentLength = 0;
190194 $request->getBody()->on('data', function ($data) use (& $contentLength) {
@@ -248,7 +252,7 @@ Note that this value may be `null` if the request body size is unknown in
248252advance because the request message uses chunked transfer encoding.
249253
250254``` php
251- $http = new Server($socket, function (RequestInterface $request) {
255+ $http = new Server($socket, function (ServerRequestInterface $request) {
252256 $size = $request->getBody()->getSize();
253257 if ($size === null) {
254258 $body = 'The request does not contain an explicit length.';
@@ -269,6 +273,26 @@ $http = new Server($socket, function (RequestInterface $request) {
269273});
270274```
271275
276+ As mentioned before the request implements the ` ServerRequestInterface ` .
277+ The ` Server ` will add server-side parameters like the remote address,
278+ remote port or cookies to this object.
279+ The remote address(` remote_address ` ) and remote port(` remote_port ` )
280+ will be added by the ` Server ` and can be received by the ` getServerParams() `
281+ method.
282+
283+ ``` php
284+ $http = new Server($socket, function (ServerRequestInterface $request) {
285+ $body = "The remote ip is: " . $request->getServerParams()['remote_address'];
286+ $body = "The remote port is: " . $request->getServerParams()['remote_port'];
287+
288+ return new Response(
289+ 200,
290+ array('Content-Type' => 'text/plain'),
291+ $body
292+ );
293+ });
294+ ```
295+
272296Note that the server supports * any* request method (including custom and non-
273297standard ones) and all request-target formats defined in the HTTP specs for each
274298respective method.
@@ -308,7 +332,7 @@ but feel free to use any implemantation of the
308332` PSR-7 ResponseInterface ` you prefer.
309333
310334``` php
311- $http = new Server($socket, function (RequestInterface $request) {
335+ $http = new Server($socket, function (ServerRequestInterface $request) {
312336 return new Response(
313337 200,
314338 array('Content-Type' => 'text/plain'),
@@ -327,7 +351,7 @@ To prevent this you SHOULD use a
327351This example shows how such a long-term action could look like:
328352
329353``` php
330- $server = new \React\Http\Server($socket, function (RequestInterface $request) use ($loop) {
354+ $server = new \React\Http\Server($socket, function (ServerRequestInterface $request) use ($loop) {
331355 return new Promise(function ($resolve, $reject) use ($request, $loop) {
332356 $loop->addTimer(1.5, function() use ($loop, $resolve) {
333357 $response = new Response(
@@ -355,7 +379,7 @@ Note that other implementations of the `PSR-7 ResponseInterface` likely
355379only support string.
356380
357381``` php
358- $server = new Server($socket, function (RequestInterface $request) use ($loop) {
382+ $server = new Server($socket, function (ServerRequestInterface $request) use ($loop) {
359383 $stream = new ReadableStream();
360384
361385 $timer = $loop->addPeriodicTimer(0.5, function () use ($stream) {
@@ -389,7 +413,7 @@ If you know the length of your stream body, you MAY specify it like this instead
389413
390414``` php
391415$stream = new ReadableStream()
392- $server = new Server($socket, function (RequestInterface $request) use ($loop, $stream) {
416+ $server = new Server($socket, function (ServerRequestInterface $request) use ($loop, $stream) {
393417 return new Response(
394418 200,
395419 array(
@@ -437,7 +461,7 @@ A `Date` header will be automatically added with the system date and time if non
437461You can add a custom ` Date ` header yourself like this:
438462
439463``` php
440- $server = new Server($socket, function (RequestInterface $request) {
464+ $server = new Server($socket, function (ServerRequestInterface $request) {
441465 return new Response(200, array('Date' => date('D, d M Y H:i:s T')));
442466});
443467```
@@ -446,7 +470,7 @@ If you don't have a appropriate clock to rely on, you should
446470unset this header with an empty string:
447471
448472``` php
449- $server = new Server($socket, function (RequestInterface $request) {
473+ $server = new Server($socket, function (ServerRequestInterface $request) {
450474 return new Response(200, array('Date' => ''));
451475});
452476```
@@ -455,7 +479,7 @@ Note that it will automatically assume a `X-Powered-By: react/alpha` header
455479unless your specify a custom ` X-Powered-By ` header yourself:
456480
457481``` php
458- $server = new Server($socket, function (RequestInterface $request) {
482+ $server = new Server($socket, function (ServerRequestInterface $request) {
459483 return new Response(200, array('X-Powered-By' => 'PHP 3'));
460484});
461485```
@@ -464,7 +488,7 @@ If you do not want to send this header at all, you can use an empty string as
464488value like this:
465489
466490``` php
467- $server = new Server($socket, function (RequestInterface $request) {
491+ $server = new Server($socket, function (ServerRequestInterface $request) {
468492 return new Response(200, array('X-Powered-By' => ''));
469493});
470494```
0 commit comments