View Source Handle.Domain (Handle.Domain v0.1.0)

Provides functionality to parse and extract domain components from a given hostname using a list of public and test suffix rules.

Overview

This module loads public suffix rules from a local file (priv/public_suffix_list.dat) and combines them with predefined test domains. It offers a parse/1 function that takes a hostname and returns a structured representation of the domain components (domain, subdomain, TLD, and type) or an error if the domain is invalid.

Parsing a Hostname

  • The parse/1 function takes a hostname as a string.
  • If a matching rule is found, it builds a domain component struct using Components.build/2.
  • If no rule matches, it returns an error tuple indicating an invalid domain.

Example

iex> Handle.Domain.parse("sub.example.co.uk")
{:ok, %Handle.Domain.Components{
  domain: "example",
  subdomain: "sub",
  tld: "co.uk",
  type: :public
}}

Summary

Functions

Parses a given hostname and returns a structured representation of its domain components.

Functions

parse(host)

@spec parse(String.t()) :: {:ok, Handle.Domain.Components.t()} | {:error, String.t()}

Parses a given hostname and returns a structured representation of its domain components.

If the hostname matches a public suffix rule, the function returns a domain component struct. If no rule matches, it returns an error tuple with a message indicating an invalid domain.

Example

iex> Handle.Domain.parse("sub.example.co.uk")
{:ok, %Handle.Domain.Components{
  domain: "example",
  subdomain: "sub",
  tld: "co.uk",
  type: :public
}}

iex> Handle.Domain.parse("sub.example.not_a_tld")
{:error, "Invalid domain"}

localhost

The function recognizes localhost as a special case and returns a domain component struct with an empty TLD and the type set to :test.

iex> Handle.Domain.parse("sub.localhost")
{:ok, %Handle.Domain.Components{
  domain: "localhost",
  subdomain: "sub",
  tld: "",
  type: :test
}}

Test Domains

The function recognizes predefined test domains that are not strictly valid TLDs.

Examples include:

  • .example
  • .invalid
  • .test
  • .local