MCPcopy
hub / github.com/inkeep/open-knowledge / forwardRequest

Function forwardRequest

packages/cli/src/commands/ui-proxy.ts:255–352  ·  view source on GitHub ↗
(
  req: IncomingMessage,
  res: ServerResponse,
  upstreamHost: string,
  upstreamPort: number,
  upstreamTimeoutMs: number,
)

Source from the content-addressed store, hash-verified

253}
254
255function forwardRequest(
256 req: IncomingMessage,
257 res: ServerResponse,
258 upstreamHost: string,
259 upstreamPort: number,
260 upstreamTimeoutMs: number,
261): void {
262 const headers: IncomingHttpHeaders = { ...req.headers };
263 delete headers.host;
264 for (const name of HOP_BY_HOP_HEADERS) {
265 delete headers[name];
266 }
267
268 req.setTimeout(DEFAULT_REQUEST_TIMEOUT_MS, () => {
269 if (!res.headersSent) {
270 try {
271 emitProblem(
272 res,
273 408,
274 'urn:ok:error:request-timeout',
275 'Proxy request exceeded the per-request deadline.',
276 `Slow-loris-class: client did not finish within ${DEFAULT_REQUEST_TIMEOUT_MS / 1000}s.`,
277 );
278 } catch {}
279 } else {
280 try {
281 res.end();
282 } catch {}
283 }
284 try {
285 req.socket?.destroy();
286 } catch {}
287 });
288
289 const upstreamReq = httpRequest(
290 {
291 host: upstreamHost,
292 port: upstreamPort,
293 method: req.method,
294 path: req.url,
295 headers: { ...headers, host: `${upstreamHost}:${upstreamPort}` },
296 },
297 (upstreamRes) => {
298 const resHeaders = { ...upstreamRes.headers };
299 for (const name of HOP_BY_HOP_HEADERS) {
300 delete resHeaders[name];
301 }
302 res.writeHead(upstreamRes.statusCode ?? 502, resHeaders);
303 upstreamRes.pipe(res);
304 upstreamRes.once('error', () => {
305 try {
306 res.end();
307 } catch {}
308 });
309 },
310 );
311
312 if (upstreamTimeoutMs > 0) {

Callers 2

startProxyServerFunction · 0.85
proxyRequestFunction · 0.85

Calls 6

emitProblemFunction · 0.90
setTimeoutMethod · 0.65
endMethod · 0.65
destroyMethod · 0.65
onceMethod · 0.65
onMethod · 0.65

Tested by

no test coverage detected