Skip to content

Truly independent web browser

1.5 KB
Raw Blame
/*
 * Copyright (c) 2024, Shannon Booth <[email protected]>
 * Copyright (c) 2024, Sam Atkins <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Array.h>
#include <AK/IPv4Address.h>
#include <AK/IPv6Address.h>
#include <AK/String.h>
#include <AK/Types.h>

namespace URL {

// https://url.spec.whatwg.org/#opaque-host
// An opaque host is a non-empty ASCII string that can be used for further processing.
struct OpaqueHost {
    String value;

    bool operator==(OpaqueHost const&) const = default;
};

// https://url.spec.whatwg.org/#concept-host
// A host is a domain, an IP address, an opaque host, or an empty host. Typically a host serves as a network address,
// but it is sometimes used as opaque identifier in URLs where a network address is not necessary.
class Host {
public:
    // NB: A String alternative is a domain, or an empty host.
    using VariantType = Variant<IPv4Address, IPv6Address, String, OpaqueHost>;
    Host(VariantType&&);
    Host(String&&);

    bool is_domain() const;
    bool is_empty_host() const;
    bool is_loopback_or_localhost() const;

    template<typename T>
    bool has() const { return m_value.template has<T>(); }

    template<typename T>
    T const& get() const { return m_value.template get<T>(); }

    bool operator==(Host const& other) const = default;

    VariantType const& value() const { return m_value; }

    Optional<String> public_suffix() const;
    Optional<String> registrable_domain() const;

    String serialize() const;

private:
    VariantType m_value;
};

}