Fix overwrite of existing cookiejar.

This commit is contained in:
y a t s
2026-06-18 10:02:16 -04:00
parent 1e9bd059f0
commit 72840af90e
5 changed files with 64 additions and 31 deletions
+27 -14
View File
@@ -14,25 +14,18 @@ import (
"golang.org/x/net/html/atom"
)
// Get XFToken (data-csrf) from page html.
func XFToken(page io.Reader) (string, error) {
z := html.NewTokenizer(page)
for i := z.Next(); i != html.ErrorToken; i = z.Next() {
tk := z.Token()
if tk.DataAtom == atom.Html {
for _, a := range tk.Attr {
switch a.Key {
case "data-csrf":
return a.Val, nil
}
}
func getCookie(jar http.CookieJar, u *url.URL, name string) *http.Cookie {
cookies := jar.Cookies(u)
for _, c := range cookies {
if c.Name == name {
return c
}
}
return "", ErrNoXFToken
return nil
}
func setCookie(u *url.URL, jar http.CookieJar, newCookie *http.Cookie) {
func setCookie(jar http.CookieJar, u *url.URL, newCookie *http.Cookie) {
cookies := jar.Cookies(u)
for i, c := range cookies {
@@ -48,6 +41,26 @@ func setCookie(u *url.URL, jar http.CookieJar, newCookie *http.Cookie) {
jar.SetCookies(u, cookies)
}
// Get XFToken (data-csrf) from page html.
func XFToken(page io.Reader) (string, error) {
z := html.NewTokenizer(page)
for i := z.Next(); i != html.ErrorToken; i = z.Next() {
tk := z.Token()
if tk.DataAtom != atom.Html {
continue
}
for _, a := range tk.Attr {
switch a.Key {
case "data-csrf":
return a.Val, nil
}
}
}
return "", ErrNoXFToken
}
func (kf *KF) Do(req *http.Request) (*http.Response, error) {
var (
ctx = req.Context()