From 3eab5189a7309813530604080e3ea4d5f6137e5c Mon Sep 17 00:00:00 2001 From: Niels Theen Date: Sun, 23 Apr 2017 02:19:49 +0200 Subject: [PATCH] Update docs and add example --- README.md | 37 ++++++++++++++++++- examples/02-client-ip.php | 25 +++++++++++++ ...unt-visitors.php => 03-count-visitors.php} | 0 ...am-response.php => 04-stream-response.php} | 0 ...ream-request.php => 05-stream-request.php} | 0 ...ror-handling.php => 06-error-handling.php} | 0 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 examples/02-client-ip.php rename examples/{02-count-visitors.php => 03-count-visitors.php} (100%) rename examples/{03-stream-response.php => 04-stream-response.php} (100%) rename examples/{04-stream-request.php => 05-stream-request.php} (100%) rename examples/{05-error-handling.php => 06-error-handling.php} (100%) diff --git a/README.md b/README.md index d8155817..d4d35e39 100644 --- a/README.md +++ b/README.md @@ -155,12 +155,47 @@ $http = new Server($socket, function (ServerRequestInterface $request) { }); ``` +The `getServerParams(): mixed[]` method can be used to +get server-side parameters similar to the `$_SERVER` variable. +The following parameters are currently available: + +* `REMOTE_ADDR` + The IP address of the request sender +* `REMOTE_PORT` + Port of the request sender +* `SERVER_ADDR` + The IP address of the server +* `SERVER_PORT` + The port of the server +* `REQUEST_TIME` + Unix timestamp when the complete request header has been received, + as integer similar to `time()` +* `REQUEST_TIME_FLOAT` + Unix timestamp when the complete request header has been received, + as float similar to `microtime(true)` +* `HTTPS` + Set to 'on' if the request used HTTPS, otherwise it won't be set + +```php +$http = new Server($socket, function (ServerRequestInterface $request) { + $body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR']; + + return new Response( + 200, + array('Content-Type' => 'text/plain'), + $body + ); +}); +``` + +See also [example #2](examples). + For more details about the request object, check out the documentation of [PSR-7 ServerRequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface) and [PSR-7 RequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface). -> Currently the the server params, cookies and uploaded files are not added by the +> Currently the cookies and uploaded files are not added by the `Server`, but you can add these parameters by yourself using the given methods. The next versions of this project will cover these features. diff --git a/examples/02-client-ip.php b/examples/02-client-ip.php new file mode 100644 index 00000000..31b7ad32 --- /dev/null +++ b/examples/02-client-ip.php @@ -0,0 +1,25 @@ +getServerParams()['REMOTE_ADDR']; + + return new Response( + 200, + array('Content-Type' => 'text/plain'), + $body + ); +}); + +echo 'Listening on http://' . $socket->getAddress() . PHP_EOL; + +$loop->run(); diff --git a/examples/02-count-visitors.php b/examples/03-count-visitors.php similarity index 100% rename from examples/02-count-visitors.php rename to examples/03-count-visitors.php diff --git a/examples/03-stream-response.php b/examples/04-stream-response.php similarity index 100% rename from examples/03-stream-response.php rename to examples/04-stream-response.php diff --git a/examples/04-stream-request.php b/examples/05-stream-request.php similarity index 100% rename from examples/04-stream-request.php rename to examples/05-stream-request.php diff --git a/examples/05-error-handling.php b/examples/06-error-handling.php similarity index 100% rename from examples/05-error-handling.php rename to examples/06-error-handling.php