@@ -23,7 +23,7 @@ This is an HTTP server which responds with `Hello World` to every request.
23
23
$loop = React\EventLoop\Factory::create();
24
24
$socket = new React\Socket\Server(8080, $loop);
25
25
26
- $http = new Server($socket, function (RequestInterface $request) {
26
+ $http = new Server($socket, function (ServerRequestInterface $request) {
27
27
return new Response(
28
28
200,
29
29
array('Content-Type' => 'text/plain'),
@@ -54,7 +54,7 @@ constructor with the respective [request](#request) and
54
54
``` php
55
55
$socket = new React\Socket\Server(8080, $loop);
56
56
57
- $http = new Server($socket, function (RequestInterface $request) {
57
+ $http = new Server($socket, function (ServerRequestInterface $request) {
58
58
return new Response(
59
59
200,
60
60
array('Content-Type' => 'text/plain'),
@@ -75,7 +75,7 @@ $socket = new React\Socket\SecureServer($socket, $loop, array(
75
75
'local_cert' => __DIR__ . '/localhost.pem'
76
76
));
77
77
78
- $http = new Server($socket, function (RequestInterface $request) {
78
+ $http = new Server($socket, function (ServerRequestInterface $request) {
79
79
return new Response(
80
80
200,
81
81
array('Content-Type' => 'text/plain'),
@@ -137,11 +137,13 @@ connections and then processing each incoming HTTP request.
137
137
The request object will be processed once the request headers have
138
138
been received by the client.
139
139
This 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
140
142
[ PSR-7 RequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface )
141
143
and will be passed to the callback function like this.
142
144
143
- ``` php
144
- $http = new Server($socket, function (RequestInterface $request) {
145
+ ``` php
146
+ $http = new Server($socket, function (ServerRequestInterface $request) {
145
147
$body = "The method of the request is: " . $request->getMethod();
146
148
$body .= "The requested path is: " . $request->getUri()->getPath();
147
149
@@ -153,8 +155,29 @@ $http = new Server($socket, function (RequestInterface $request) {
153
155
});
154
156
```
155
157
158
+ As mentioned before the request implements the ` ServerRequestInterface ` .
159
+ The ` Server ` will currently add several server-side parameters to the request like:
160
+
161
+ * ` server_address `
162
+ The current IP address of the server
163
+ * ` remote_address `
164
+ The IP address of the request sender
165
+ * ` remote_port `
166
+ Port of the request sender
167
+ * ` request_time `
168
+ Unix timestamp of the moment, the complete request header was received
169
+ * ` request_time_float `
170
+ Unix timestamp in microseconds of the moment, the complete request header wa
171
+ received
172
+
173
+ The cookies of the request, will also be added to the request object by the ` Server ` .
174
+ Use the ` getCookies ` method to receive these cookies
175
+
176
+ Any other parameters can be obtained by the request itself.
156
177
For more details about the request object, check out the documentation of
157
178
[ PSR-7 RequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface ) .
179
+ and
180
+ [ PSR-7 ServerRequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface )
158
181
159
182
Note that the request object will be processed once the request headers have
160
183
been received.
@@ -184,7 +207,7 @@ Instead, you should use the `ReactPHP ReadableStreamInterface` which
184
207
gives you access to the incoming request body as the individual chunks arrive:
185
208
186
209
``` php
187
- $http = new Server($socket, function (RequestInterface $request) {
210
+ $http = new Server($socket, function (ServerRequestInterface $request) {
188
211
return new Promise(function ($resolve, $reject) use ($request) {
189
212
$contentLength = 0;
190
213
$request->getBody()->on('data', function ($data) use (& $contentLength) {
@@ -248,7 +271,7 @@ Note that this value may be `null` if the request body size is unknown in
248
271
advance because the request message uses chunked transfer encoding.
249
272
250
273
``` php
251
- $http = new Server($socket, function (RequestInterface $request) {
274
+ $http = new Server($socket, function (ServerRequestInterface $request) {
252
275
$size = $request->getBody()->getSize();
253
276
if ($size === null) {
254
277
$body = 'The request does not contain an explicit length.';
@@ -308,7 +331,7 @@ but feel free to use any implemantation of the
308
331
` PSR-7 ResponseInterface ` you prefer.
309
332
310
333
``` php
311
- $http = new Server($socket, function (RequestInterface $request) {
334
+ $http = new Server($socket, function (ServerRequestInterface $request) {
312
335
return new Response(
313
336
200,
314
337
array('Content-Type' => 'text/plain'),
@@ -327,7 +350,7 @@ To prevent this you SHOULD use a
327
350
This example shows how such a long-term action could look like:
328
351
329
352
``` php
330
- $server = new \React\Http\Server($socket, function (RequestInterface $request) use ($loop) {
353
+ $server = new \React\Http\Server($socket, function (ServerRequestInterface $request) use ($loop) {
331
354
return new Promise(function ($resolve, $reject) use ($request, $loop) {
332
355
$loop->addTimer(1.5, function() use ($loop, $resolve) {
333
356
$response = new Response(
@@ -355,7 +378,7 @@ Note that other implementations of the `PSR-7 ResponseInterface` likely
355
378
only support string.
356
379
357
380
``` php
358
- $server = new Server($socket, function (RequestInterface $request) use ($loop) {
381
+ $server = new Server($socket, function (ServerRequestInterface $request) use ($loop) {
359
382
$stream = new ReadableStream();
360
383
361
384
$timer = $loop->addPeriodicTimer(0.5, function () use ($stream) {
@@ -389,7 +412,7 @@ If you know the length of your stream body, you MAY specify it like this instead
389
412
390
413
``` php
391
414
$stream = new ReadableStream()
392
- $server = new Server($socket, function (RequestInterface $request) use ($loop, $stream) {
415
+ $server = new Server($socket, function (ServerRequestInterface $request) use ($loop, $stream) {
393
416
return new Response(
394
417
200,
395
418
array(
@@ -437,7 +460,7 @@ A `Date` header will be automatically added with the system date and time if non
437
460
You can add a custom ` Date ` header yourself like this:
438
461
439
462
``` php
440
- $server = new Server($socket, function (RequestInterface $request) {
463
+ $server = new Server($socket, function (ServerRequestInterface $request) {
441
464
return new Response(200, array('Date' => date('D, d M Y H:i:s T')));
442
465
});
443
466
```
@@ -446,7 +469,7 @@ If you don't have a appropriate clock to rely on, you should
446
469
unset this header with an empty string:
447
470
448
471
``` php
449
- $server = new Server($socket, function (RequestInterface $request) {
472
+ $server = new Server($socket, function (ServerRequestInterface $request) {
450
473
return new Response(200, array('Date' => ''));
451
474
});
452
475
```
@@ -455,7 +478,7 @@ Note that it will automatically assume a `X-Powered-By: react/alpha` header
455
478
unless your specify a custom ` X-Powered-By ` header yourself:
456
479
457
480
``` php
458
- $server = new Server($socket, function (RequestInterface $request) {
481
+ $server = new Server($socket, function (ServerRequestInterface $request) {
459
482
return new Response(200, array('X-Powered-By' => 'PHP 3'));
460
483
});
461
484
```
@@ -464,7 +487,7 @@ If you do not want to send this header at all, you can use an empty string as
464
487
value like this:
465
488
466
489
``` php
467
- $server = new Server($socket, function (RequestInterface $request) {
490
+ $server = new Server($socket, function (ServerRequestInterface $request) {
468
491
return new Response(200, array('X-Powered-By' => ''));
469
492
});
470
493
```
0 commit comments