Skip to content

Commit fcb3495

Browse files
authored
Fix echo-content-escaped.py for real (#26655)
See #26645 (comment)
1 parent da46f68 commit fcb3495

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

FileAPI/file/resources/echo-content-escaped.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
from six import PY3
12
from wptserve.utils import isomorphic_encode
23

34
# Outputs the request body, with controls and non-ASCII bytes escaped
45
# (b"\n" becomes b"\\x0a"), and with backslashes doubled.
56
# As a convenience, CRLF newlines are left as is.
67

78
def escape_byte(byte):
8-
# Iterating over a binary string gives different types in Py2 & Py3.
9-
# Py3: bytes -> int
10-
# Py2: str -> str (of length 1), so we convert it to int
11-
code = byte if type(byte) is int else ord(byte)
12-
if 0 <= code <= 0x1F or code >= 0x7F:
13-
return b"\\x%02x" % code
14-
if code == ord(b"\\"):
9+
# We want a single-char binary string, but in Python 3 we may get an int
10+
# from iterating over bytes.
11+
if PY3:
12+
byte = bytes([byte])
13+
if b"\0" <= byte <= b"\x1F" or byte >= b"\x7F":
14+
return b"\\x%02x" % ord(byte)
15+
if byte == b"\\":
1516
return b"\\\\"
1617
return byte
1718

0 commit comments

Comments
 (0)