pax_global_header00006660000000000000000000000064142657666430014535gustar00rootroot0000000000000052 comment=7de50c5ae1ad10ba3e8e34283c318d54553fa478 go-tld-1.2.1/000077500000000000000000000000001426576664300127245ustar00rootroot00000000000000go-tld-1.2.1/.github/000077500000000000000000000000001426576664300142645ustar00rootroot00000000000000go-tld-1.2.1/.github/dependabot.yml000066400000000000000000000001521426576664300171120ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: gomod directory: "/" schedule: interval: monthly go-tld-1.2.1/.github/workflows/000077500000000000000000000000001426576664300163215ustar00rootroot00000000000000go-tld-1.2.1/.github/workflows/test.yml000066400000000000000000000010011426576664300200130ustar00rootroot00000000000000on: [push, pull_request] name: Tests jobs: test: strategy: matrix: go-version: [1.15.x, 1.16.x, 1.17.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: - name: Install Go uses: actions/setup-go@v1 with: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v2 - name: Build run: go build -v . - name: Test run: go test -v ./... go-tld-1.2.1/LICENSE000066400000000000000000000020701426576664300137300ustar00rootroot00000000000000Copyright © 2019-2020 Jaime Pillora Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. go-tld-1.2.1/README.md000066400000000000000000000054521426576664300142110ustar00rootroot00000000000000# TLD Parser in Go [![GoDoc](https://godoc.org/github.com/jpillora/go-tld?status.svg)](https://pkg.go.dev/github.com/jpillora/go-tld?tab=doc) [![Tests](https://github.com/jpillora/go-tld/workflows/Tests/badge.svg)](https://github.com/jpillora/go-tld/actions?workflow=Tests) The `tld` package has the same API ([see godoc](http://godoc.org/github.com/jpillora/go-tld)) as `net/url` except `tld.URL` contains extra fields: `Subdomain`, `Domain`, `TLD` and `Port`. _Note:_ This is a wrapper around http://golang.org/x/net/publicsuffix. ### Install ``` go get github.com/jpillora/go-tld ``` ### Usage ```go package main import ( "fmt" tld "github.com/jpillora/go-tld" ) func main() { urls := []string{ "http://google.com", "http://blog.google", "https://www.medi-cal.ca.gov/", "https://ato.gov.au", "http://a.very.complex-domain.co.uk:8080/foo/bar", "http://a.domain.that.is.unmanaged", } for _, url := range urls { u, _ := tld.Parse(url) fmt.Printf("%50s = [ %s ] [ %s ] [ %s ] [ %s ] [ %s ] [ %t ]\n", u, u.Subdomain, u.Domain, u.TLD, u.Port, u.Path, u.ICANN) } } ``` ``` $ go run main.go http://google.com = [ ] [ google ] [ com ] [ ] [ ] [ true ] http://blog.google = [ ] [ blog ] [ google ] [ ] [ ] [ true ] https://www.medi-cal.ca.gov/ = [ www.medi-cal ] [ ca ] [ gov ] [ ] [ / ] [ true ] https://ato.gov.au = [ ] [ ato ] [ gov.au ] [ ] [ ] [ true ] http://a.very.complex-domain.co.uk:8080/foo/bar = [ a.very ] [ complex-domain ] [ co.uk ] [ 8080 ] [ /foo/bar ] [ true ] http://a.domain.that.is.unmanaged = [ a.domain.that ] [ is ] [ unmanaged ] [ ] [ ] [ false ] ``` #### MIT License Copyright © 2019 Jaime Pillora <dev@jpillora.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. go-tld-1.2.1/example/000077500000000000000000000000001426576664300143575ustar00rootroot00000000000000go-tld-1.2.1/example/main.go000066400000000000000000000007431426576664300156360ustar00rootroot00000000000000package main import ( "fmt" "github.com/jpillora/go-tld" ) func main() { urls := []string{ "http://google.com", "http://blog.google", "https://www.medi-cal.ca.gov/", "https://ato.gov.au", "http://a.very.complex-domain.co.uk:8080/foo/bar", "http://a.domain.that.is.unmanaged", } for _, url := range urls { u, _ := tld.Parse(url) fmt.Printf("%50s = [ %s ] [ %s ] [ %s ] [ %s ] [ %s ] [ %t ]\n", u, u.Subdomain, u.Domain, u.TLD, u.Port, u.Path, u.ICANN) } } go-tld-1.2.1/go.mod000066400000000000000000000001501426576664300140260ustar00rootroot00000000000000module github.com/jpillora/go-tld go 1.14 require golang.org/x/net v0.0.0-20220225172249-27dd8689420f go-tld-1.2.1/go.sum000066400000000000000000000013161426576664300140600ustar00rootroot00000000000000golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= go-tld-1.2.1/parse.go000066400000000000000000000033171426576664300143710ustar00rootroot00000000000000//go:generate sh generate.sh //Package tld has the same API as net/url except //tld.URL contains extra fields: Subdomain, Domain, TLD and Port. package tld import ( "fmt" "net/url" "strings" "golang.org/x/net/publicsuffix" ) //URL embeds net/url and adds extra fields ontop type URL struct { Subdomain, Domain, TLD, Port string ICANN bool *url.URL } //Parse mirrors net/url.Parse except instead it returns //a tld.URL, which contains extra fields. func Parse(s string) (*URL, error) { url, err := url.Parse(s) if err != nil { return nil, err } if url.Host == "" { return &URL{URL: url}, nil } dom, port := domainPort(url.Host) //etld+1 etld1, err := publicsuffix.EffectiveTLDPlusOne(dom) suffix, icann := publicsuffix.PublicSuffix(strings.ToLower(dom)) // HACK: attempt to support valid domains which are not registered with ICAN if err != nil && !icann && suffix == dom { etld1 = dom err = nil } if err != nil { return nil, err } //convert to domain name, and tld i := strings.Index(etld1, ".") if i < 0 { return nil, fmt.Errorf("tld: failed parsing %q", s) } domName := etld1[0:i] tld := etld1[i+1:] //and subdomain sub := "" if rest := strings.TrimSuffix(dom, "."+etld1); rest != dom { sub = rest } return &URL{ Subdomain: sub, Domain: domName, TLD: tld, Port: port, ICANN: icann, URL: url, }, nil } func domainPort(host string) (string, string) { for i := len(host) - 1; i >= 0; i-- { if host[i] == ':' { return host[:i], host[i+1:] } else if host[i] < '0' || host[i] > '9' { return host, "" } } //will only land here if the string is all digits, //net/url should prevent that from happening return host, "" } go-tld-1.2.1/parse_test.go000066400000000000000000000033521426576664300154270ustar00rootroot00000000000000package tld import ( "testing" ) func run(input, sub, dom, tld string, icann, errorExpected bool, t *testing.T) { u, err := Parse(input) if err != nil && errorExpected { return } else if err != nil { t.Errorf("errored '%s'", err) } else if u.TLD != tld { t.Errorf("should have TLD '%s', got '%s'", tld, u.TLD) } else if u.Domain != dom { t.Errorf("should have Domain '%s', got '%s'", dom, u.Domain) } else if u.Subdomain != sub { t.Errorf("should have Subdomain '%s', got '%s'", sub, u.Subdomain) } else if u.ICANN != icann { t.Errorf("should have Icann '%t', got '%t'", icann, u.ICANN) } } func Test0(t *testing.T) { run("http://foo.com", "", "foo", "com", true, false, t) } func Test1(t *testing.T) { run("http://zip.zop.foo.com", "zip.zop", "foo", "com", true, false, t) } func Test2(t *testing.T) { run("http://au.com.au", "", "au", "com.au", true, false, t) } func Test3(t *testing.T) { run("http://im.from.england.co.uk:1900", "im.from", "england", "co.uk", true, false, t) } func Test4(t *testing.T) { run("https://google.com", "", "google", "com", true, false, t) } func Test5(t *testing.T) { run("https://foo.notmanaged", "", "foo", "notmanaged", false, false, t) } func Test6(t *testing.T) { run("https://google.Com", "", "google", "Com", true, false, t) } func Test7(t *testing.T) { run("https://github.io", "", "github", "io", false, false, t) } func Test8(t *testing.T) { // test expects error run("https://no_dot_should_not_panic", "", "", "", false, true, t) } func Test9(t *testing.T) { // test expects error run("https://.start_with_dot_should_fail", "", "", "", false, true, t) } func Test10(t *testing.T) { // test expects error run("https://ends_with_dot_should_fail.", "", "", "", false, true, t) }