forked from hoisie/web
/
cookie.go
217 lines (205 loc) · 6.29 KB
/
cookie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2009 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 web
import (
"bytes"
"fmt"
"http"
"io"
"os"
"sort"
"strings"
"time"
"url"
)
func sanitizeName(n string) string {
n = strings.Replace(n, "\n", "-", -1)
n = strings.Replace(n, "\r", "-", -1)
return n
}
func sanitizeValue(v string) string {
v = strings.Replace(v, "\n", " ", -1)
v = strings.Replace(v, "\r", " ", -1)
v = strings.Replace(v, ";", " ", -1)
return v
}
func isCookieByte(c byte) bool {
switch true {
case c == 0x21, 0x23 <= c && c <= 0x2b, 0x2d <= c && c <= 0x3a,
0x3c <= c && c <= 0x5b, 0x5d <= c && c <= 0x7e:
return true
}
return false
}
func isSeparator(c byte) bool {
switch c {
case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t':
return true
}
return false
}
func isChar(c byte) bool { return 0 <= c && c <= 127 }
func isCtl(c byte) bool { return (0 <= c && c <= 31) || c == 127 }
func isToken(c byte) bool { return isChar(c) && !isCtl(c) && !isSeparator(c) }
func parseCookieValue(raw string) (string, bool) {
raw = unquoteCookieValue(raw)
for i := 0; i < len(raw); i++ {
if !isCookieByte(raw[i]) {
return "", false
}
}
return raw, true
}
func unquoteCookieValue(v string) string {
if len(v) > 1 && v[0] == '"' && v[len(v)-1] == '"' {
return v[1 : len(v)-1]
}
return v
}
func isCookieNameValid(raw string) bool {
for _, c := range raw {
if !isToken(byte(c)) {
return false
}
}
return true
}
// 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
// line-length, so it seems safer to place cookies on separate lines.
func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error {
if kk == nil {
return nil
}
lines := make([]string, 0, len(kk))
var b bytes.Buffer
for _, c := range kk {
b.Reset()
// TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted
fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(c.Name), c.Value)
if len(c.Path) > 0 {
fmt.Fprintf(&b, "; Path=%s", url.QueryEscape(c.Path))
}
if len(c.Domain) > 0 {
fmt.Fprintf(&b, "; Domain=%s", url.QueryEscape(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)
}
if c.HttpOnly {
fmt.Fprintf(&b, "; HttpOnly")
}
if c.Secure {
fmt.Fprintf(&b, "; Secure")
}
lines = append(lines, "Set-Cookie: "+b.String()+"\r\n")
}
sort.Strings(lines)
for _, l := range lines {
if _, err := io.WriteString(w, l); err != nil {
return err
}
}
return nil
}
// writeCookies writes the wire representation of the cookies
// to w. Each cookie is written on a separate "Cookie: " line.
// This choice is made because HTTP parsers tend to have a limit on
// line-length, so it seems safer to place cookies on separate lines.
func writeCookies(w io.Writer, kk []*http.Cookie) os.Error {
lines := make([]string, 0, len(kk))
var b bytes.Buffer
for _, c := range kk {
b.Reset()
n := c.Name
// TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted
fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(n), c.Value)
if len(c.Path) > 0 {
fmt.Fprintf(&b, "; $Path=%s", url.QueryEscape(c.Path))
}
if len(c.Domain) > 0 {
fmt.Fprintf(&b, "; $Domain=%s", url.QueryEscape(c.Domain))
}
if c.HttpOnly {
fmt.Fprintf(&b, "; $HttpOnly")
}
lines = append(lines, "Cookie: "+b.String()+"\r\n")
}
sort.Strings(lines)
for _, l := range lines {
if _, err := io.WriteString(w, l); err != nil {
return err
}
}
return nil
}
// readCookies parses all "Cookie" values from
// the header h, removes the successfully parsed values from the
// "Cookie" key in h and returns the parsed Cookies.
func readCookies(h http.Header) []*http.Cookie {
cookies := []*http.Cookie{}
lines, ok := h["Cookie"]
if !ok {
return cookies
}
unparsedLines := []string{}
for _, line := range lines {
parts := strings.Split(strings.TrimSpace(line), ";")
if len(parts) == 1 && parts[0] == "" {
continue
}
// Per-line attributes
var lineCookies = make(map[string]string)
var path string
var domain string
var httponly bool
for i := 0; i < len(parts); i++ {
parts[i] = strings.TrimSpace(parts[i])
if len(parts[i]) == 0 {
continue
}
attr, val := parts[i], ""
var err os.Error
if j := strings.Index(attr, "="); j >= 0 {
attr, val = attr[:j], attr[j+1:]
val, err = url.QueryUnescape(val)
if err != nil {
continue
}
}
switch strings.ToLower(attr) {
case "$httponly":
httponly = true
case "$domain":
domain = val
// TODO: Add domain parsing
case "$path":
path = val
// TODO: Add path parsing
default:
lineCookies[attr] = val
}
}
if len(lineCookies) == 0 {
unparsedLines = append(unparsedLines, line)
}
for n, v := range lineCookies {
cookies = append(cookies, &http.Cookie{
Name: n,
Value: v,
Path: path,
Domain: domain,
HttpOnly: httponly,
MaxAge: -1,
Raw: line,
})
}
}
h["Cookie"] = unparsedLines, len(unparsedLines) > 0
return cookies
}