diff --git a/cheroot/server.py b/cheroot/server.py index 89793309aa..dc4f2b5dfb 100644 --- a/cheroot/server.py +++ b/cheroot/server.py @@ -253,9 +253,11 @@ def __call__(self, rfile, hdict=None): # noqa: C901 # FIXME if not self._allow_header(k): continue - if k in comma_separated_headers: - existing = hdict.get(hname) - if existing: + existing = hdict.get(hname) + if existing: + if line[:1] in {SPACE, TAB}: + v = SPACE.join((existing, v)) + elif k in comma_separated_headers: v = b', '.join((existing, v)) hdict[hname] = v diff --git a/cheroot/test/test_header_folding.py b/cheroot/test/test_header_folding.py new file mode 100644 index 0000000000..4d4726299e --- /dev/null +++ b/cheroot/test/test_header_folding.py @@ -0,0 +1,83 @@ +"""Preserve HTTP header values across continuation lines.""" + +from __future__ import annotations + +import socket +from http import HTTPStatus +from http.client import HTTPResponse + +import pytest + +from cheroot.server import Gateway, HTTPServer +from cheroot.test import webtest + + +_CUSTOM_HEADER = b'X-Test' + + +class _HeaderGateway(Gateway): + """Echo the header selected by the request path.""" + + def respond(self) -> None: + """Return the complete header value received by the application.""" + header_value = self.req.inheaders[self.req.path[1:]] + self.req.status = b'200 OK' + self.req.outheaders = [ + (b'Content-Length', str(len(header_value)).encode('ascii')), + ] + self.req.ensure_headers_sent() + self.req.write(header_value) + + +@pytest.mark.parametrize( + ('header_name', 'header_lines', 'expected_value'), + ( + (_CUSTOM_HEADER, b'X-Test: a\r\n b\r\n\tc', b'a b c'), + (b'Accept', b'Accept: a\r\n b\r\n\tc', b'a b c'), + (_CUSTOM_HEADER, b'X-Test: a\r\n\t b', b'a b'), + ( + b'Accept', + b'Accept: a\r\n b\r\nAccept: c\r\n d', + b'a b, c d', + ), + (_CUSTOM_HEADER, b'X-Test: a\r\n b\r\nX-Test: c\r\n d', b'c d'), + (_CUSTOM_HEADER, b'X-Test:\r\n b', b'b'), + (b'Accept', b'Accept: a\r\nAccept: b', b'a, b'), + (_CUSTOM_HEADER, b'X-Test: a', b'a'), + ), +) +def test_header_continuation_values( + native_server: HTTPServer, + http_request_timeout: float, + header_name: bytes, + header_lines: bytes, + expected_value: bytes, +) -> None: + """Preserve continuation values and repeated header fields. + + :param native_server: live HTTP server + :param http_request_timeout: socket timeout in seconds + :param header_name: field to echo through the application + :param header_lines: header bytes including any continuation lines + :param expected_value: value required by the HTTP field rules + """ + native_server.gateway = _HeaderGateway + host, port = native_server.bind_addr + request = b''.join( + ( + b'GET /', + header_name, + b' HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n', + header_lines, + b'\r\n\r\n', + ), + ) + with socket.create_connection( + (webtest.interface(host), port), + timeout=http_request_timeout, + ) as connection: + connection.sendall(request) + with HTTPResponse(connection) as response: + response.begin() + assert response.status == HTTPStatus.OK + assert response.read() == expected_value diff --git a/docs/changelog-fragments.d/726.bugfix.rst b/docs/changelog-fragments.d/726.bugfix.rst new file mode 100644 index 0000000000..b95790af2b --- /dev/null +++ b/docs/changelog-fragments.d/726.bugfix.rst @@ -0,0 +1,3 @@ +Preserved all values in HTTP header continuation lines, joining them with +spaces instead of discarding earlier values or treating continuation lines +as separate comma-delimited fields (:issue:`726`).