MCPcopy
hub / github.com/caddyserver/caddy / ServeHTTP

Method ServeHTTP

modules/caddyhttp/encode/encode.go:162–199  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler)

Source from the content-addressed store, hash-verified

160}
161
162func (enc *Encode) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
163 if isEncodeAllowed(r.Header) {
164 for _, encName := range AcceptedEncodings(r, enc.Prefer) {
165 if _, ok := enc.writerPools[encName]; !ok {
166 continue // encoding not offered
167 }
168 w = enc.openResponseWriter(encName, w, r.Method == http.MethodConnect)
169 defer w.(*responseWriter).Close()
170
171 // to comply with RFC 9110 section 8.8.3(.3), we modify the Etag when encoding
172 // by appending a hyphen and the encoder name; the problem is, the client will
173 // send back that Etag in an If-None-Match header, but upstream handlers that set
174 // the Etag in the first place don't know that we appended to their Etag! so here
175 // we have to strip our addition so the upstream handlers can still honor client
176 // caches without knowing about our changes...
177 if etag := r.Header.Get("If-None-Match"); etag != "" && !strings.HasPrefix(etag, "W/") {
178 ourSuffix := "-" + encName + `"`
179 if before, ok := strings.CutSuffix(etag, ourSuffix); ok {
180 etag = before + `"`
181 r.Header.Set("If-None-Match", etag)
182 }
183 }
184
185 break
186 }
187 }
188
189 err := next.ServeHTTP(w, r)
190 // If there was an error, disable encoding completely
191 // This prevents corruption when handle_errors processes the response
192 if err != nil {
193 if ew, ok := w.(*responseWriter); ok {
194 ew.disabled = true
195 }
196 }
197
198 return err
199}
200
201func (enc *Encode) addEncoding(e Encoding) error {
202 ae := e.AcceptEncoding()

Callers

nothing calls this directly

Calls 7

openResponseWriterMethod · 0.95
isEncodeAllowedFunction · 0.85
AcceptedEncodingsFunction · 0.85
GetMethod · 0.65
ServeHTTPMethod · 0.65
CloseMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected