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

Function handleStatic

scripts/static-server.mjs:546–600  ·  view source on GitHub ↗
(
  req,
  res,
  dirAbs,
  staticMiddleware,
  injectionOpts = {},
  rejectPrefixes = [],
  basePath = "/",
)

Source from the content-addressed store, hash-verified

544}
545
546async function handleStatic(
547 req,
548 res,
549 dirAbs,
550 staticMiddleware,
551 injectionOpts = {},
552 rejectPrefixes = [],
553 basePath = "/",
554) {
555 const urlPath = parseUrlPath(req, res);
556 if (urlPath === null) return;
557
558 if (!isMountedPath(urlPath, basePath)) {
559 if (matchesAnyPrefix(urlPath, rejectPrefixes)) {
560 rejectUnavailable(res);
561 return;
562 }
563 if (!redirectToMountedPath(req, res, urlPath, basePath)) notFound(res);
564 return;
565 }
566
567 const mountedUrl = stripBasePathFromUrl(req.url ?? "/", basePath);
568 const mountedPath = parseUrlPath({ ...req, url: mountedUrl }, res);
569 if (mountedPath === null) return;
570
571 const injectRuntimeConfig = needsRuntimeInjection(injectionOpts);
572 const indexPath = resolve(dirAbs, "index.html");
573
574 if (
575 injectRuntimeConfig &&
576 isGetOrHead(req) &&
577 (mountedPath === "/" || mountedPath === "/index.html")
578 ) {
579 if (await serveInjectedIndexHtml(req, res, indexPath, injectionOpts))
580 return;
581 }
582
583 const mountedReq = Object.create(req);
584 mountedReq.url = mountedUrl;
585
586 staticMiddleware(mountedReq, res, async () => {
587 if (matchesAnyPrefix(mountedPath, rejectPrefixes)) {
588 rejectUnavailable(res);
589 return;
590 }
591
592 if (isGetOrHead(req) && !looksLikeAssetRequest(mountedPath)) {
593 if (await serveInjectedIndexHtml(req, res, indexPath, injectionOpts)) {
594 return;
595 }
596 }
597
598 notFound(res);
599 });
600}
601
602// ─────────────────────────────────────────────────────────────────────────────
603// Server

Callers 1

startStaticServerFunction · 0.85

Calls 12

parseUrlPathFunction · 0.85
isMountedPathFunction · 0.85
matchesAnyPrefixFunction · 0.85
rejectUnavailableFunction · 0.85
redirectToMountedPathFunction · 0.85
stripBasePathFromUrlFunction · 0.85
needsRuntimeInjectionFunction · 0.85
isGetOrHeadFunction · 0.85
serveInjectedIndexHtmlFunction · 0.85
looksLikeAssetRequestFunction · 0.85
notFoundFunction · 0.70
createMethod · 0.45

Tested by

no test coverage detected