Reviewers: nigeltao,
Message:
Hello
nige...@golang.org (cc:
golan...@googlegroups.com),
I'd like you to review this change to
https://code.google.com/p/go/
Description:
exp/cookiejar: implement defaul-path of cookie
The Path of a cookie is set according to sections 5.1.4 and
5.2.4 of RFC 6265.
Update issue 1960.
Please review this at
https://codereview.appspot.com/7069054/
Affected files:
M src/pkg/exp/cookiejar/jar.go
A src/pkg/exp/cookiejar/jar_test.go
Index: src/pkg/exp/cookiejar/jar.go
===================================================================
--- a/src/pkg/exp/cookiejar/jar.go
+++ b/src/pkg/exp/cookiejar/jar.go
@@ -11,6 +11,7 @@
import (
"net/http"
"net/url"
+ "strings"
)
// PublicSuffixList provides the public suffix of a domain. For example:
@@ -85,5 +86,31 @@
//
// It does nothing if the URL's scheme is not HTTP or HTTPS.
func (j *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {
- // TODO.
+ if u.Scheme != "http" && u.Scheme != "https" {
+ return
+ }
+
+ defPath := defaultPath(u.Path)
+ for _, cookie := range cookies {
+ if cookie.Path == "" || cookie.Path[0] != '/' {
+ cookie.Path = defPath
+ }
+ // TODO: handle domain
+ // TODO: handle expiration/deletion
+ // TODO: update storage
+ }
}
+
+// defaultPath returns the "directory" part of an URL's path according to
+// RFC 6265 section 5.1.4.
+func defaultPath(path string) string {
+ if len(path) == 0 || path[0] != '/' {
+ return "/" // empty or malformed
+ }
+
+ i := strings.LastIndex(path, "/") // path starts with "/", so i != -1
+ if i == 0 {
+ return "/" // the "/abc" case
+ }
+ return path[:i] // the "/abc/xyz" and "/abc/xyz/" case
+}
Index: src/pkg/exp/cookiejar/jar_test.go
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/pkg/exp/cookiejar/jar_test.go
@@ -0,0 +1,27 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cookiejar
+
+import (
+ "testing"
+)
+
+var defaultPathTests = map[string]string{
+ "/": "/",
+ "/abc": "/",
+ "/abc/": "/abc",
+ "/abc/xyz": "/abc",
+ "/abc/xyz/": "/abc/xyz",
+ "": "/",
+ "strange": "/",
+}
+
+func TestDefaultPath(t *testing.T) {
+ for path, want := range defaultPathTests {
+ if got := defaultPath(path); got != want {
+ t.Errorf("%q: got %q, want %q", path, got, want)
+ }
+ }
+}