Learn R Programming

rurl (version 3.0.1)

get_host_type: Classify the host of each URL under a standard profile

Description

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,
  scheme_policy = c("infer", "require"),
  scheme_acceptance = c("web", "general")
)

Value

A character vector the same length as url, each element one of the host_type tokens above, or NA for a row that cannot be classified under the selected standard.

NA means exactly one thing: this row is unclassifiable under the standard you named. It can no longer also mean “no selector was passed”, because omitting url_standard is an error rather than a mode (ADR 0015). An all-NA result is therefore evidence about the input, not about the call.

Arguments

url

A character vector of URLs.

url_standard

Standard profile governing host interpretation: either "rfc3986" or "whatwg". Required, with no default (ADR 0015). The requirement is semantic, not stylistic: whether a host is an IPv4 literal or a registered name is a question only a standard answers, so there is no profile-neutral classification a default could stand for. 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.

Note that file: is admitted under both values, including the default. A file: URL denotes local-filesystem access, and one with a non-empty host (file://server/share/x) is a UNC path on Windows, so dereferencing it reaches a remote SMB share. rurl parses file: URLs; it never opens them. Restricting schemes before anything dereferences them is the caller's job — see SECURITY.md.

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, keeping those functions' output shapes fixed (ADR 0006).

See Also

get_url_diagnostics, get_parse_verdicts, safe_parse_url

Examples

Run this code
get_host_type("http://example.com/", url_standard = "rfc3986")
get_host_type("http://2130706433/", url_standard = "whatwg")

Run the code above in your browser using DataLab