|
| 1 | +# Content-Length Middleware |
| 2 | + |
| 3 | +- **Available since zend-expressive-helpers version 4.1.0.** |
| 4 | + |
| 5 | +In some cases, you may want to include an explicit `Content-Length` response |
| 6 | +header, without having to inject it manually. To facilitate this, we provide |
| 7 | +`Zend\Expressive\Helper\ContentLengthMiddleware`. |
| 8 | + |
| 9 | +> ### When to use this middleware |
| 10 | +> |
| 11 | +> In most cases, you do not need to provide an explicit Content-Length value |
| 12 | +> in your responses. While the HTTP/1.1 specification indicates the header |
| 13 | +> SHOULD be provided, most clients will not degrade to HTTP/1.0 if the header |
| 14 | +> is omitted. |
| 15 | +> |
| 16 | +> The one exception that has been reported is when working with |
| 17 | +> [New Relic](https://newrelic.com), which requires valid `Content-Length` |
| 18 | +> headers for some of its analytics; in such cases, enabling this middleware |
| 19 | +> will fix those situations. |
| 20 | +
|
| 21 | +This middleware delegates the request, and operates on the returned response. It |
| 22 | +will return a new response with the `Content-Length` header injected under the |
| 23 | +following conditions: |
| 24 | + |
| 25 | +- No `Content-Length` header is already present AND |
| 26 | +- the body size is non-null. |
| 27 | + |
| 28 | +To register it in your application, you will need to do two things: register the |
| 29 | +middleware with the container, and register the middleware in either your |
| 30 | +application pipeline, or within routed middleware. |
| 31 | + |
| 32 | +To add it to your container, add the following configuration: |
| 33 | + |
| 34 | +```php |
| 35 | +// In a `config/autoload/*.global.php` file, or a `ConfigProvider` class: |
| 36 | + |
| 37 | +use Zend\Expressive\Helper; |
| 38 | + |
| 39 | +return [ |
| 40 | + 'dependencies' => [ |
| 41 | + 'invokables' => [ |
| 42 | + Helper\ContentLengthMiddleware::class => Helper\ContentLengthMiddleware::class, |
| 43 | + ], |
| 44 | + ], |
| 45 | +]; |
| 46 | +``` |
| 47 | + |
| 48 | +To register it as pipeline middleware to execute on any request: |
| 49 | + |
| 50 | +```php |
| 51 | +// In `config/pipeline.php`: |
| 52 | + |
| 53 | +use Zend\Expressive\Helper; |
| 54 | + |
| 55 | +$app->pipe(Helper\ContentLengthMiddleware::class); |
| 56 | +``` |
| 57 | + |
| 58 | +To register it within a routed middleware pipeline: |
| 59 | + |
| 60 | +```php |
| 61 | +// In `config/routes.php`: |
| 62 | + |
| 63 | +use Zend\Expressive\Helper; |
| 64 | + |
| 65 | +$app->get('/download/tarball', [ |
| 66 | + Helper\ContentLengthMiddleware::class, |
| 67 | + Download\Tarball::class, |
| 68 | +], 'download-tar'); |
| 69 | +``` |
| 70 | + |
| 71 | +## Caveats |
| 72 | + |
| 73 | +One caveat to note is that if you use this middleware, but also write directly |
| 74 | +to the output buffer (e.g., via a `var_dump`, or if `display_errors` is on and |
| 75 | +an uncaught error or exception occurs), the output will not appear as you |
| 76 | +expect. Generally in such situations, the contents of the output buffer will |
| 77 | +appear, up to the specified `Content-Length` value. This can lead to truncated |
| 78 | +error content and/or truncated application content. |
| 79 | + |
| 80 | +We recommend that if you use this feature, you also use a PHP error and/or |
| 81 | +exception handler that logs errors in order to prevent truncated output. |
0 commit comments