mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.9] gh-100001: Omit control characters in http.server stderr logs. (GH-100002) (#100032)
* 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 d8ab0a4dfa
)
Co-authored-by: Gregory P. Smith <greg@krypto.org>
* also escape \s (backport of PR #100038).
* add versionadded and remove extra 'to'
Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
parent
7b98207aa4
commit
3b81c13ac3
4 changed files with 46 additions and 2 deletions
|
@ -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,11 @@ 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))})
|
||||
_control_char_table[ord('\\')] = r'\\'
|
||||
|
||||
def log_message(self, format, *args):
|
||||
"""Log an arbitrary message.
|
||||
|
||||
|
@ -578,12 +584,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."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue