Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure wire always contains a full H/2 frame #15

Merged
merged 7 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/protocol/http2/frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/protocol/http2/framer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Released under the MIT License.
# Copyright, 2019-2024, by Samuel Williams.


require "async/io/stream"

require_relative 'error'

require_relative 'data_frame'
Expand Down Expand Up @@ -37,7 +40,12 @@ module HTTP2

class Framer
def initialize(stream, frames = FRAMES)
@stream = stream
@stream = case stream
when Async::IO::Stream
stream
else
Async::IO::Stream.new(stream)
end
@frames = frames
end

Expand Down Expand Up @@ -99,7 +107,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
Expand Down
1 change: 1 addition & 0 deletions protocol-http2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 3.1"

spec.add_dependency "async-io", "~> 1.37"
spec.add_dependency "protocol-hpack", "~> 1.4"
spec.add_dependency "protocol-http", "~> 0.18"
end
Loading