-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #409 from janko-m/enable-request-body-streaming
Enable request body streaming with an IO object
- Loading branch information
Showing
9 changed files
with
380 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module HTTP | ||
class Request | ||
class Body | ||
# Maximum chunk size used for reading content of an IO | ||
BUFFER_SIZE = Connection::BUFFER_SIZE | ||
|
||
def initialize(body) | ||
@body = body | ||
|
||
validate_body_type! | ||
end | ||
|
||
# Returns size which should be used for the "Content-Length" header. | ||
# | ||
# @return [Integer] | ||
def size | ||
if @body.is_a?(String) | ||
@body.bytesize | ||
elsif @body.respond_to?(:read) | ||
raise RequestError, "IO object must respond to #size" unless @body.respond_to?(:size) | ||
@body.size | ||
elsif @body.nil? | ||
0 | ||
else | ||
raise RequestError, "cannot determine size of body: #{@body.inspect}" | ||
end | ||
end | ||
|
||
# Yields chunks of content to be streamed to the request body. | ||
# | ||
# @yieldparam [String] | ||
def each | ||
return enum_for(__method__) unless block_given? | ||
|
||
if @body.is_a?(String) | ||
yield @body | ||
elsif @body.respond_to?(:read) | ||
while (data = @body.read(BUFFER_SIZE)) | ||
yield data | ||
end | ||
elsif @body.is_a?(Enumerable) | ||
@body.each { |chunk| yield chunk } | ||
end | ||
end | ||
|
||
private | ||
|
||
def validate_body_type! | ||
return if @body.is_a?(String) | ||
return if @body.respond_to?(:read) | ||
return if @body.is_a?(Enumerable) | ||
return if @body.nil? | ||
|
||
raise RequestError, "body of wrong type: #{@body.class}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.