def is_failing_parsing(set_cookie_header): try: biscuits.parse(set_cookie_header) except Exception: """it seems cookies library has difficulties in parsing a set-cookie header containing 'Expires=Fri, 24 Feb 2017 00:58:28 GMT'. This code tries to be defensive about it for alternative libraries used by responses""" return True else: return False
def _cookies_from_headers(headers): if sys.version_info[:2] < (3, 4): from cookies import Cookies resp_cookies = Cookies.from_request(headers["set-cookie"]) cookies_dict = {v.name: v.value for _, v in resp_cookies.items()} else: import biscuits cookies_dict = biscuits.parse(headers["set-cookie"]) return cookiejar_from_dict(cookies_dict)
def get_id(self, cookie): if cookie is not None: morsels = parse(cookie) signed_sid = morsels.get(self.cookie_name) if signed_sid is not None: try: sid = self.verify_id(signed_sid) return False, str(sid, 'utf-8') except itsdangerous.exc.SignatureExpired: # Session expired. We generate a new one. pass return True, self.generate_id()
def extract_session(self, environ): cookie = environ.get('HTTP_COOKIE') if cookie is not None: morsels = parse(cookie) token = morsels.get(self.cookie_name) if token is not None: try: session_data = self.check_token(token) return session_data except (ExpiredToken, InvalidToken) as e: # The token is expired. # We'll return an empty session. pass return {}
def test_parse(input, expected): assert parse(input) == expected
def test_value_encoding(value, expected): cookie = Cookie('key', value) assert str(cookie) == expected # Make sure we can read a cookie we have just encoded. assert parse(expected)['key'] == value
def cookies(self): if self._cookies is None: self._cookies = parse(self.headers.get('Cookie', b'')) return self._cookies