Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
V4NSH4J
GitHub Repository: V4NSH4J/discord-mass-DM-GO
Path: blob/main/client/cookie.go
310 views
1
package client
2
3
import (
4
"net/http"
5
"strconv"
6
"strings"
7
"time"
8
)
9
10
// Time wraps time.Time overriddin the json marshal/unmarshal to pass
11
// timestamp as integer
12
type Time struct {
13
time.Time
14
}
15
16
type data struct {
17
Time Time `json:"time"`
18
}
19
20
// A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an
21
// HTTP response or the Cookie header of an HTTP request.
22
//
23
// See https://tools.ietf.org/html/rfc6265 for details.
24
//Stolen from Net/http/cookies
25
type Cookie struct {
26
Name string `json:"name"`
27
Value string `json:"value"`
28
29
Path string `json:"path"` // optional
30
Domain string `json:"domain"` // optional
31
Expires time.Time
32
JSONExpires Time `json:"expires"` // optional
33
RawExpires string `json:"rawExpires"` // for reading cookies only
34
35
// MaxAge=0 means no 'Max-Age' attribute specified.
36
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
37
// MaxAge>0 means Max-Age attribute present and given in seconds
38
MaxAge int `json:"maxAge"`
39
Secure bool `json:"secure"`
40
HTTPOnly bool `json:"httpOnly"`
41
SameSite http.SameSite `json:"sameSite"`
42
Raw string
43
Unparsed []string `json:"unparsed"` // Raw text of unparsed attribute-value pairs
44
}
45
46
// UnmarshalJSON implements json.Unmarshaler inferface.
47
func (t *Time) UnmarshalJSON(buf []byte) error {
48
// Try to parse the timestamp integer
49
ts, err := strconv.ParseInt(string(buf), 10, 64)
50
if err == nil {
51
if len(buf) == 19 {
52
t.Time = time.Unix(ts/1e9, ts%1e9)
53
} else {
54
t.Time = time.Unix(ts, 0)
55
}
56
return nil
57
}
58
str := strings.Trim(string(buf), `"`)
59
if str == "null" || str == "" {
60
return nil
61
}
62
// Try to manually parse the data
63
tt, err := ParseDateString(str)
64
if err != nil {
65
return err
66
}
67
t.Time = tt
68
return nil
69
}
70
71
// ParseDateString takes a string and passes it through Approxidate
72
// Parses into a time.Time
73
func ParseDateString(dt string) (time.Time, error) {
74
const layout = "Mon, 02-Jan-2006 15:04:05 MST"
75
76
return time.Parse(layout, dt)
77
}
78
79