Commit dba0a4c
authored
Fix the wrong Content-Length in python-server.py for non-ascii characters. (#24480)
Resolves: #24479
`python-server.py` currently uses `sys.stdin.read` for reading the
input, and it receives the length in `str` (utf-8 string).
ref: https://docs.python.org/3/library/sys.html
On the other "Content-Length" is the size in **bytes**, therefore we
should not pass `content_length` to `sys.stdin.read`. For example,
`print("こんにちは世界")`'s length is 16 in str, but 30 in bytes.
```
>>> len('print("こんにちは世界")')
16
>>> len('print("こんにちは世界")'.encode())
30
```
This PR have two changes.
1. Replace `sys.stdin.read(content_length)` with
`sys.stdin.buffer.read(content_length).decode()`.
2. Make `_send_message` calculate "Content-Length" from bytes, not str.
By these changes, original issue
#24479 can be resolved.
1 parent 42b63b9 commit dba0a4c
1 file changed
+9
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| 59 | + | |
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
61 | | - | |
| 63 | + | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
| |||
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| 79 | + | |
77 | 80 | | |
78 | 81 | | |
79 | 82 | | |
80 | | - | |
| 83 | + | |
81 | 84 | | |
82 | 85 | | |
83 | 86 | | |
| |||
160 | 163 | | |
161 | 164 | | |
162 | 165 | | |
163 | | - | |
| 166 | + | |
164 | 167 | | |
165 | 168 | | |
166 | 169 | | |
| |||
172 | 175 | | |
173 | 176 | | |
174 | 177 | | |
| 178 | + | |
175 | 179 | | |
176 | 180 | | |
177 | 181 | | |
178 | | - | |
| 182 | + | |
179 | 183 | | |
180 | 184 | | |
181 | 185 | | |
| |||
0 commit comments