From c5cb337791372c7130e49c39691d50332190a1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= Date: Fri, 11 Apr 2025 09:14:57 -0500 Subject: [PATCH] Make response handler management more flexible Extend AsyncDnsServer.install_response_handler() so that the provided response handler can be inserted at the beginning of the handler list. This enables installing a response handler that takes priority over all previously installed handlers. Add a new method, AsyncDnsServer.uninstall_response_handler(), which enables removing a previously installed response handler. Together, these two methods provide full control over the response handler list at runtime. (cherry picked from commit 92b072bff4376e02bf1d0cf8bd01b179fbea5358) --- bin/tests/system/isctest/asyncserver.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/bin/tests/system/isctest/asyncserver.py b/bin/tests/system/isctest/asyncserver.py index fb5f2c21c3..0ace0f277f 100644 --- a/bin/tests/system/isctest/asyncserver.py +++ b/bin/tests/system/isctest/asyncserver.py @@ -532,15 +532,31 @@ class AsyncDnsServer(AsyncServer): if load_zones: self._load_zones() - def install_response_handler(self, handler: ResponseHandler) -> None: + def install_response_handler( + self, handler: ResponseHandler, prepend: bool = False + ) -> None: """ - Add a response handler which will be used to handle matching queries. + Add a response handler that will be used to handle matching queries. Response handlers can modify, replace, or suppress the answers prepared from zone file contents. + + The provided handler is installed at the end of the response handler + list unless `prepend` is set to True, in which case it is installed at + the beginning of the response handler list. """ logging.info("Installing response handler: %s", handler) - self._response_handlers.append(handler) + if prepend: + self._response_handlers.insert(0, handler) + else: + self._response_handlers.append(handler) + + def uninstall_response_handler(self, handler: ResponseHandler) -> None: + """ + Remove the specified handler from the list of response handlers. + """ + logging.info("Uninstalling response handler: %s", handler) + self._response_handlers.remove(handler) def _load_zones(self) -> None: for entry in os.scandir():