238
238
` Server ` , but you can add these parameters by yourself using the given methods.
239
239
The next versions of this project will cover these features.
240
240
241
- Note that the request object will be processed once the request headers have
242
- been received.
241
+ Note that by default, the request object will be processed once the request headers have
242
+ been received (see also [ ` RequestBodyBufferMiddleware ` ] ( #requestbodybuffermiddleware )
243
+ for an alternative).
243
244
This means that this happens irrespective of (i.e. * before* ) receiving the
244
245
(potentially much larger) request body.
245
246
While this may be uncommon in the PHP ecosystem, this is actually a very powerful
@@ -254,16 +255,18 @@ approach that gives you several advantages not otherwise possible:
254
255
such as accepting a huge file upload or possibly unlimited request body stream.
255
256
256
257
The ` getBody() ` method can be used to access the request body stream.
257
- This method returns a stream instance that implements both the
258
+ In the default streaming mode, this method returns a stream instance that implements both the
258
259
[ PSR-7 StreamInterface] ( http://www.php-fig.org/psr/psr-7/#psrhttpmessagestreaminterface )
259
260
and the [ ReactPHP ReadableStreamInterface] ( https://github.com/reactphp/stream#readablestreaminterface ) .
260
261
However, most of the ` PSR-7 StreamInterface ` methods have been
261
262
designed under the assumption of being in control of the request body.
262
263
Given that this does not apply to this server, the following
263
264
` PSR-7 StreamInterface ` methods are not used and SHOULD NOT be called:
264
265
` tell() ` , ` eof() ` , ` seek() ` , ` rewind() ` , ` write() ` and ` read() ` .
265
- Instead, you should use the ` ReactPHP ReadableStreamInterface ` which
266
- gives you access to the incoming request body as the individual chunks arrive:
266
+ If this is an issue for your use case, it's highly recommended to use the
267
+ [ ` RequestBodyBufferMiddleware ` ] ( #requestbodybuffermiddleware ) instead.
268
+ The ` ReactPHP ReadableStreamInterface ` gives you access to the incoming
269
+ request body as the individual chunks arrive:
267
270
268
271
``` php
269
272
$server = new Server(function (ServerRequestInterface $request) {
@@ -298,7 +301,7 @@ $server = new Server(function (ServerRequestInterface $request) {
298
301
The above example simply counts the number of bytes received in the request body.
299
302
This can be used as a skeleton for buffering or processing the request body.
300
303
301
- See also [ example #4 ] ( examples ) for more details.
304
+ See also [ example #9 ] ( examples ) for more details.
302
305
303
306
The ` data ` event will be emitted whenever new data is available on the request
304
307
body stream.
@@ -415,7 +418,7 @@ non-alphanumeric characters.
415
418
This encoding is also used internally when decoding the name and value of cookies
416
419
(which is in line with other implementations, such as PHP's cookie functions).
417
420
418
- See also [ example #6 ] ( examples ) for more details.
421
+ See also [ example #5 ] ( examples ) for more details.
419
422
420
423
### Response
421
424
@@ -680,24 +683,41 @@ $server = new Server(new MiddlewareRunner([
680
683
681
684
#### RequestBodyBufferMiddleware
682
685
683
- One of the build in middleware is ` RequestBodyBufferMiddleware ` which will buffer the incoming
684
- request body until the reported size has been reached. Then it will call the next
685
- middleware in line with the new request instance containing the full request body.
686
- The constructor accepts one argument, a maximum request body size. When one isn't
687
- provided it will use ` post_max_size ` from PHP's configuration.
688
- (** Note that the value from the CLI configuration will be used.** )
686
+ One of the built-in middleware is the ` RequestBodyBufferMiddleware ` which
687
+ can be used to buffer the whole incoming request body in memory.
688
+ This can be useful if full PSR-7 compatibility is needed for the request handler
689
+ and the default streaming request body handling is not needed.
690
+ The constructor accepts one optional argument, the maximum request body size.
691
+ When one isn't provided it will use ` post_max_size ` (default 8 MiB) from PHP's
692
+ configuration.
693
+ (Note that the value from your matching SAPI will be used, which is the CLI
694
+ configuration in most cases.)
695
+
696
+ Any incoming request that has a request body that exceeds this limit will be
697
+ rejected with a ` 413 ` (Request Entity Too Large) error message without calling
698
+ the next middleware handlers.
699
+
700
+ Any incoming request that does not have its size defined and uses the (rare)
701
+ ` Transfer-Encoding: chunked ` will be rejected with a ` 411 ` (Length Required)
702
+ error message without calling the next middleware handlers.
703
+ Note that this only affects incoming requests, the much more common chunked
704
+ transfer encoding for outgoing responses is not affected.
705
+ It is recommended to define a ` Content-Length ` header instead.
706
+ Note that this does not affect normal requests without a request body
707
+ (such as a simple ` GET ` request).
689
708
690
- Before buffering the request body the ` RequestBodyBufferMiddleware ` will check if the request
691
- body has a size. When size is null a [ HTTP 411] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/411 )
692
- response will be send to the client. When size is bigger then supplied to the ` RequestBodyBufferMiddleware `
693
- constructor or taken from ` post_max_size ` a [ HTTP 413] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413 )
694
- response will be dispatched.
709
+ All other requests will be buffered in memory until the request body end has
710
+ been reached and then call the next middleware handler with the complete,
711
+ buffered request.
712
+ Similarly, this will immediately invoke the next middleware handler for requests
713
+ that have an empty request body (such as a simple ` GET ` request) and requests
714
+ that are already buffered (such as due to another middleware).
695
715
696
716
Usage:
697
717
698
718
``` php
699
719
$middlewares = new MiddlewareRunner([
700
- new RequestBodyBufferMiddleware(),
720
+ new RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB
701
721
function (ServerRequestInterface $request, callable $next) {
702
722
// The body from $request->getBody() is now fully available without the need to stream it
703
723
return new Response(200);
0 commit comments