ServeHTTP implements http.Handler.
(w http.ResponseWriter, r *http.Request)
| 103 | |
| 104 | // ServeHTTP implements http.Handler. |
| 105 | func (c *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 106 | v, ok := r.Header["Accept-Encoding"] |
| 107 | if !ok { // No header, use "identity". |
| 108 | c.next.ServeHTTP(w, r) |
| 109 | return |
| 110 | } |
| 111 | ae, nok := parseAccept(v[0]) |
| 112 | var zw zwriter |
| 113 | // Find the first accept-encoding we support. See |
| 114 | // https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.3 for all the |
| 115 | // semantics. |
| 116 | // |
| 117 | // NB The "identity" encoding shouldn't show up in the Content-Encoding |
| 118 | // response header. |
| 119 | for _, a := range ae { |
| 120 | switch a.Type { |
| 121 | case "gzip", "x-gzip": |
| 122 | w.Header().Set("content-encoding", "gzip") |
| 123 | gz := c.gzip.Get().(*gzip.Writer) |
| 124 | gz.Reset(w) |
| 125 | defer c.gzip.Put(gz) |
| 126 | zw = gz |
| 127 | case "deflate": |
| 128 | w.Header().Set("content-encoding", "deflate") |
| 129 | z := c.flate.Get().(*flate.Writer) |
| 130 | z.Reset(w) |
| 131 | defer c.flate.Put(z) |
| 132 | zw = z |
| 133 | case "zstd": |
| 134 | w.Header().Set("content-encoding", "zstd") |
| 135 | s := c.zstd.Get().(*zstd.Encoder) |
| 136 | s.Reset(w) |
| 137 | defer c.zstd.Put(s) |
| 138 | zw = s |
| 139 | case "identity": |
| 140 | w.Header().Set("accept-encoding", acceptable) |
| 141 | case "*": |
| 142 | // If we hit a star, it's technically OK to return any encoding not |
| 143 | // already specified. So, attempt to use gzip and then identity and |
| 144 | // give up. |
| 145 | // Clients that do extremely weird things like |
| 146 | // *;q=1.0, gzip;q=0.1, identity;q=0.1" |
| 147 | // deserve extremely weird replies. |
| 148 | _, gznok := nok["gzip"] |
| 149 | _, idnok := nok["identity"] |
| 150 | switch { |
| 151 | case !gznok: |
| 152 | w.Header().Set("content-encoding", "gzip") |
| 153 | gz := c.gzip.Get().(*gzip.Writer) |
| 154 | gz.Reset(w) |
| 155 | defer c.gzip.Put(gz) |
| 156 | zw = gz |
| 157 | case !idnok: |
| 158 | // "Identity" isn't not OK, so fallthrough. |
| 159 | default: |
| 160 | w.Header().Set("accept-encoding", acceptable) |
| 161 | w.WriteHeader(http.StatusUnsupportedMediaType) |
| 162 | return |
nothing calls this directly
no test coverage detected