访问正常
This commit is contained in:
parent
a7172bd3aa
commit
6240e453b0
@ -125,25 +125,33 @@ def handle_request(request_data):
|
|||||||
target_sock.sendall(modified_data)
|
target_sock.sendall(modified_data)
|
||||||
|
|
||||||
response = b""
|
response = b""
|
||||||
|
content_length = 0
|
||||||
|
is_chunked = False
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
chunk = target_sock.recv(8192)
|
chunk = target_sock.recv(8192)
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
response += chunk
|
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_end = response.find(b"\r\n\r\n")
|
||||||
headers = response[:headers_end].decode('utf-8')
|
headers = response[:headers_end].decode('utf-8')
|
||||||
|
|
||||||
content_length = 0
|
|
||||||
for line in headers.split("\r\n"):
|
for line in headers.split("\r\n"):
|
||||||
if line.lower().startswith("content-length:"):
|
if line.lower().startswith("content-length:"):
|
||||||
content_length = int(line.split(":", 1)[1].strip())
|
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 content_length > 0:
|
||||||
if len(response) - body_start >= content_length:
|
body_start = headers_end + 4
|
||||||
break
|
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 request_id:
|
||||||
if b"\r\n\r\n" in response:
|
if b"\r\n\r\n" in response:
|
||||||
@ -191,7 +199,9 @@ def process_request(b_socket, request_data):
|
|||||||
|
|
||||||
with socket_write_lock:
|
with socket_write_lock:
|
||||||
try:
|
try:
|
||||||
|
log_info("System", "Sending response back to B, length: %d bytes" % len(response))
|
||||||
b_socket.sendall(response)
|
b_socket.sendall(response)
|
||||||
|
log_info("System", "Response sent successfully")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_error("System", "Failed to send response: %s" % str(e))
|
log_error("System", "Failed to send response: %s" % str(e))
|
||||||
|
|
||||||
|
|||||||
@ -121,9 +121,16 @@ def process_response_from_a():
|
|||||||
sock = a_device_conn
|
sock = a_device_conn
|
||||||
|
|
||||||
response = b""
|
response = b""
|
||||||
|
content_length = 0
|
||||||
|
is_chunked = False
|
||||||
|
request_id = None
|
||||||
|
headers_found = False
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
chunk = sock.recv(8192)
|
chunk = sock.recv(8192)
|
||||||
if not chunk:
|
if not chunk:
|
||||||
|
if response:
|
||||||
|
break
|
||||||
log_info("System", "Device A connection closed in response processor")
|
log_info("System", "Device A connection closed in response processor")
|
||||||
with a_device_lock:
|
with a_device_lock:
|
||||||
a_device_conn = None
|
a_device_conn = None
|
||||||
@ -131,25 +138,39 @@ def process_response_from_a():
|
|||||||
return
|
return
|
||||||
response += chunk
|
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_end = response.find(b"\r\n\r\n")
|
||||||
headers = response[:headers_end].decode('utf-8')
|
headers = response[:headers_end].decode('utf-8')
|
||||||
|
headers_found = True
|
||||||
|
|
||||||
request_id = None
|
|
||||||
for line in headers.split("\r\n"):
|
for line in headers.split("\r\n"):
|
||||||
if line.lower().startswith("x-proxy-request-id:"):
|
if line.lower().startswith("x-proxy-request-id:"):
|
||||||
request_id = line.split(":", 1)[1].strip()
|
request_id = line.split(":", 1)[1].strip()
|
||||||
break
|
elif line.lower().startswith("content-length:"):
|
||||||
|
|
||||||
content_length = 0
|
|
||||||
for line in headers.split("\r\n"):
|
|
||||||
if line.lower().startswith("content-length:"):
|
|
||||||
content_length = int(line.split(":", 1)[1].strip())
|
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 content_length == 0 and not is_chunked:
|
||||||
if len(response) - body_start >= content_length:
|
|
||||||
break
|
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:
|
if request_id:
|
||||||
with request_queue_lock:
|
with request_queue_lock:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user