-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcookies.go
More file actions
68 lines (63 loc) Β· 1.79 KB
/
Copy pathcookies.go
File metadata and controls
68 lines (63 loc) Β· 1.79 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
60
61
62
63
64
65
66
67
68
package metaai
import (
"fmt"
"net/http"
"os"
"sort"
"strings"
)
// Cookie environment-variable name to request-cookie key.
var cookieEnvKeys = []struct{ env, cookie string }{
{"META_AI_DATR", "datr"},
{"META_AI_ABRA_SESS", "abra_sess"},
{"META_AI_ECTO_1_SESS", "ecto_1_sess"},
{"META_AI_DPR", "dpr"},
{"META_AI_WD", "wd"},
{"META_AI_JS_DATR", "_js_datr"},
{"META_AI_ABRA_CSRF", "abra_csrf"},
{"META_AI_RD_CHALLENGE", "rd_challenge"},
{"META_AI_PS_L", "ps_l"},
{"META_AI_PS_N", "ps_n"},
}
// loadCookiesFromEnv builds the cookie map from META_AI_* environment variables.
// Returns nil when META_AI_DATR (the only strictly-required cookie) is absent.
// Unknown or empty environment values are ignored.
func loadCookiesFromEnv() map[string]string {
if os.Getenv("META_AI_DATR") == "" {
return nil
}
cookies := map[string]string{}
for _, m := range cookieEnvKeys {
if v := strings.TrimSpace(os.Getenv(m.env)); v != "" {
cookies[m.cookie] = v
}
}
return cookies
}
// cookieHeader formats a cookie map as an HTTP Cookie header value
// ("k1=v1; k2=v2"), with keys in stable (sorted) order so tests are deterministic.
// Keys are sorted to keep the header deterministic.
func cookieHeader(cookies map[string]string) string {
if len(cookies) == 0 {
return ""
}
keys := make([]string, 0, len(cookies))
for k := range cookies {
keys = append(keys, k)
}
sort.Strings(keys)
parts := make([]string, 0, len(keys))
for _, k := range keys {
parts = append(parts, fmt.Sprintf("%s=%s", k, cookies[k]))
}
return strings.Join(parts, "; ")
}
// attachCookies sets the cookie map on an http.Request as a Cookie header.
func attachCookies(req *http.Request, cookies map[string]string) {
if len(cookies) == 0 {
return
}
if h := cookieHeader(cookies); h != "" {
req.Header.Set("Cookie", h)
}
}