nytimes / gziphandler

Go middleware to gzip HTTP responses
https://godoc.org/github.com/NYTimes/gziphandler
Apache License 2.0
868 stars 129 forks source link

"identity" Content-Encoding should also be compressed #99

Closed hhromic closed 4 years ago

hhromic commented 4 years ago

At the moment, gziphandler only compresses responses if there is no Content-Encoding header already set in the response:

https://github.com/nytimes/gziphandler/blob/master/gzip.go#L120-L141

This is ok however the Content Encoding specification contains an identity directive [1]:

identity
Indicates the identity function (i.e., no compression or modification).
This token, except if explicitly specified, is always deemed acceptable.

This directive essentially means a no-op encoding and should be threated the same as an empty content-encoding in gziphandler, i.e. candidate for on-the-fly compression.

[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding#Directives

hhromic commented 4 years ago

RFC 7231 explicitly indicates identity to be a synonym for no encoding [1]:

+----------+----------------------------------------+---------------+
| Name     | Description                            | Reference     |
+----------+----------------------------------------+---------------+
| identity | Reserved (synonym for "no encoding" in | Section 5.3.4 |
|          | Accept-Encoding)                       |               |
+----------+----------------------------------------+---------------+

[1] https://tools.ietf.org/html/rfc7231#section-8.4.2

hhromic commented 4 years ago

Oh, I now realise that RFC 2616 (HTTP) states that ìdentity should not be used in Content-Encoding but only in the Accept-Encoding header [1]:

identity
    The default (identity) encoding; the use of no transformation
    whatsoever. This content-coding is used only in the Accept-
    Encoding header, and SHOULD NOT be used in the Content-Encoding
    header.

Therefore what gziphandler is doing is correct. I will migrate the issue to the component generating this header in my middleware pipeline. Sorry!

[1] https://tools.ietf.org/html/rfc2616#section-3.5