SetByHost stores the given cookies for the specified host. If a cookie key already exists, it will be replaced by the new cookie value. CookieJar stores copies of the provided cookies, so they may be safely released after use.
(host []byte, cookies ...*fasthttp.Cookie)
| 192 | // |
| 193 | // CookieJar stores copies of the provided cookies, so they may be safely released after use. |
| 194 | func (cj *CookieJar) SetByHost(host []byte, cookies ...*fasthttp.Cookie) { |
| 195 | hostStr := utils.UnsafeString(host) |
| 196 | if h, _, err := net.SplitHostPort(hostStr); err == nil { |
| 197 | hostStr = h |
| 198 | } |
| 199 | hostStr = utilsstrings.ToLower(hostStr) |
| 200 | hostKey := utils.CopyString(hostStr) |
| 201 | |
| 202 | cj.mu.Lock() |
| 203 | defer cj.mu.Unlock() |
| 204 | |
| 205 | if cj.hostCookies == nil { |
| 206 | cj.hostCookies = make(map[string][]storedCookie) |
| 207 | } |
| 208 | |
| 209 | for _, cookie := range cookies { |
| 210 | domain := utils.TrimLeft(cookie.Domain(), '.') |
| 211 | utilsbytes.UnsafeToLower(domain) |
| 212 | key := hostKey |
| 213 | storedDomain := hostStr |
| 214 | isHostOnly := len(domain) == 0 |
| 215 | if !isHostOnly { |
| 216 | acceptance := acceptCookieDomain(hostStr, utils.UnsafeString(domain)) |
| 217 | if !acceptance.isOk { |
| 218 | continue |
| 219 | } |
| 220 | isHostOnly = acceptance.isHostOnly |
| 221 | if !isHostOnly { |
| 222 | key = utils.CopyString(acceptance.domain) |
| 223 | storedDomain = acceptance.domain |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | cj.ensureHostCapacityLocked(key, time.Now()) |
| 228 | hostCookies := cj.hostCookies[key] |
| 229 | |
| 230 | existing := searchCookieByKeyAndPath(cookie.Key(), cookie.Path(), hostCookies) |
| 231 | if existing == nil { |
| 232 | existing = fasthttp.AcquireCookie() |
| 233 | hostCookies = append(hostCookies, storedCookie{cookie: existing, isHostOnly: isHostOnly}) |
| 234 | } else { |
| 235 | for i := range hostCookies { |
| 236 | if hostCookies[i].cookie == existing { |
| 237 | hostCookies[i].isHostOnly = isHostOnly |
| 238 | break |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | existing.CopyTo(cookie) |
| 243 | existing.SetDomain(storedDomain) |
| 244 | cj.hostCookies[key] = hostCookies |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // SetKeyValue sets a cookie for the specified host with the given key and value. |
| 249 | // |