list.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:generate go run gen.go
  5. // Package publicsuffix provides a public suffix list based on data from
  6. // https://publicsuffix.org/
  7. //
  8. // A public suffix is one under which Internet users can directly register
  9. // names. It is related to, but different from, a TLD (top level domain).
  10. //
  11. // "com" is a TLD (top level domain). Top level means it has no dots.
  12. //
  13. // "com" is also a public suffix. Amazon and Google have registered different
  14. // siblings under that domain: "amazon.com" and "google.com".
  15. //
  16. // "au" is another TLD, again because it has no dots. But it's not "amazon.au".
  17. // Instead, it's "amazon.com.au".
  18. //
  19. // "com.au" isn't an actual TLD, because it's not at the top level (it has
  20. // dots). But it is an eTLD (effective TLD), because that's the branching point
  21. // for domain name registrars.
  22. //
  23. // Another name for "an eTLD" is "a public suffix". Often, what's more of
  24. // interest is the eTLD+1, or one more label than the public suffix. For
  25. // example, browsers partition read/write access to HTTP cookies according to
  26. // the eTLD+1. Web pages served from "amazon.com.au" can't read cookies from
  27. // "google.com.au", but web pages served from "maps.google.com" can share
  28. // cookies from "www.google.com", so you don't have to sign into Google Maps
  29. // separately from signing into Google Web Search. Note that all four of those
  30. // domains have 3 labels and 2 dots. The first two domains are each an eTLD+1,
  31. // the last two are not (but share the same eTLD+1: "google.com").
  32. //
  33. // All of these domains have the same eTLD+1:
  34. // - "www.books.amazon.co.uk"
  35. // - "books.amazon.co.uk"
  36. // - "amazon.co.uk"
  37. // Specifically, the eTLD+1 is "amazon.co.uk", because the eTLD is "co.uk".
  38. //
  39. // There is no closed form algorithm to calculate the eTLD of a domain.
  40. // Instead, the calculation is data driven. This package provides a
  41. // pre-compiled snapshot of Mozilla's PSL (Public Suffix List) data at
  42. // https://publicsuffix.org/
  43. package publicsuffix // import "golang.org/x/net/publicsuffix"
  44. // TODO: specify case sensitivity and leading/trailing dot behavior for
  45. // func PublicSuffix and func EffectiveTLDPlusOne.
  46. import (
  47. "fmt"
  48. "net/http/cookiejar"
  49. "strings"
  50. )
  51. // List implements the cookiejar.PublicSuffixList interface by calling the
  52. // PublicSuffix function.
  53. var List cookiejar.PublicSuffixList = list{}
  54. type list struct{}
  55. func (list) PublicSuffix(domain string) string {
  56. ps, _ := PublicSuffix(domain)
  57. return ps
  58. }
  59. func (list) String() string {
  60. return version
  61. }
  62. // PublicSuffix returns the public suffix of the domain using a copy of the
  63. // publicsuffix.org database compiled into the library.
  64. //
  65. // icann is whether the public suffix is managed by the Internet Corporation
  66. // for Assigned Names and Numbers. If not, the public suffix is privately
  67. // managed. For example, foo.org and foo.co.uk are ICANN domains,
  68. // foo.dyndns.org and foo.blogspot.co.uk are private domains.
  69. //
  70. // Use cases for distinguishing ICANN domains like foo.com from private
  71. // domains like foo.appspot.com can be found at
  72. // https://wiki.mozilla.org/Public_Suffix_List/Use_Cases
  73. func PublicSuffix(domain string) (publicSuffix string, icann bool) {
  74. lo, hi := uint32(0), uint32(numTLD)
  75. s, suffix, wildcard := domain, len(domain), false
  76. loop:
  77. for {
  78. dot := strings.LastIndex(s, ".")
  79. if wildcard {
  80. suffix = 1 + dot
  81. }
  82. if lo == hi {
  83. break
  84. }
  85. f := find(s[1+dot:], lo, hi)
  86. if f == notFound {
  87. break
  88. }
  89. u := nodes[f] >> (nodesBitsTextOffset + nodesBitsTextLength)
  90. icann = u&(1<<nodesBitsICANN-1) != 0
  91. u >>= nodesBitsICANN
  92. u = children[u&(1<<nodesBitsChildren-1)]
  93. lo = u & (1<<childrenBitsLo - 1)
  94. u >>= childrenBitsLo
  95. hi = u & (1<<childrenBitsHi - 1)
  96. u >>= childrenBitsHi
  97. switch u & (1<<childrenBitsNodeType - 1) {
  98. case nodeTypeNormal:
  99. suffix = 1 + dot
  100. case nodeTypeException:
  101. suffix = 1 + len(s)
  102. break loop
  103. }
  104. u >>= childrenBitsNodeType
  105. wildcard = u&(1<<childrenBitsWildcard-1) != 0
  106. if dot == -1 {
  107. break
  108. }
  109. s = s[:dot]
  110. }
  111. if suffix == len(domain) {
  112. // If no rules match, the prevailing rule is "*".
  113. return domain[1+strings.LastIndex(domain, "."):], icann
  114. }
  115. return domain[suffix:], icann
  116. }
  117. const notFound uint32 = 1<<32 - 1
  118. // find returns the index of the node in the range [lo, hi) whose label equals
  119. // label, or notFound if there is no such node. The range is assumed to be in
  120. // strictly increasing node label order.
  121. func find(label string, lo, hi uint32) uint32 {
  122. for lo < hi {
  123. mid := lo + (hi-lo)/2
  124. s := nodeLabel(mid)
  125. if s < label {
  126. lo = mid + 1
  127. } else if s == label {
  128. return mid
  129. } else {
  130. hi = mid
  131. }
  132. }
  133. return notFound
  134. }
  135. // nodeLabel returns the label for the i'th node.
  136. func nodeLabel(i uint32) string {
  137. x := nodes[i]
  138. length := x & (1<<nodesBitsTextLength - 1)
  139. x >>= nodesBitsTextLength
  140. offset := x & (1<<nodesBitsTextOffset - 1)
  141. return text[offset : offset+length]
  142. }
  143. // EffectiveTLDPlusOne returns the effective top level domain plus one more
  144. // label. For example, the eTLD+1 for "foo.bar.golang.org" is "golang.org".
  145. func EffectiveTLDPlusOne(domain string) (string, error) {
  146. suffix, _ := PublicSuffix(domain)
  147. if len(domain) <= len(suffix) {
  148. return "", fmt.Errorf("publicsuffix: cannot derive eTLD+1 for domain %q", domain)
  149. }
  150. i := len(domain) - len(suffix) - 1
  151. if domain[i] != '.' {
  152. return "", fmt.Errorf("publicsuffix: invalid public suffix %q for domain %q", suffix, domain)
  153. }
  154. return domain[1+strings.LastIndex(domain[:i], "."):], nil
  155. }