// an HTTP server can set a cookie for a domain.
//
// A nil value is valid and may be useful for testing but it is not
- // secure: it means that the HTTP server for foo.com can set a cookie
- // for bar.com.
+ // secure: it means that the HTTP server for foo.co.uk can set a cookie
+ // for bar.co.uk.
PublicSuffixList PublicSuffixList
}
if isIP(host) {
return host
}
+
+ var i int
if psl == nil {
- // Key cookies under TLD of host.
- return host[1+strings.LastIndex(host, "."):]
- }
- suffix := psl.PublicSuffix(host)
- if suffix == host {
- return host
- }
- i := len(host) - len(suffix)
- if i <= 0 || host[i-1] != '.' {
- // The provided public suffix list psl is broken.
- // Storing cookies under host is a safe stopgap.
- return host
+ i = strings.LastIndex(host, ".")
+ if i == -1 {
+ return host
+ }
+ } else {
+ suffix := psl.PublicSuffix(host)
+ if suffix == host {
+ return host
+ }
+ i = len(host) - len(suffix)
+ if i <= 0 || host[i-1] != '.' {
+ // The provided public suffix list psl is broken.
+ // Storing cookies under host is a safe stopgap.
+ return host
+ }
}
prevDot := strings.LastIndex(host[:i-1], ".")
return host[prevDot+1:]
t.Errorf("%q: got %q, want %q", host, got, want)
}
}
+}
- for _, host := range []string{"www.example.com", "example.com", "com"} {
- if got := jarKey(host, nil); got != "com" {
- t.Errorf(`%q: got %q, want "com"`, host, got)
+var jarKeyNilPSLTests = map[string]string{
+ "foo.www.example.com": "example.com",
+ "www.example.com": "example.com",
+ "example.com": "example.com",
+ "com": "com",
+ "foo.www.bbc.co.uk": "co.uk",
+ "www.bbc.co.uk": "co.uk",
+ "bbc.co.uk": "co.uk",
+ "co.uk": "co.uk",
+ "uk": "uk",
+ "192.168.0.5": "192.168.0.5",
+}
+
+func TestJarKeyNilPSL(t *testing.T) {
+ for host, want := range jarKeyNilPSLTests {
+ if got := jarKey(host, nil); got != want {
+ t.Errorf("%q: got %q, want %q", host, got, want)
}
}
}