summaryrefslogtreecommitdiff
path: root/Lib/http/server.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-12-05 13:16:14 -0800
committerGitHub <noreply@github.com>2022-12-05 13:16:14 -0800
commitec8c06bc28b29b62d31b953e54f1d8d8535faa80 (patch)
tree96eb04b0a2930f4431d2bdeb60c94c81ccc17bc8 /Lib/http/server.py
parente5075986a7bbc4b1f6b1b3fc85f18501d3b48ec4 (diff)
downloadcpython-git-ec8c06bc28b29b62d31b953e54f1d8d8535faa80.tar.gz
gh-100001: Omit control characters in http.server stderr logs. (GH-100002)
Replace control characters in http.server.BaseHTTPRequestHandler.log_message with an escaped \xHH sequence to avoid causing problems for the terminal the output is printed to. (cherry picked from commit d8ab0a4dfa48f881b4ac9ab857d2e9de42f72828) Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/http/server.py')
-rw-r--r--Lib/http/server.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py
index e8517a7e44..ca429428fd 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -93,6 +93,7 @@ import email.utils
import html
import http.client
import io
+import itertools
import mimetypes
import os
import posixpath
@@ -563,6 +564,10 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
self.log_message(format, *args)
+ # https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes
+ _control_char_table = str.maketrans(
+ {c: fr'\x{c:02x}' for c in itertools.chain(range(0x20), range(0x7f,0xa0))})
+
def log_message(self, format, *args):
"""Log an arbitrary message.
@@ -578,12 +583,16 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
The client ip and current date/time are prefixed to
every message.
+ Unicode control characters are replaced with escaped hex
+ before writing the output to stderr.
+
"""
+ message = format % args
sys.stderr.write("%s - - [%s] %s\n" %
(self.address_string(),
self.log_date_time_string(),
- format%args))
+ message.translate(self._control_char_table)))
def version_string(self):
"""Return the server software version string."""