MCPcopy Create free account
hub / github.com/agent-tower/core / proxyHttpRequest

Function proxyHttpRequest

packages/server/src/routes/previews.ts:335–409  ·  view source on GitHub ↗
(
  request: FastifyRequest,
  response: ServerResponse,
  workspaceId: string,
  target: NormalizedPreviewTarget,
  previewToken?: string | null,
)

Source from the content-addressed store, hash-verified

333 const targetOrigin = ${JSON.stringify(target.origin)};
334 const targetBasePath = ${JSON.stringify(target.basePath)};
335 const loopbackHosts = new Set(['localhost', '127.0.0.1', '::1', '[::1]', '0.0.0.0']);
336 const normalizeProtocol = (protocol) => protocol === 'ws:' ? 'http:' : protocol === 'wss:' ? 'https:' : protocol;
337 const normalizeHost = (hostname) => loopbackHosts.has(hostname.toLowerCase()) ? 'loopback' : hostname.toLowerCase();
338 const effectivePort = (url) => url.port || (normalizeProtocol(url.protocol) === 'https:' ? '443' : '80');
339 const sameEndpoint = (left, right) => normalizeProtocol(left.protocol) === normalizeProtocol(right.protocol)
340 && normalizeHost(left.hostname) === normalizeHost(right.hostname)
341 && effectivePort(left) === effectivePort(right);
342 const isPrefixed = (pathname) => pathname === prefix || pathname.startsWith(prefix + '/');
343 const rewrite = (value) => {
344 if (value === undefined || value === null) return value;
345 const raw = value instanceof URL ? value.toString() : String(value);
346 let url;
347 try {
348 url = new URL(raw, window.location.href);
349 } catch {
350 return value;
351 }
352 if (isPrefixed(url.pathname)) return raw;
353
354 const browserUrl = new URL(window.location.href);
355 const localTarget = new URL(targetOrigin);
356 const isRootRelative = raw.startsWith('/') && !raw.startsWith('//');
357 const isBrowserEndpoint = sameEndpoint(url, browserUrl);
358 const isTargetEndpoint = sameEndpoint(url, localTarget);
359 if (!isRootRelative && !isTargetEndpoint) return value;
360 if (isBrowserEndpoint && !isRootRelative) return value;
361
362 let pathname = url.pathname;
363 if (isTargetEndpoint && targetBasePath) {
364 const underBasePath = pathname === targetBasePath || pathname.startsWith(targetBasePath + '/');
365 if (underBasePath) pathname = pathname.slice(targetBasePath.length) || '/';
366 }
367 const protocol = url.protocol === 'ws:' || url.protocol === 'wss:'
368 ? (window.location.protocol === 'https:' ? 'wss:' : 'ws:')
369 : window.location.protocol;
370 return protocol + '//' + window.location.host + prefix + pathname + url.search + url.hash;
371 };
372
373 const nativePushState = History.prototype.pushState;
374 const nativeReplaceState = History.prototype.replaceState;
375 History.prototype.pushState = function(state, unused, url) {
376 return nativePushState.call(this, state, unused, url == null ? url : rewrite(url));
377 };
378 History.prototype.replaceState = function(state, unused, url) {
379 return nativeReplaceState.call(this, state, unused, url == null ? url : rewrite(url));
380 };
381
382 const nativeFetch = window.fetch.bind(window);
383 window.fetch = (input, init) => {
384 if (typeof input === 'string' || input instanceof URL) return nativeFetch(rewrite(input), init);
385 if (input instanceof Request) return nativeFetch(new Request(rewrite(input.url), input), init);
386 return nativeFetch(input, init);
387 };
388
389 const nativeXhrOpen = XMLHttpRequest.prototype.open;
390 XMLHttpRequest.prototype.open = function(method, url, ...rest) {
391 return nativeXhrOpen.call(this, method, rewrite(url), ...rest);
392 };

Callers 1

handlerFunction · 0.85

Calls 10

parsePreviewPathFunction · 0.85
requestBodyBufferFunction · 0.85
proxyRequestHeadersFunction · 0.85
joinTargetPathFunction · 0.85
proxyResponseHeadersFunction · 0.85
isRewritableContentFunction · 0.85
rewritePreviewBodyFunction · 0.85
requestMethod · 0.45
onMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected