async def get_form( scope: Scope, _info: Info, _matches: RouteMatches, _content: Content ) -> HttpResponse: """A response handler which returns a form and sets some cookies""" cookies = header.cookie(scope['headers']) first_name = cookies.get(b'first_name', [b'Micky'])[0] last_name = cookies.get(b'last_name', [b'Mouse'])[0] html_list = '<dl>' for name, values in cookies.items(): for value in values: html_list += f'<dt>{name.decode()}</dt><dd>{value.decode()}</dd>' html_list += '</dl>' html = FORM_HTML.format( first_name=first_name.decode(), last_name=last_name.decode(), cookies=html_list ) headers = [ (b'content-type', b'text/html'), ] return 200, headers, text_writer(html)
def _get_session_key_from_cookie(self, request: HttpRequest) -> Optional[str]: cookies = header.cookie(request.scope['headers']) session_cookie = cookies.get(self.cookie_name) if not session_cookie: return None return session_cookie[0].decode('ascii')
async def post_form( scope: Scope, info: Info, matches: RouteMatches, content: Content ) -> HttpResponse: """A response handler that reads the cookies from a posted form.""" cookies = header.cookie(scope['headers']) html_list = '<dl>' for name, values in cookies.items(): for value in values: html_list += f'<dt>{name.decode()}</dt><dd>{value.decode()}</dd>' html_list += '</dl>' return 200, [(b'content-type', b'text/html')], text_writer(COOKIES.format(cookies=html_list))
def get_token_from_headers(self, headers: List[Header]) -> Optional[bytes]: """Get the token from the headers or None if not present :param headers: The headers :type headers: List[Header] :return: The token of None if not present :rtype: Optional[bytes] """ tokens = header.cookie(headers).get(self.cookie_name) if tokens is None or not tokens: return None if len(tokens) > 1: logger.warning('Multiple tokens in header - using first') token = tokens[0] return token
def get_token_from_headers(self, headers: List[Header]) -> Optional[bytes]: """Gets the token from the headers if present. Args: headers (List[Header]): The headers Returns: Optional[bytes]: The token or None if not found. """ tokens = header.cookie(headers).get(self.cookie_name) if tokens is None or not tokens: return None if len(tokens) > 1: logger.warning('Multiple tokens in header - using first') token = tokens[0] return token
def test_cookie(): """Test cookie""" headers = [(b'content-type', b'application/json'), (b'cookie', b'foo=bar'), (b'cookie', b'one=first; two=second; three=third;'), (b'cookie', b'four=fourth; '), (b'cookie', b'four=fourth again; ')] cookies = header.cookie(headers) assert len(cookies) == 5 assert b'one' in cookies assert cookies[b'one'][0] == b'first' assert b'two' in cookies assert cookies[b'two'][0] == b'second' assert b'three' in cookies assert cookies[b'three'][0] == b'third' assert b'four' in cookies assert cookies[b'four'][0] == b'fourth' assert cookies[b'four'][1] == b'fourth again'
async def get_form(scope, info, matches, content): cookies = header.cookie(scope['headers']) first_name = cookies.get(b'first_name', [b'Micky'])[0] last_name = cookies.get(b'last_name', [b'Mouse'])[0] html_list = '<dl>' for name, values in cookies.items(): for value in values: html_list += f'<dt>{name.decode()}</dt><dd>{value.decode()}</dd>' html_list += '</dl>' html = FORM_HTML.format(first_name=first_name.decode(), last_name=last_name.decode(), cookies=html_list) headers = [ (b'content-type', b'text/html'), ] return 200, headers, text_writer(html)