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