Skip to content

Commit

Permalink
Merge 1a61644 into 13ce3c2
Browse files Browse the repository at this point in the history
  • Loading branch information
jeraki committed Dec 2, 2022
2 parents 13ce3c2 + 1a61644 commit d5f4ddf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
49 changes: 48 additions & 1 deletion lib/http/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class URI
def_delegators :@uri, :scheme, :normalized_scheme, :scheme=
def_delegators :@uri, :user, :normalized_user, :user=
def_delegators :@uri, :password, :normalized_password, :password=
def_delegators :@uri, :host, :normalized_host, :host=
def_delegators :@uri, :authority, :normalized_authority, :authority=
def_delegators :@uri, :origin, :origin=
def_delegators :@uri, :normalized_port, :port=
Expand Down Expand Up @@ -110,6 +109,54 @@ def hash
@hash ||= to_s.hash * -1
end

# Host, either a domain name or IP address. If the host is an IPv6 address, it will be returned
# without brackets surrounding it.
#
# @return [String] The host of the URI
def host
@host ||= begin
ip = IPAddr.new(@uri.host)

if ip.ipv6?
ip.to_s
else
@uri.host
end
rescue IPAddr::Error
@uri.host
end
end

# Normalized host, either a domain name or IP address. If the host is an IPv6 address, it will
# be returned without brackets surrounding it.
#
# @return [String] The normalized host of the URI
def normalized_host
@normalized_host ||= begin
ip = IPAddr.new(@uri.normalized_host)

if ip.ipv6?
ip.to_s
else
@uri.normalized_host
end
rescue IPAddr::Error
@uri.normalized_host
end
end

# Sets the host component for the URI.
#
# @param [String, #to_str] new_host The new host component.
# @return [void]
def host=(new_host)
@uri.host = new_host

# Reset dependent values
remove_instance_variable(:@host) if defined?(@host)
remove_instance_variable(:@normalized_host) if defined?(@normalized_host)
end

# Port number, either as specified or the default if unspecified
#
# @return [Integer] port number
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/http/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
RSpec.describe HTTP::URI do
let(:example_http_uri_string) { "http://example.com" }
let(:example_https_uri_string) { "https://example.com" }
let(:example_ipv6_uri_string) { "https://[2606:2800:220:1:248:1893:25c8:1946]" }

subject(:http_uri) { described_class.parse(example_http_uri_string) }
subject(:https_uri) { described_class.parse(example_https_uri_string) }
subject(:ipv6_uri) { described_class.parse(example_ipv6_uri_string) }

it "knows URI schemes" do
expect(http_uri.scheme).to eq "http"
Expand All @@ -20,6 +22,30 @@
expect(https_uri.port).to eq 443
end

describe "#host" do
it "strips brackets from IPv6 addresses" do
expect(ipv6_uri.host).to eq("2606:2800:220:1:248:1893:25c8:1946")
end
end

describe "#normalized_host" do
it "strips brackets from IPv6 addresses" do
expect(ipv6_uri.normalized_host).to eq("2606:2800:220:1:248:1893:25c8:1946")
end
end

describe "#host=" do
it "resets cached values for #host and #normalized_host" do
expect(http_uri.host).to eq("example.com")
expect(http_uri.normalized_host).to eq("example.com")

http_uri.host = "[2606:2800:220:1:248:1893:25c8:1946]"

expect(http_uri.host).to eq("2606:2800:220:1:248:1893:25c8:1946")
expect(http_uri.normalized_host).to eq("2606:2800:220:1:248:1893:25c8:1946")
end
end

describe "#dup" do
it "doesn't share internal value between duplicates" do
duplicated_uri = http_uri.dup
Expand Down

0 comments on commit d5f4ddf

Please sign in to comment.