MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / proxyServerInfoRequest

Function proxyServerInfoRequest

scripts/proxy-utils.mjs:69–185  ·  view source on GitHub ↗
(
  req,
  res,
  backendUrl,
  runtimeServicesInfo,
)

Source from the content-addressed store, hash-verified

67}
68
69export function proxyServerInfoRequest(
70 req,
71 res,
72 backendUrl,
73 runtimeServicesInfo,
74) {
75 let backend;
76 try {
77 backend = parseBackendUrl(backendUrl);
78 } catch {
79 writeInvalidBackendUrlResponse(req, res);
80 return;
81 }
82
83 const request = backend.protocol === "https:" ? httpsRequest : httpRequest;
84 const proxyReq = request(
85 {
86 hostname: backend.hostname,
87 port: backend.port,
88 path: req.url,
89 method: req.method,
90 headers: {
91 ...req.headers,
92 host: `${backend.hostname}:${backend.port}`,
93 },
94 },
95 (proxyRes) => {
96 const chunks = [];
97
98 proxyRes.on("data", (chunk) => {
99 chunks.push(Buffer.from(chunk));
100 });
101
102 proxyRes.on("error", (err) => {
103 if (!isBenignSocketError(err)) {
104 console.error(`Upstream response error for ${req.url}:`, err.message);
105 }
106 if (!res.headersSent) {
107 res.writeHead(502);
108 res.end(`Bad Gateway: ${err.message}`);
109 } else {
110 res.destroy();
111 }
112 });
113
114 proxyRes.on("end", () => {
115 const statusCode = proxyRes.statusCode ?? 502;
116 const headers = { ...proxyRes.headers };
117 const originalBody = Buffer.concat(chunks);
118
119 if (statusCode < 200 || statusCode >= 300 || req.method === "HEAD") {
120 res.writeHead(statusCode, headers);
121 res.end(req.method === "HEAD" ? "" : originalBody);
122 return;
123 }
124
125 try {
126 const serverInfo = JSON.parse(originalBody.toString("utf8"));

Callers 2

startStaticServerFunction · 0.90
startIngressFunction · 0.90

Calls 4

parseBackendUrlFunction · 0.85
isBenignSocketErrorFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected