Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

178 строки
5.8 KiB

  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 either a
  67. // privately managed domain (and in practice, not a top level domain) or an
  68. // unmanaged top level domain (and not explicitly mentioned in the
  69. // publicsuffix.org list). For example, "foo.org" and "foo.co.uk" are ICANN
  70. // domains, "foo.dyndns.org" and "foo.blogspot.co.uk" are private domains and
  71. // "cromulent" is an unmanaged top level domain.
  72. //
  73. // Use cases for distinguishing ICANN domains like "foo.com" from private
  74. // domains like "foo.appspot.com" can be found at
  75. // https://wiki.mozilla.org/Public_Suffix_List/Use_Cases
  76. func PublicSuffix(domain string) (publicSuffix string, icann bool) {
  77. lo, hi := uint32(0), uint32(numTLD)
  78. s, suffix, icannNode, wildcard := domain, len(domain), false, false
  79. loop:
  80. for {
  81. dot := strings.LastIndex(s, ".")
  82. if wildcard {
  83. icann = icannNode
  84. suffix = 1 + dot
  85. }
  86. if lo == hi {
  87. break
  88. }
  89. f := find(s[1+dot:], lo, hi)
  90. if f == notFound {
  91. break
  92. }
  93. u := nodes[f] >> (nodesBitsTextOffset + nodesBitsTextLength)
  94. icannNode = u&(1<<nodesBitsICANN-1) != 0
  95. u >>= nodesBitsICANN
  96. u = children[u&(1<<nodesBitsChildren-1)]
  97. lo = u & (1<<childrenBitsLo - 1)
  98. u >>= childrenBitsLo
  99. hi = u & (1<<childrenBitsHi - 1)
  100. u >>= childrenBitsHi
  101. switch u & (1<<childrenBitsNodeType - 1) {
  102. case nodeTypeNormal:
  103. suffix = 1 + dot
  104. case nodeTypeException:
  105. suffix = 1 + len(s)
  106. break loop
  107. }
  108. u >>= childrenBitsNodeType
  109. wildcard = u&(1<<childrenBitsWildcard-1) != 0
  110. if !wildcard {
  111. icann = icannNode
  112. }
  113. if dot == -1 {
  114. break
  115. }
  116. s = s[:dot]
  117. }
  118. if suffix == len(domain) {
  119. // If no rules match, the prevailing rule is "*".
  120. return domain[1+strings.LastIndex(domain, "."):], icann
  121. }
  122. return domain[suffix:], icann
  123. }
  124. const notFound uint32 = 1<<32 - 1
  125. // find returns the index of the node in the range [lo, hi) whose label equals
  126. // label, or notFound if there is no such node. The range is assumed to be in
  127. // strictly increasing node label order.
  128. func find(label string, lo, hi uint32) uint32 {
  129. for lo < hi {
  130. mid := lo + (hi-lo)/2
  131. s := nodeLabel(mid)
  132. if s < label {
  133. lo = mid + 1
  134. } else if s == label {
  135. return mid
  136. } else {
  137. hi = mid
  138. }
  139. }
  140. return notFound
  141. }
  142. // nodeLabel returns the label for the i'th node.
  143. func nodeLabel(i uint32) string {
  144. x := nodes[i]
  145. length := x & (1<<nodesBitsTextLength - 1)
  146. x >>= nodesBitsTextLength
  147. offset := x & (1<<nodesBitsTextOffset - 1)
  148. return text[offset : offset+length]
  149. }
  150. // EffectiveTLDPlusOne returns the effective top level domain plus one more
  151. // label. For example, the eTLD+1 for "foo.bar.golang.org" is "golang.org".
  152. func EffectiveTLDPlusOne(domain string) (string, error) {
  153. suffix, _ := PublicSuffix(domain)
  154. if len(domain) <= len(suffix) {
  155. return "", fmt.Errorf("publicsuffix: cannot derive eTLD+1 for domain %q", domain)
  156. }
  157. i := len(domain) - len(suffix) - 1
  158. if domain[i] != '.' {
  159. return "", fmt.Errorf("publicsuffix: invalid public suffix %q for domain %q", suffix, domain)
  160. }
  161. return domain[1+strings.LastIndex(domain[:i], "."):], nil
  162. }