mirror of
https://gitgud.io/yats/libkiwi.git
synced 2026-06-20 10:05:24 -04:00
41 lines
718 B
Go
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)
|
|
}
|