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
99module 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:
0 commit comments