or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bpf.mdcontext-ctxhttp.mdcontext.mddict.mddns-dnsmessage.mdhtml-atom.mdhtml-charset.mdhtml.mdhttp-httpguts.mdhttp-httpproxy.mdhttp2-h2c.mdhttp2-hpack.mdhttp2.mdicmp.mdidna.mdindex.mdipv4.mdipv6.mdnettest.mdnetutil.mdproxy.mdpublicsuffix.mdquic-qlog.mdquic.mdtrace.mdwebdav.mdwebsocket.mdxsrftoken.md
tile.json

publicsuffix.mddocs/

Public Suffix List

Package publicsuffix provides a public suffix list based on data from https://publicsuffix.org/

A public suffix is one under which Internet users can directly register names. It is related to, but different from, a TLD (top level domain).

Import

import "golang.org/x/net/publicsuffix"

Overview

Key Concepts

  • TLD (Top Level Domain): A domain with no dots, like "com" or "au"
  • eTLD (Effective TLD): The branching point for domain name registrars, like "com.au"
  • Public Suffix: Another name for an eTLD
  • eTLD+1: One more label than the public suffix (e.g., "amazon.co.uk" where "co.uk" is the eTLD)

Examples

  • "com" is both a TLD and a public suffix
  • "amazon.com" and "google.com" are siblings under "com"
  • "com.au" is a public suffix (eTLD) but not a TLD
  • "amazon.com.au" has eTLD "com.au" and eTLD+1 "amazon.com.au"

All of these domains have the same eTLD+1 "amazon.co.uk":

Variables

// List implements the cookiejar.PublicSuffixList interface
var List cookiejar.PublicSuffixList = list{}

Functions

// EffectiveTLDPlusOne returns the effective top level domain plus one more label
func EffectiveTLDPlusOne(domain string) (string, error)

// PublicSuffix returns the public suffix of the domain
func PublicSuffix(domain string) (publicSuffix string, icann bool)

EffectiveTLDPlusOne

EffectiveTLDPlusOne returns the effective top level domain plus one more label. For example, the eTLD+1 for "foo.bar.golang.org" is "golang.org".

PublicSuffix

PublicSuffix returns the public suffix of the domain using a copy of the publicsuffix.org database compiled into the library.

The icann boolean indicates whether the public suffix is managed by the Internet Corporation for Assigned Names and Numbers. If not, the public suffix is either a privately managed domain (not a top level domain) or an unmanaged top level domain (not explicitly mentioned in the publicsuffix.org list).

Examples:

  • "foo.org" and "foo.co.uk" are ICANN domains
  • "foo.dyndns.org" is a private domain
  • "cromulent" is an unmanaged top level domain

Usage Examples

Getting eTLD+1

import (
    "fmt"
    "golang.org/x/net/publicsuffix"
)

func getDomain(hostname string) {
    etldPlusOne, err := publicsuffix.EffectiveTLDPlusOne(hostname)
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    fmt.Printf("eTLD+1 for %s is %s\n", hostname, etldPlusOne)
}

// Usage:
// getDomain("www.books.amazon.co.uk")  // Output: amazon.co.uk
// getDomain("foo.bar.golang.org")      // Output: golang.org
// getDomain("www.google.com")          // Output: google.com

Getting Public Suffix

func getPublicSuffix(domain string) {
    suffix, icann := publicsuffix.PublicSuffix(domain)

    fmt.Printf("Domain: %s\n", domain)
    fmt.Printf("Public Suffix: %s\n", suffix)

    if icann {
        fmt.Println("Type: ICANN managed")
    } else {
        fmt.Println("Type: Private or unmanaged")
    }
}

// Usage:
// getPublicSuffix("www.amazon.co.uk")  // Suffix: co.uk, ICANN: true
// getPublicSuffix("foo.dyndns.org")    // Suffix: dyndns.org, ICANN: false
// getPublicSuffix("www.google.com")    // Suffix: com, ICANN: true

Cookie Domain Partitioning

func canShareCookies(domain1, domain2 string) (bool, error) {
    etld1, err := publicsuffix.EffectiveTLDPlusOne(domain1)
    if err != nil {
        return false, err
    }

    etld2, err := publicsuffix.EffectiveTLDPlusOne(domain2)
    if err != nil {
        return false, err
    }

    // Domains can share cookies if they have the same eTLD+1
    return etld1 == etld2, nil
}

// Usage:
// canShareCookies("maps.google.com", "www.google.com")      // true
// canShareCookies("amazon.com.au", "google.com.au")         // false

Using with HTTP Cookie Jar

import (
    "golang.org/x/net/publicsuffix"
    "net/http"
    "net/http/cookiejar"
)

func createCookieJar() (*cookiejar.Jar, error) {
    // Use the publicsuffix list for proper cookie domain handling
    jar, err := cookiejar.New(&cookiejar.Options{
        PublicSuffixList: publicsuffix.List,
    })
    if err != nil {
        return nil, err
    }

    return jar, nil
}