资源类请求成功
This commit is contained in:
+33
-26
@@ -98,9 +98,14 @@ def modify_request_headers(request_data):
|
||||
|
||||
def handle_request(request_data):
|
||||
"""Handle a single request from device B - completely independent thread"""
|
||||
request_id = extract_request_id(request_data)
|
||||
if isinstance(request_data, bytes):
|
||||
request_data_str = request_data.decode('utf-8')
|
||||
else:
|
||||
request_data_str = request_data
|
||||
|
||||
modified_data, method, path = modify_request_headers(request_data)
|
||||
request_id = extract_request_id(request_data_str)
|
||||
|
||||
modified_data, method, path = modify_request_headers(request_data_str)
|
||||
|
||||
body_size = 0
|
||||
if "\r\n\r\n" in modified_data:
|
||||
@@ -115,18 +120,20 @@ def handle_request(request_data):
|
||||
target_sock.connect((TARGET_HOST, TARGET_PORT))
|
||||
log_info("System", "Connected to target: %s:%d" % (TARGET_HOST, TARGET_PORT))
|
||||
|
||||
if isinstance(modified_data, str):
|
||||
modified_data = modified_data.encode('utf-8')
|
||||
target_sock.sendall(modified_data)
|
||||
|
||||
response = ""
|
||||
response = b""
|
||||
while True:
|
||||
chunk = target_sock.recv(8192)
|
||||
if not chunk:
|
||||
break
|
||||
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')
|
||||
|
||||
content_length = 0
|
||||
for line in headers.split("\r\n"):
|
||||
@@ -139,16 +146,16 @@ def handle_request(request_data):
|
||||
break
|
||||
|
||||
if request_id:
|
||||
if "\r\n\r\n" in response:
|
||||
headers_end = response.find("\r\n\r\n")
|
||||
status_line = response[:headers_end].split("\r\n")[0]
|
||||
rest = response[headers_end:]
|
||||
response = status_line + "\r\nX-Proxy-Request-ID: " + request_id + rest
|
||||
if b"\r\n\r\n" in response:
|
||||
headers_end = response.find(b"\r\n\r\n")
|
||||
headers = response[:headers_end]
|
||||
body = response[headers_end:]
|
||||
response = headers + ("\r\nX-Proxy-Request-ID: " + request_id).encode('utf-8') + body
|
||||
|
||||
status_code = "500"
|
||||
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')
|
||||
for line in headers.split("\r\n"):
|
||||
if line.startswith("HTTP/"):
|
||||
status_code = line.split()[1]
|
||||
@@ -161,19 +168,19 @@ def handle_request(request_data):
|
||||
except socket.error as e:
|
||||
if e.errno == 111 or e.errno == 10061:
|
||||
log_error("System", "Cannot connect to target: %s:%d" % (TARGET_HOST, TARGET_PORT))
|
||||
error_response = "HTTP/1.1 502 Bad Gateway\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nCannot connect to target: %s:%d\r\n" % (TARGET_HOST, TARGET_PORT)
|
||||
error_response = ("HTTP/1.1 502 Bad Gateway\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nCannot connect to target: %s:%d\r\n" % (TARGET_HOST, TARGET_PORT)).encode('utf-8')
|
||||
return error_response
|
||||
else:
|
||||
log_error("System", "Socket error: %s" % str(e))
|
||||
error_response = "HTTP/1.1 502 Bad Gateway\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nSocket error\r\n"
|
||||
error_response = ("HTTP/1.1 502 Bad Gateway\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nSocket error\r\n").encode('utf-8')
|
||||
return error_response
|
||||
except socket.timeout:
|
||||
log_error("System", "Target connection timeout")
|
||||
error_response = "HTTP/1.1 504 Gateway Timeout\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nConnection timeout\r\n"
|
||||
error_response = ("HTTP/1.1 504 Gateway Timeout\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nConnection timeout\r\n").encode('utf-8')
|
||||
return error_response
|
||||
except Exception as e:
|
||||
log_error("System", "Error handling request: %s" % str(e))
|
||||
error_response = "HTTP/1.1 500 Internal Server Error\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nInternal error\r\n"
|
||||
error_response = ("HTTP/1.1 500 Internal Server Error\r\nX-Proxy-Request-ID: " + (request_id if request_id else "") + "\r\nContent-Type: text/plain\r\n\r\nInternal error\r\n").encode('utf-8')
|
||||
return error_response
|
||||
finally:
|
||||
target_sock.close()
|
||||
@@ -194,16 +201,16 @@ def listen_for_requests(sock):
|
||||
|
||||
while True:
|
||||
try:
|
||||
data = ""
|
||||
data = b""
|
||||
while True:
|
||||
chunk = sock.recv(8192)
|
||||
if not chunk:
|
||||
log_info("System", "Connection closed by device B")
|
||||
return
|
||||
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"):
|
||||
@@ -220,8 +227,8 @@ def listen_for_requests(sock):
|
||||
t.start()
|
||||
|
||||
except socket.timeout:
|
||||
log_info("System", "Connection idle timeout")
|
||||
return
|
||||
log_info("System", "Connection idle timeout, continuing to wait")
|
||||
continue
|
||||
except Exception as e:
|
||||
log_error("System", "Connection error: %s" % str(e))
|
||||
return
|
||||
@@ -239,14 +246,14 @@ def connect_to_b_device(b_host, b_port):
|
||||
response = sock.recv(1024)
|
||||
log_info("System", "Received from B: %r" % response)
|
||||
|
||||
if not response or "PROXY_CONNECTED" not in response:
|
||||
if not response or b"PROXY_CONNECTED" not in response:
|
||||
log_error("System", "Failed to receive connection confirmation")
|
||||
sock.close()
|
||||
time.sleep(3)
|
||||
continue
|
||||
|
||||
log_info("System", "Sending READY to device B")
|
||||
sock.sendall("READY\r\n")
|
||||
sock.sendall(b"READY\r\n")
|
||||
log_info("System", "Successfully connected to device B: %s:%d" % (b_host, b_port))
|
||||
|
||||
listen_for_requests(sock)
|
||||
|
||||
Reference in New Issue
Block a user