def from_headers(strheader): """Parse cookie data from a string in HTTP header (RFC 2616) format. @return: list of cookies @raises: ValueError for incomplete or invalid data """ res = [] fp = StringIO(strheader) headers = HTTPMessage(fp, seekable=True) if "Host" not in headers: raise ValueError("Required header 'Host:' missing") host = headers["Host"] # XXX: our --help says we also pay attention to the Scheme: header, # but we don't?! path = headers.get("Path", "/") for header in headers.getallmatchingheaders("Set-Cookie"): headervalue = header.split(':', 1)[1] for pairs in split_header_words([headervalue]): for name, value in pairs: cookie = requests.cookies.create_cookie(name, value, domain=host, path=path) res.append(cookie) return res
def get_content_type_from_headers(res_headers: HTTPMessage): if not res_headers: return None content_type: str = res_headers.get('Content-Type', failobj=None) return get_content_type_from_header(content_type)