Skip to content

Commit

Permalink
Fix the wrong Content-Length in python-server.py for non-ascii charac…
Browse files Browse the repository at this point in the history
…ters.

Content-Length is the data in bytes, not len of str. We should use sys.stdin.buffer.read instead of sys.stdin.read to receive bytes.

_send_message should calculate "Content-Length" from bytes, not str.
  • Loading branch information
tomoki authored and karthiknadig committed Nov 26, 2024
1 parent 5cec0e0 commit b190169
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions python_files/python_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@


def _send_message(msg: str):
length_msg = len(msg)
# Content-Length is the data size in bytes.
length_msg = len(msg.encode())
STDOUT.buffer.write(f"Content-Length: {length_msg}\r\n\r\n{msg}".encode())
STDOUT.buffer.flush()

Expand Down Expand Up @@ -55,10 +56,11 @@ def custom_input(prompt=""):
try:
send_request({"prompt": prompt})
headers = get_headers()
# Content-Length is the data size in bytes.
content_length = int(headers.get("Content-Length", 0))

if content_length:
message_text = STDIN.read(content_length)
message_text = STDIN.buffer.read(content_length).decode()
message_json = json.loads(message_text)
return message_json["result"]["userInput"]
except Exception:
Expand All @@ -74,10 +76,11 @@ def handle_response(request_id):
while not STDIN.closed:
try:
headers = get_headers()
# Content-Length is the data size in bytes.
content_length = int(headers.get("Content-Length", 0))

if content_length:
message_text = STDIN.read(content_length)
message_text = STDIN.buffer.read(content_length).decode()
message_json = json.loads(message_text)
our_user_input = message_json["result"]["userInput"]
if message_json["id"] == request_id:
Expand Down Expand Up @@ -172,10 +175,11 @@ def get_headers():
while not STDIN.closed:
try:
headers = get_headers()
# Content-Length is the data size in bytes.
content_length = int(headers.get("Content-Length", 0))

if content_length:
request_text = STDIN.read(content_length)
request_text = STDIN.buffer.read(content_length).decode()
request_json = json.loads(request_text)
if request_json["method"] == "execute":
execute(request_json, USER_GLOBALS)
Expand Down

0 comments on commit b190169

Please sign in to comment.