]> Cypherpunks repositories - gostls13.git/commitdiff
http: add http.SetCookie(ResponseWriter, *Cookie)
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 17 May 2011 22:07:44 +0000 (15:07 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 17 May 2011 22:07:44 +0000 (15:07 -0700)
R=golang-dev, gary.burd, rsc
CC=golang-dev
https://golang.org/cl/4526062

src/pkg/http/cookie.go
src/pkg/http/cookie_test.go

index 8e8ff89aca9f7fea27ed6a64cff768135e618900..5add1ccc260baba1702be43867fafd996f65d904 100644 (file)
@@ -130,6 +130,37 @@ func readSetCookies(h Header) []*Cookie {
        return cookies
 }
 
+// SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers.
+func SetCookie(w ResponseWriter, cookie *Cookie) {
+       var b bytes.Buffer
+       writeSetCookieToBuffer(&b, cookie)
+       w.Header().Add("Set-Cookie", b.String())
+}
+
+func writeSetCookieToBuffer(buf *bytes.Buffer, c *Cookie) {
+       fmt.Fprintf(buf, "%s=%s", sanitizeName(c.Name), sanitizeValue(c.Value))
+       if len(c.Path) > 0 {
+               fmt.Fprintf(buf, "; Path=%s", sanitizeValue(c.Path))
+       }
+       if len(c.Domain) > 0 {
+               fmt.Fprintf(buf, "; Domain=%s", sanitizeValue(c.Domain))
+       }
+       if len(c.Expires.Zone) > 0 {
+               fmt.Fprintf(buf, "; Expires=%s", c.Expires.Format(time.RFC1123))
+       }
+       if c.MaxAge > 0 {
+               fmt.Fprintf(buf, "; Max-Age=%d", c.MaxAge)
+       } else if c.MaxAge < 0 {
+               fmt.Fprintf(buf, "; Max-Age=0")
+       }
+       if c.HttpOnly {
+               fmt.Fprintf(buf, "; HttpOnly")
+       }
+       if c.Secure {
+               fmt.Fprintf(buf, "; Secure")
+       }
+}
+
 // writeSetCookies writes the wire representation of the set-cookies
 // to w. Each cookie is written on a separate "Set-Cookie: " line.
 // This choice is made because HTTP parsers tend to have a limit on
@@ -142,27 +173,7 @@ func writeSetCookies(w io.Writer, kk []*Cookie) os.Error {
        var b bytes.Buffer
        for _, c := range kk {
                b.Reset()
-               fmt.Fprintf(&b, "%s=%s", sanitizeName(c.Name), sanitizeValue(c.Value))
-               if len(c.Path) > 0 {
-                       fmt.Fprintf(&b, "; Path=%s", sanitizeValue(c.Path))
-               }
-               if len(c.Domain) > 0 {
-                       fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(c.Domain))
-               }
-               if len(c.Expires.Zone) > 0 {
-                       fmt.Fprintf(&b, "; Expires=%s", c.Expires.Format(time.RFC1123))
-               }
-               if c.MaxAge > 0 {
-                       fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge)
-               } else if c.MaxAge < 0 {
-                       fmt.Fprintf(&b, "; Max-Age=0")
-               }
-               if c.HttpOnly {
-                       fmt.Fprintf(&b, "; HttpOnly")
-               }
-               if c.Secure {
-                       fmt.Fprintf(&b, "; Secure")
-               }
+               writeSetCookieToBuffer(&b, c)
                lines = append(lines, "Set-Cookie: "+b.String()+"\r\n")
        }
        sort.SortStrings(lines)
index e8b3df2ccf9ee297acb654531f28a6bfbc7f239b..13c9fff4ae986e9c7af0cccc474e987986e92a25 100644 (file)
@@ -8,6 +8,7 @@ import (
        "bytes"
        "fmt"
        "json"
+       "os"
        "reflect"
        "testing"
 )
@@ -43,6 +44,35 @@ func TestWriteSetCookies(t *testing.T) {
        }
 }
 
+type headerOnlyResponseWriter Header
+
+func (ho headerOnlyResponseWriter) Header() Header {
+       return Header(ho)
+}
+
+func (ho headerOnlyResponseWriter) Write([]byte) (int, os.Error) {
+       panic("NOIMPL")
+}
+
+func (ho headerOnlyResponseWriter) WriteHeader(int) {
+       panic("NOIMPL")
+}
+
+func TestSetCookie(t *testing.T) {
+       m := make(Header)
+       SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-1", Value: "one", Path: "/restricted/"})
+       SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600})
+       if l := len(m["Set-Cookie"]); l != 2 {
+               t.Fatalf("expected %d cookies, got %d", 2, l)
+       }
+       if g, e := m["Set-Cookie"][0], "cookie-1=one; Path=/restricted/"; g != e {
+               t.Errorf("cookie #1: want %q, got %q", e, g)
+       }
+       if g, e := m["Set-Cookie"][1], "cookie-2=two; Max-Age=3600"; g != e {
+               t.Errorf("cookie #2: want %q, got %q", e, g)
+       }
+}
+
 var writeCookiesTests = []struct {
        Cookies []*Cookie
        Raw     string