MCPcopy
hub / github.com/gofiber/fiber / parseCookiesFromResp

Method parseCookiesFromResp

client/cookiejar.go:284–356  ·  view source on GitHub ↗

parseCookiesFromResp parses the cookies from the response and stores them for the specified host and path.

(host, _ []byte, resp *fasthttp.Response)

Source from the content-addressed store, hash-verified

282
283// parseCookiesFromResp parses the cookies from the response and stores them for the specified host and path.
284func (cj *CookieJar) parseCookiesFromResp(host, _ []byte, resp *fasthttp.Response) {
285 hostStr := utils.UnsafeString(host)
286 if h, _, err := net.SplitHostPort(hostStr); err == nil {
287 hostStr = h
288 }
289 hostStr = utilsstrings.ToLower(hostStr)
290 hostKey := utils.CopyString(hostStr)
291
292 cj.mu.Lock()
293 defer cj.mu.Unlock()
294
295 if cj.hostCookies == nil {
296 cj.hostCookies = make(map[string][]storedCookie)
297 }
298
299 now := time.Now()
300 for _, value := range resp.Header.Cookies() {
301 tmp := fasthttp.AcquireCookie()
302 _ = tmp.ParseBytes(value) //nolint:errcheck // ignore error
303
304 domainBytes := utils.TrimLeft(tmp.Domain(), '.')
305 utilsbytes.UnsafeToLower(domainBytes)
306 key := hostKey
307 isHostOnly := len(domainBytes) == 0
308 if isHostOnly {
309 tmp.SetDomain(hostStr)
310 } else {
311 domain := utils.UnsafeString(domainBytes)
312 acceptance := acceptCookieDomain(hostStr, domain)
313 if !acceptance.isOk {
314 fasthttp.ReleaseCookie(tmp)
315 continue
316 }
317 isHostOnly = acceptance.isHostOnly
318 if isHostOnly {
319 tmp.SetDomain(hostStr)
320 } else {
321 key = utils.CopyString(acceptance.domain)
322 tmp.SetDomain(acceptance.domain)
323 }
324 }
325
326 cj.ensureHostCapacityLocked(key, now)
327 cookies := cj.hostCookies[key]
328 c := searchCookieByKeyAndPath(tmp.Key(), tmp.Path(), cookies)
329 if c == nil {
330 c = fasthttp.AcquireCookie()
331 cookies = append(cookies, storedCookie{cookie: c, isHostOnly: isHostOnly})
332 } else {
333 for i := range cookies {
334 if cookies[i].cookie == c {
335 cookies[i].isHostOnly = isHostOnly
336 break
337 }
338 }
339 }
340
341 c.CopyTo(tmp)

Calls 9

acceptCookieDomainFunction · 0.85
searchCookieByKeyAndPathFunction · 0.85
LockMethod · 0.65
UnlockMethod · 0.65
CookiesMethod · 0.65
DomainMethod · 0.65
PathMethod · 0.65
NowMethod · 0.45