Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the wrong Content-Length in python-server.py for non-ascii characters. #24480

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 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 @@ -160,7 +163,7 @@ def get_value(self) -> str:
def get_headers():
headers = {}
while True:
line = STDIN.readline().strip()
line = STDIN.buffer.readline().decode().strip()
if not line:
break
name, value = line.split(":", 1)
Expand All @@ -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