@@ -12,7 +12,17 @@ Event-driven, streaming plaintext HTTP and secure HTTPS server for [ReactPHP](ht
1212 * [ Server] ( #server )
1313 * [ StreamingServer] ( #streamingserver )
1414 * [ Request] ( #request )
15+ * [ Request parameters] ( #request-parameters )
16+ * [ Query parameters] ( #query-parameters )
17+ * [ Streaming request] ( #streaming-request )
18+ * [ Request method] ( #request-method )
19+ * [ Cookie parameters] ( #cookie-parameters )
1520 * [ Response] ( #response )
21+ * [ Deferred response] ( #deferred-response )
22+ * [ Streaming response] ( #streaming-response )
23+ * [ Response length] ( #response-length )
24+ * [ Invalid response] ( #invalid-response )
25+ * [ Default response headers] ( #default-response-headers )
1626 * [ Middleware] ( #middleware )
1727 * [ LimitConcurrentRequestsMiddleware] ( #limitconcurrentrequestsmiddleware )
1828 * [ RequestBodyBufferMiddleware] ( #requestbodybuffermiddleware )
@@ -217,6 +227,13 @@ $server = new Server(function (ServerRequestInterface $request) {
217227});
218228```
219229
230+ For more details about the request object, also check out the documentation of
231+ [ PSR-7 ServerRequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface )
232+ and
233+ [ PSR-7 RequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface ) .
234+
235+ #### Request parameters
236+
220237The ` getServerParams(): mixed[] ` method can be used to
221238get server-side parameters similar to the ` $_SERVER ` variable.
222239The following parameters are currently available:
@@ -252,7 +269,9 @@ $server = new Server(function (ServerRequestInterface $request) {
252269});
253270```
254271
255- See also [ example #2 ] ( examples ) .
272+ See also [ example #3 ] ( examples ) .
273+
274+ #### Query parameters
256275
257276The ` getQueryParams(): array ` method can be used to get the query parameters
258277similiar to the ` $_GET ` variable.
@@ -284,18 +303,19 @@ Use [`htmlentities`](http://php.net/manual/en/function.htmlentities.php)
284303like in this example to prevent
285304[ Cross-Site Scripting (abbreviated as XSS)] ( https://en.wikipedia.org/wiki/Cross-site_scripting ) .
286305
287- See also [ example #3 ] ( examples ) .
306+ See also [ example #4 ] ( examples ) .
288307
289- For more details about the request object, check out the documentation of
290- [ PSR-7 ServerRequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface )
291- and
292- [ PSR-7 RequestInterface] ( https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface ) .
308+ #### Streaming request
293309
294- Note that by default, the request object will be processed once the request headers have
295- been received (see also [ ` RequestBodyBufferMiddleware ` ] ( #requestbodybuffermiddleware )
296- for an alternative).
310+ If you're using the [ ` Server ` ] ( #server ) , then the request object will be
311+ buffered and parsed in memory and contains the full request body.
312+ This includes the parsed request body and any file uploads.
313+
314+ If you're using the advanced [ ` StreamingServer ` ] ( #streamingserver ) , the
315+ request object will be procesed once the request headers have been received.
297316This means that this happens irrespective of (i.e. * before* ) receiving the
298317(potentially much larger) request body.
318+
299319While this may be uncommon in the PHP ecosystem, this is actually a very powerful
300320approach that gives you several advantages not otherwise possible:
301321
@@ -416,6 +436,8 @@ $server = new StreamingServer(function (ServerRequestInterface $request) {
416436});
417437```
418438
439+ #### Request method
440+
419441Note that the server supports * any* request method (including custom and non-
420442standard ones) and all request-target formats defined in the HTTP specs for each
421443respective method, including * normal* ` origin-form ` requests as well as
@@ -445,6 +467,8 @@ Allowed).
445467 request-target than the ` Host ` header value (such as removing default ports)
446468 and the request-target MUST take precendence when forwarding.
447469
470+ #### Cookie parameters
471+
448472The ` getCookieParams(): string[] ` method can be used to
449473get all cookies sent with the current request.
450474
@@ -513,6 +537,8 @@ $server = new Server(function (ServerRequestInterface $request) {
513537});
514538```
515539
540+ #### Deferred response
541+
516542The example above returns the response directly, because it needs
517543no time to be processed.
518544Using a database, the file system or long calculations
@@ -550,6 +576,8 @@ The promise cancellation handler can be used to clean up any pending resources
550576allocated in this case (if applicable).
551577If a promise is resolved after the client closes, it will simply be ignored.
552578
579+ #### Streaming response
580+
553581The ` Response ` class in this project supports to add an instance which implements the
554582[ ReactPHP ReadableStreamInterface] ( https://github.com/reactphp/stream#readablestreaminterface )
555583for the response body.
@@ -595,52 +623,6 @@ response stream will automatically be closed.
595623The ` close ` event can be used to clean up any pending resources allocated
596624in this case (if applicable).
597625
598- If the response body is a ` string ` , a ` Content-Length ` header will be added
599- automatically.
600- If the response body is a ReactPHP ` ReadableStreamInterface ` and you do not
601- specify a ` Content-Length ` header, HTTP/1.1 responses will automatically use
602- chunked transfer encoding and send the respective header
603- (` Transfer-Encoding: chunked ` ) automatically.
604- The server is responsible for handling ` Transfer-Encoding ` , so you SHOULD NOT
605- pass this header yourself.
606- If you know the length of your stream body, you MAY specify it like this instead:
607-
608- ``` php
609- $stream = new ThroughStream()
610- $server = new Server(function (ServerRequestInterface $request) use ($stream) {
611- return new Response(
612- 200,
613- array(
614- 'Content-Length' => '5',
615- 'Content-Type' => 'text/plain',
616- ),
617- $stream
618- );
619- });
620- ```
621-
622- An invalid return value or an unhandled ` Exception ` or ` Throwable ` in the code
623- of the callback function, will result in an ` 500 Internal Server Error ` message.
624- Make sure to catch ` Exceptions ` or ` Throwables ` to create own response messages.
625-
626- After the return in the callback function the response will be processed by the ` StreamingServer ` .
627- The ` StreamingServer ` will add the protocol version of the request, so you don't have to.
628-
629- Any response to a ` HEAD ` request and any response with a ` 1xx ` (Informational),
630- ` 204 ` (No Content) or ` 304 ` (Not Modified) status code will * not* include a
631- message body as per the HTTP specs.
632- This means that your callback does not have to take special care of this and any
633- response body will simply be ignored.
634-
635- Similarly, any ` 2xx ` (Successful) response to a ` CONNECT ` request, any response
636- with a ` 1xx ` (Informational) or ` 204 ` (No Content) status code will * not*
637- include a ` Content-Length ` or ` Transfer-Encoding ` header as these do not apply
638- to these messages.
639- Note that a response to a ` HEAD ` request and any response with a ` 304 ` (Not
640- Modified) status code MAY include these headers even though
641- the message does not contain a response body, because these header would apply
642- to the message if the same request would have used an (unconditional) ` GET ` .
643-
644626> Note that special care has to be taken if you use a body stream instance that
645627 implements ReactPHP's
646628 [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface )
@@ -688,6 +670,59 @@ to the message if the same request would have used an (unconditional) `GET`.
688670 body has been processed (which should be empty in most cases).
689671 See also [ example #22 ] ( examples ) for more details.
690672
673+ #### Response length
674+
675+ If the response body is a ` string ` , a ` Content-Length ` header will be added
676+ automatically.
677+ If the response body is a ReactPHP ` ReadableStreamInterface ` and you do not
678+ specify a ` Content-Length ` header, HTTP/1.1 responses will automatically use
679+ chunked transfer encoding and send the respective header
680+ (` Transfer-Encoding: chunked ` ) automatically.
681+ The server is responsible for handling ` Transfer-Encoding ` , so you SHOULD NOT
682+ pass this header yourself.
683+ If you know the length of your stream body, you MAY specify it like this instead:
684+
685+ ``` php
686+ $stream = new ThroughStream()
687+ $server = new Server(function (ServerRequestInterface $request) use ($stream) {
688+ return new Response(
689+ 200,
690+ array(
691+ 'Content-Length' => '5',
692+ 'Content-Type' => 'text/plain',
693+ ),
694+ $stream
695+ );
696+ });
697+ ```
698+
699+ Any response to a ` HEAD ` request and any response with a ` 1xx ` (Informational),
700+ ` 204 ` (No Content) or ` 304 ` (Not Modified) status code will * not* include a
701+ message body as per the HTTP specs.
702+ This means that your callback does not have to take special care of this and any
703+ response body will simply be ignored.
704+
705+ Similarly, any ` 2xx ` (Successful) response to a ` CONNECT ` request, any response
706+ with a ` 1xx ` (Informational) or ` 204 ` (No Content) status code will * not*
707+ include a ` Content-Length ` or ` Transfer-Encoding ` header as these do not apply
708+ to these messages.
709+ Note that a response to a ` HEAD ` request and any response with a ` 304 ` (Not
710+ Modified) status code MAY include these headers even though
711+ the message does not contain a response body, because these header would apply
712+ to the message if the same request would have used an (unconditional) ` GET ` .
713+
714+ #### Invalid response
715+
716+ An invalid return value or an unhandled ` Exception ` or ` Throwable ` in the code
717+ of the callback function, will result in an ` 500 Internal Server Error ` message.
718+ Make sure to catch ` Exceptions ` or ` Throwables ` to create own response messages.
719+
720+ #### Default response headers
721+
722+ After the return in the callback function the response will be processed by the
723+ [ ` Server ` ] ( #server ) or [ ` StreamingServer ` ] ( #streamingserver ) respectively.
724+ They will add the protocol version of the request, so you don't have to.
725+
691726A ` Date ` header will be automatically added with the system date and time if none is given.
692727You can add a custom ` Date ` header yourself like this:
693728
0 commit comments