Skip to contents

Companion helper for the url_standard selector: reports the host type of each URL as exactly one of "domain", "ipv4", "ipv6", "reg-name", or "missing". Unlike a raw host string, host_type is a function of both the host and the selected standard: the numeric host 2130706433 is a "reg-name" under "rfc3986" but an "ipv4" address under "whatwg". Callers reading the result must therefore know which selector produced it.

Usage

get_host_type(
  url,
  url_standard = NULL,
  scheme_policy = c("infer", "require"),
  scheme_acceptance = c("web", "general")
)

Arguments

url

A character vector of URLs.

url_standard

Standard profile governing host interpretation: NULL (default; no classification, returns NA), "rfc3986", or "whatwg". The gate is semantic, not stylistic: whether a host is an IPv4 literal or a registered name is a question only a standard answers, so with no selector there is no fact to report. get_parse_verdicts is deliberately not gated this way — its layers describe the parse that actually ran, which is defined with or without a selector.

scheme_policy

Controls whether scheme-less, host-shaped input is accepted (an input-acceptance axis, distinct from protocol_handling, which only controls how the scheme is presented, and from url_standard, which controls interpretation). Defaults to "infer".

  • "infer": (Default) Fabricate http:// for scheme-less host-shaped input (e.g. example.com parses as http://example.com), a browser-omnibox-style affordance. This is the historical behavior.

  • "require": Reject scheme-less input — a scheme-less host-shaped value becomes parse_status = "error" rather than gaining a fabricated scheme. Use this for a strict, pure-parser posture. Note this governs only bare host input; scheme-relative //host input is governed separately by scheme_relative_handling.

scheme_acceptance

Which scheme tokens may enter parsing (a scheme-acceptance axis, distinct from scheme_policy, which governs scheme-less input, and from url_standard, which governs interpretation). Defaults to "web".

  • "web": (Default) Only the curated web-scheme allowlist (http/https/ftp/ftps/file) is admitted; a scheme-bearing input outside it is parse_status = "error". This is the historical, byte-for-byte compatible behavior.

  • "general": Admit any syntactically valid scheme token and parse opaque (mailto:x), non-special (foo://host), and RFC-generic URLs. Requires an explicit url_standard ("rfc3986" or "whatwg"), which decides the interpretation; general with url_standard = NULL is an error. Non-special / opaque hosts receive no www-stripping, no domain/TLD derivation, and are never run through the IDNA/punycode helpers. A non-special scheme with no // is an opaque path: it has no authority, so host, user, port and the domain/tld columns are all NA and the entire remainder is the path (query/fragment are still split off). This includes mailto: — the recipient's @ never re-triggers authority parsing. To decompose a mailto: recipient, use the accessors (get_host() / get_domain() / get_user(), ADR 0012 D7) or get_mailto_recipients(); those deliberately return a recipient's parts where this table presents NA, because a recipient domain is extraction metadata, not the URL's authority.

Value

A character vector the same length as url, each element one of the host_type tokens above, or NA when no selector is given.

Details

The metadata is intentionally exposed through this helper rather than as a column on safe_parse_urls or a field on safe_parse_url, so that passing no selector leaves every existing function's output shape unchanged.

Examples

get_host_type("http://example.com/", url_standard = "rfc3986")
#> [1] "domain"
get_host_type("http://2130706433/", url_standard = "whatwg")
#> [1] "ipv4"