Skip to content

Commit

Permalink
[PR #9895/d8ec1b4 backport][3.11] Defer creation of SimpleCookie obje…
Browse files Browse the repository at this point in the history
…cts in the web server until needed (#9971)

Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
patchback[bot] and bdraco authored Nov 19, 2024
1 parent 85ef646 commit 454f914
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES/9895.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of creating web responses when there are no cookies -- by :user:`bdraco`.
13 changes: 7 additions & 6 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(
self._compression = False
self._compression_strategy: int = zlib.Z_DEFAULT_STRATEGY
self._compression_force: Optional[ContentCoding] = None
self._cookies = SimpleCookie()
self._cookies: Optional[SimpleCookie] = None

self._req: Optional[BaseRequest] = None
self._payload_writer: Optional[AbstractStreamWriter] = None
Expand Down Expand Up @@ -209,6 +209,8 @@ def headers(self) -> "CIMultiDict[str]":

@property
def cookies(self) -> SimpleCookie:
if self._cookies is None:
self._cookies = SimpleCookie()
return self._cookies

def set_cookie(
Expand All @@ -230,10 +232,8 @@ def set_cookie(
Sets new cookie or updates existent with new value.
Also updates only those params which are not None.
"""
old = self._cookies.get(name)
if old is not None and old.coded_value == "":
# deleted cookie
self._cookies.pop(name, None)
if self._cookies is None:
self._cookies = SimpleCookie()

self._cookies[name] = value
c = self._cookies[name]
Expand Down Expand Up @@ -277,7 +277,8 @@ def del_cookie(
Creates new empty expired cookie.
"""
# TODO: do we need domain/path here?
self._cookies.pop(name, None)
if self._cookies is not None:
self._cookies.pop(name, None)
self.set_cookie(
name,
"",
Expand Down
6 changes: 6 additions & 0 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,10 @@ def test_response_cookies() -> None:
assert resp.cookies == {}
assert str(resp.cookies) == ""

resp.set_cookie("name", "value")
assert str(resp.cookies) == "Set-Cookie: name=value; Path=/"
resp.set_cookie("name", "")
assert str(resp.cookies) == 'Set-Cookie: name=""; Path=/'
resp.set_cookie("name", "value")
assert str(resp.cookies) == "Set-Cookie: name=value; Path=/"
resp.set_cookie("name", "other_value")
Expand All @@ -879,6 +883,8 @@ def test_response_cookies() -> None:
"expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/"
)
assert Matches(expected) == str(resp.cookies)
resp.del_cookie("name")
assert str(resp.cookies) == Matches(expected)

resp.set_cookie("name", "value", domain="local.host")
expected = "Set-Cookie: name=value; Domain=local.host; Path=/"
Expand Down

0 comments on commit 454f914

Please sign in to comment.