0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2025-01-27 23:03:37 -05:00

encode: write status immediately when status code is informational (#6164)

This commit is contained in:
WeidiDeng 2024-03-10 22:49:49 +08:00 committed by GitHub
parent c27425ef5d
commit e698ec5139
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -20,11 +20,9 @@
package encode
import (
"bufio"
"fmt"
"io"
"math"
"net"
"net/http"
"sort"
"strconv"
@ -221,6 +219,12 @@ type responseWriter struct {
// to actually write the header.
func (rw *responseWriter) WriteHeader(status int) {
rw.statusCode = status
// write status immediately when status code is informational
// see: https://caddy.community/t/disappear-103-early-hints-response-with-encode-enable-caddy-v2-7-6/23081/5
if 100 <= status && status <= 199 {
rw.ResponseWriter.WriteHeader(status)
}
}
// Match determines, if encoding should be done based on the ResponseMatcher.
@ -242,19 +246,6 @@ func (rw *responseWriter) Flush() {
http.NewResponseController(rw.ResponseWriter).Flush()
}
// Hijack implements http.Hijacker. It will flush status code if set. We don't track actual hijacked
// status assuming http middlewares will track its status.
func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if !rw.wroteHeader {
if rw.statusCode != 0 {
rw.ResponseWriter.WriteHeader(rw.statusCode)
}
rw.wroteHeader = true
}
//nolint:bodyclose
return http.NewResponseController(rw.ResponseWriter).Hijack()
}
// Write writes to the response. If the response qualifies,
// it is encoded using the encoder, which is initialized
// if not done so already.