From f03ba0ad52e6f1deb42f022090f226573b22f630 Mon Sep 17 00:00:00 2001 From: Illia Volochii Date: Mon, 2 Aug 2021 10:24:48 +0300 Subject: [PATCH] Simplified serializing HTTP response headers. Since ResponseHeaders was introduced, header names and values are stored as strings. There is no need to check whether they are bytes. Co-authored-by: Nick Pope --- django/http/response.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/django/http/response.py b/django/http/response.py index 901501dad1..929129342f 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -153,14 +153,10 @@ class HttpResponseBase: def serialize_headers(self): """HTTP headers as a bytestring.""" - def to_bytes(val, encoding): - return val if isinstance(val, bytes) else val.encode(encoding) - - headers = [ - (to_bytes(key, 'ascii') + b': ' + to_bytes(value, 'latin-1')) + return b'\r\n'.join([ + key.encode('ascii') + b': ' + value.encode('latin-1') for key, value in self.headers.items() - ] - return b'\r\n'.join(headers) + ]) __bytes__ = serialize_headers