Skip to content

Commit bddfd7e

Browse files
committed
Use IO::Stream::Readable for Protocol::Rack::Input.
1 parent 5bf4f87 commit bddfd7e

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

lib/protocol/rack/input.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Copyright, 2022-2025, by Samuel Williams.
55
# Copyright, 2023, by Genki Takiuchi.
66

7-
require "protocol/http/body/stream"
7+
require "io/stream/readable"
88

99
module Protocol
1010
module Rack
@@ -14,22 +14,21 @@ module Rack
1414
#
1515
# This implementation is not always rewindable, to avoid buffering the input when handling large uploads. See {Rewindable} for more details.
1616
class Input
17+
include IO::Stream::Readable
18+
1719
# Initialize the input wrapper.
1820
# @parameter body [Protocol::HTTP::Body::Readable]
19-
def initialize(body)
21+
def initialize(body, ...)
2022
@body = body
2123
@closed = false
2224

23-
# Will hold remaining data in `#read`.
24-
@buffer = nil
25+
super(...)
2526
end
2627

2728
# The input body.
2829
# @attribute [Protocol::HTTP::Body::Readable]
2930
attr :body
3031

31-
include Protocol::HTTP::Body::Stream::Reader
32-
3332
# Enumerate chunks of the request body.
3433
# @yields {|chunk| ...}
3534
# @parameter chunk [String]
@@ -65,7 +64,6 @@ def rewind
6564
# If the body is not rewindable, this will fail.
6665
@body.rewind
6766

68-
@buffer = nil
6967
@finished = false
7068
@closed = false
7169

@@ -87,7 +85,11 @@ def empty?
8785

8886
private
8987

90-
def read_next
88+
def flush
89+
# No-op.
90+
end
91+
92+
def sysread(size, buffer = nil)
9193
if @body
9294
# User's may forget to call #close...
9395
if chunk = @body.read
@@ -98,7 +100,15 @@ def read_next
98100
@closed = true
99101
end
100102

101-
return chunk
103+
if buffer
104+
# If a buffer is provided, we copy the chunk into it:
105+
buffer.replace(chunk)
106+
else
107+
# Otherwise we return the chunk directly:
108+
buffer = chunk
109+
end
110+
111+
return buffer
102112
else
103113
unless @closed
104114
# So if we are at the end of the stream, we close it automatically:

protocol-rack.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ Gem::Specification.new do |spec|
2525
spec.required_ruby_version = ">= 3.2"
2626

2727
spec.add_dependency "protocol-http", "~> 0.43"
28+
spec.add_dependency "io-stream", ">= 0.10"
2829
spec.add_dependency "rack", ">= 1.0"
2930
end

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Use `IO::Stream::Readable` for the input body, which is a better tested and more robust interface.
6+
37
## v0.14.0
48

59
- Handling of `HEAD` requests is now more robust.

0 commit comments

Comments
 (0)