Only use non-postfixed cookie if no postfixed one exists
Example: * both "session" and "session_P5000" cookies available: "session" value from "session_P5000" * only "session" cookie available: "session" value from "session" * only "session_P5000" cookie available: "session" value from "session_P5000"
This commit is contained in:
parent
53b74f9caa
commit
bbe6e44d44
2 changed files with 20 additions and 10 deletions
|
|
@ -369,10 +369,16 @@ class OctoPrintFlaskRequest(flask.Request):
|
|||
# strip cookie_suffix from all cookies in the request, return result
|
||||
cookies = flask.Request.cookies.__get__(self)
|
||||
|
||||
def cookie_name_converter(key):
|
||||
return key[:-len(self.cookie_suffix)] if key.endswith(self.cookie_suffix) else key
|
||||
result = dict()
|
||||
desuffixed = dict()
|
||||
for key, value in cookies.items():
|
||||
if key.endswith(self.cookie_suffix):
|
||||
desuffixed[key[:-len(self.cookie_suffix)]] = value
|
||||
else:
|
||||
result[key] = value
|
||||
|
||||
return dict((cookie_name_converter(key), value) for key, value in cookies.items())
|
||||
result.update(desuffixed)
|
||||
return result
|
||||
|
||||
@cached_property
|
||||
def server_name(self):
|
||||
|
|
|
|||
|
|
@ -433,28 +433,32 @@ class OctoPrintFlaskRequestTest(unittest.TestCase):
|
|||
|
||||
def test_server_name(self):
|
||||
request = OctoPrintFlaskRequest(standard_environ)
|
||||
self.assertEquals(request.server_name, "localhost")
|
||||
self.assertEquals("localhost", request.server_name)
|
||||
|
||||
def test_server_port(self):
|
||||
request = OctoPrintFlaskRequest(standard_environ)
|
||||
self.assertEquals(request.server_port, "5000")
|
||||
self.assertEquals("5000", request.server_port)
|
||||
|
||||
def test_cookie_suffix(self):
|
||||
request = OctoPrintFlaskRequest(standard_environ)
|
||||
self.assertEquals(request.cookie_suffix, "_P5000")
|
||||
self.assertEquals("_P5000", request.cookie_suffix)
|
||||
|
||||
def test_cookies(self):
|
||||
environ = dict(standard_environ)
|
||||
environ["HTTP_COOKIE"] = "postfixed_P5000=postfixed_value; " \
|
||||
"postfixed_wrong_P5001=postfixed_wrong_value; " \
|
||||
"unpostfixed=unpostfixed_value"
|
||||
"unpostfixed=unpostfixed_value; " \
|
||||
"both_P5000=both_postfixed_value; " \
|
||||
"both=both_unpostfixed_value;"
|
||||
|
||||
request = OctoPrintFlaskRequest(environ)
|
||||
|
||||
cookies = request.cookies
|
||||
self.assertDictEqual(cookies, {"postfixed": "postfixed_value",
|
||||
"postfixed_wrong_P5001": "postfixed_wrong_value",
|
||||
"unpostfixed": "unpostfixed_value"})
|
||||
self.assertDictEqual({"postfixed": u"postfixed_value",
|
||||
"postfixed_wrong_P5001": u"postfixed_wrong_value",
|
||||
"unpostfixed": u"unpostfixed_value",
|
||||
"both": u"both_postfixed_value"},
|
||||
cookies)
|
||||
|
||||
##~~
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue