1+ """
2+ Extends aiohttp_session.cookie_storage
3+
4+ """
5+
16import logging
27import time
38
1116def _share_cookie_across_all_subdomains (
1217 response : web .StreamResponse , params : aiohttp_session ._CookieParams
1318) -> aiohttp_session ._CookieParams :
14- # share cookie across all subdomains, by appending a dot (`.`) in front of the domain name
15- # overwrite domain from `None` (browser sets `example.com`) to `.example.com`
19+ """
20+ Shares cookie across all subdomains, by appending a dot (`.`) in front of the domain name
21+ overwrite domain from `None` (browser sets `example.com`) to `.example.com`
22+ """
1623 request = response ._req # pylint:disable=protected-access # noqa: SLF001
1724 assert isinstance (request , web .Request ) # nosec
18- params ["domain" ] = f".{ request .url .host } "
25+
26+ if (host := request .url .host ) and host is not None :
27+ params ["domain" ] = f".{ host .lstrip ('.' )} "
28+
1929 return params
2030
2131
@@ -38,20 +48,24 @@ def save_cookie(
3848 * ,
3949 max_age : int | None = None ,
4050 ) -> None :
41- # NOTE: WARNING: the only difference between the superclass and this implementation
42- # is the statement below where the domain name is set. Adjust in case the base library changes.
4351 params = _share_cookie_across_all_subdomains (
4452 response , self ._cookie_params .copy ()
4553 )
4654
55+ # WARNING: the code below is taken and adapted from the superclass implementation `EncryptedCookieStorage.save_cookie`
56+ # Adjust in case the base library changes.
57+ assert aiohttp_session .__version__ == "2.11.0" # nosec
58+ # ---
4759 if max_age is not None :
4860 params ["max_age" ] = max_age
4961 t = time .gmtime (time .time () + max_age )
5062 params ["expires" ] = time .strftime ("%a, %d-%b-%Y %T GMT" , t )
5163
5264 if not cookie_data :
5365 response .del_cookie (
54- self ._cookie_name , domain = params ["domain" ], path = params ["path" ]
66+ self ._cookie_name ,
67+ domain = params .get ("domain" ),
68+ path = params .get ("path" , "/" ),
5569 )
5670 else :
5771 response .set_cookie (self ._cookie_name , cookie_data , ** params )
0 commit comments