-
Notifications
You must be signed in to change notification settings - Fork 0
/
elinks.go
103 lines (86 loc) · 2.23 KB
/
elinks.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
package elinks
import (
"bufio"
"errors"
"net/http"
"strconv"
"strings"
"time"
"github.com/dvgamerr-app/go-kooky"
"github.com/dvgamerr-app/go-kooky/internal/cookies"
)
type elinksCookieStore struct {
cookies.DefaultCookieStore
}
var _ cookies.CookieStore = (*elinksCookieStore)(nil)
func ReadCookies(filename string, filters ...kooky.Filter) ([]*kooky.Cookie, error) {
s, err := cookieStore(filename, filters...)
if err != nil {
return nil, err
}
defer s.Close()
return s.ReadCookies(filters...)
}
func (s *elinksCookieStore) ReadCookies(filters ...kooky.Filter) ([]*kooky.Cookie, error) {
if s == nil {
return nil, errors.New(`cookie store is nil`)
}
if err := s.Open(); err != nil {
return nil, err
} else if s.File == nil {
return nil, errors.New(`file is nil`)
}
var ret []*kooky.Cookie
scanner := bufio.NewScanner(s.File)
for scanner.Scan() {
line := scanner.Text()
sp := strings.Split(line, "\t")
if len(sp) != 8 {
continue
}
exp, err := strconv.ParseInt(sp[5], 10, 64)
if err != nil {
continue
}
sec, err := strconv.Atoi(sp[6])
if err != nil {
continue
}
cookie := &kooky.Cookie{}
cookie.Name = sp[0]
cookie.Value = sp[1]
cookie.Path = sp[3]
cookie.Domain = sp[4]
cookie.Expires = time.Unix(exp, 0)
cookie.Secure = sec == 1
if !kooky.FilterCookie(cookie, filters...) {
continue
}
ret = append(ret, cookie)
}
return ret, nil
}
// CookieJar returns an initiated http.CookieJar based on the cookies stored by
// the ELinks browser. Set cookies are memory stored and do not modify any
// browser files.
func CookieJar(filename string, filters ...kooky.Filter) (http.CookieJar, error) {
j, err := cookieStore(filename, filters...)
if err != nil {
return nil, err
}
defer j.Close()
if err := j.InitJar(); err != nil {
return nil, err
}
return j, nil
}
// CookieStore has to be closed with CookieStore.Close() after use.
func CookieStore(filename string, filters ...kooky.Filter) (kooky.CookieStore, error) {
return cookieStore(filename, filters...)
}
func cookieStore(filename string, filters ...kooky.Filter) (*cookies.CookieJar, error) {
s := &elinksCookieStore{}
s.FileNameStr = filename
s.BrowserStr = `elinks`
return &cookies.CookieJar{CookieStore: s}, nil
}