Class: URI::WhatwgParser::HostParser

Inherits:
Object
  • Object
show all
Includes:
ParserHelper
Defined in:
lib/uri/whatwg_parser/host_parser.rb

Constant Summary collapse

FORBIDDEN_HOST_CODE_POINT =
Set["\x00", "\t", "\x0a", "\x0d", " ", "#", "/", ":", "<", ">", "?", "@", "[", "\\", "]", "^", "|"]
FORBIDDEN_DOMAIN_CODE_POINT =
FORBIDDEN_HOST_CODE_POINT | C0_CONTROL_PERCENT_ENCODE_SET | Set["%", "\x7f"]
FORBIDDEN_HOST_REGEX =
Regexp.union(FORBIDDEN_HOST_CODE_POINT.to_a)
FORBIDDEN_DOMAIN_REGEX =
Regexp.union(FORBIDDEN_DOMAIN_CODE_POINT.to_a)

Constants included from ParserHelper

ParserHelper::C0_CONTROL_PERCENT_ENCODE_SET

Instance Method Summary collapse

Methods included from ParserHelper

#utf8_percent_encode, #utf8_percent_encode_string

Instance Method Details

#parse(input, opaque = false) ⇒ Object

:nodoc:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/uri/whatwg_parser/host_parser.rb', line 15

def parse(input, opaque = false) # :nodoc:
  return "" if input&.empty?

  if input.start_with?("[")
    raise ParseError, "invalid IPv6 format" unless input.end_with?("]")
    return parse_ipv6(input)
  end

  return parse_opaque_host(input) if opaque

  domain = percent_decode(input)
  ascii_domain = domain_to_ascii(domain)
  if ends_in_number?(ascii_domain)
    ipv4 = parse_ipv4(ascii_domain)
    return serialize_ipv4(ipv4)
  end

  ascii_domain
rescue URI::IDNA::Error, Encoding::CompatibilityError, ArgumentError => _e
  raise ParseError, "invalid host value"
end