diff --git a/yt_dlp/networking/common.py b/yt_dlp/networking/common.py index e8951c7e7d..d3a5f4e753 100644 --- a/yt_dlp/networking/common.py +++ b/yt_dlp/networking/common.py @@ -257,7 +257,8 @@ def _make_sslcontext(self, legacy_ssl_support=None): ) 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) diff --git a/yt_dlp/utils/networking.py b/yt_dlp/utils/networking.py index 933b164be9..96e4b30eb3 100644 --- a/yt_dlp/utils/networking.py +++ b/yt_dlp/utils/networking.py @@ -79,6 +79,22 @@ def __contains__(self, key): 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',