From affa53fc86c659726bf629fef5900a8b254c58a0 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 5 May 2020 23:35:03 +1200 Subject: [PATCH 1/2] Expose `Body#ready?` for more efficient response handling. --- lib/protocol/http/body/buffered.rb | 5 +++++ lib/protocol/http/body/file.rb | 4 ++++ lib/protocol/http/body/head.rb | 4 ++++ lib/protocol/http/body/readable.rb | 7 +++++++ lib/protocol/http/body/rewindable.rb | 4 ++++ lib/protocol/http/body/wrapper.rb | 4 ++++ spec/protocol/http/body/buffered_spec.rb | 4 ++++ spec/protocol/http/body/file_spec.rb | 4 ++++ 8 files changed, 36 insertions(+) diff --git a/lib/protocol/http/body/buffered.rb b/lib/protocol/http/body/buffered.rb index a8d318d..9d723b2 100644 --- a/lib/protocol/http/body/buffered.rb +++ b/lib/protocol/http/body/buffered.rb @@ -72,6 +72,11 @@ def empty? @index >= @chunks.length end + # A buffered response is always ready. + def ready? + true + end + def read if chunk = @chunks[@index] @index += 1 diff --git a/lib/protocol/http/body/file.rb b/lib/protocol/http/body/file.rb index 7b37751..92cee35 100644 --- a/lib/protocol/http/body/file.rb +++ b/lib/protocol/http/body/file.rb @@ -65,6 +65,10 @@ def empty? @remaining == 0 end + def ready? + true + end + def rewind @file.seek(@offset) end diff --git a/lib/protocol/http/body/head.rb b/lib/protocol/http/body/head.rb index a818e6d..4097804 100644 --- a/lib/protocol/http/body/head.rb +++ b/lib/protocol/http/body/head.rb @@ -42,6 +42,10 @@ def empty? true end + def ready? + true + end + def length @length end diff --git a/lib/protocol/http/body/readable.rb b/lib/protocol/http/body/readable.rb index 3bbe11c..13918aa 100644 --- a/lib/protocol/http/body/readable.rb +++ b/lib/protocol/http/body/readable.rb @@ -44,6 +44,13 @@ def empty? false end + # Whether calling read will block. + # We prefer pessimistic implementation, and thus default to `false`. + # @return [Boolean] + def ready? + false + end + def length nil end diff --git a/lib/protocol/http/body/rewindable.rb b/lib/protocol/http/body/rewindable.rb index db9a2ef..25a1455 100644 --- a/lib/protocol/http/body/rewindable.rb +++ b/lib/protocol/http/body/rewindable.rb @@ -39,6 +39,10 @@ def empty? (@index >= @chunks.size) && super end + def ready? + (@index < @chunks.size) || super + end + # A rewindable body wraps some other body. Convert it to a buffered body def buffered Buffered.new(@chunks) diff --git a/lib/protocol/http/body/wrapper.rb b/lib/protocol/http/body/wrapper.rb index b5c6574..8bd504a 100644 --- a/lib/protocol/http/body/wrapper.rb +++ b/lib/protocol/http/body/wrapper.rb @@ -55,6 +55,10 @@ def empty? @body.empty? end + def ready? + @body.ready? + end + def length @body.length end diff --git a/spec/protocol/http/body/buffered_spec.rb b/spec/protocol/http/body/buffered_spec.rb index 1c1d832..b8a3a4c 100644 --- a/spec/protocol/http/body/buffered_spec.rb +++ b/spec/protocol/http/body/buffered_spec.rb @@ -92,6 +92,10 @@ end end + describe '#ready?' do + it {is_expected.to be_ready} + end + describe "#finish" do it "returns self" do expect(subject.finish).to be == subject diff --git a/spec/protocol/http/body/file_spec.rb b/spec/protocol/http/body/file_spec.rb index bfb374c..f7f26e9 100644 --- a/spec/protocol/http/body/file_spec.rb +++ b/spec/protocol/http/body/file_spec.rb @@ -39,6 +39,10 @@ expect(chunk.encoding).to be == Encoding::BINARY end + + describe '#ready?' do + it {is_expected.to be_ready} + end end context 'partial file' do From 9fd4abae8227b0707efbe56ca820ce87676a642e Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 6 May 2020 00:25:09 +1200 Subject: [PATCH 2/2] Minor version bump. --- lib/protocol/http/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/protocol/http/version.rb b/lib/protocol/http/version.rb index d91c35a..12e8b3e 100644 --- a/lib/protocol/http/version.rb +++ b/lib/protocol/http/version.rb @@ -22,6 +22,6 @@ module Protocol module HTTP - VERSION = "0.18.0" + VERSION = "0.19.0" end end