MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / _proxy_connect_then_http

Function _proxy_connect_then_http

e2e/python/test_sandbox_policy.py:96–174  ·  view source on GitHub ↗

Return a closure that CONNECTs, does TLS + HTTP, returns JSON string.

()

Source from the content-addressed store, hash-verified

94
95
96def _proxy_connect_then_http():
97 """Return a closure that CONNECTs, does TLS + HTTP, returns JSON string."""
98
99 def fn(host, port, method="GET", path="/"):
100 import json as _json
101 import socket
102 import ssl
103
104 conn = socket.create_connection(("10.200.0.1", 3128), timeout=30)
105 try:
106 conn.sendall(
107 f"CONNECT {host}:{port} HTTP/1.1\r\nHost: {host}\r\n\r\n".encode()
108 )
109 connect_resp = conn.recv(256).decode("latin1")
110 if "200" not in connect_resp:
111 return _json.dumps(
112 {"connect_status": connect_resp.strip(), "http_status": 0}
113 )
114
115 sock = conn
116 if port == 443:
117 import os
118
119 ctx = ssl.create_default_context()
120 ca_file = os.environ.get("SSL_CERT_FILE")
121 if ca_file:
122 ctx.load_verify_locations(ca_file)
123 sock = ctx.wrap_socket(conn, server_hostname=host)
124
125 sock.settimeout(15)
126
127 request = (
128 f"{method} {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n"
129 )
130 sock.sendall(request.encode())
131
132 # Read response. The L7 relay loops back to parse the next
133 # request after relaying, so neither side closes — read until
134 # we have headers, then drain body with a short timeout.
135 data = b""
136 while b"\r\n\r\n" not in data:
137 chunk = sock.recv(4096)
138 if not chunk:
139 break
140 data += chunk
141
142 # Drain body with short timeout
143 sock.settimeout(2)
144 while len(data) < 65536:
145 try:
146 chunk = sock.recv(4096)
147 if not chunk:
148 break
149 data += chunk
150 except (socket.timeout, TimeoutError):
151 break
152
153 response = data.decode("latin1", errors="replace")

Calls

no outgoing calls

Tested by

no test coverage detected