Skip to content
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

Accept IO objects as HTTP request bodies #2096

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,13 @@ module HTTP
client.get("/")
end
end

it "accepts IO objects as body" do
body = MemoryIO.new("hello world")

TestServer.open("localhost", 0, 0) do |server|
Client.exec("POST", "http://localhost:#{server.addr.ip_port}/", nil, body)
end
end
end
end
8 changes: 8 additions & 0 deletions spec/std/http/request_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ module HTTP
io.to_s.should eq("POST / HTTP/1.1\r\nContent-Length: 13\r\n\r\nthisisthebody")
end

it "serialize chunked POST (with IO as body)" do
body = MemoryIO.new "hello world"
request = Request.new "POST", "/", body: body
io = MemoryIO.new
request.to_io(io)
io.to_s.should eq("POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\nb\r\nhello world\r\n0\r\n\r\n")
end

it "parses GET" do
request = Request.from_io(MemoryIO.new("GET / HTTP/1.1\r\nHost: host.example.org\r\n\r\n")).not_nil!
request.method.should eq("GET")
Expand Down
16 changes: 8 additions & 8 deletions src/http/client/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class HTTP::Client
# response = client.{{method.id}}("/", headers: HTTP::Headers{"User-agent": "AwesomeApp"}, body: "Hello!")
# response.body #=> "..."
# ```
def {{method.id}}(path, headers = nil : HTTP::Headers?, body = nil : String?) : HTTP::Client::Response
def {{method.id}}(path, headers = nil : HTTP::Headers?, body = nil : String | IO | Nil) : HTTP::Client::Response
exec {{method.upcase}}, path, headers, body
end

Expand All @@ -206,7 +206,7 @@ class HTTP::Client
# response.body_io.gets #=> "..."
# end
# ```
def {{method.id}}(path, headers = nil : HTTP::Headers?, body = nil : String?)
def {{method.id}}(path, headers = nil : HTTP::Headers?, body = nil : String | IO | Nil)
exec {{method.upcase}}, path, headers, body do |response|
yield response
end
Expand All @@ -219,7 +219,7 @@ class HTTP::Client
# response = HTTP::Client.{{method.id}}("/", headers: HTTP::Headers{"User-agent": "AwesomeApp"}, body: "Hello!")
# response.body #=> "..."
# ```
def self.{{method.id}}(url : String | URI, headers = nil : HTTP::Headers?, body = nil : String?) : HTTP::Client::Response
def self.{{method.id}}(url : String | URI, headers = nil : HTTP::Headers?, body = nil : String | IO | Nil) : HTTP::Client::Response
exec {{method.upcase}}, url, headers, body
end

Expand All @@ -231,7 +231,7 @@ class HTTP::Client
# response.body_io.gets #=> "..."
# end
# ```
def self.{{method.id}}(url : String | URI, headers = nil : HTTP::Headers?, body = nil : String?)
def self.{{method.id}}(url : String | URI, headers = nil : HTTP::Headers?, body = nil : String | IO | Nil)
exec {{method.upcase}}, url, headers, body do |response|
yield response
end
Expand Down Expand Up @@ -348,7 +348,7 @@ class HTTP::Client
# response = client.exec "GET", "/"
# response.body # => "..."
# ```
def exec(method : String, path, headers = nil : HTTP::Headers?, body = nil : String?) : HTTP::Client::Response
def exec(method : String, path, headers = nil : HTTP::Headers?, body = nil : String | IO | Nil) : HTTP::Client::Response
exec new_request method, path, headers, body
end

Expand All @@ -361,7 +361,7 @@ class HTTP::Client
# response.body_io.gets # => "..."
# end
# ```
def exec(method : String, path, headers = nil : HTTP::Headers?, body = nil : String?)
def exec(method : String, path, headers = nil : HTTP::Headers?, body = nil : String | IO | Nil)
exec(new_request(method, path, headers, body)) do |response|
yield response
end
Expand All @@ -374,7 +374,7 @@ class HTTP::Client
# response = HTTP::Client.exec "GET", "http://www.example.com"
# response.body # => "..."
# ```
def self.exec(method, url : String | URI, headers = nil : HTTP::Headers?, body = nil : String?) : HTTP::Client::Response
def self.exec(method, url : String | URI, headers = nil : HTTP::Headers?, body = nil : String | IO | Nil) : HTTP::Client::Response
exec(url) do |client, path|
client.exec method, path, headers, body
end
Expand All @@ -388,7 +388,7 @@ class HTTP::Client
# response.body_io.gets # => "..."
# end
# ```
def self.exec(method, url : String | URI, headers = nil : HTTP::Headers?, body = nil : String?)
def self.exec(method, url : String | URI, headers = nil : HTTP::Headers?, body = nil : String | IO | Nil)
exec(url) do |client, path|
client.exec(method, path, headers, body) do |response|
yield response
Expand Down
8 changes: 5 additions & 3 deletions src/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class HTTP::Request
getter body
getter version

def initialize(@method : String, @resource, @headers = Headers.new : Headers, @body = nil, @version = "HTTP/1.1")
if body = @body
def initialize(@method : String, @resource, @headers = Headers.new : Headers, body = nil, @version = "HTTP/1.1")
if body.is_a?(String)
@headers["Content-Length"] = body.bytesize.to_s
elsif @method == "POST" || @method == "PUT"
elsif !body && (@method == "POST" || @method == "PUT")
@headers["Content-Length"] = "0"
end

@body = body
end

# Returns a convenience wrapper around querying and setting cookie related
Expand Down
2 changes: 1 addition & 1 deletion src/oauth/signature.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct OAuth::Signature
body = request.body
content_type = request.headers["Content-type"]?
if body && content_type == "application/x-www-form-urlencoded"
params.add_query body
params.add_query body.to_s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this rather raise if it's an IO, or at least read it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my bad, I thought to_s returned the entire content as a String but this seems to be only the case for MemoryIO and not File etc. I believe the most intuitive thing is that we read it without raising an error.

end

params
Expand Down