-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcookies.go
More file actions
59 lines (54 loc) · 1.66 KB
/
Copy pathcookies.go
File metadata and controls
59 lines (54 loc) · 1.66 KB
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
package rbxmk
import (
"bufio"
"fmt"
"io"
"net/http"
"net/textproto"
"strings"
"github.com/anaminus/rbxmk/rtypes"
)
// CookiesFrom retrieves cookies from a known location. location is
// case-insensitive. The following locations are implemented:
//
// - studio: Returns the cookies used for authentication when logging into
// Roblox Studio.
func CookiesFrom(location string) (cookies rtypes.Cookies, err error) {
switch strings.ToLower(location) {
case "studio":
cookies = cookiesFromStudio()
return cookies, nil
default:
return nil, fmt.Errorf("unknown location %q", location)
}
}
// DecodeCookies parses cookies from r and returns a list of cookies. Cookies
// are parsed as a number of "Set-Cookie" HTTP headers. Returns an empty list if
// the reader is empty.
func DecodeCookies(r io.Reader) (cookies rtypes.Cookies, err error) {
// There's no direct way to parse cookies, so we have to cheat a little.
h, err := textproto.NewReader(bufio.NewReader(r)).ReadMIMEHeader()
if err != nil && err != io.EOF {
return nil, fmt.Errorf("decode cookies: %w", err)
}
resp := http.Response{Header: http.Header(h)}
cs := resp.Cookies()
cookies = make(rtypes.Cookies, len(cs))
for i, c := range cs {
cookies[i] = rtypes.Cookie{Cookie: c}
}
return cookies, nil
}
// EncodeCookies formats a list of cookies as a number of "Set-Cookie" HTTP
// headers and writes them to w.
func EncodeCookies(w io.Writer, cookies rtypes.Cookies) (err error) {
// More cheating.
h := http.Header{}
for _, cookie := range cookies {
h.Add("Set-Cookie", cookie.Cookie.String())
}
if err = h.Write(w); err != nil {
return fmt.Errorf("encode cookies: %w", err)
}
return nil
}