-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookies.go
39 lines (36 loc) · 830 Bytes
/
cookies.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
package icloud
import (
"fmt"
"net/http"
"net/http/cookiejar"
"os"
log "github.com/sirupsen/logrus"
netscapeCookieJar "github.com/vanym/golang-netscape-cookiejar"
)
func newCookieJar(path string) (http.CookieJar, error) {
baseJar, err := cookiejar.New(nil)
if err != nil {
return nil, fmt.Errorf("cannot create basic cookie jar: %w", err)
}
opt := netscapeCookieJar.Options{
SubJar: baseJar,
AutoWritePath: path,
WriteHeader: true,
}
jar, err := netscapeCookieJar.New(&opt)
if err != nil {
return nil, fmt.Errorf("cannot create on-disk cookie jar: %w", err)
}
file, err := os.Open(path)
if err == nil {
_, err = jar.ReadFrom(file)
_ = file.Close()
}
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
log.Debugf("Cookie file not found: %s", path)
}
return jar, nil
}