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

Function handleStatic

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

Source from the content-addressed store, hash-verified

607}
608
609async function handleStatic(
610 req,
611 res,
612 dirAbs,
613 staticMiddleware,
614 injectionOpts = {},
615 rejectPrefixes = [],
616 basePath = "/",
617) {
618 const urlPath = parseUrlPath(req, res);
619 if (urlPath === null) return;
620
621 if (!isMountedPath(urlPath, basePath)) {
622 if (matchesAnyPrefix(urlPath, rejectPrefixes)) {
623 rejectUnavailable(res);
624 return;
625 }
626 if (!redirectToMountedPath(req, res, urlPath, basePath)) notFound(res);
627 return;
628 }
629
630 const mountedUrl = stripBasePathFromUrl(req.url ?? "/", basePath);
631 const mountedPath = parseUrlPath({ ...req, url: mountedUrl }, res);
632 if (mountedPath === null) return;
633
634 const injectRuntimeConfig = needsRuntimeInjection(injectionOpts);
635 const indexPath = resolve(dirAbs, "index.html");
636
637 if (
638 injectRuntimeConfig &&
639 isGetOrHead(req) &&
640 (mountedPath === "/" || mountedPath === "/index.html")
641 ) {
642 if (await serveInjectedIndexHtml(req, res, indexPath, injectionOpts))
643 return;
644 }
645
646 const mountedReq = Object.create(req);
647 mountedReq.url = mountedUrl;
648
649 staticMiddleware(mountedReq, res, async () => {
650 if (matchesAnyPrefix(mountedPath, rejectPrefixes)) {
651 rejectUnavailable(res);
652 return;
653 }
654
655 if (isGetOrHead(req) && !looksLikeAssetRequest(mountedPath)) {
656 if (await serveInjectedIndexHtml(req, res, indexPath, injectionOpts)) {
657 return;
658 }
659 }
660
661 notFound(res);
662 });
663}
664
665// ─────────────────────────────────────────────────────────────────────────────
666// Server

Callers 1

startStaticServerFunction · 0.85

Calls 13

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
resolveFunction · 0.50

Tested by

no test coverage detected