Convert cookies to werkzeug MultiDict instead of plain dict.

I discovered that werkzeug stores cookies in lists, with its .get
returning only the first item of the list. By converting the cookies
to a plain dict, I was breaking that functionality of cookies.get.
So, using werkzeug's MultiDict is the correct choice.
This commit is contained in:
voussoir 2020-09-09 18:53:26 -07:00
parent 82758ed336
commit 049d620789

View file

@ -1,6 +1,7 @@
import flask; from flask import request import flask; from flask import request
import functools import functools
import werkzeug.wrappers import werkzeug.wrappers
import werkzeug.datastructures
from voussoirkit import cacheclass from voussoirkit import cacheclass
@ -62,7 +63,13 @@ class SessionManager:
token = request.cookies.get('etiquette_session', None) token = request.cookies.get('etiquette_session', None)
if not token or token not in self.sessions: if not token or token not in self.sessions:
token = _generate_token() token = _generate_token()
request.cookies = dict(request.cookies) # cookies is currently an ImmutableMultiDict, but in order to
# trick the wrapped function I'm gonna have to mutate it.
# It is important to use a werkzeug MultiDict and not a plain
# Python dict, because werkzeug puts cookies into lists like
# {name: [value]} and then cookies.get pulls the first item out
# of that list. A plain dict wouldn't have this .get behavior.
request.cookies = werkzeug.datastructures.MultiDict(request.cookies)
request.cookies['etiquette_session'] = token request.cookies['etiquette_session'] = token
try: try: