Allow sending case insensitve headers

This commit is contained in:
sepro 2024-11-24 22:00:55 +01:00
parent fe70f20aed
commit a9bc34feb1
2 changed files with 18 additions and 1 deletions

View file

@ -257,7 +257,8 @@ class RequestHandler(abc.ABC):
)
def _merge_headers(self, request_headers):
return HTTPHeaderDict(self.headers, request_headers)
header_type = type(request_headers) if isinstance(request_headers, HTTPHeaderDict) else HTTPHeaderDict
return header_type(self.headers, request_headers)
def _calculate_timeout(self, request):
return float(request.extensions.get('timeout') or self.timeout)

View file

@ -79,6 +79,22 @@ class HTTPHeaderDict(collections.UserDict, dict):
return super().__contains__(key.title() if isinstance(key, str) else key)
class CaseSensitiveDict(HTTPHeaderDict):
def __setitem__(self, key, value):
if isinstance(value, bytes):
value = value.decode('latin-1')
self.data[key] = str(value).strip()
def __getitem__(self, key):
return self.data[key]
def __delitem__(self, key):
del self.data[key]
def __contains__(self, key):
return key in self.data
std_headers = HTTPHeaderDict({
'User-Agent': random_user_agent(),
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',