Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions modules/caddyhttp/encode/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
package encode

import (
"bufio"
"fmt"
"io"
"math"
"net"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
Loading