def _update_cookies(self, response): cookies = Cookie() # Load any generated cookies. for cookie in response.headers.getall('Set-Cookie'): cookies.load(cookie) self.cookies.update({i.name.decode('ascii'): i.value.decode('ascii') for i in cookies.values()})
def get_cookie(self, name, default=None): # webtest currently doesn't expose the unescaped cookie values # so we're using webob to parse them for us # see https://github.com/Pylons/webtest/issues/171 cookie = Cookie(' '.join('%s=%s' % (c.name, c.value) for c in self.cookiejar if c.name == name)) return next( (m.value.decode('latin-1') for m in cookie.values()), default, )
def _handle_cookies(self, resp): existing = resp.headers.getall("Set-Cookie") if not existing: return dict() cookies = Cookie() for header in existing: cookies.load(header) for key in cookies: self._cookies[key] = cookies[key].value
def get_cookie(response, key): existing = response.headers.getall("Set-Cookie") if not existing: return False cookies = Cookie() for header in existing: cookies.load(header) if isinstance(key, text_type): key = key.encode("utf8") if key in cookies: return cookies[key] return None
def parse_response_cookies(request_or_response) -> Cookie: try: response = make_response(request_or_response) except AttributeError: response = request_or_response headers = response.headers.getall('Set-Cookie') return Cookie('\r\n'.join(headers))
def unset_cookie(self, key, strict=True): """ Unset a cookie with the given name (remove it from the response). """ existing = self.headers.getall('Set-Cookie') if not existing and not strict: return cookies = Cookie() for header in existing: cookies.load(header) if key in cookies: del cookies[key] del self.headers['Set-Cookie'] for m in list(cookies.values()): self.headerlist.append(('Set-Cookie', str(m))) elif strict: raise KeyError("No cookie has been set with the name %r" % key)
def unset_cookie(self, key, strict=True): """ Unset a cookie with the given name (remove it from the response). """ existing = self.headers.getall('Set-Cookie') if not existing and not strict: return cookies = Cookie() for header in existing: cookies.load(header) if key in cookies: del cookies[key] del self.headers['Set-Cookie'] for m in cookies.values(): self.headerlist.append(('Set-Cookie', str(m))) elif strict: raise KeyError("No cookie has been set with the name %r" % key)
def unset_cookie(self, name, strict=True): """ Unset a cookie with the given name (remove it from the response). """ existing = self.headers.getall('Set-Cookie') if not existing and not strict: return cookies = Cookie() for header in existing: cookies.load(header) if isinstance(name, text_type): name = name.encode('utf8') if name in cookies: del cookies[name] del self.headers['Set-Cookie'] for m in cookies.values(): self.headerlist.append(('Set-Cookie', m.serialize())) elif strict: raise KeyError("No cookie has been set with the name %r" % name)
def normalize_cookie_tween(request): if request.path in ignore or request.path.startswith("/static/"): return handler(request) session = request.session if session or session._cookie_name not in request.cookies: return handler(request) response = handler(request) existing = response.headers.getall("Set-Cookie") if existing: cookies = Cookie() for header in existing: cookies.load(header) if session._cookie_name in cookies: return response response.delete_cookie(session._cookie_name, path=session._cookie_path, domain=session._cookie_domain) return response
def unset_cookie(self, key, strict=True): """ Unset a cookie with the given name (remove it from the response). """ existing = self.headers.getall("Set-Cookie") if not existing and not strict: return cookies = Cookie() for header in existing: cookies.load(header) if isinstance(key, text_type): key = key.encode("utf8") if key in cookies: del cookies[key] del self.headers["Set-Cookie"] for m in cookies.values(): self.headerlist.append(("Set-Cookie", m.serialize())) elif strict: raise KeyError("No cookie has been set with the name %r" % key)
def _str_cookies(self): env = self.environ source = env.get('HTTP_COOKIE', '') if 'webob._parsed_cookies' in env: vars, var_source = env['webob._parsed_cookies'] if var_source == source: return vars vars = {} if source: cookies = Cookie(source) for name in cookies: vars[name] = cookies[name].value env['webob._parsed_cookies'] = (vars, source) return vars
def normalize_cookie_tween(request): if request.path in ignore or request.path.startswith('/static/'): return handler(request) session = request.session if session or session._cookie_name not in request.cookies: return handler(request) response = handler(request) existing = response.headers.getall('Set-Cookie') if existing: cookies = Cookie() for header in existing: cookies.load(header) if session._cookie_name in cookies: return response response.delete_cookie( session._cookie_name, path=session._cookie_path, domain=session._cookie_domain, ) return response
def str_cookies(self): """ Return a *plain* dictionary of cookies as found in the request. """ env = self.environ source = env.get('HTTP_COOKIE', '') if 'webob._parsed_cookies' in env: vars, var_source = env['webob._parsed_cookies'] if var_source == source: return vars vars = {} if source: cookies = Cookie(source) for name in cookies: vars[name] = cookies[name].value env['webob._parsed_cookies'] = (vars, source) return vars
def cookies(self): """ A dictionary of cookies as found in the request. """ env = self.environ source = env.get('HTTP_COOKIE', '') if 'webob._parsed_cookies' in env: vars, var_source = env['webob._parsed_cookies'] if var_source == source: return vars vars = {} if source: cookies = Cookie(source) for name in cookies: vars[name] = cookies[name].value #@@ decode directly vars = UnicodeMultiDict(vars, encoding=self.charset, errors=self.unicode_errors) env['webob._parsed_cookies'] = (vars, source) return vars