diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-01-13 05:11:45 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-01-13 05:11:45 +0000 |
commit | 86240434eb153c149dbc3d77f4fedf9cffcbfc53 (patch) | |
tree | eb5eccc07097c5fcf940967f33ab84a7d47c96fe /libgo/go/net/http | |
parent | 9599f526f8b241e01ca4d54b5bff9c2e6f6dd75a (diff) | |
download | gcc-86240434eb153c149dbc3d77f4fedf9cffcbfc53.tar.gz |
libgo: Update to weekly.2011-12-22.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@183150 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/net/http')
-rw-r--r-- | libgo/go/net/http/client.go | 17 | ||||
-rw-r--r-- | libgo/go/net/http/jar.go | 30 | ||||
-rw-r--r-- | libgo/go/net/http/readrequest_test.go | 2 | ||||
-rw-r--r-- | libgo/go/net/http/request_test.go | 18 | ||||
-rw-r--r-- | libgo/go/net/http/serve_test.go | 6 |
5 files changed, 61 insertions, 12 deletions
diff --git a/libgo/go/net/http/client.go b/libgo/go/net/http/client.go index 211ac44c58a..a4f8f19aac8 100644 --- a/libgo/go/net/http/client.go +++ b/libgo/go/net/http/client.go @@ -38,6 +38,11 @@ type Client struct { // If CheckRedirect is nil, the Client uses its default policy, // which is to stop after 10 consecutive requests. CheckRedirect func(req *Request, via []*Request) error + + // Jar specifies the cookie jar. + // If Jar is nil, cookies are not sent in requests and ignored + // in responses. + Jar CookieJar } // DefaultClient is the default Client and is used by Get, Head, and Post. @@ -180,6 +185,11 @@ func (c *Client) doFollowingRedirects(ireq *Request) (r *Response, err error) { return nil, errors.New("http: nil Request.URL") } + jar := c.Jar + if jar == nil { + jar = blackHoleJar{} + } + req := ireq urlStr := "" // next relative or absolute URL to fetch (after first request) for redirect := 0; ; redirect++ { @@ -203,12 +213,19 @@ func (c *Client) doFollowingRedirects(ireq *Request) (r *Response, err error) { break } } + for _, cookie := range jar.Cookies(req.URL) { + req.AddCookie(cookie) + } } urlStr = req.URL.String() if r, err = send(req, c.Transport); err != nil { break } + if c := r.Cookies(); len(c) > 0 { + jar.SetCookies(req.URL, c) + } + if shouldRedirect(r.StatusCode) { r.Body.Close() if urlStr = r.Header.Get("Location"); urlStr == "" { diff --git a/libgo/go/net/http/jar.go b/libgo/go/net/http/jar.go new file mode 100644 index 00000000000..2c2caa251fe --- /dev/null +++ b/libgo/go/net/http/jar.go @@ -0,0 +1,30 @@ +// Copyright 2011 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 http + +import ( + "net/url" +) + +// A CookieJar manages storage and use of cookies in HTTP requests. +// +// Implementations of CookieJar must be safe for concurrent use by multiple +// goroutines. +type CookieJar interface { + // SetCookies handles the receipt of the cookies in a reply for the + // given URL. It may or may not choose to save the cookies, depending + // on the jar's policy and implementation. + SetCookies(u *url.URL, cookies []*Cookie) + + // Cookies returns the cookies to send in a request for the given URL. + // It is up to the implementation to honor the standard cookie use + // restrictions such as in RFC 6265. + Cookies(u *url.URL) []*Cookie +} + +type blackHoleJar struct{} + +func (blackHoleJar) SetCookies(u *url.URL, cookies []*Cookie) {} +func (blackHoleJar) Cookies(u *url.URL) []*Cookie { return nil } diff --git a/libgo/go/net/http/readrequest_test.go b/libgo/go/net/http/readrequest_test.go index c64fff6109f..ad7e3c02b0c 100644 --- a/libgo/go/net/http/readrequest_test.go +++ b/libgo/go/net/http/readrequest_test.go @@ -219,7 +219,7 @@ func TestReadRequest(t *testing.T) { t.Errorf("#%d: Body = %q want %q", i, body, tt.Body) } if !reflect.DeepEqual(tt.Trailer, req.Trailer) { - t.Errorf("%#d. Trailers differ.\n got: %v\nwant: %v", i, req.Trailer, tt.Trailer) + t.Errorf("#%d. Trailers differ.\n got: %v\nwant: %v", i, req.Trailer, tt.Trailer) } } } diff --git a/libgo/go/net/http/request_test.go b/libgo/go/net/http/request_test.go index 714cb64f47f..7b78645169e 100644 --- a/libgo/go/net/http/request_test.go +++ b/libgo/go/net/http/request_test.go @@ -202,8 +202,8 @@ func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) { if g, e := req.FormValue("texta"), textaValue; g != e { t.Errorf("texta value = %q, want %q", g, e) } - if g, e := req.FormValue("texta"), textaValue; g != e { - t.Errorf("texta value = %q, want %q", g, e) + if g, e := req.FormValue("textb"), textbValue; g != e { + t.Errorf("textb value = %q, want %q", g, e) } if g := req.FormValue("missing"); g != "" { t.Errorf("missing value = %q, want empty string", g) @@ -214,14 +214,16 @@ func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) { t.Error(n, " is *os.File, should not be") } } - fd := testMultipartFile(t, req, "filea", "filea.txt", fileaContents) - assertMem("filea", fd) - fd = testMultipartFile(t, req, "fileb", "fileb.txt", filebContents) + fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents) + defer fda.Close() + assertMem("filea", fda) + fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents) + defer fdb.Close() if allMem { - assertMem("fileb", fd) + assertMem("fileb", fdb) } else { - if _, ok := fd.(*os.File); !ok { - t.Errorf("fileb has unexpected underlying type %T", fd) + if _, ok := fdb.(*os.File); !ok { + t.Errorf("fileb has unexpected underlying type %T", fdb) } } diff --git a/libgo/go/net/http/serve_test.go b/libgo/go/net/http/serve_test.go index c68e6614b19..24e6b50dab8 100644 --- a/libgo/go/net/http/serve_test.go +++ b/libgo/go/net/http/serve_test.go @@ -1164,15 +1164,15 @@ func BenchmarkClientServer(b *testing.B) { for i := 0; i < b.N; i++ { res, err := Get(ts.URL) if err != nil { - panic("Get: " + err.Error()) + b.Fatal("Get:", err) } all, err := ioutil.ReadAll(res.Body) if err != nil { - panic("ReadAll: " + err.Error()) + b.Fatal("ReadAll:", err) } body := string(all) if body != "Hello world.\n" { - panic("Got body: " + body) + b.Fatal("Got body:", body) } } |