资源类请求成功
This commit is contained in:
+26
-20
@@ -60,7 +60,7 @@ def handle_a_device(conn, addr):
|
||||
|
||||
with a_device_lock:
|
||||
if a_device_conn:
|
||||
conn.send("ERROR: Another A device is already connected\r\n")
|
||||
conn.send(b"ERROR: Another A device is already connected\r\n")
|
||||
conn.close()
|
||||
log_info("DeviceA", "Rejected connection from %s:%d" % addr)
|
||||
return
|
||||
@@ -68,7 +68,7 @@ def handle_a_device(conn, addr):
|
||||
a_device_conn = conn
|
||||
|
||||
log_info("DeviceA", "Sending PROXY_CONNECTED to %s:%d" % addr)
|
||||
conn.send("PROXY_CONNECTED\r\n")
|
||||
conn.send(b"PROXY_CONNECTED\r\n")
|
||||
|
||||
try:
|
||||
conn.settimeout(30)
|
||||
@@ -76,7 +76,7 @@ def handle_a_device(conn, addr):
|
||||
response = conn.recv(1024)
|
||||
log_info("DeviceA", "Received from %s:%d: %r" % (addr[0], addr[1], response))
|
||||
|
||||
if response and "READY" in response:
|
||||
if response and b"READY" in response:
|
||||
a_device_connected.set()
|
||||
log_info("DeviceA", "Connected: %s:%d" % addr)
|
||||
else:
|
||||
@@ -120,7 +120,7 @@ def process_response_from_a():
|
||||
|
||||
sock = a_device_conn
|
||||
|
||||
response = ""
|
||||
response = b""
|
||||
while True:
|
||||
chunk = sock.recv(8192)
|
||||
if not chunk:
|
||||
@@ -131,9 +131,9 @@ def process_response_from_a():
|
||||
return
|
||||
response += chunk
|
||||
|
||||
if "\r\n\r\n" in response:
|
||||
headers_end = response.find("\r\n\r\n")
|
||||
headers = response[:headers_end]
|
||||
if b"\r\n\r\n" in response:
|
||||
headers_end = response.find(b"\r\n\r\n")
|
||||
headers = response[:headers_end].decode('utf-8')
|
||||
|
||||
request_id = None
|
||||
for line in headers.split("\r\n"):
|
||||
@@ -172,13 +172,15 @@ def handle_browser_request(browser_conn, browser_addr):
|
||||
try:
|
||||
browser_conn.settimeout(30)
|
||||
|
||||
data = ""
|
||||
data = b""
|
||||
while True:
|
||||
chunk = browser_conn.recv(8192)
|
||||
if not chunk:
|
||||
break
|
||||
data += chunk
|
||||
if "\r\n\r\n" in data:
|
||||
headers_end = data.find("\r\n\r\n")
|
||||
headers = data[:headers_end]
|
||||
if b"\r\n\r\n" in data:
|
||||
headers_end = data.find(b"\r\n\r\n")
|
||||
headers = data[:headers_end].decode('utf-8')
|
||||
|
||||
content_length = 0
|
||||
for line in headers.split("\r\n"):
|
||||
@@ -189,13 +191,11 @@ def handle_browser_request(browser_conn, browser_addr):
|
||||
body_start = headers_end + 4
|
||||
if len(data) - body_start >= content_length:
|
||||
break
|
||||
elif chunk == "":
|
||||
break
|
||||
|
||||
if not data:
|
||||
return
|
||||
|
||||
request_lines = data.split("\r\n")
|
||||
request_lines = data.decode('utf-8').split("\r\n")
|
||||
if request_lines:
|
||||
first_line = request_lines[0]
|
||||
parts = first_line.split()
|
||||
@@ -203,14 +203,14 @@ def handle_browser_request(browser_conn, browser_addr):
|
||||
method = parts[0]
|
||||
path = parts[1]
|
||||
|
||||
body_size = len(data) - data.find("\r\n\r\n") - 4 if "\r\n\r\n" in data else 0
|
||||
body_size = len(data) - data.find(b"\r\n\r\n") - 4 if b"\r\n\r\n" in data else 0
|
||||
|
||||
request_id = get_request_id()
|
||||
log_info("Browser", "Request: %s %s - Body: %d bytes - ID: %s" % (method, path, body_size, request_id))
|
||||
|
||||
with a_device_lock:
|
||||
if not a_device_conn:
|
||||
error_response = "HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\n\r\nDevice A not connected\r\n"
|
||||
error_response = b"HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\n\r\nDevice A not connected\r\n"
|
||||
browser_conn.sendall(error_response)
|
||||
log_error("Browser", "Device A not connected for request %s" % request_id)
|
||||
return
|
||||
@@ -221,7 +221,11 @@ def handle_browser_request(browser_conn, browser_addr):
|
||||
'timestamp': time.time()
|
||||
}
|
||||
|
||||
modified_data = "X-Proxy-Request-ID: " + request_id + "\r\n" + data
|
||||
if isinstance(data, bytes):
|
||||
modified_data = ("X-Proxy-Request-ID: " + request_id + "\r\n").encode('utf-8') + data
|
||||
else:
|
||||
modified_data = "X-Proxy-Request-ID: " + request_id + "\r\n" + data
|
||||
modified_data = modified_data.encode('utf-8')
|
||||
|
||||
with a_device_lock:
|
||||
try:
|
||||
@@ -232,7 +236,7 @@ def handle_browser_request(browser_conn, browser_addr):
|
||||
with request_queue_lock:
|
||||
if request_id in request_queue:
|
||||
del request_queue[request_id]
|
||||
error_response = "HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\n\r\nFailed to forward request to Device A\r\n"
|
||||
error_response = b"HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\n\r\nFailed to forward request to Device A\r\n"
|
||||
browser_conn.sendall(error_response)
|
||||
return
|
||||
|
||||
@@ -251,6 +255,8 @@ def handle_browser_request(browser_conn, browser_addr):
|
||||
time.sleep(0.1)
|
||||
|
||||
if response:
|
||||
if isinstance(response, str):
|
||||
response = response.encode('utf-8')
|
||||
browser_conn.sendall(response)
|
||||
log_info("Browser", "Response sent for request %s" % request_id)
|
||||
else:
|
||||
@@ -258,12 +264,12 @@ def handle_browser_request(browser_conn, browser_addr):
|
||||
if request_id in request_queue:
|
||||
del request_queue[request_id]
|
||||
log_error("Browser", "Response timeout for request %s" % request_id)
|
||||
error_response = "HTTP/1.1 504 Gateway Timeout\r\nContent-Type: text/plain\r\n\r\nResponse timeout\r\n"
|
||||
error_response = b"HTTP/1.1 504 Gateway Timeout\r\nContent-Type: text/plain\r\n\r\nResponse timeout\r\n"
|
||||
browser_conn.sendall(error_response)
|
||||
|
||||
except socket.timeout:
|
||||
log_error("Browser", "Request timeout from %s:%d" % browser_addr)
|
||||
error_response = "HTTP/1.1 504 Gateway Timeout\r\nContent-Type: text/plain\r\n\r\nRequest timeout\r\n"
|
||||
error_response = b"HTTP/1.1 504 Gateway Timeout\r\nContent-Type: text/plain\r\n\r\nRequest timeout\r\n"
|
||||
browser_conn.sendall(error_response)
|
||||
except Exception as e:
|
||||
log_error("Browser", "Error handling request: %s" % str(e))
|
||||
|
||||
Reference in New Issue
Block a user