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 <nick@nickpope.me.uk>
This commit is contained in:
Illia Volochii 2021-08-02 10:24:48 +03:00 committed by GitHub
parent 947bdec60c
commit f03ba0ad52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 7 deletions

View File

@ -153,14 +153,10 @@ class HttpResponseBase:
def serialize_headers(self): def serialize_headers(self):
"""HTTP headers as a bytestring.""" """HTTP headers as a bytestring."""
def to_bytes(val, encoding): return b'\r\n'.join([
return val if isinstance(val, bytes) else val.encode(encoding) key.encode('ascii') + b': ' + value.encode('latin-1')
headers = [
(to_bytes(key, 'ascii') + b': ' + to_bytes(value, 'latin-1'))
for key, value in self.headers.items() for key, value in self.headers.items()
] ])
return b'\r\n'.join(headers)
__bytes__ = serialize_headers __bytes__ = serialize_headers