From 6240e453b01dd90cd434348eb91c3efbf8cfb297 Mon Sep 17 00:00:00 2001 From: cheney Date: Tue, 26 May 2026 15:22:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BF=E9=97=AE=E6=AD=A3=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/proxy_client.py | 22 ++++++++++++++++------ proxy/proxy_server.py | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/proxy/proxy_client.py b/proxy/proxy_client.py index 1a7e3b9..620a150 100644 --- a/proxy/proxy_client.py +++ b/proxy/proxy_client.py @@ -125,25 +125,33 @@ def handle_request(request_data): target_sock.sendall(modified_data) response = b"" + content_length = 0 + is_chunked = False + while True: chunk = target_sock.recv(8192) if not chunk: break response += chunk - if b"\r\n\r\n" in response: + if b"\r\n\r\n" in response and content_length == 0 and not is_chunked: headers_end = response.find(b"\r\n\r\n") headers = response[:headers_end].decode('utf-8') - content_length = 0 for line in headers.split("\r\n"): if line.lower().startswith("content-length:"): content_length = int(line.split(":", 1)[1].strip()) - break + elif line.lower().startswith("transfer-encoding:"): + if "chunked" in line.lower(): + is_chunked = True - body_start = headers_end + 4 - if len(response) - body_start >= content_length: - break + if content_length > 0: + body_start = headers_end + 4 + if len(response) - body_start >= content_length: + break + elif is_chunked: + if response.endswith(b"0\r\n\r\n"): + break if request_id: if b"\r\n\r\n" in response: @@ -191,7 +199,9 @@ def process_request(b_socket, request_data): with socket_write_lock: try: + log_info("System", "Sending response back to B, length: %d bytes" % len(response)) b_socket.sendall(response) + log_info("System", "Response sent successfully") except Exception as e: log_error("System", "Failed to send response: %s" % str(e)) diff --git a/proxy/proxy_server.py b/proxy/proxy_server.py index d7bdc67..a03a6a0 100644 --- a/proxy/proxy_server.py +++ b/proxy/proxy_server.py @@ -121,9 +121,16 @@ def process_response_from_a(): sock = a_device_conn response = b"" + content_length = 0 + is_chunked = False + request_id = None + headers_found = False + while True: chunk = sock.recv(8192) if not chunk: + if response: + break log_info("System", "Device A connection closed in response processor") with a_device_lock: a_device_conn = None @@ -131,25 +138,39 @@ def process_response_from_a(): return response += chunk - if b"\r\n\r\n" in response: + if b"\r\n\r\n" in response and not headers_found: headers_end = response.find(b"\r\n\r\n") headers = response[:headers_end].decode('utf-8') + headers_found = True - request_id = None for line in headers.split("\r\n"): if line.lower().startswith("x-proxy-request-id:"): request_id = line.split(":", 1)[1].strip() - break - - content_length = 0 - for line in headers.split("\r\n"): - if line.lower().startswith("content-length:"): + elif line.lower().startswith("content-length:"): content_length = int(line.split(":", 1)[1].strip()) - break + elif line.lower().startswith("transfer-encoding:"): + if "chunked" in line.lower(): + is_chunked = True - body_start = headers_end + 4 - if len(response) - body_start >= content_length: + if content_length == 0 and not is_chunked: break + + if content_length > 0: + body_start = headers_end + 4 + if len(response) - body_start >= content_length: + break + elif is_chunked: + if response.endswith(b"0\r\n\r\n"): + break + elif headers_found: + if content_length > 0: + headers_end = response.find(b"\r\n\r\n") + body_start = headers_end + 4 + if len(response) - body_start >= content_length: + break + elif is_chunked: + if response.endswith(b"0\r\n\r\n"): + break if request_id: with request_queue_lock: