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 "golang.org/x/net/publicsuffix"All of these domains have the same eTLD+1 "amazon.co.uk":
// List implements the cookiejar.PublicSuffixList interface
var List cookiejar.PublicSuffixList = list{}// 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 returns the effective top level domain plus one more label. For example, the eTLD+1 for "foo.bar.golang.org" is "golang.org".
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:
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.comfunc 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: truefunc 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") // falseimport (
"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
}