def parse_ns_headers(ns_headers): """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 effort to parse all the crap that Netscape Cookie headers contain. Ronald Tschalar's HTTPClient parser is probably better, so could do worse than following that if this ever gives any trouble. Currently, this is also used for parsing RFC 2109 cookies. """ known_attrs = ( "expires", "domain", "path", "secure", # RFC 2109 attrs (may turn up in Netscape cookies, too) "port", "max-age") result = [] for ns_header in ns_headers: pairs = [] version_set = False params = re.split(r";\s*", ns_header) for ii in range(len(params)): param = params[ii] param = string.rstrip(param) if param == "": continue if "=" not in param: k, v = param, None else: k, v = re.split(r"\s*=\s*", param, 1) k = string.lstrip(k) if ii != 0: lc = string.lower(k) if lc in known_attrs: k = lc if k == "version": # This is an RFC 2109 cookie. version_set = True if k == "expires": # convert expires date to seconds since epoch if startswith(v, '"'): v = v[1:] if endswith(v, '"'): v = v[:-1] v = http2time(v) # None if invalid pairs.append((k, v)) if pairs: if not version_set: pairs.append(("version", "0")) result.append(pairs) return result
def parse_ns_headers(ns_headers): """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 effort to parse all the crap that Netscape Cookie headers contain. Ronald Tschalar's HTTPClient parser is probably better, so could do worse than following that if this ever gives any trouble. Currently, this is also used for parsing RFC 2109 cookies. """ known_attrs = ("expires", "domain", "path", "secure", # RFC 2109 attrs (may turn up in Netscape cookies, too) "port", "max-age") result = [] for ns_header in ns_headers: pairs = [] version_set = False for param in re.split(r";\s*", ns_header): param = string.rstrip(param) if param == "": continue if "=" not in param: if string.lower(param) in known_attrs: k, v = param, None else: # cookie with missing name k, v = None, param else: k, v = re.split(r"\s*=\s*", param, 1) k = string.lstrip(k) if k is not None: lc = string.lower(k) if lc in known_attrs: k = lc if k == "version": # This is an RFC 2109 cookie. Will be treated as RFC 2965 # cookie in rest of code. # Probably it should be parsed with split_header_words, but # that's too much hassle. version_set = True if k == "expires": # convert expires date to seconds since epoch if startswith(v, '"'): v = v[1:] if endswith(v, '"'): v = v[:-1] v = http2time(v) # None if invalid pairs.append((k, v)) if pairs: if not version_set: pairs.append(("version", "0")) result.append(pairs) return result
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() magic = f.readline() if not re.search(self.magic_re, magic): f.close() raise LoadError( "%s does not look like a Netscape format cookies file" % filename) try: while 1: line = f.readline() if line == "": break # last field may be absent, so keep any trailing tab if endswith(line, "\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (startswith(string.strip(line), "#") or startswith(string.strip(line), "$") or string.strip(line) == ""): continue domain, domain_specified, path, secure, expires, name, value = \ string.split(line, "\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": name = value value = None initial_dot = startswith(domain, ".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except: reraise_unmasked_exceptions((IOError, )) raise LoadError("invalid Netscape format file %s: %s" % (filename, line))
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() magic = f.readline() if not re.search(self.magic_re, magic): f.close() raise LoadError( "%s does not look like a Netscape format cookies file" % filename) try: while 1: line = f.readline() if line == "": break # last field may be absent, so keep any trailing tab if endswith(line, "\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (startswith(string.strip(line), "#") or startswith(string.strip(line), "$") or string.strip(line) == ""): continue domain, domain_specified, path, secure, expires, name, value = \ string.split(line, "\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": name = value value = None initial_dot = startswith(domain, ".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except: reraise_unmasked_exceptions((IOError,)) raise LoadError("invalid Netscape format file %s: %s" % (filename, line))