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

Allow HTTP::Client to work with any IO #9543

Merged
merged 5 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,40 @@ module HTTP
request.host.should eq "other.example.com"
end
end

it "works with IO" do
io_response = IO::Memory.new <<-RESPONSE.gsub('\n', "\r\n")
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 3

Hi!
RESPONSE
io_request = IO::Memory.new
io = IO::Stapled.new(io_response, io_request)
client = Client.new(io)
response = client.get("/")
response.body.should eq("Hi!")

io_request.rewind
request = HTTP::Request.from_io(io_request).as(HTTP::Request)
request.host.should eq("")
end

it "can specify host and port when initialized with IO" do
client = Client.new(IO::Memory.new, "host", 1234)
client.host.should eq("host")
client.port.should eq(1234)
end

it "cannot reconnect when initialized with IO" do
io = IO::Memory.new
client = Client.new(io)
client.close
io.closed?.should be_true
expect_raises(Exception, "This HTTP::Client cannot be reconnected") do
client.get("/")
end
end
end
end
4 changes: 2 additions & 2 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ describe "#remote_address" do
HTTP::Client.new(URI.parse("http://#{address1}/")) do |client|
client.get("/")

remote_address.should eq(client.@socket.as(IPSocket).local_address)
remote_address.should eq(client.@io.as(IPSocket).local_address)
end
end
end
Expand All @@ -516,7 +516,7 @@ describe "#remote_address" do
uri: URI.parse("https://#{ip_address1}"),
tls: client_context) do |client|
client.get("/")
remote_address.should eq(client.@socket.as(OpenSSL::SSL::Socket).local_address)
remote_address.should eq(client.@io.as(OpenSSL::SSL::Socket).local_address)
end
end
end
Expand Down
49 changes: 31 additions & 18 deletions src/http/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,21 @@ class HTTP::Client
# ```
{% if flag?(:without_openssl) %}
getter! tls : Nil
@socket : TCPSocket | Nil
alias TLSContext = Bool | Nil
{% else %}
getter! tls : OpenSSL::SSL::Context::Client
@socket : TCPSocket | OpenSSL::SSL::Socket | Nil
alias TLSContext = OpenSSL::SSL::Context::Client | Bool | Nil
{% end %}

# Whether automatic compression/decompression is enabled.
property? compress : Bool = true

@io : IO?
@dns_timeout : Float64?
@connect_timeout : Float64?
@read_timeout : Float64?
@write_timeout : Float64?
@reconnect = true

# Creates a new HTTP client with the given *host*, *port* and *tls*
# configurations. If no port is given, the default one will
Expand Down Expand Up @@ -148,6 +148,16 @@ class HTTP::Client
@port = (port || (@tls ? 443 : 80)).to_i
end

# Creates a new HTTP client bound to an existing `IO`.
# *host* and *port* can be specified and they will be used
# to conform the `Host` header on each request.
# Instances created with this constructor cannot be reconnected. Once
# `close` is called explicitly or if the connection doesn't support keep-alive,
# the next call to make a request will raise an exception.
def initialize(@io : IO, @host = "", @port = 80)
@reconnect = false
end

private def check_host_only(string : String)
# When parsing a URI with just a host
# we end up with a URI with just a path
Expand Down Expand Up @@ -585,7 +595,7 @@ class HTTP::Client

private def exec_internal_single(request)
decompress = send_request(request)
HTTP::Client::Response.from_io?(socket, ignore_body: request.ignore_body?, decompress: decompress)
HTTP::Client::Response.from_io?(io, ignore_body: request.ignore_body?, decompress: decompress)
end

private def handle_response(response)
Expand Down Expand Up @@ -632,7 +642,7 @@ class HTTP::Client

private def exec_internal_single(request)
decompress = send_request(request)
HTTP::Client::Response.from_io?(socket, ignore_body: request.ignore_body?, decompress: decompress) do |response|
HTTP::Client::Response.from_io?(io, ignore_body: request.ignore_body?, decompress: decompress) do |response|
yield response
end
end
Expand All @@ -647,8 +657,8 @@ class HTTP::Client
private def send_request(request)
decompress = set_defaults request
run_before_request_callbacks(request)
request.to_io(socket)
socket.flush
request.to_io(io)
io.flush
decompress
end

Expand Down Expand Up @@ -746,29 +756,32 @@ class HTTP::Client

# Closes this client. If used again, a new connection will be opened.
def close
@socket.try &.close
@socket = nil
@io.try &.close
@io = nil
end

private def new_request(method, path, headers, body : BodyType)
HTTP::Request.new(method, path, headers, body)
end

private def socket
socket = @socket
return socket if socket
private def io
io = @io
return io if io
unless @reconnect
raise "This HTTP::Client cannot be reconnected"
end

hostname = @host.starts_with?('[') && @host.ends_with?(']') ? @host[1..-2] : @host
socket = TCPSocket.new hostname, @port, @dns_timeout, @connect_timeout
socket.read_timeout = @read_timeout if @read_timeout
socket.write_timeout = @write_timeout if @write_timeout
socket.sync = false
io = TCPSocket.new hostname, @port, @dns_timeout, @connect_timeout
io.read_timeout = @read_timeout if @read_timeout
io.write_timeout = @write_timeout if @write_timeout
io.sync = false

{% if !flag?(:without_openssl) %}
if tls = @tls
tcp_socket = socket
tcp_socket = io
begin
socket = OpenSSL::SSL::Socket::Client.new(tcp_socket, context: tls, sync_close: true, hostname: @host)
io = OpenSSL::SSL::Socket::Client.new(tcp_socket, context: tls, sync_close: true, hostname: @host)
rescue exc
# don't leak the TCP socket when the SSL connection failed
tcp_socket.close
Expand All @@ -777,7 +790,7 @@ class HTTP::Client
end
{% end %}

@socket = socket
@io = io
end

private def host_header
Expand Down