forked from browserutils/kooky
/
cookiestore.go
92 lines (81 loc) · 1.67 KB
/
cookiestore.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
package cookies
import (
"errors"
"io"
"os"
"github.com/etng/kooky"
)
// kooky.CookieStore without http.CookieJar and SubJar()
type CookieStore interface {
ReadCookies(...kooky.Filter) ([]*kooky.Cookie, error)
Browser() string
Profile() string
IsDefaultProfile() bool
FilePath() string
Close() error
}
/*
DefaultCookieStore implements most of the kooky.CookieStore interface except for the ReadCookies method
func (s *DefaultCookieStore) ReadCookies(filters ...kooky.Filter) ([]*kooky.Cookie, error)
DefaultCookieStore also provides an Open() method
*/
type DefaultCookieStore struct {
FileNameStr string
File *os.File
BrowserStr string
ProfileStr string
OSStr string
IsDefaultProfileBool bool
}
func (s *DefaultCookieStore) FilePath() string {
if s == nil {
return ``
}
return s.FileNameStr
}
func (s *DefaultCookieStore) Browser() string {
if s == nil {
return ``
}
return s.BrowserStr
}
func (s *DefaultCookieStore) Profile() string {
if s == nil {
return ``
}
return s.ProfileStr
}
func (s *DefaultCookieStore) IsDefaultProfile() bool {
return s != nil && s.IsDefaultProfileBool
}
func (s *DefaultCookieStore) Open() error {
if s == nil {
return errors.New(`cookie store is nil`)
}
if s.File != nil {
s.File.Seek(0, io.SeekStart)
return nil
}
if len(s.FileNameStr) < 1 {
return nil
}
f, err := os.Open(s.FileNameStr)
if err != nil {
return err
}
s.File = f
return nil
}
func (s *DefaultCookieStore) Close() error {
if s == nil {
return errors.New(`cookie store is nil`)
}
if s.File == nil {
return nil
}
err := s.File.Close()
if err == nil {
s.File = nil
}
return err
}