From 8990b75138a9a81f098db31ff9b0f4f5dd45eefc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 13 Oct 2024 13:35:59 -0500 Subject: [PATCH] Speed up matching the host header when updating client headers (#9477) (cherry picked from commit ce33b69584e7b5e9270540691912eb9d39af5490) --- aiohttp/client_reqrep.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index d536c0a1ca4..c063c56314e 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -3,6 +3,7 @@ import contextlib import functools import io +import itertools import re import sys import traceback @@ -244,6 +245,9 @@ class ClientRequest: } POST_METHODS = {hdrs.METH_PATCH, hdrs.METH_POST, hdrs.METH_PUT} ALL_METHODS = GET_METHODS.union(POST_METHODS).union({hdrs.METH_DELETE}) + _HOST_STRINGS = frozenset( + map("".join, itertools.product(*zip("host".upper(), "host".lower()))) + ) DEFAULT_HEADERS = { hdrs.ACCEPT: "*/*", @@ -470,7 +474,7 @@ def update_headers(self, headers: Optional[LooseHeaders]) -> None: for key, value in headers: # type: ignore[misc] # A special case for Host header - if key.lower() == "host": + if key in self._HOST_STRINGS: self.headers[key] = value else: self.headers.add(key, value)