getCookiesByHost returns cookies stored for a specific host, removing any that have expired.
(host string)
| 98 | |
| 99 | // getCookiesByHost returns cookies stored for a specific host, removing any that have expired. |
| 100 | func (cj *CookieJar) getCookiesByHost(host string) []*fasthttp.Cookie { |
| 101 | cj.mu.Lock() |
| 102 | defer cj.mu.Unlock() |
| 103 | |
| 104 | now := time.Now() |
| 105 | stored := cj.hostCookies[host] |
| 106 | |
| 107 | kept := stored[:0] |
| 108 | for _, sc := range stored { |
| 109 | c := sc.cookie |
| 110 | // Remove expired cookies. |
| 111 | if !c.Expire().Equal(fasthttp.CookieExpireUnlimited) && c.Expire().Before(now) { |
| 112 | fasthttp.ReleaseCookie(c) |
| 113 | continue |
| 114 | } |
| 115 | kept = append(kept, sc) |
| 116 | } |
| 117 | if len(kept) == 0 { |
| 118 | delete(cj.hostCookies, host) |
| 119 | } else { |
| 120 | cj.hostCookies[host] = kept |
| 121 | } |
| 122 | |
| 123 | out := make([]*fasthttp.Cookie, 0, len(kept)) |
| 124 | for _, sc := range kept { |
| 125 | out = append(out, sc.cookie) |
| 126 | } |
| 127 | return out |
| 128 | } |
| 129 | |
| 130 | // cookiesForRequest returns cookies that match the given host, path and security settings. |
| 131 | func (cj *CookieJar) cookiesForRequest(host string, path []byte, secure bool) []*fasthttp.Cookie { //nolint:revive // secure is a deliberate scheme filter, not a control-flow flag |