From ddcb2df09a5724577e297542bc2c0291bda3682f Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 16 Sep 2024 11:28:57 +1200 Subject: [PATCH] Initial idea. --- lib/protocol/http/body/buffered.rb | 4 ++++ lib/protocol/http/body/readable.rb | 6 ++++++ lib/protocol/http/body/reader.rb | 8 ++++++++ lib/protocol/http/body/wrapper.rb | 4 ++++ 4 files changed, 22 insertions(+) diff --git a/lib/protocol/http/body/buffered.rb b/lib/protocol/http/body/buffered.rb index 088252b..9753a00 100644 --- a/lib/protocol/http/body/buffered.rb +++ b/lib/protocol/http/body/buffered.rb @@ -90,6 +90,10 @@ def read end end + def discard + clear + end + def write(chunk) @chunks << chunk end diff --git a/lib/protocol/http/body/readable.rb b/lib/protocol/http/body/readable.rb index 68e075c..4dc1259 100644 --- a/lib/protocol/http/body/readable.rb +++ b/lib/protocol/http/body/readable.rb @@ -133,6 +133,12 @@ def finish Buffered.read(self) end + def discard + while chunk = self.read + chunk.clear + end + end + def as_json(...) { class: self.class.name, diff --git a/lib/protocol/http/body/reader.rb b/lib/protocol/http/body/reader.rb index 274d6a0..8859db9 100644 --- a/lib/protocol/http/body/reader.rb +++ b/lib/protocol/http/body/reader.rb @@ -40,6 +40,14 @@ def finish end end + # Discard the body as efficiently as possible. + def discard + if body = @body + @body = nil + body.discard + end + end + # Buffer the entire request/response body. # @returns [Reader] itself. def buffered! diff --git a/lib/protocol/http/body/wrapper.rb b/lib/protocol/http/body/wrapper.rb index 7c4189b..f1dd2b7 100644 --- a/lib/protocol/http/body/wrapper.rb +++ b/lib/protocol/http/body/wrapper.rb @@ -59,6 +59,10 @@ def read @body.read end + def discard + @body.discard + end + def as_json(...) { class: self.class.name,