Files
libkiwi/cookies.go
T
2026-06-19 10:42:39 -04:00

41 lines
718 B
Go

package libkiwi
import (
"net/http"
"net/url"
)
// http.CookieJar interface wrapper to add convenience functions.
type cookieJar struct {
jar *http.CookieJar
}
func (j *cookieJar) GetCookie(u *url.URL, name string) *http.Cookie {
jar := (*j.jar)
cookies := jar.Cookies(u)
for _, c := range cookies {
if c.Name == name {
return c
}
}
return nil
}
func (j *cookieJar) SetCookie(u *url.URL, newCookie *http.Cookie) {
jar := (*j.jar)
cookies := jar.Cookies(u)
for i, c := range cookies {
if c.Name == newCookie.Name {
cookies[i] = newCookie
jar.SetCookies(u, cookies)
return
}
}
// Append if not already existing.
cookies = append(cookies, newCookie)
jar.SetCookies(u, cookies)
}