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 use ipv6 addresses #6147

Merged
merged 5 commits into from
Jun 4, 2018
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
10 changes: 10 additions & 0 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ module HTTP
end
end

it "sends the host header ipv6 with brackets" do
server = HTTP::Server.new do |context|
context.response.print context.request.headers["Host"]
end
address = server.bind_unused_port "::1"
spawn { server.listen }

HTTP::Client.get("http://[::1]:#{address.port}/").body.should eq("[::1]:#{address.port}")
end

it "doesn't read the body if request was HEAD" do
resp_get = TestServer.open("localhost", 0, 0) do |server|
client = Client.new("localhost", server.local_address.port)
Expand Down
7 changes: 7 additions & 0 deletions spec/std/uri_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ end
describe "URI" do
assert_uri("http://www.example.com", scheme: "http", host: "www.example.com")
assert_uri("http://www.example.com:81", scheme: "http", host: "www.example.com", port: 81)
assert_uri("http://[::1]:81", scheme: "http", host: "[::1]", port: 81)
assert_uri("http://www.example.com/foo", scheme: "http", host: "www.example.com", path: "/foo")
assert_uri("http://www.example.com/foo?q=1", scheme: "http", host: "www.example.com", path: "/foo", query: "q=1")
assert_uri("http://www.example.com?q=1", scheme: "http", host: "www.example.com", query: "q=1")
Expand All @@ -32,6 +33,12 @@ describe "URI" do
assert_uri("/foo?q=1", path: "/foo", query: "q=1")
assert_uri("mailto:[email protected]", scheme: "mailto", path: nil, opaque: "[email protected]")

describe "hostname" do
it { URI.parse("http://www.example.com/foo").hostname.should eq("www.example.com") }
it { URI.parse("http://[::1]/foo").hostname.should eq("::1") }
it { URI.parse("/foo").hostname.should be_nil }
end

describe "full_path" do
it { URI.parse("http://www.example.com/foo").full_path.should eq("/foo") }
it { URI.parse("http://www.example.com").full_path.should eq("/") }
Expand Down
3 changes: 2 additions & 1 deletion src/http/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ class HTTP::Client
socket = @socket
return socket if socket

socket = TCPSocket.new @host, @port, @dns_timeout, @connect_timeout
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.sync = false
@socket = socket
Expand Down
10 changes: 10 additions & 0 deletions src/uri.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ class URI
def initialize(@scheme = nil, @host = nil, @port = nil, @path = nil, @query = nil, @user = nil, @password = nil, @fragment = nil, @opaque = nil)
end

# Returns the host part of the URI and unwrap brackets for IPv6 addresses.
#
# ```
# URI.parse("http://[::1]/bar").hostname # => "::1"
# URI.parse("http://[::1]/bar").host # => "[::1]"
# ```
def hostname
host.try { |host| host.starts_with?('[') && host.ends_with?(']') ? host[1..-2] : host }
end

# Returns the full path of this URI.
#
# ```
Expand Down