Backtestsscrapyrun-20261025-211112#2711

Issue #2711 Merged fix PR #7893 Run 25 Oct 2026 · run-20261025-211112 scrapy/scrapy

Scrapy capitalizes headers for request

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

The issue is that lowercase request header names arrive at the server Title-Cased when using Scrapy's default (HTTP11, Twisted-based) download handler, including through an HTTP proxy. Patch B fixes exactly that path: it records each header's original spelling in Headers (keeping the stored keys Title-Case for backward compatibility), exposes original_case_items(), and wraps Twisted's Headers in a _CasePreservingTxHeaders whose getAllRawHeaders() emits the original names, with copy() and removeHeader() overridden so the Agent's internal header copy and the tunneling agent's Proxy-Authorization removal keep working. It also propagates original case to to_dict/request_from_dict, request_to_curl, to_tuple_list/to_unicode_dict, copy and pickling. The independent tests confirm it: all five pass, including the default-handler and proxy on-the-wire checks that fail on base. Patch B's own tests use a raw socket server to assert the actual bytes sent, both direct and via proxy; the skip for secure/http2 is a test-infrastructure limitation and the same TxHeaders code path serves HTTPS, so the fix covers the issue's https URL. Patch A changes Headers.normkey to preserve the first-seen spelling, which fixes to_tuple_list and the aiohttp/httpx handlers, but it never touches the HTTP11 handler, which still builds a plain TxHeaders that Twisted canonicalizes to Title-Case. The three default-handler independent tests still FAIL-ASSERT on Patch A, and Patch A's own docs table admits HTTP11 remains 'Capitalized', so the reported scenario stays broken. Patch A also makes normkey O(n) on every lookup and changes the observable keys() of both request and response Headers (e.g. httpx response headers now in raw server case), a wider behavioral change than the ask with no benefit for the default handler. B resolves the root cause at the wire for the handler the issue describes and has stronger tests; A masks only the in-memory representation.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

21m 58s

Wall-clock time in the container

Full test suite

No new failures

1,999 passed · 339 skipped · 2,348 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

294vs 82 merged

Changed lines, ours vs merged

I'm setting the headers following way

headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'cache-control': 'no-cache',
    ...
}

And calling request like that:

yield scrapy.Request(url='https:/myurl.com/', callback=self.parse, headers=headers, cookies=cookies, meta={'proxy': 'http://localhost:8888'})

And it makes that scrapy capitalizes all these headers and it looks like that (I'm using Charles proxy for debugging):

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Cache-Control: no-cache

And this is not working correctly for my case.

If I'm using curl and set headers lowercase

accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
cache-control: no-cache

everything works like a charm.

Is there any way how I can disable this capitalizing behavior in Scrapy?
Thanks for any help!

The two patches

Our patch+280 −14
docs/topics/request-response.rstindex 39f8701f1..637d06806 100644@@ -51,6 +51,10 @@ Request objects        (for single valued headers) or lists (for multi-valued headers). If        ``None`` is passed as value, the HTTP header will not be sent at all. +       Header names are case-insensitive, but they are sent in the case in+       which they were set (e.g. ``accept`` is not sent as ``Accept``), except+       over HTTP/2, where header names are always sent in lowercase.+        .. caution:: Cookies set via the ``Cookie`` header are not considered by the            :ref:`cookie middleware <cookies>`. If you need to set cookies for a            request, use the ``cookies`` argument.scrapy/core/downloader/handlers/http11.pyindex 37b27c8a6..22d168655 100644@@ -67,6 +67,9 @@ from scrapy.utils.url import _add_http_if_no_scheme from ._base_http import BaseHttpDownloadHandler  if TYPE_CHECKING:+    from collections.abc import Iterator, Mapping, Sequence+    from typing import AnyStr+     from twisted.internet.base import ReactorBase     from twisted.internet.interfaces import IAddress, IConsumer     from twisted.web._newclient import Request as TxRequest@@ -424,6 +427,33 @@ class _ScrapyProxyAgent(_BindAddressAgent):         )  +class _CasePreservingTxHeaders(TxHeaders):+    """Twisted headers that are sent with the header name case in which they+    were set, instead of the canonical case that Twisted uses."""++    __slots__ = ("_original_names",)++    def __init__(self, rawHeaders: Mapping[bytes, Sequence[bytes]] | None = None):+        super().__init__(rawHeaders)+        # Maps lowercase header names to header names as they were set.+        self._original_names: dict[bytes, bytes] = {+            name.lower(): name for name in rawHeaders or ()+        }++    def copy(self) -> _CasePreservingTxHeaders:+        copy = _CasePreservingTxHeaders(self._rawHeaders)+        copy._original_names = self._original_names.copy()+        return copy++    def removeHeader(self, name: AnyStr) -> None:+        super().removeHeader(name)+        self._original_names.pop(to_bytes(name, "iso-8859-1").lower(), None)++    def getAllRawHeaders(self) -> Iterator[tuple[bytes, Sequence[bytes]]]:+        for name, values in super().getAllRawHeaders():+            yield self._original_names.get(name.lower(), name), values++ class _ScrapyAgent:     def __init__(         self,@@ -504,7 +534,9 @@ class _ScrapyAgent:         # request details         url = urldefrag(request.url)[0]         method = to_bytes(request.method)-        headers = TxHeaders(request.headers)+        headers = _CasePreservingTxHeaders(+            dict(request.headers.original_case_items())+        )         if isinstance(agent, _TunnelingAgent):             headers.removeHeader(b"Proxy-Authorization")         bodyproducer = _RequestBodyProducer(request.body) if request.body else Nonescrapy/http/headers.pyindex fb26d5f18..47b624d02 100644@@ -19,7 +19,12 @@ _RawValue: TypeAlias = bytes | str | int   class Headers(dict):  # type: ignore[type-arg]-    """Case insensitive http headers dictionary"""+    """Case insensitive http headers dictionary++    Header names are looked up case-insensitively, but the case in which each+    header name was last set is remembered, and used when a request is sent+    (see :meth:`original_case_items`).+    """      def __init__(         self,@@ -30,15 +35,21 @@ class Headers(dict):  # type: ignore[type-arg]         encoding: str = "utf-8",     ):         self.encoding: str = encoding+        # Maps normalized header names to header names as they were set.+        self._original_keys: dict[bytes, bytes] = {}         super().__init__()         if seq:             self.update(seq)      def __setitem__(self, key: str | bytes, value: Any) -> None:-        dict.__setitem__(self, self.normkey(key), self.normvalue(value))+        normkey = self.normkey(key)+        dict.__setitem__(self, normkey, self.normvalue(value))+        self._original_keys[normkey] = self._tobytes(key)      def __delitem__(self, key: str | bytes) -> None:-        dict.__delitem__(self, self.normkey(key))+        normkey = self.normkey(key)+        dict.__delitem__(self, normkey)+        self._original_keys.pop(normkey, None)      def __contains__(self, key: str | bytes) -> bool:  # type: ignore[override]         return dict.__contains__(self, self.normkey(key))@@ -46,7 +57,10 @@ class Headers(dict):  # type: ignore[type-arg]     has_key = __contains__      def setdefault(self, key: str | bytes, def_val: Any = None) -> Any:-        return dict.setdefault(self, self.normkey(key), self.normvalue(def_val))+        normkey = self.normkey(key)+        if not dict.__contains__(self, normkey):+            self._original_keys[normkey] = self._tobytes(key)+        return dict.setdefault(self, normkey, self.normvalue(def_val))      @classmethod     def fromkeys(  # type: ignore[override]@@ -55,7 +69,18 @@ class Headers(dict):  # type: ignore[type-arg]         return cls((k, value) for k in keys)      def pop(self, key: str | bytes, *args: Any) -> Any:-        return dict.pop(self, self.normkey(key), *args)+        normkey = self.normkey(key)+        self._original_keys.pop(normkey, None)+        return dict.pop(self, normkey, *args)++    def popitem(self) -> tuple[bytes, list[bytes]]:+        key, value = dict.popitem(self)+        self._original_keys.pop(key, None)+        return key, value++    def clear(self) -> None:+        dict.clear(self)+        self._original_keys.clear()      def update(  # type: ignore[override]         self,@@ -63,11 +88,21 @@ class Headers(dict):  # type: ignore[type-arg]         | Mapping[bytes, Any]         | Iterable[tuple[str | bytes, Any]],     ) -> None:-        items = seq.items() if isinstance(seq, Mapping) else seq+        items: Iterable[tuple[str | bytes, Any]]+        if isinstance(seq, Headers):+            items = seq.original_case_items()+        elif isinstance(seq, Mapping):+            items = seq.items()+        else:+            items = seq         iseq: dict[bytes, list[bytes]] = {}+        original_keys: dict[bytes, bytes] = {}         for k, v in items:-            iseq.setdefault(self.normkey(k), []).extend(self.normvalue(v))+            normkey = self.normkey(k)+            iseq.setdefault(normkey, []).extend(self.normvalue(v))+            original_keys[normkey] = self._tobytes(k)         dict.update(self, iseq)+        self._original_keys.update(original_keys)      def normkey(self, key: str | bytes) -> bytes:         """Normalize key to bytes"""@@ -132,7 +167,24 @@ class Headers(dict):  # type: ignore[type-arg]     def appendlist(self, key: str | bytes, value: Iterable[_RawValue]) -> None:         lst = self.getlist(key)         lst.extend(self.normvalue(value))-        self[key] = lst+        normkey = self.normkey(key)+        dict.__setitem__(self, normkey, lst)+        # Keep the original case of the header name if it was already set.+        self._original_keys.setdefault(normkey, self._tobytes(key))++    def original_key(self, key: str | bytes) -> bytes:+        """Return the name of the *key* header in the case in which it was+        set.++        If the header is not set, the normalized header name is returned.+        """+        normkey = self.normkey(key)+        return self._original_keys.get(normkey, normkey)++    def original_case_items(self) -> Iterable[tuple[bytes, list[bytes]]]:+        """Like :meth:`items`, but header names are in the case in which they+        were set."""+        return ((self.original_key(k), v) for k, v in self.items())      def items(self) -> Iterable[tuple[bytes, list[bytes]]]:  # type: ignore[override]         return ((k, self.getlist(k)) for k in self.keys())@@ -156,7 +208,7 @@ class Headers(dict):  # type: ignore[type-arg]                 to_unicode(key, encoding=self.encoding, errors="replace"),                 to_unicode(b",".join(value), encoding=self.encoding, errors="replace"),             )-            for key, value in self.items()+            for key, value in self.original_case_items()         )      def to_tuple_list(self) -> list[tuple[str, str]]:@@ -170,7 +222,7 @@ class Headers(dict):  # type: ignore[type-arg]                 key.decode(self.encoding, errors="replace"),                 value.decode(self.encoding, errors="replace"),             )-            for key, values in self.items()+            for key, values in self.original_case_items()             for value in values         ] @@ -178,3 +230,11 @@ class Headers(dict):  # type: ignore[type-arg]         return self.__class__(self)      copy = __copy__++    def __reduce__(self) -> tuple[Any, ...]:+        # The default dict subclass pickling calls __setitem__ before the+        # instance attributes are restored.+        return (+            self.__class__,+            (list(self.original_case_items()), self.encoding),+        )scrapy/http/request/__init__.pyindex d3c575468..054a749b8 100644@@ -423,7 +423,7 @@ class Request(object_ref):                 if callable(self.errback)                 else self.errback             ),-            "headers": dict(self.headers),+            "headers": dict(self.headers.original_case_items()),         }         for attr in self.attributes:             d.setdefault(attr, getattr(self, attr))scrapy/utils/request.pyindex 24db88b49..d23ac04cb 100644@@ -240,7 +240,8 @@ def request_to_curl(request: Request) -> str:     data = f"--data-raw '{request.body.decode('utf-8')}'" if request.body else ""      headers = " ".join(-        f"-H '{k.decode()}: {v[0].decode()}'" for k, v in request.headers.items()+        f"-H '{k.decode()}: {v[0].decode()}'"+        for k, v in request.headers.original_case_items()     )      url = request.urltests/test_http_headers.pyindex 514ab2513..c67f01356 100644@@ -1,4 +1,5 @@ import copy+import pickle  import pytest @@ -198,3 +199,76 @@ def test_to_tuple_list_undecodable() -> None:         ("Public-Key-Pins", 'pin-sha256=�"a"'),         ("X-�", "ok"),     ]+++def test_original_case_preserved() -> None:+    h = Headers({"accept": "text/html", b"cache-control": b"no-cache"})+    assert h["Accept"] == b"text/html"+    assert "CACHE-CONTROL" in h+    # keys are still normalized for backward compatibility+    assert sorted(h.keys()) == [b"Accept", b"Cache-Control"]+    assert h.original_key("ACCEPT") == b"accept"+    assert h.original_key("Cache-Control") == b"cache-control"+    assert h.original_key("X-Missing") == b"X-Missing"+    assert sorted(h.original_case_items()) == [+        (b"accept", [b"text/html"]),+        (b"cache-control", [b"no-cache"]),+    ]+    assert h.to_tuple_list() == [+        ("accept", "text/html"),+        ("cache-control", "no-cache"),+    ]+    assert list(h.to_unicode_dict().keys()) == ["accept", "cache-control"]+++def test_original_case_last_set_wins() -> None:+    h = Headers({"accept": "a"})+    h["ACCEPT"] = "b"+    assert h.to_tuple_list() == [("ACCEPT", "b")]+    h.update({"Accept": "c"})+    assert h.to_tuple_list() == [("Accept", "c")]+++def test_original_case_setdefault_and_appendlist_keep_case() -> None:+    h = Headers({"user-agent": "foo", "x-multi": "1"})+    h.setdefault("User-Agent", "bar")+    h.setdefault("Accept-Encoding", "gzip")+    h.appendlist("X-Multi", "2")+    h.appendlist("x-new", "3")+    assert h.to_tuple_list() == [+        ("user-agent", "foo"),+        ("x-multi", "1"),+        ("x-multi", "2"),+        ("Accept-Encoding", "gzip"),+        ("x-new", "3"),+    ]+++def test_original_case_forgotten_on_removal() -> None:+    h = Headers({"accept": "a", "x-a": "1", "x-b": "2"})+    del h["Accept"]+    h.pop("X-A")+    h["Accept"] = "b"+    h["X-A"] = "1"+    assert sorted(h.to_tuple_list()) == [("Accept", "b"), ("X-A", "1"), ("x-b", "2")]+    h.clear()+    h["X-B"] = "3"+    assert h.to_tuple_list() == [("X-B", "3")]+++def test_original_case_copy_and_pickle() -> None:+    h = Headers({"accept": "text/html", "x-multi": ["1", "2"]}, encoding="latin1")+    for h2 in (+        h.copy(),+        copy.copy(h),+        copy.deepcopy(h),+        Headers(h),+        pickle.loads(pickle.dumps(h)),  # noqa: S301+    ):+        assert h2 == h+        assert h2.to_tuple_list() == [+            ("accept", "text/html"),+            ("x-multi", "1"),+            ("x-multi", "2"),+        ]+    assert pickle.loads(pickle.dumps(h)).encoding == "latin1"  # noqa: S301tests/test_request_dict.pyindex f85be8977..a96c7d8a7 100644@@ -238,3 +238,10 @@ class MethodsSpider(Spider, SpiderMixin):         self, response: Response     ) -> None:         pass+++def test_header_name_case() -> None:+    r = Request("http://www.example.com", headers={"accept": "text/html"})+    r2 = request_from_dict(r.to_dict())+    _assert_same_request(r, r2)+    assert r2.headers.to_tuple_list() == [("accept", "text/html")]tests/test_utils_request.pyindex 6ca608053..032000a72 100644@@ -389,6 +389,16 @@ class TestRequestToCurl:         )         self._test_request(request_object, expected_curl_command) +    def test_headers_original_case(self) -> None:+        request_object = Request(+            "https://www.example.com",+            headers={"accept": "application/json"},+        )+        expected_curl_command = (+            "curl -X GET https://www.example.com -H 'accept: application/json'"+        )+        self._test_request(request_object, expected_curl_command)+     def test_headers(self) -> None:         request_object = Request(             "https://www.httpbin.org/post",tests/utils/bases/download_handlers_http.pyindex 914b8ce80..2211edea9 100644@@ -7,8 +7,9 @@ import os import re import socket import sys+import threading from abc import ABC, abstractmethod-from contextlib import asynccontextmanager+from contextlib import asynccontextmanager, contextmanager from http import HTTPStatus from ipaddress import IPv4Address from typing import TYPE_CHECKING, Any, ClassVar, Literal@@ -63,6 +64,54 @@ if TYPE_CHECKING: BadHeaderHandling = Literal["skip-bad", "skip-rest", "fail"]  +@contextmanager+def _raw_http_server() -> Generator[tuple[str, list[bytes]]]:+    """Run a plain HTTP server that records the raw bytes of the request+    head it receives, for checks that an HTTP server library would hide, like+    the case of header names."""+    received: list[bytes] = []+    server = socket.create_server(("127.0.0.1", 0))+    server.settimeout(10)++    def serve() -> None:+        conn, _ = server.accept()+        with conn:+            data = b""+            while b"\r\n\r\n" not in data:+                chunk = conn.recv(65536)+                if not chunk:+                    break+                data += chunk+            received.append(data)+            conn.sendall(+                b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"+            )++    thread = threading.Thread(target=serve, daemon=True)+    thread.start()+    try:+        yield f"http://127.0.0.1:{server.getsockname()[1]}", received+    finally:+        thread.join(timeout=10)+        server.close()+++_CASED_REQUEST_HEADERS = {+    "accept": "text/html",+    "cache-control": "no-cache",+    "X-CUSTOM-header": "value",+}+++def _assert_header_name_case_preserved(received: list[bytes]) -> None:+    assert len(received) == 1+    raw_lines = received[0].split(b"\r\n")+    for name, value in _CASED_REQUEST_HEADERS.items():+        assert f"{name}: {value}".encode() in raw_lines, received[0]+    for name in (b"Accept:", b"Cache-Control:", b"X-Custom-Header:"):+        assert not any(line.startswith(name) for line in raw_lines), received[0]++ class TestHttpBase(ABC):     is_secure: bool = False     http2: bool = False@@ -199,6 +248,18 @@ class TestHttpBase(ABC):             assert header_name in body["headers"]             assert body["headers"][header_name] == [header_value] +    @coroutine_test+    async def test_request_header_name_case(self) -> None:+        """Request header names are sent in the case in which they were set."""+        if self.is_secure or self.http2:+            pytest.skip("Needs plain HTTP/1.1; HTTP/2 header names are lowercase")+        with _raw_http_server() as (url, received):+            request = Request(url + "/", headers=_CASED_REQUEST_HEADERS)+            async with self.get_dh() as download_handler:+                response = await download_handler.download_request(request)+        assert response.status == 200+        _assert_header_name_case_preserved(received)+     @coroutine_test     async def test_request_header_none(self, mockserver: MockServer) -> None:         """Adding a header with None as the value should not send that header."""@@ -1524,6 +1585,23 @@ class TestHttpProxyBase(ABC):         finally:             await dh.close() +    @coroutine_test+    async def test_download_with_proxy_header_name_case(self) -> None:+        """Request header names are sent to an HTTP proxy in the case in which+        they were set."""+        if self.is_secure:+            pytest.skip("Needs a plain HTTP proxy")+        with _raw_http_server() as (proxy_url, received):+            request = Request(+                "http://example.com/",+                headers=_CASED_REQUEST_HEADERS,+                meta={"proxy": proxy_url},+            )+            async with self.get_dh() as download_handler:+                response = await download_handler.download_request(request)+        assert response.status == 200+        _assert_header_name_case_preserved(received)+     @coroutine_test     async def test_download_with_proxy(         self, proxy_mockserver: ProxyEchoMockServer 
Merged fix · PR #7893+66 −16
docs/topics/download-handlers.rstindex ba8bf1174..aa6bef579 100644@@ -140,36 +140,44 @@ individual handler docs for more differences:      - Proxies      - Bad headers      - TLS+     - Header case    * - :class:`Aiohttp <scrapy.core.downloader.handlers._aiohttp.AiohttpDownloadHandler>`      - asyncio      - 1.1      - HTTP      - Fail      - Stdlib ``ssl``+     - As written    * - :class:`H2 <scrapy.core.downloader.handlers.http2.H2DownloadHandler>`      - Reactor, :ref:`twisted-http2 <extras>` extra      - 2      - None      - Not applicable      - ``cryptography``+     - Lowercase    * - :class:`HTTP11 <scrapy.core.downloader.handlers.http11.HTTP11DownloadHandler>`      - Reactor      - 1.1      - HTTP      - Skip bad      - ``cryptography``+     - Capitalized    * - :class:`Httpx <scrapy.core.downloader.handlers._httpx.HttpxDownloadHandler>`      - asyncio, :ref:`httpx <extras>` extra      - 1.1, 2      - HTTP, SOCKS      - Fail      - Stdlib ``ssl``+     - As written  Bad header handling is what a handler does when a response has a bad header line, e.g. one with no colon in it, which some servers send. Handlers that skip bad header lines, like web browsers do, still parse the header lines that follow them; other handlers also lose those, or cannot download such responses at all. +Because HTTP/2 requires lowercase header names, handlers only keep the case of+your header names over HTTP/1.1.+ You can find additional HTTP download handlers in the scrapy-download-handlers-incubator_ package. This package is made by the Scrapy developers and contains experimental handlers that may be included in somedocs/topics/request-response.rstindex 39f8701f1..473aedf63 100644@@ -107,6 +107,13 @@ Request objects         A dictionary-like (:class:`scrapy.http.headers.Headers`) object which contains         the request headers. +        .. versionchanged:: VERSION+            Header names keep the case you write them in, instead of being+            converted to ``Title-Case``.++        Lookups are case-insensitive. Whether your case reaches the server+        depends on the :ref:`download handler <download-handlers-ref>`.+     .. attribute:: Request.body          The request body as bytes.scrapy/core/_http2/stream.pyindex 4e078b1b9..df895554c 100644@@ -247,11 +247,10 @@ class Stream:         content_length = str(len(self._request.body))         headers.append(("Content-Length", content_length)) -        content_length_name = self._request.headers.normkey(b"Content-Length")         for name, values in self._request.headers.items():             for value_bytes in values:                 value = str(value_bytes, "utf-8")-                if name == content_length_name:+                if name.lower() == b"content-length":                     if value != content_length:                         logger.warning(                             "Ignoring bad Content-Length header %r of request %r, "scrapy/core/downloader/handlers/_httpx.pyindex 1d138254c..7b1917bf6 100644@@ -184,7 +184,7 @@ class HttpxDownloadHandler(_Base):      @staticmethod     def _extract_headers(response: httpx.Response) -> Headers:-        return Headers(response.headers.multi_items())+        return Headers(response.headers.raw)      @staticmethod     def _build_base_response_args(scrapy/core/downloader/handlers/ftp.pyindex 3155302d2..05bd0ee5d 100644@@ -126,7 +126,7 @@ class FTPDownloadHandler(BaseDownloadHandler):             protocol.close()             assert client.transport             client.transport.loseConnection()-        headers = {"local filename": protocol.filename or b"", "size": protocol.size}+        headers = {"Local Filename": protocol.filename or b"", "Size": protocol.size}         body = protocol.filename or protocol.body.read()         respcls = responsetypes.from_args(url=request.url, body=body)         return respcls(url=request.url, status=200, body=body, headers=headers)scrapy/http/headers.pyindex fb26d5f18..27063b377 100644@@ -65,13 +65,26 @@ class Headers(dict):  # type: ignore[type-arg]     ) -> None:         items = seq.items() if isinstance(seq, Mapping) else seq         iseq: dict[bytes, list[bytes]] = {}+        # normkey() only sees keys already stored, so keys from seq that differ+        # only in case are mapped to a single spelling here.+        spellings: dict[bytes, bytes] = {}         for k, v in items:-            iseq.setdefault(self.normkey(k), []).extend(self.normvalue(v))+            key = self.normkey(k)+            key = spellings.setdefault(key.lower(), key)+            iseq.setdefault(key, []).extend(self.normvalue(v))         dict.update(self, iseq)      def normkey(self, key: str | bytes) -> bytes:-        """Normalize key to bytes"""-        return self._tobytes(key.title())+        """Normalize key to bytes, matching the case of an existing key if any"""+        key = self._tobytes(key)+        if dict.__contains__(self, key):+            return key+        lower_key = key.lower()+        existing_key: bytes+        for existing_key in dict.keys(self):+            if existing_key.lower() == lower_key:+                return existing_key+        return key      def normvalue(self, value: _RawValue | Iterable[_RawValue]) -> list[bytes]:         """Normalize values to bytes"""tests/test_http2_client_protocol.pyindex 10b13607e..9582b4298 100644@@ -792,7 +792,9 @@ class TestHttps2ClientProtocol:          response_headers = json.loads(str(response.body, "utf-8"))         assert isinstance(response_headers, dict)+        # The server reports header names in its own case.+        received = {k.lower(): v for k, v in response_headers.items()}         for k, v in request.headers.items():-            k_decoded, v_decoded = str(k, "utf-8"), str(v[0], "utf-8")-            assert k_decoded in response_headers-            assert v_decoded == response_headers[k_decoded]+            k_decoded, v_decoded = str(k, "utf-8").lower(), str(v[0], "utf-8")+            assert k_decoded in received+            assert v_decoded == received[k_decoded]tests/test_http_headers.pyindex 514ab2513..a479edfee 100644@@ -67,6 +67,27 @@ def test_encode_multiple() -> None:     assert val[0] == b"\xc2\xa3"  +def test_key_case_kept() -> None:+    h = Headers({"accept": "text/html", "access_token": "foo"})+    assert sorted(h.keys()) == [b"accept", b"access_token"]+++def test_key_case_of_first_spelling_wins() -> None:+    h = Headers({"accept": "a", "Accept": "b"})+    assert h.getlist("ACCEPT") == [b"a", b"b"]+    assert list(h.keys()) == [b"accept"]++    h["ACCEPT"] = "c"+    h.appendlist("aCCept", "d")+    h.update({"ACCEPT": "e"})+    assert list(h.keys()) == [b"accept"]+    assert h.getlist("accept") == [b"e"]++    del h["ACCEPT"]+    h["ACCEPT"] = "f"+    assert list(h.keys()) == [b"ACCEPT"]++ def test_delete_and_contains() -> None:     h = Headers()     h["Content-Type"] = "text/html"tests/test_utils_request.pyindex 6ca608053..08e03970c 100644@@ -39,7 +39,7 @@ if TYPE_CHECKING:                 headers={"Content-type": b"text/html"},                 body=b"Some body",             ),-            b"POST / HTTP/1.1\r\nHost: www.example.com\r\nContent-Type: text/html\r\n\r\nSome body",+            b"POST / HTTP/1.1\r\nHost: www.example.com\r\nContent-type: text/html\r\n\r\nSome body",         ),     ], )tests/utils/bases/download_handlers_http.pyindex 914b8ce80..aad138513 100644@@ -330,12 +330,12 @@ class TestHttpBase(ABC):         async with self.get_dh() as download_handler:             response = await download_handler.download_request(request)         assert response.status == 200-        received_headers = set(response.headers.keys())+        received_headers = {key.lower() for key in response.headers}         allowed_headers = {-            b"Content-Length",-            b"Content-Type",-            b"Date",-            b"Server",+            b"content-length",+            b"content-type",+            b"date",+            b"server",         }         extra_headers = received_headers - allowed_headers         assert not extra_headers, response.headers