@@ -32,7 +32,9 @@ $loop = React\EventLoop\Factory::create();
3232$server = new Server(function (ServerRequestInterface $request) {
3333 return new Response(
3434 200,
35- array('Content-Type' => 'text/plain'),
35+ array(
36+ 'Content-Type' => 'text/plain'
37+ ),
3638 "Hello World!\n"
3739 );
3840});
@@ -49,6 +51,28 @@ See also the [examples](examples).
4951
5052### Server
5153
54+ The ` Server ` class is responsible for handling incoming connections and then
55+ processing each incoming HTTP request.
56+
57+ It buffers and parses the complete incoming HTTP request in memory. Once the
58+ complete request has been received, it will invoke the request handler.
59+
60+ For each request, it executes the callback function passed to the
61+ constructor with the respective [ request] ( #request ) object and expects
62+ a respective [ response] ( #response ) object in return.
63+
64+ ``` php
65+ $server = new Server(function (ServerRequestInterface $request) {
66+ return new Response(
67+ 200,
68+ array(
69+ 'Content-Type' => 'text/plain'
70+ ),
71+ "Hello World!\n"
72+ );
73+ });
74+ ```
75+
5276For most users a server that buffers and parses a requests before handling it over as a
5377PSR-7 request is what they want. The ` Server ` facade takes care of that, and takes the more
5478advanced configuration out of hand. Under the hood it uses [ StreamingServer] ( #streamingserver )
@@ -64,9 +88,13 @@ division of half of `memory_limit` by `memory_limit` rounded up.
6488
6589### StreamingServer
6690
67- The ` StreamingServer ` class is responsible for handling incoming connections and then
91+ The advanced ` StreamingServer ` class is responsible for handling incoming connections and then
6892processing each incoming HTTP request.
6993
94+ Unlike the [ ` Server ` ] ( #server ) class, it does not buffer and parse the incoming
95+ HTTP request body by default. This means that the request handler will be
96+ invoked with a streaming request body.
97+
7098For each request, it executes the callback function passed to the
7199constructor with the respective [ request] ( #request ) object and expects
72100a respective [ response] ( #response ) object in return.
@@ -75,7 +103,9 @@ a respective [response](#response) object in return.
75103$server = new StreamingServer(function (ServerRequestInterface $request) {
76104 return new Response(
77105 200,
78- array('Content-Type' => 'text/plain'),
106+ array(
107+ 'Content-Type' => 'text/plain'
108+ ),
79109 "Hello World!\n"
80110 );
81111});
@@ -160,10 +190,11 @@ Check out [request](#request) for more details.
160190
161191### Request
162192
163- An seen above, the ` StreamingServer ` class is responsible for handling incoming
164- connections and then processing each incoming HTTP request.
193+ As seen above, the [ ` Server ` ] ( #server ) and [ ` StreamingServer ` ] ( #streamingserver )
194+ classes are responsible for handling incoming connections and then processing
195+ each incoming HTTP request.
165196
166- The request object will be processed once the request headers have
197+ The request object will be processed once the request has
167198been received by the client.
168199This request object implements the
169200[ PSR-7 ServerRequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface )
@@ -172,13 +203,15 @@ which in turn extends the
172203and will be passed to the callback function like this.
173204
174205 ``` php
175- $server = new StreamingServer (function (ServerRequestInterface $request) {
206+ $server = new Server (function (ServerRequestInterface $request) {
176207 $body = "The method of the request is: " . $request->getMethod();
177208 $body .= "The requested path is: " . $request->getUri()->getPath();
178209
179210 return new Response(
180211 200,
181- array('Content-Type' => 'text/plain'),
212+ array(
213+ 'Content-Type' => 'text/plain'
214+ ),
182215 $body
183216 );
184217});
@@ -206,12 +239,14 @@ The following parameters are currently available:
206239 Set to 'on' if the request used HTTPS, otherwise it won't be set
207240
208241``` php
209- $server = new StreamingServer (function (ServerRequestInterface $request) {
242+ $server = new Server (function (ServerRequestInterface $request) {
210243 $body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];
211244
212245 return new Response(
213246 200,
214- array('Content-Type' => 'text/plain'),
247+ array(
248+ 'Content-Type' => 'text/plain'
249+ ),
215250 $body
216251 );
217252});
@@ -223,7 +258,7 @@ The `getQueryParams(): array` method can be used to get the query parameters
223258similiar to the ` $_GET ` variable.
224259
225260``` php
226- $server = new StreamingServer (function (ServerRequestInterface $request) {
261+ $server = new Server (function (ServerRequestInterface $request) {
227262 $queryParams = $request->getQueryParams();
228263
229264 $body = 'The query parameter "foo" is not set. Click the following link ';
@@ -235,7 +270,9 @@ $server = new StreamingServer(function (ServerRequestInterface $request) {
235270
236271 return new Response(
237272 200,
238- array('Content-Type' => 'text/html'),
273+ array(
274+ 'Content-Type' => 'text/html'
275+ ),
239276 $body
240277 );
241278});
@@ -296,7 +333,9 @@ $server = new StreamingServer(function (ServerRequestInterface $request) {
296333 $request->getBody()->on('end', function () use ($resolve, & $contentLength){
297334 $response = new Response(
298335 200,
299- array('Content-Type' => 'text/plain'),
336+ array(
337+ 'Content-Type' => 'text/plain'
338+ ),
300339 "The length of the submitted request body is: " . $contentLength
301340 );
302341 $resolve($response);
@@ -306,7 +345,9 @@ $server = new StreamingServer(function (ServerRequestInterface $request) {
306345 $request->getBody()->on('error', function (\Exception $exception) use ($resolve, & $contentLength) {
307346 $response = new Response(
308347 400,
309- array('Content-Type' => 'text/plain'),
348+ array(
349+ 'Content-Type' => 'text/plain'
350+ ),
310351 "An error occured while reading at length: " . $contentLength
311352 );
312353 $resolve($response);
@@ -358,14 +399,18 @@ $server = new StreamingServer(function (ServerRequestInterface $request) {
358399
359400 return new Response(
360401 411,
361- array('Content-Type' => 'text/plain'),
402+ array(
403+ 'Content-Type' => 'text/plain'
404+ ),
362405 $body
363406 );
364407 }
365408
366409 return new Response(
367410 200,
368- array('Content-Type' => 'text/plain'),
411+ array(
412+ 'Content-Type' => 'text/plain'
413+ ),
369414 "Request body size: " . $size . " bytes\n"
370415 );
371416});
@@ -404,15 +449,17 @@ The `getCookieParams(): string[]` method can be used to
404449get all cookies sent with the current request.
405450
406451``` php
407- $server = new StreamingServer (function (ServerRequestInterface $request) {
452+ $server = new Server (function (ServerRequestInterface $request) {
408453 $key = 'react\php';
409454
410455 if (isset($request->getCookieParams()[$key])) {
411456 $body = "Your cookie value is: " . $request->getCookieParams()[$key];
412457
413458 return new Response(
414459 200,
415- array('Content-Type' => 'text/plain'),
460+ array(
461+ 'Content-Type' => 'text/plain'
462+ ),
416463 $body
417464 );
418465 }
@@ -439,9 +486,9 @@ See also [example #5](examples) for more details.
439486
440487### Response
441488
442- The callback function passed to the constructor of the [ StreamingServer ] ( #server )
443- is responsible for processing the request and returning a response,
444- which will be delivered to the client.
489+ The callback function passed to the constructor of the [ ` Server ` ] ( #server ) or
490+ advanced [ ` StreamingServer ` ] ( #server ) is responsible for processing the request
491+ and returning a response, which will be delivered to the client.
445492This function MUST return an instance implementing
446493[ PSR-7 ResponseInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#33-psrhttpmessageresponseinterface )
447494object or a
@@ -455,10 +502,12 @@ but feel free to use any implemantation of the
455502` PSR-7 ResponseInterface ` you prefer.
456503
457504``` php
458- $server = new StreamingServer (function (ServerRequestInterface $request) {
505+ $server = new Server (function (ServerRequestInterface $request) {
459506 return new Response(
460507 200,
461- array('Content-Type' => 'text/plain'),
508+ array(
509+ 'Content-Type' => 'text/plain'
510+ ),
462511 "Hello World!\n"
463512 );
464513});
@@ -474,12 +523,14 @@ To prevent this you SHOULD use a
474523This example shows how such a long-term action could look like:
475524
476525``` php
477- $server = new StreamingServer (function (ServerRequestInterface $request) use ($loop) {
526+ $server = new Server (function (ServerRequestInterface $request) use ($loop) {
478527 return new Promise(function ($resolve, $reject) use ($request, $loop) {
479528 $loop->addTimer(1.5, function() use ($loop, $resolve) {
480529 $response = new Response(
481530 200,
482- array('Content-Type' => 'text/plain'),
531+ array(
532+ 'Content-Type' => 'text/plain'
533+ ),
483534 "Hello world"
484535 );
485536 $resolve($response);
@@ -507,7 +558,7 @@ Note that other implementations of the `PSR-7 ResponseInterface` likely
507558only support strings.
508559
509560``` php
510- $server = new StreamingServer (function (ServerRequestInterface $request) use ($loop) {
561+ $server = new Server (function (ServerRequestInterface $request) use ($loop) {
511562 $stream = new ThroughStream();
512563
513564 $timer = $loop->addPeriodicTimer(0.5, function () use ($stream) {
@@ -519,7 +570,13 @@ $server = new StreamingServer(function (ServerRequestInterface $request) use ($l
519570 $stream->emit('end');
520571 });
521572
522- return new Response(200, array('Content-Type' => 'text/plain'), $stream);
573+ return new Response(
574+ 200,
575+ array(
576+ 'Content-Type' => 'text/plain'
577+ ),
578+ $stream
579+ );
523580});
524581```
525582
@@ -550,7 +607,7 @@ If you know the length of your stream body, you MAY specify it like this instead
550607
551608``` php
552609$stream = new ThroughStream()
553- $server = new StreamingServer (function (ServerRequestInterface $request) use ($stream) {
610+ $server = new Server (function (ServerRequestInterface $request) use ($stream) {
554611 return new Response(
555612 200,
556613 array(
@@ -635,35 +692,55 @@ A `Date` header will be automatically added with the system date and time if non
635692You can add a custom ` Date ` header yourself like this:
636693
637694``` php
638- $server = new StreamingServer(function (ServerRequestInterface $request) {
639- return new Response(200, array('Date' => date('D, d M Y H:i:s T')));
695+ $server = new Server(function (ServerRequestInterface $request) {
696+ return new Response(
697+ 200,
698+ array(
699+ 'Date' => date('D, d M Y H:i:s T')
700+ )
701+ );
640702});
641703```
642704
643705If you don't have a appropriate clock to rely on, you should
644706unset this header with an empty string:
645707
646708``` php
647- $server = new StreamingServer(function (ServerRequestInterface $request) {
648- return new Response(200, array('Date' => ''));
709+ $server = new Server(function (ServerRequestInterface $request) {
710+ return new Response(
711+ 200,
712+ array(
713+ 'Date' => ''
714+ )
715+ );
649716});
650717```
651718
652719Note that it will automatically assume a ` X-Powered-By: react/alpha ` header
653720unless your specify a custom ` X-Powered-By ` header yourself:
654721
655722``` php
656- $server = new StreamingServer(function (ServerRequestInterface $request) {
657- return new Response(200, array('X-Powered-By' => 'PHP 3'));
723+ $server = new Server(function (ServerRequestInterface $request) {
724+ return new Response(
725+ 200,
726+ array(
727+ 'X-Powered-By' => 'PHP 3'
728+ )
729+ );
658730});
659731```
660732
661733If you do not want to send this header at all, you can use an empty string as
662734value like this:
663735
664736``` php
665- $server = new StreamingServer(function (ServerRequestInterface $request) {
666- return new Response(200, array('X-Powered-By' => ''));
737+ $server = new Server(function (ServerRequestInterface $request) {
738+ return new Response(
739+ 200,
740+ array(
741+ 'X-Powered-By' => ''
742+ )
743+ );
667744});
668745```
669746
0 commit comments