Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion pkg/x/http/response_recorder.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package http

import "net/http"
import (
"bufio"
"net"
"net/http"
)

var _ http.Hijacker = (*ResponseRecorder)(nil)

type ResponseRecorder struct {
http.Hijacker
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Embedding http.Hijacker as an interface field is unnecessary since the Hijack method is explicitly implemented below. This embedded field serves no purpose and should be removed to avoid confusion.

Suggested change
http.Hijacker

Copilot uses AI. Check for mistakes.
http.ResponseWriter

status int
Expand Down Expand Up @@ -34,6 +41,14 @@ func (r *ResponseRecorder) WriteHeader(s int) {
}
}

// Hijack implements http.Hijacker.
func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, http.ErrHijacked
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error return should be http.ErrNotSupported instead of http.ErrHijacked. http.ErrHijacked indicates the connection was already hijacked, while http.ErrNotSupported correctly indicates the underlying ResponseWriter doesn't implement the Hijacker interface.

Suggested change
return nil, nil, http.ErrHijacked
return nil, nil, http.ErrNotSupported

Copilot uses AI. Check for mistakes.
}

func (r *ResponseRecorder) Status() int {
return r.status
}
Expand Down
26 changes: 23 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,26 @@ func (s *HTTPServer) buildHandler(tripper http.RoundTripper) http.HandlerFunc {
}()

want := resp.Header.Get("Content-Length")
hasChunked := slices.Contains(resp.TransferEncoding, "chunked")

sent, err := io.CopyBuffer(w, resp.Body, *buf)

// handle copy error
if err != nil && !errors.Is(err, io.EOF) {
// abort ? continue ?
// quickabort ? continue ?
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spacing in comment from 'quickabort' to 'quick abort'.

Suggested change
// quickabort ? continue ?
// quick abort ? continue ?

Copilot uses AI. Check for mistakes.

// if the body is chunked or no Content-Length, just close the connection
if hasChunked || want == "" {
wrapUpstreamError(w)
}

clog.Errorf("failed to copy response body to client: [%s] %s %s sent=%d want=%s err=%s", req.Proto, req.Method, req.URL.Path, sent, want, err)
_metricRequestUnexpectedClosedTotal.WithLabelValues(req.Proto, req.Method).Inc()
return
}

if slices.Contains(resp.TransferEncoding, "chunked") || want == "" {
clog.Debugf("copied %d response body bytes chunked body from upstream to client", sent)
if hasChunked || want == "" {
clog.Debugf("copied %d response chunked body bytes to client", sent)
return
}

Expand Down Expand Up @@ -362,3 +371,14 @@ func (s *HTTPServer) globalOptions(src map[string]any) map[string]any {

return src
}

func wrapUpstreamError(w http.ResponseWriter) {
if hj, ok := w.(http.Hijacker); ok {
conn, _, err := hj.Hijack()
if err != nil {
return
}

_ = conn.Close()
}
}