forked from asmcos/requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests_cookie.go
42 lines (37 loc) · 1.06 KB
/
requests_cookie.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
package requests
import (
"net/http"
"net/http/cookiejar"
)
// cookies only save to Client.Jar
// session.initCookies is temporary
func (session *Session) SetCookie(cookie *http.Cookie) *Session {
session.initCookies = append(session.initCookies, cookie)
return session
}
func (session *Session) ClearCookies() {
session.initCookies = session.initCookies[0:0]
}
func (session *Session) clientLoadCookies() {
if len(session.initCookies) > 0 {
// session.httpreq.AddCookie(cookie) // Do not use httpreq here, this is a temporary cookie, client will drop it in next request
// 1. set session cookies
session.Client.Jar.SetCookies(session.httpreq.URL, session.initCookies)
// 2. clear init cookies
session.ClearCookies()
}
}
/**
1. The new Jar is a reference to old jar
2. We could use jar in multiple routines safely.
**/
func (session *Session) clientCloneCookies(client *http.Client) {
if client != nil && client.Jar != nil {
switch jar := client.Jar.(type) {
case *cookiejar.Jar:
session.Client.Jar = jar
default:
session.Client.Jar = client.Jar
}
}
}