From de36239d9abcc93f0bd0fa56ed9d0af2234294dc Mon Sep 17 00:00:00 2001 From: Maruth Goyal Date: Mon, 6 Nov 2023 11:19:27 -0800 Subject: [PATCH] ensure wire always contains a full frame --- lib/protocol/http2/frame.rb | 7 ++++--- lib/protocol/http2/framer.rb | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/protocol/http2/frame.rb b/lib/protocol/http2/frame.rb index 690816e..3c05a9c 100644 --- a/lib/protocol/http2/frame.rb +++ b/lib/protocol/http2/frame.rb @@ -146,7 +146,7 @@ def self.parse_header(buffer) end def read_header(stream) - if buffer = stream.read(9) and buffer.bytesize == 9 + if buffer = stream.peek(9) and buffer.bytesize == 9 @length, @type, @flags, @stream_id = Frame.parse_header(buffer) # puts "read_header: #{@length} #{@type} #{@flags} #{@stream_id}" else @@ -155,8 +155,9 @@ def read_header(stream) end def read_payload(stream) - if payload = stream.read(@length) and payload.bytesize == @length - @payload = payload + length_with_header = 9 + @length + if full_frame = stream.read(length_with_header) and full_frame.bytesize == length_with_header + @payload = full_frame[9..] else raise EOFError, "Could not read frame payload!" end diff --git a/lib/protocol/http2/framer.rb b/lib/protocol/http2/framer.rb index 6c5bcc7..67e5667 100644 --- a/lib/protocol/http2/framer.rb +++ b/lib/protocol/http2/framer.rb @@ -95,7 +95,7 @@ def write_frame(frame) end def read_header - if buffer = @stream.read(9) + if buffer = @stream.peek(9) if buffer.bytesize == 9 return Frame.parse_header(buffer) end