mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-21 23:39:43 +00:00
reverseproxy: log HTTP/3 client cancellations at debug level (#8135)
Some checks are pending
Tests / test (./cmd/caddy/caddy, ~1.26.0, macos-14, 0, 1.26, mac) (push) Waiting to run
Tests / test (./cmd/caddy/caddy, ~1.26.0, ubuntu-latest, 0, 1.26, linux) (push) Waiting to run
Tests / test (./cmd/caddy/caddy.exe, ~1.26.0, windows-latest, True, 1.26, windows) (push) Waiting to run
Tests / test (s390x on IBM Z) (push) Waiting to run
Tests / goreleaser-check (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, aix) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, linux) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, windows) (push) Waiting to run
Lint / lint (push) Waiting to run
Lint / lint-1 (push) Waiting to run
Lint / lint-2 (push) Waiting to run
Lint / govulncheck (push) Waiting to run
Lint / dependency-review (push) Waiting to run
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Some checks are pending
Tests / test (./cmd/caddy/caddy, ~1.26.0, macos-14, 0, 1.26, mac) (push) Waiting to run
Tests / test (./cmd/caddy/caddy, ~1.26.0, ubuntu-latest, 0, 1.26, linux) (push) Waiting to run
Tests / test (./cmd/caddy/caddy.exe, ~1.26.0, windows-latest, True, 1.26, windows) (push) Waiting to run
Tests / test (s390x on IBM Z) (push) Waiting to run
Tests / goreleaser-check (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, aix) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, linux) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, windows) (push) Waiting to run
Lint / lint (push) Waiting to run
Lint / lint-1 (push) Waiting to run
Lint / lint-2 (push) Waiting to run
Lint / govulncheck (push) Waiting to run
Lint / dependency-review (push) Waiting to run
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
This commit is contained in:
parent
7ba5e147ac
commit
f7eeb1d65c
3 changed files with 120 additions and 2 deletions
|
|
@ -2,11 +2,19 @@ package reverseproxy
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/quic-go/quic-go/http3"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
|
|
@ -52,3 +60,91 @@ func TestClientDisconnectRecordsStatus(t *testing.T) {
|
|||
t.Errorf("expected status 499 after client disconnect, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
// failingWriter is a ResponseWriter whose body writes fail with err.
|
||||
type failingWriter struct {
|
||||
*httptest.ResponseRecorder
|
||||
err error
|
||||
}
|
||||
|
||||
func (w failingWriter) Write([]byte) (int, error) { return 0, w.err }
|
||||
|
||||
// failingReader is a response body from the backend whose reads fail with err.
|
||||
type failingReader struct{ err error }
|
||||
|
||||
func (r failingReader) Read([]byte) (int, error) { return 0, r.err }
|
||||
|
||||
// TestClientCancelAbortLogLevel verifies that an HTTP/3 client canceling the
|
||||
// request while the response is streamed is logged at debug level, while
|
||||
// other errors that abort the response, including the same error coming from
|
||||
// the backend, are still logged as warnings.
|
||||
func TestClientCancelAbortLogLevel(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
err error
|
||||
readErr bool // fail reading from the backend instead of writing to the client
|
||||
want zapcore.Level
|
||||
}{
|
||||
{
|
||||
name: "http3 request canceled by client",
|
||||
err: &http3.Error{Remote: true, ErrorCode: http3.ErrCodeRequestCanceled},
|
||||
want: zapcore.DebugLevel,
|
||||
},
|
||||
{
|
||||
name: "http3 request canceled locally",
|
||||
err: &http3.Error{Remote: false, ErrorCode: http3.ErrCodeRequestCanceled},
|
||||
want: zapcore.WarnLevel,
|
||||
},
|
||||
{
|
||||
name: "other http3 error",
|
||||
err: &http3.Error{Remote: true, ErrorCode: http3.ErrCodeInternalError},
|
||||
want: zapcore.WarnLevel,
|
||||
},
|
||||
{
|
||||
name: "other error",
|
||||
err: errors.New("broken pipe"),
|
||||
want: zapcore.WarnLevel,
|
||||
},
|
||||
{
|
||||
name: "http3 response canceled by backend",
|
||||
err: &http3.Error{Remote: true, ErrorCode: http3.ErrCodeRequestCanceled},
|
||||
readErr: true,
|
||||
want: zapcore.WarnLevel,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
core, logs := observer.New(zapcore.DebugLevel)
|
||||
h := &Handler{logger: zap.New(core)}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req = req.WithContext(context.WithValue(req.Context(), caddyhttp.VarsCtxKey, map[string]any{}))
|
||||
res := &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: http.Header{},
|
||||
Body: fakeRWC{strings.NewReader("hello")},
|
||||
}
|
||||
var rw http.ResponseWriter = failingWriter{ResponseRecorder: httptest.NewRecorder(), err: tc.err}
|
||||
if tc.readErr {
|
||||
res.Body = fakeRWC{failingReader{tc.err}}
|
||||
rw = httptest.NewRecorder()
|
||||
}
|
||||
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != http.ErrAbortHandler {
|
||||
t.Fatalf("expected panic with http.ErrAbortHandler, got %v", r)
|
||||
}
|
||||
}()
|
||||
_ = h.finalizeResponse(rw, req, res, caddy.NewReplacer(), fakeStart, h.logger, "example.test:443")
|
||||
}()
|
||||
|
||||
entries := logs.FilterMessage("aborting with incomplete response").All()
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("expected 1 log entry, got %d", len(entries))
|
||||
}
|
||||
if got := entries[0].Level; got != tc.want {
|
||||
t.Errorf("log level = %s, want %s", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/dunglas/httpsfv"
|
||||
"github.com/quic-go/quic-go/http3"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"golang.org/x/net/http/httpguts"
|
||||
|
|
@ -1452,7 +1453,13 @@ func (h *Handler) finalizeResponse(
|
|||
// we'll just log the error and abort the stream here and panic just as
|
||||
// the standard lib's proxy to propagate the stream error.
|
||||
// see issue https://github.com/caddyserver/caddy/issues/5951
|
||||
if c := logger.Check(zapcore.WarnLevel, "aborting with incomplete response"); c != nil {
|
||||
lvl := zapcore.WarnLevel
|
||||
if isClientCanceledH3(err) {
|
||||
// the client canceled the request; expected and not actionable
|
||||
// see issue https://github.com/caddyserver/caddy/issues/5766
|
||||
lvl = zapcore.DebugLevel
|
||||
}
|
||||
if c := logger.Check(lvl, "aborting with incomplete response"); c != nil {
|
||||
c.Write(zap.Error(err))
|
||||
}
|
||||
// flush the buffer to ensure the client sees the partial response
|
||||
|
|
@ -1494,6 +1501,17 @@ func (h *Handler) finalizeResponse(
|
|||
return nil
|
||||
}
|
||||
|
||||
// isClientCanceledH3 returns true if err is an HTTP/3 client canceling
|
||||
// its request (H3_REQUEST_CANCELLED) while the response was being written.
|
||||
// The same error read from an HTTP/3 backend is the backend's doing.
|
||||
func isClientCanceledH3(err error) bool {
|
||||
if !errors.Is(err, errWritingDownstream) {
|
||||
return false
|
||||
}
|
||||
h3Err, ok := errors.AsType[*http3.Error](err)
|
||||
return ok && h3Err.Remote && h3Err.ErrorCode == http3.ErrCodeRequestCanceled
|
||||
}
|
||||
|
||||
// tryAgain takes the time that the handler was initially invoked,
|
||||
// the amount of retries already performed, as well as any error
|
||||
// currently obtained, and the request being tried, and returns
|
||||
|
|
|
|||
|
|
@ -464,6 +464,10 @@ func (h Handler) copyResponse(dst http.ResponseWriter, src io.Reader, flushInter
|
|||
return err
|
||||
}
|
||||
|
||||
// errWritingDownstream wraps errors from writing the response to the client,
|
||||
// to tell them apart from errors reading the response from the backend.
|
||||
var errWritingDownstream = errors.New("writing")
|
||||
|
||||
// copyBuffer returns any write errors or non-EOF read errors, and the amount
|
||||
// of bytes written.
|
||||
func (h Handler) copyBuffer(dst io.Writer, src io.Reader, buf []byte, logger *zap.Logger) (int64, error) {
|
||||
|
|
@ -504,7 +508,7 @@ func (h Handler) copyBuffer(dst io.Writer, src io.Reader, buf []byte, logger *za
|
|||
)
|
||||
}
|
||||
if werr != nil {
|
||||
return written, fmt.Errorf("writing: %w", werr)
|
||||
return written, fmt.Errorf("%w: %w", errWritingDownstream, werr)
|
||||
}
|
||||
if nr != nw {
|
||||
return written, io.ErrShortWrite
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue