MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / parse_ns_headers

Function parse_ns_headers

tools/python-3.11.9-amd64/Lib/http/cookiejar.py:466–531  ·  view source on GitHub ↗

Ad-hoc parser for Netscape protocol cookie-attributes. The old Netscape cookie format for Set-Cookie can for instance contain an unquoted "," in the expires field, so we have to use this ad-hoc parser instead of split_header_words. XXX This may not make the best possible effo

(ns_headers)

Source from the content-addressed store, hash-verified

464 return text
465
466def parse_ns_headers(ns_headers):
467 """Ad-hoc parser for Netscape protocol cookie-attributes.
468
469 The old Netscape cookie format for Set-Cookie can for instance contain
470 an unquoted "," in the expires field, so we have to use this ad-hoc
471 parser instead of split_header_words.
472
473 XXX This may not make the best possible effort to parse all the crap
474 that Netscape Cookie headers contain. Ronald Tschalar's HTTPClient
475 parser is probably better, so could do worse than following that if
476 this ever gives any trouble.
477
478 Currently, this is also used for parsing RFC 2109 cookies.
479
480 """
481 known_attrs = ("expires", "domain", "path", "secure",
482 # RFC 2109 attrs (may turn up in Netscape cookies, too)
483 "version", "port", "max-age")
484
485 result = []
486 for ns_header in ns_headers:
487 pairs = []
488 version_set = False
489
490 # XXX: The following does not strictly adhere to RFCs in that empty
491 # names and values are legal (the former will only appear once and will
492 # be overwritten if multiple occurrences are present). This is
493 # mostly to deal with backwards compatibility.
494 for ii, param in enumerate(ns_header.split(';')):
495 param = param.strip()
496
497 key, sep, val = param.partition('=')
498 key = key.strip()
499
500 if not key:
501 if ii == 0:
502 break
503 else:
504 continue
505
506 # allow for a distinction between present and empty and missing
507 # altogether
508 val = val.strip() if sep else None
509
510 if ii != 0:
511 lc = key.lower()
512 if lc in known_attrs:
513 key = lc
514
515 if key == "version":
516 # This is an RFC 2109 cookie.
517 if val is not None:
518 val = strip_quotes(val)
519 version_set = True
520 elif key == "expires":
521 # convert expires date to seconds since epoch
522 if val is not None:
523 val = http2time(strip_quotes(val)) # None if invalid

Callers 1

make_cookiesMethod · 0.85

Calls 8

enumerateFunction · 0.85
strip_quotesFunction · 0.85
http2timeFunction · 0.85
stripMethod · 0.80
partitionMethod · 0.80
splitMethod · 0.45
lowerMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected