|
| 1 | +// Copyright 2009 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 | + |
| 5 | +package web |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "fmt" |
| 10 | + "http" |
| 11 | + "io" |
| 12 | + "os" |
| 13 | + "sort" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +// writeSetCookies writes the wire representation of the set-cookies |
| 20 | +// to w. Each cookie is written on a separate "Set-Cookie: " line. |
| 21 | +// This choice is made because HTTP parsers tend to have a limit on |
| 22 | +// line-length, so it seems safer to place cookies on separate lines. |
| 23 | +func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error { |
| 24 | + if kk == nil { |
| 25 | + return nil |
| 26 | + } |
| 27 | + lines := make([]string, 0, len(kk)) |
| 28 | + var b bytes.Buffer |
| 29 | + for _, c := range kk { |
| 30 | + b.Reset() |
| 31 | + // TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted |
| 32 | + fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(c.Name), c.Value) |
| 33 | + if c.Version > 0 { |
| 34 | + fmt.Fprintf(&b, "Version=%d; ", c.Version) |
| 35 | + } |
| 36 | + if len(c.Path) > 0 { |
| 37 | + fmt.Fprintf(&b, "; Path=%s", http.URLEscape(c.Path)) |
| 38 | + } |
| 39 | + if len(c.Domain) > 0 { |
| 40 | + fmt.Fprintf(&b, "; Domain=%s", http.URLEscape(c.Domain)) |
| 41 | + } |
| 42 | + if len(c.Expires.Zone) > 0 { |
| 43 | + fmt.Fprintf(&b, "; Expires=%s", c.Expires.Format(time.RFC1123)) |
| 44 | + } |
| 45 | + if c.MaxAge >= 0 { |
| 46 | + fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge) |
| 47 | + } |
| 48 | + if c.HttpOnly { |
| 49 | + fmt.Fprintf(&b, "; HttpOnly") |
| 50 | + } |
| 51 | + if c.Secure { |
| 52 | + fmt.Fprintf(&b, "; Secure") |
| 53 | + } |
| 54 | + if len(c.Comment) > 0 { |
| 55 | + fmt.Fprintf(&b, "; Comment=%s", http.URLEscape(c.Comment)) |
| 56 | + } |
| 57 | + lines = append(lines, "Set-Cookie: "+b.String()+"\r\n") |
| 58 | + } |
| 59 | + sort.SortStrings(lines) |
| 60 | + for _, l := range lines { |
| 61 | + if _, err := io.WriteString(w, l); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + } |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// writeCookies writes the wire representation of the cookies |
| 69 | +// to w. Each cookie is written on a separate "Cookie: " line. |
| 70 | +// This choice is made because HTTP parsers tend to have a limit on |
| 71 | +// line-length, so it seems safer to place cookies on separate lines. |
| 72 | +func writeCookies(w io.Writer, kk []*http.Cookie) os.Error { |
| 73 | + lines := make([]string, 0, len(kk)) |
| 74 | + var b bytes.Buffer |
| 75 | + for _, c := range kk { |
| 76 | + b.Reset() |
| 77 | + n := c.Name |
| 78 | + if c.Version > 0 { |
| 79 | + fmt.Fprintf(&b, "$Version=%d; ", c.Version) |
| 80 | + } |
| 81 | + // TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted |
| 82 | + fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(n), c.Value) |
| 83 | + if len(c.Path) > 0 { |
| 84 | + fmt.Fprintf(&b, "; $Path=%s", http.URLEscape(c.Path)) |
| 85 | + } |
| 86 | + if len(c.Domain) > 0 { |
| 87 | + fmt.Fprintf(&b, "; $Domain=%s", http.URLEscape(c.Domain)) |
| 88 | + } |
| 89 | + if c.HttpOnly { |
| 90 | + fmt.Fprintf(&b, "; $HttpOnly") |
| 91 | + } |
| 92 | + if len(c.Comment) > 0 { |
| 93 | + fmt.Fprintf(&b, "; $Comment=%s", http.URLEscape(c.Comment)) |
| 94 | + } |
| 95 | + lines = append(lines, "Cookie: "+b.String()+"\r\n") |
| 96 | + } |
| 97 | + sort.SortStrings(lines) |
| 98 | + for _, l := range lines { |
| 99 | + if _, err := io.WriteString(w, l); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// readCookies parses all "Cookie" values from |
| 107 | +// the header h, removes the successfully parsed values from the |
| 108 | +// "Cookie" key in h and returns the parsed Cookies. |
| 109 | +func readCookies(h http.Header) []*http.Cookie { |
| 110 | + cookies := []*http.Cookie{} |
| 111 | + lines, ok := h["Cookie"] |
| 112 | + if !ok { |
| 113 | + return cookies |
| 114 | + } |
| 115 | + unparsedLines := []string{} |
| 116 | + for _, line := range lines { |
| 117 | + parts := strings.Split(strings.TrimSpace(line), ";", -1) |
| 118 | + if len(parts) == 1 && parts[0] == "" { |
| 119 | + continue |
| 120 | + } |
| 121 | + // Per-line attributes |
| 122 | + var lineCookies = make(map[string]string) |
| 123 | + var version int |
| 124 | + var path string |
| 125 | + var domain string |
| 126 | + var comment string |
| 127 | + var httponly bool |
| 128 | + for i := 0; i < len(parts); i++ { |
| 129 | + parts[i] = strings.TrimSpace(parts[i]) |
| 130 | + if len(parts[i]) == 0 { |
| 131 | + continue |
| 132 | + } |
| 133 | + attr, val := parts[i], "" |
| 134 | + var err os.Error |
| 135 | + if j := strings.Index(attr, "="); j >= 0 { |
| 136 | + attr, val = attr[:j], attr[j+1:] |
| 137 | + val, err = http.URLUnescape(val) |
| 138 | + if err != nil { |
| 139 | + continue |
| 140 | + } |
| 141 | + } |
| 142 | + switch strings.ToLower(attr) { |
| 143 | + case "$httponly": |
| 144 | + httponly = true |
| 145 | + case "$version": |
| 146 | + version, err = strconv.Atoi(val) |
| 147 | + if err != nil { |
| 148 | + version = 0 |
| 149 | + continue |
| 150 | + } |
| 151 | + case "$domain": |
| 152 | + domain = val |
| 153 | + // TODO: Add domain parsing |
| 154 | + case "$path": |
| 155 | + path = val |
| 156 | + // TODO: Add path parsing |
| 157 | + case "$comment": |
| 158 | + comment = val |
| 159 | + default: |
| 160 | + lineCookies[attr] = val |
| 161 | + } |
| 162 | + } |
| 163 | + if len(lineCookies) == 0 { |
| 164 | + unparsedLines = append(unparsedLines, line) |
| 165 | + } |
| 166 | + for n, v := range lineCookies { |
| 167 | + cookies = append(cookies, &http.Cookie{ |
| 168 | + Name: n, |
| 169 | + Value: v, |
| 170 | + Path: path, |
| 171 | + Domain: domain, |
| 172 | + Comment: comment, |
| 173 | + Version: version, |
| 174 | + HttpOnly: httponly, |
| 175 | + MaxAge: -1, |
| 176 | + Raw: line, |
| 177 | + }) |
| 178 | + } |
| 179 | + } |
| 180 | + h["Cookie"] = unparsedLines, len(unparsedLines) > 0 |
| 181 | + return cookies |
| 182 | +} |
0 commit comments