From bab7a7adb91fc0a080b81abf2c4d559b9e8a5fd5 Mon Sep 17 00:00:00 2001 From: Bert JW Regeer Date: Thu, 21 Apr 2022 13:41:30 -0600 Subject: [PATCH] Always encode the responses to utf-8 and set Content-Type --- src/waitress/utilities.py | 4 ++-- tests/test_functional.py | 10 +++++----- tests/test_task.py | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/waitress/utilities.py b/src/waitress/utilities.py index 6ae47420..a9d33618 100644 --- a/src/waitress/utilities.py +++ b/src/waitress/utilities.py @@ -262,8 +262,8 @@ def to_response(self): status = "%s %s" % (self.code, self.reason) body = "%s\r\n\r\n%s" % (self.reason, self.body) tag = "\r\n\r\n(generated by waitress)" - body = body + tag - headers = [("Content-Type", "text/plain")] + body = (body + tag).encode("utf-8") + headers = [("Content-Type", "text/plain; charset=utf-8")] return status, headers, body diff --git a/tests/test_functional.py b/tests/test_functional.py index 212e247b..2ac8d11e 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -359,7 +359,7 @@ def test_broken_chunked_encoding(self): sorted(headers.keys()), ["connection", "content-length", "content-type", "date", "server"], ) - self.assertEqual(headers["content-type"], "text/plain") + self.assertEqual(headers["content-type"], "text/plain; charset=utf-8") # connection has been closed self.send_check_error(to_send) self.assertRaises(ConnectionClosed, read_http, fp) @@ -381,7 +381,7 @@ def test_broken_chunked_encoding_invalid_hex(self): sorted(headers.keys()), ["connection", "content-length", "content-type", "date", "server"], ) - self.assertEqual(headers["content-type"], "text/plain") + self.assertEqual(headers["content-type"], "text/plain; charset=utf-8") # connection has been closed self.send_check_error(to_send) self.assertRaises(ConnectionClosed, read_http, fp) @@ -403,7 +403,7 @@ def test_broken_chunked_encoding_invalid_extension(self): sorted(headers.keys()), ["connection", "content-length", "content-type", "date", "server"], ) - self.assertEqual(headers["content-type"], "text/plain") + self.assertEqual(headers["content-type"], "text/plain; charset=utf-8") # connection has been closed self.send_check_error(to_send) self.assertRaises(ConnectionClosed, read_http, fp) @@ -428,7 +428,7 @@ def test_broken_chunked_encoding_missing_chunk_end(self): sorted(headers.keys()), ["connection", "content-length", "content-type", "date", "server"], ) - self.assertEqual(headers["content-type"], "text/plain") + self.assertEqual(headers["content-type"], "text/plain; charset=utf-8") # connection has been closed self.send_check_error(to_send) self.assertRaises(ConnectionClosed, read_http, fp) @@ -1121,7 +1121,7 @@ def test_request_body_too_large_chunked_encoding(self): self.assertline(line, "413", "Request Entity Too Large", "HTTP/1.1") cl = int(headers["content-length"]) self.assertEqual(cl, len(response_body)) - self.assertEqual(headers["content-type"], "text/plain") + self.assertEqual(headers["content-type"], "text/plain; charset=utf-8") # connection has been closed self.send_check_error(to_send) self.assertRaises(ConnectionClosed, read_http, fp) diff --git a/tests/test_task.py b/tests/test_task.py index cc579b03..47868e15 100644 --- a/tests/test_task.py +++ b/tests/test_task.py @@ -869,7 +869,7 @@ def test_execute_http_10(self): self.assertEqual(lines[0], b"HTTP/1.0 432 Too Ugly") self.assertEqual(lines[1], b"Connection: close") self.assertEqual(lines[2], b"Content-Length: 43") - self.assertEqual(lines[3], b"Content-Type: text/plain") + self.assertEqual(lines[3], b"Content-Type: text/plain; charset=utf-8") self.assertTrue(lines[4]) self.assertEqual(lines[5], b"Server: waitress") self.assertEqual(lines[6], b"Too Ugly") @@ -885,7 +885,7 @@ def test_execute_http_11(self): self.assertEqual(lines[0], b"HTTP/1.1 432 Too Ugly") self.assertEqual(lines[1], b"Connection: close") self.assertEqual(lines[2], b"Content-Length: 43") - self.assertEqual(lines[3], b"Content-Type: text/plain") + self.assertEqual(lines[3], b"Content-Type: text/plain; charset=utf-8") self.assertTrue(lines[4]) self.assertEqual(lines[5], b"Server: waitress") self.assertEqual(lines[6], b"Too Ugly") @@ -902,7 +902,7 @@ def test_execute_http_11_close(self): self.assertEqual(lines[0], b"HTTP/1.1 432 Too Ugly") self.assertEqual(lines[1], b"Connection: close") self.assertEqual(lines[2], b"Content-Length: 43") - self.assertEqual(lines[3], b"Content-Type: text/plain") + self.assertEqual(lines[3], b"Content-Type: text/plain; charset=utf-8") self.assertTrue(lines[4]) self.assertEqual(lines[5], b"Server: waitress") self.assertEqual(lines[6], b"Too Ugly") @@ -919,7 +919,7 @@ def test_execute_http_11_keep_forces_close(self): self.assertEqual(lines[0], b"HTTP/1.1 432 Too Ugly") self.assertEqual(lines[1], b"Connection: close") self.assertEqual(lines[2], b"Content-Length: 43") - self.assertEqual(lines[3], b"Content-Type: text/plain") + self.assertEqual(lines[3], b"Content-Type: text/plain; charset=utf-8") self.assertTrue(lines[4]) self.assertEqual(lines[5], b"Server: waitress") self.assertEqual(lines[6], b"Too Ugly")