From 9580396fa10c471c82e392d436186be817d5af25 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 7 Jun 2025 13:21:53 +0800 Subject: [PATCH] http3: simplify connection closing in the frame parser (#5196) No functional change expected. --- http3/client.go | 4 ++-- http3/conn.go | 2 +- http3/frames.go | 4 ++-- http3/frames_test.go | 4 ++-- http3/server.go | 2 +- http3/stream.go | 6 +++--- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/http3/client.go b/http3/client.go index d9666bb7a..fa6520a7f 100644 --- a/http3/client.go +++ b/http3/client.go @@ -155,8 +155,8 @@ func (c *ClientConn) handleBidirectionalStreams(streamHijacker func(FrameType, q return } fp := &frameParser{ - r: str, - conn: &c.connection, + r: str, + closeConn: c.CloseWithError, unknownFrameHandler: func(ft FrameType, e error) (processed bool, err error) { id := c.Context().Value(quic.ConnectionTracingKey).(quic.ConnectionTracingID) return streamHijacker(ft, id, str, e) diff --git a/http3/conn.go b/http3/conn.go index 61a83745e..f3bcecd8f 100644 --- a/http3/conn.go +++ b/http3/conn.go @@ -287,7 +287,7 @@ func (c *connection) handleUnidirectionalStreams(hijack func(StreamType, quic.Co } func (c *connection) handleControlStream(str *quic.ReceiveStream) { - fp := &frameParser{conn: c.Connection, r: str} + fp := &frameParser{closeConn: c.Connection.CloseWithError, r: str} f, err := fp.ParseNext() if err != nil { var serr *quic.StreamError diff --git a/http3/frames.go b/http3/frames.go index b54afb313..e5149cb21 100644 --- a/http3/frames.go +++ b/http3/frames.go @@ -21,7 +21,7 @@ var errHijacked = errors.New("hijacked") type frameParser struct { r io.Reader - conn quic.Connection + closeConn func(quic.ApplicationErrorCode, string) error unknownFrameHandler unknownFrameHandlerFunc } @@ -70,7 +70,7 @@ func (p *frameParser) ParseNext() (frame, error) { return parseGoAwayFrame(qr, l) case 0xd: // MAX_PUSH_ID case 0x2, 0x6, 0x8, 0x9: - p.conn.CloseWithError(quic.ApplicationErrorCode(ErrCodeFrameUnexpected), "") + p.closeConn(quic.ApplicationErrorCode(ErrCodeFrameUnexpected), "") return nil, fmt.Errorf("http3: reserved frame type: %d", t) } // skip over unknown frames diff --git a/http3/frames_test.go b/http3/frames_test.go index f8e20f330..dcb317b2e 100644 --- a/http3/frames_test.go +++ b/http3/frames_test.go @@ -37,8 +37,8 @@ func TestParserReservedFrameType(t *testing.T) { data = append(data, []byte("foobar")...) fp := frameParser{ - r: bytes.NewReader(data), - conn: client, + r: bytes.NewReader(data), + closeConn: client.CloseWithError, } _, err := fp.ParseNext() require.Error(t, err) diff --git a/http3/server.go b/http3/server.go index 85f1356be..8884650c4 100644 --- a/http3/server.go +++ b/http3/server.go @@ -562,7 +562,7 @@ func (s *Server) handleRequest(conn *connection, str datagramStream, decoder *qp ) } } - fp := &frameParser{conn: conn, r: str, unknownFrameHandler: ufh} + fp := &frameParser{closeConn: conn.CloseWithError, r: str, unknownFrameHandler: ufh} frame, err := fp.ParseNext() if err != nil { if !errors.Is(err, errHijacked) { diff --git a/http3/stream.go b/http3/stream.go index 05f3c108a..b13fbfd83 100644 --- a/http3/stream.go +++ b/http3/stream.go @@ -54,8 +54,8 @@ func newStream(str datagramStream, conn *connection, parseTrailer func(io.Reader func (s *Stream) Read(b []byte) (int, error) { fp := &frameParser{ - r: s.datagramStream, - conn: s.conn, + r: s.datagramStream, + closeConn: s.conn.CloseWithError, } if s.bytesRemainingInFrame == 0 { parseLoop: @@ -196,7 +196,7 @@ func (s *RequestStream) SendRequestHeader(req *http.Request) error { func (s *RequestStream) ReadResponse() (*http.Response, error) { qstr := s.datagramStream fp := &frameParser{ - conn: s.conn, + closeConn: s.conn.CloseWithError, r: &tracingReader{ Reader: qstr, first: &s.firstByte,