Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 7258f44

Browse files
committed
Adds documentation for the new ContentLengthMiddleware
Per zendframework/zend-expressive-helpers#45
1 parent 0a362d1 commit 7258f44

File tree

3 files changed

+82
-0
lines changed

3 files changed

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

doc/book/features/helpers/intro.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ may integrate features or simply provide standalone benefits.
66

77
Currently, these include:
88

9+
- [Body Parsing Middleware](body-parse.md)
10+
- [Content-Length Middleware](content-length.md)
911
- [UrlHelper](url-helper.md)
1012
- [ServerUrlHelper](server-url-helper.md)
1113

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pages:
4040
- UrlHelper: features/helpers/url-helper.md
4141
- ServerUrlHelper: features/helpers/server-url-helper.md
4242
- 'Body Parsing Middleware': features/helpers/body-parse.md
43+
- 'Content-Length Middleware': features/helpers/content-length.md
4344
- Emitters: features/emitters.md
4445
- Cookbook:
4546
- 'Autowiring routes and pipeline middleware': cookbook/autowiring-routes-and-pipelines.md

0 commit comments

Comments
 (0)