-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable request body streaming #12
Closed
Closed
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aa48de1
Allow any IO object in FormData::File
janko bddb450
Open File for given path in binary mode
janko 829936f
Officially support Pathname in FormData::File.new
janko fc84d5f
Make all components into IO objects
janko 9298959
Refactor and conform to the code style
janko 22af98d
Make CompositeIO convert strings to StringIOs
janko e9c4cef
Fix comments that were too long
janko 32be2d3
Remove unnecessary variable assignment
janko 41f0b40
Attempt to reduce cyclomatic complexity of CompositeIO#read
janko 42e44cd
Increase maximum allowed complexity in Rubocop
janko 7abde00
Use a buffer when reading IO files in CompositeIO
janko ad1dbef
Always accept IOs as an array in CompositeIO
janko 9153140
Validate type of IOs in CompositeIO.new
janko 43bff5b
Alias Multipart#content_length to Multipart#size
janko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require "stringio" | ||
|
||
module HTTP | ||
module FormData | ||
# Provides IO interface across multiple IO objects. | ||
class CompositeIO | ||
# @param [Array<IO>] ios Array of IO objects | ||
def initialize(*ios) | ||
@ios = ios.flatten.map { |io| io.is_a?(String) ? StringIO.new(io) : io } | ||
@index = 0 | ||
@buffer = String.new | ||
end | ||
|
||
# Reads and returns partial content acrosss multiple IO objects. | ||
# | ||
# @param [Integer] length Number of bytes to retrieve | ||
# @param [String] outbuf String to be replaced with retrieved data | ||
# | ||
# @return [String, nil] | ||
def read(length = nil, outbuf = nil) | ||
outbuf = outbuf.to_s.replace("") | ||
|
||
while current_io | ||
current_io.read(length, @buffer) | ||
outbuf << @buffer | ||
length -= @buffer.length if length | ||
|
||
break if length && length.zero? | ||
|
||
advance_io | ||
end | ||
|
||
outbuf unless length && outbuf.empty? | ||
end | ||
|
||
# Returns sum of all IO sizes. | ||
def size | ||
@size ||= @ios.map(&:size).inject(0, :+) | ||
end | ||
|
||
# Rewinds all IO objects and set cursor to the first IO object. | ||
def rewind | ||
@ios.each(&:rewind) | ||
@index = 0 | ||
end | ||
|
||
private | ||
|
||
# Returns IO object under the cursor. | ||
def current_io | ||
@ios[@index] | ||
end | ||
|
||
# Advances cursor to the next IO object. | ||
def advance_io | ||
@index += 1 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,23 +3,21 @@ | |
require "securerandom" | ||
|
||
require "http/form_data/multipart/param" | ||
require "http/form_data/readable" | ||
require "http/form_data/composite_io" | ||
|
||
module HTTP | ||
module FormData | ||
# `multipart/form-data` form data. | ||
class Multipart | ||
include Readable | ||
|
||
# @param [#to_h, Hash] data form data key-value Hash | ||
def initialize(data) | ||
@parts = Param.coerce FormData.ensure_hash data | ||
@boundary = (Array.new(21, "-") << SecureRandom.hex(21)).join("") | ||
@content_length = nil | ||
end | ||
parts = Param.coerce FormData.ensure_hash data | ||
|
||
# Returns content to be used for HTTP request body. | ||
# | ||
# @return [String] | ||
def to_s | ||
head + @parts.map(&:to_s).join(glue) + tail | ||
@boundary = ("-" * 21) << SecureRandom.hex(21) | ||
@io = CompositeIO.new(*parts.flat_map { |part| [glue, part] }, tail) | ||
end | ||
|
||
# Returns MIME type to be used for HTTP request `Content-Type` header. | ||
|
@@ -34,30 +32,19 @@ def content_type | |
# | ||
# @return [Integer] | ||
def content_length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably worth get rid of this method and use |
||
unless @content_length | ||
@content_length = head.bytesize + tail.bytesize | ||
@content_length += @parts.map(&:size).reduce(:+) | ||
@content_length += (glue.bytesize * (@parts.count - 1)) | ||
end | ||
|
||
@content_length | ||
size | ||
end | ||
|
||
private | ||
|
||
# @return [String] | ||
def head | ||
@head ||= "--#{@boundary}#{CRLF}" | ||
end | ||
|
||
# @return [String] | ||
def glue | ||
@glue ||= "#{CRLF}--#{@boundary}#{CRLF}" | ||
@glue ||= "--#{@boundary}#{CRLF}" | ||
end | ||
|
||
# @return [String] | ||
def tail | ||
@tail ||= "#{CRLF}--#{@boundary}--" | ||
@tail ||= "--#{@boundary}--" | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we should fail if
io
is neitherString
norIO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, CompositeIO seems to be be internal class, and I don't see any point in allowing passing *any amount of args... Instead it should simply accept a single argument as flat array, thus here we will just map it.