Turn _get_cookie() into a method

Since the _get_cookie() function is only used by the CookieHandler
class, make the former a method of the latter to keep related logic
close in the source code.
This commit is contained in:
Michał Kępień 2026-05-21 11:52:56 +02:00
parent 5fa2bd7e53
commit c3839e830c
No known key found for this signature in database

View file

@ -28,16 +28,6 @@ from isctest.asyncserver import (
)
def _get_cookie(qctx: QueryContext) -> dns.edns.CookieOption | None:
for o in qctx.query.options:
if o.otype == dns.edns.OptionType.COOKIE:
cookie = o
cookie.server = b"\x11\x22\x33\x44\x55\x66\x77\x88"
return cookie
return None
def rrset(
qname: dns.name.Name | str,
rtype: dns.rdatatype.RdataType,
@ -64,10 +54,19 @@ class ExampleNSHandler(QnameQtypeHandler, StaticResponseHandler):
class CookieHandler(DomainHandler):
domains = ["example."]
def _get_cookie(self, qctx: QueryContext) -> dns.edns.CookieOption | None:
for o in qctx.query.options:
if o.otype == dns.edns.OptionType.COOKIE:
cookie = o
cookie.server = b"\x11\x22\x33\x44\x55\x66\x77\x88"
return cookie
return None
async def get_responses(
self, qctx: QueryContext
) -> AsyncGenerator[DnsResponseSend, None]:
if cookie := _get_cookie(qctx):
if cookie := self._get_cookie(qctx):
# If there is a client cookie, mock BADCOOKIE to trigger
# the resend loop logic.
qctx.response.use_edns(options=[cookie])