From 53d76b666420239ff7f03dcb89ed2eb4b3139c44 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 26 Aug 2018 09:49:11 +0700 Subject: [PATCH] only use duplicate stream data error internally in the streamFrameSorter --- receive_stream.go | 2 +- stream_frame_sorter.go | 15 ++++++++++----- stream_frame_sorter_test.go | 14 +++++++------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/receive_stream.go b/receive_stream.go index fd7a56bb..3a96c395 100644 --- a/receive_stream.go +++ b/receive_stream.go @@ -209,7 +209,7 @@ func (s *receiveStream) handleStreamFrame(frame *wire.StreamFrame) error { s.mutex.Lock() defer s.mutex.Unlock() - if err := s.frameQueue.Push(frame.Data, frame.Offset, frame.FinBit); err != nil && err != errDuplicateStreamData { + if err := s.frameQueue.Push(frame.Data, frame.Offset, frame.FinBit); err != nil { return err } s.signalRead() diff --git a/stream_frame_sorter.go b/stream_frame_sorter.go index 48812a6b..355b665d 100644 --- a/stream_frame_sorter.go +++ b/stream_frame_sorter.go @@ -14,10 +14,7 @@ type streamFrameSorter struct { gaps *utils.ByteIntervalList } -var ( - errTooManyGapsInReceivedStreamData = errors.New("Too many gaps in received StreamFrame data") - errDuplicateStreamData = errors.New("Duplicate Stream Data") -) +var errDuplicateStreamData = errors.New("Duplicate Stream Data") func newStreamFrameSorter() *streamFrameSorter { s := streamFrameSorter{ @@ -30,6 +27,14 @@ func newStreamFrameSorter() *streamFrameSorter { } func (s *streamFrameSorter) Push(data []byte, offset protocol.ByteCount, fin bool) error { + err := s.push(data, offset, fin) + if err == errDuplicateStreamData { + return nil + } + return err +} + +func (s *streamFrameSorter) push(data []byte, offset protocol.ByteCount, fin bool) error { if fin { s.finalOffset = offset + protocol.ByteCount(len(data)) } @@ -129,7 +134,7 @@ func (s *streamFrameSorter) Push(data []byte, offset protocol.ByteCount, fin boo } if s.gaps.Len() > protocol.MaxStreamFrameSorterGaps { - return errTooManyGapsInReceivedStreamData + return errors.New("Too many gaps in received data") } if wasCut { diff --git a/stream_frame_sorter_test.go b/stream_frame_sorter_test.go index 7221ee74..e0379cfb 100644 --- a/stream_frame_sorter_test.go +++ b/stream_frame_sorter_test.go @@ -375,21 +375,21 @@ var _ = Describe("STREAM frame sorter", func() { }) It("does not modify data when receiving a duplicate", func() { - err := s.Push([]byte("fffff"), 0, false) + err := s.push([]byte("fffff"), 0, false) Expect(err).To(MatchError(errDuplicateStreamData)) Expect(s.queue[0]).ToNot(Equal([]byte("fffff"))) }) It("detects a duplicate frame that is smaller than the original, starting at the beginning", func() { // 10 to 12 - err := s.Push([]byte("12"), 10, false) + err := s.push([]byte("12"), 10, false) Expect(err).To(MatchError(errDuplicateStreamData)) Expect(s.queue[10]).To(HaveLen(5)) }) It("detects a duplicate frame that is smaller than the original, somewhere in the middle", func() { // 1 to 4 - err := s.Push([]byte("123"), 1, false) + err := s.push([]byte("123"), 1, false) Expect(err).To(MatchError(errDuplicateStreamData)) Expect(s.queue[0]).To(HaveLen(5)) Expect(s.queue).ToNot(HaveKey(protocol.ByteCount(1))) @@ -397,7 +397,7 @@ var _ = Describe("STREAM frame sorter", func() { It("detects a duplicate frame that is smaller than the original, somewhere in the middle in the last block", func() { // 11 to 14 - err := s.Push([]byte("123"), 11, false) + err := s.push([]byte("123"), 11, false) Expect(err).To(MatchError(errDuplicateStreamData)) Expect(s.queue[10]).To(HaveLen(5)) Expect(s.queue).ToNot(HaveKey(protocol.ByteCount(11))) @@ -405,7 +405,7 @@ var _ = Describe("STREAM frame sorter", func() { It("detects a duplicate frame that is smaller than the original, with aligned end in the last block", func() { // 11 to 15 - err := s.Push([]byte("1234"), 1, false) + err := s.push([]byte("1234"), 1, false) Expect(err).To(MatchError(errDuplicateStreamData)) Expect(s.queue[10]).To(HaveLen(5)) Expect(s.queue).ToNot(HaveKey(protocol.ByteCount(11))) @@ -413,7 +413,7 @@ var _ = Describe("STREAM frame sorter", func() { It("detects a duplicate frame that is smaller than the original, with aligned end", func() { // 3 to 5 - err := s.Push([]byte("12"), 3, false) + err := s.push([]byte("12"), 3, false) Expect(err).To(MatchError(errDuplicateStreamData)) Expect(s.queue[0]).To(HaveLen(5)) Expect(s.queue).ToNot(HaveKey(protocol.ByteCount(3))) @@ -427,7 +427,7 @@ var _ = Describe("STREAM frame sorter", func() { } Expect(s.gaps.Len()).To(Equal(protocol.MaxStreamFrameSorterGaps)) err := s.Push([]byte("foobar"), protocol.ByteCount(protocol.MaxStreamFrameSorterGaps*7)+100, false) - Expect(err).To(MatchError(errTooManyGapsInReceivedStreamData)) + Expect(err).To(MatchError("Too many gaps in received data")) }) }) })