extending URI to include non ascii hostname attribute; added own custom uri initialization function, which performs additional checks when needed

This commit is contained in:
HoneyryderChuck 2020-11-07 01:12:36 +00:00
parent cbfb5c968e
commit f9c1d7de44
2 changed files with 25 additions and 1 deletions

View File

@ -56,9 +56,17 @@ module HTTPX
module URIExtensions
refine URI::Generic do
def non_ascii_hostname
@non_ascii_hostname
end
def non_ascii_hostname=(hostname)
@non_ascii_hostname = hostname
end
def authority
port_string = port == default_port ? nil : ":#{port}"
"#{host}#{port_string}"
"#{@non_ascii_hostname || host}#{port_string}"
end
def origin

View File

@ -2,6 +2,8 @@
module HTTPX
module Utils
using URIExtensions
module_function
# The value of this field can be either an HTTP-date or a number of
@ -14,5 +16,19 @@ module HTTPX
time = Time.httpdate(retry_after)
time - Time.now
end
def uri(uri)
return Kernel.URI(uri) unless uri.is_a?(String) && !uri.ascii_only?
uri = Kernel.URI(URI.escape(uri))
non_ascii_hostname = URI.unescape(uri.host)
idna_hostname = DomainName.new(non_ascii_hostname).hostname
uri.host = idna_hostname
uri.non_ascii_hostname = non_ascii_hostname
uri
end
end
end