Skip to content

Commit

Permalink
Create a new FrameHeader for each comparison
Browse files Browse the repository at this point in the history
This saves some initialization time.

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
  • Loading branch information
colega committed Nov 14, 2024
1 parent 3d58e97 commit 4abf41d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
15 changes: 5 additions & 10 deletions http2/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,10 @@ var fhBytes = sync.Pool{
},
}

var invalidHTTP1LookingFrameHeader = func() FrameHeader {
fh, err := readFrameHeader(make([]byte, frameHeaderLen), strings.NewReader("HTTP/1.1 "))
if err != nil {
panic(err)
}
func invalidHTTP1LookingFrameHeader() FrameHeader {
fh, _ := readFrameHeader(make([]byte, frameHeaderLen), strings.NewReader("HTTP/1.1 "))
return fh
}()

func (h FrameHeader) looksLikeHTTP1Header() bool { return h == invalidHTTP1LookingFrameHeader }
}

// ReadFrameHeader reads 9 bytes from r and returns a FrameHeader.
// Most users should use Framer.ReadFrame instead.
Expand Down Expand Up @@ -513,14 +508,14 @@ func (fr *Framer) ReadFrame() (Frame, error) {
return nil, err
}
if fh.Length > fr.maxReadSize {
if fh.looksLikeHTTP1Header() {
if fh == invalidHTTP1LookingFrameHeader() {
return nil, fmt.Errorf("http2: failed reading the frame payload: %w, note that the frame header looked like an HTTP/1.1 header", err)
}
return nil, ErrFrameTooLarge
}
payload := fr.getReadBuf(fh.Length)
if _, err := io.ReadFull(fr.r, payload); err != nil {
if fh.looksLikeHTTP1Header() {
if fh == invalidHTTP1LookingFrameHeader() {
return nil, fmt.Errorf("http2: failed reading the frame payload: %w, note that the frame header looked like an HTTP/1.1 header", err)
}
return nil, err
Expand Down
21 changes: 0 additions & 21 deletions http2/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,27 +626,6 @@ func TestReadFrameHeader(t *testing.T) {
}
}

func TestReadFrameHeader_FromHTTP1Header(t *testing.T) {
tests := []struct {
in string
looksLikeHTTP1 bool
}{
// Ignore high bit:
{in: "\xff\xff\xff" + "\xff" + "\xff" + "\xff\xff\xff\xff", looksLikeHTTP1: false},
{in: "HTTP/1.1 400 Bad Request\r\n", looksLikeHTTP1: true},
}
for i, tt := range tests {
got, err := readFrameHeader(make([]byte, 9), strings.NewReader(tt.in))
if err != nil {
t.Errorf("%d. readFrameHeader(%q) = %v", i, tt.in, err)
continue
}
if got.looksLikeHTTP1Header() != tt.looksLikeHTTP1 {
t.Errorf("%d. readFrameHeader(%q).looksLikeHTTP1Header = %v; want %v", i, tt.in, got.looksLikeHTTP1Header(), tt.looksLikeHTTP1)
}
}
}

func TestReadWriteFrameHeader(t *testing.T) {
tests := []struct {
len uint32
Expand Down

0 comments on commit 4abf41d

Please sign in to comment.