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

Commit 995c4b0

Browse files
committed
Merge branch 'hotfix/content-length-middleware-docs'
Close #508
2 parents 0a362d1 + 94c67fd commit 995c4b0

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#508](https://github.com/zendframework/zend-expressive/pull/508) adds
10+
documentation covering `Zend\Expressive\Helper\ContentLengthMiddleware`,
11+
introduced in zend-expressive-helpers 4.1.0.
1012

1113
### Deprecated
1214

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.

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) (since 4.1.0)
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)