From c0286b0c2e28837f45b495ea481599b750514eab Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 10 Aug 2019 13:50:09 +0700 Subject: [PATCH] fix calculation of the length of appended STREAM frames For the last STREAM frame we omit the Length field. When packing STREAM frames, we need to account for this byte saving when calculating the length of the payload. --- framer.go | 6 +++++- framer_test.go | 29 ++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/framer.go b/framer.go index 2f4d131b..84cf1a8e 100644 --- a/framer.go +++ b/framer.go @@ -114,7 +114,11 @@ func (f *framerI) AppendStreamFrames(frames []wire.Frame, maxLen protocol.ByteCo } f.mutex.Unlock() if frameAdded { - frames[len(frames)-1].(*wire.StreamFrame).DataLenPresent = false + lastFrame := frames[len(frames)-1].(*wire.StreamFrame) + lastFrameLen := lastFrame.Length(f.version) + // acount for the smaller size of the last STREAM frame + lastFrame.DataLenPresent = false + length += lastFrame.Length(f.version) - lastFrameLen } return frames, length } diff --git a/framer_test.go b/framer_test.go index 536bc64b..37f68342 100644 --- a/framer_test.go +++ b/framer_test.go @@ -80,22 +80,25 @@ var _ = Describe("Framer", func() { It("returns STREAM frames", func() { streamGetter.EXPECT().GetOrOpenSendStream(id1).Return(stream1, nil) f := &wire.StreamFrame{ - StreamID: id1, - Data: []byte("foobar"), - Offset: 42, + StreamID: id1, + Data: []byte("foobar"), + Offset: 42, + DataLenPresent: true, } stream1.EXPECT().popStreamFrame(gomock.Any()).Return(f, false) framer.AddActiveStream(id1) fs, length := framer.AppendStreamFrames(nil, 1000) Expect(fs).To(Equal([]wire.Frame{f})) + Expect(fs[0].(*wire.StreamFrame).DataLenPresent).To(BeFalse()) Expect(length).To(Equal(f.Length(version))) }) It("appends to a frame slice", func() { streamGetter.EXPECT().GetOrOpenSendStream(id1).Return(stream1, nil) f := &wire.StreamFrame{ - StreamID: id1, - Data: []byte("foobar"), + StreamID: id1, + Data: []byte("foobar"), + DataLenPresent: true, } stream1.EXPECT().popStreamFrame(gomock.Any()).Return(f, false) framer.AddActiveStream(id1) @@ -103,6 +106,7 @@ var _ = Describe("Framer", func() { frames := []wire.Frame{mdf} fs, length := framer.AppendStreamFrames(frames, 1000) Expect(fs).To(Equal([]wire.Frame{mdf, f})) + Expect(fs[1].(*wire.StreamFrame).DataLenPresent).To(BeFalse()) Expect(length).To(Equal(f.Length(version))) }) @@ -110,8 +114,9 @@ var _ = Describe("Framer", func() { streamGetter.EXPECT().GetOrOpenSendStream(id1).Return(nil, nil) streamGetter.EXPECT().GetOrOpenSendStream(id2).Return(stream2, nil) f := &wire.StreamFrame{ - StreamID: id2, - Data: []byte("foobar"), + StreamID: id2, + Data: []byte("foobar"), + DataLenPresent: true, } stream2.EXPECT().popStreamFrame(gomock.Any()).Return(f, false) framer.AddActiveStream(id1) @@ -124,8 +129,9 @@ var _ = Describe("Framer", func() { streamGetter.EXPECT().GetOrOpenSendStream(id1).Return(stream1, nil) streamGetter.EXPECT().GetOrOpenSendStream(id2).Return(stream2, nil) f := &wire.StreamFrame{ - StreamID: id2, - Data: []byte("foobar"), + StreamID: id2, + Data: []byte("foobar"), + DataLenPresent: true, } stream1.EXPECT().popStreamFrame(gomock.Any()).Return(nil, false) stream2.EXPECT().popStreamFrame(gomock.Any()).Return(f, false) @@ -287,8 +293,9 @@ var _ = Describe("Framer", func() { streamGetter.EXPECT().GetOrOpenSendStream(id1).Return(stream1, nil) // pop a frame such that the remaining size is one byte less than the minimum STREAM frame size f := &wire.StreamFrame{ - StreamID: id1, - Data: bytes.Repeat([]byte("f"), int(500-protocol.MinStreamFrameSize)), + StreamID: id1, + Data: bytes.Repeat([]byte("f"), int(500-protocol.MinStreamFrameSize)), + DataLenPresent: true, } stream1.EXPECT().popStreamFrame(gomock.Any()).Return(f, false) framer.AddActiveStream(id1)