-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
157 lines (138 loc) · 3.12 KB
/
read.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
// Copyright ©2022 The mozcookie 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 mozcookie
import (
"bufio"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
)
// Read returns the cookies stored in the name file.
func Read(name string) ([]*http.Cookie, error) {
f, err := os.Open(name)
if err != nil {
return nil, fmt.Errorf("mozcookie: could not open cookie file %q: %w", name, err)
}
defer f.Close()
return Decode(f)
}
func Decode(r io.Reader) ([]*http.Cookie, error) {
cs, err := decode(r)
if err != nil {
return nil, err
}
return cs, nil
}
// // Open returns the cookies stored in the name file, as a cookiejar.
// func Open(name string) (*cookiejar.Jar, error) {
// f, err := os.Open(name)
// if err != nil {
// return nil, fmt.Errorf("mozcookie: could not open cookie file %q: %w", name, err)
// }
// defer f.Close()
//
// cs, err := decode(f)
// if err != nil {
// return nil, err
// }
//
// cookies := make(map[string][]*http.Cookie, len(cs))
// for _, c := range cs {
// k := c.Domain
// switch {
// case c.Secure:
// k = "https://" + k
// default:
// k = "http://" + k
// }
// cookies[k] = append(cookies[k], c)
// }
//
// jar, err := cookiejar.New(nil)
// if err != nil {
// return nil, err
// }
// for k, v := range cookies {
// u, err := url.Parse(k)
// if err != nil {
// return nil, fmt.Errorf("mozcookie: could not parse url %q: %w", k, err)
// }
// jar.SetCookies(u, v)
// }
// return jar, nil
// }
func decode(r io.Reader) ([]*http.Cookie, error) {
scan := bufio.NewScanner(r)
// read header.
if !scan.Scan() || scan.Err() != nil {
if scan.Err() != nil {
return nil, fmt.Errorf("mozcookie: could not read header: %w", scan.Err())
}
// empty file, or empty header.
return nil, nil
}
hdr := strings.TrimSpace(scan.Text())
if !magicRe.MatchString(hdr) {
return nil, fmt.Errorf("mozcookie: invalid cookie magic")
}
var cookies []*http.Cookie
for scan.Scan() {
txt := strings.TrimSpace(scan.Text())
if txt == "" {
continue
}
c := &http.Cookie{
SameSite: http.SameSiteDefaultMode,
}
if strings.HasPrefix(txt, httpOnlyPrefix) {
c.HttpOnly = true
txt = txt[len(httpOnlyPrefix):]
}
if txt == "" || strings.HasPrefix(txt, "#") {
continue
}
toks := strings.Split(txt, "\t")
if len(toks) > 0 {
c.Domain = strings.ToLower(toks[0])
}
// domain-specified: toks[1] == "TRUE"
if len(toks) > 2 {
c.Path = toks[2]
}
if len(toks) > 3 {
c.Secure = toks[3] == "TRUE"
}
if len(toks) > 4 {
c.RawExpires = toks[4]
}
if len(toks) > 5 {
c.Name = toks[5]
}
if len(toks) > 6 {
c.Value = toks[6]
}
if c.RawExpires != "" {
v, err := strconv.ParseInt(c.RawExpires, 10, 64)
if err != nil {
return nil, err
}
c.Expires = time.Unix(v, 0).UTC()
}
cookies = append(cookies, c)
}
if err := scan.Err(); err != nil {
switch {
case errors.Is(err, io.EOF):
// ok
default:
return nil, fmt.Errorf("mozcookie: could not decode cookie stream: %w", err)
}
}
return cookies, nil
}