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

Function handleStatic

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

Source from the content-addressed store, hash-verified

481}
482
483async function handleStatic(
484 req,
485 res,
486 dirAbs,
487 staticMiddleware,
488 injectionOpts = {},
489 rejectPrefixes = [],
490 basePath = "/",
491) {
492 const urlPath = parseUrlPath(req, res);
493 if (urlPath === null) return;
494
495 if (!isMountedPath(urlPath, basePath)) {
496 if (matchesAnyPrefix(urlPath, rejectPrefixes)) {
497 rejectUnavailable(res);
498 return;
499 }
500 if (!redirectToMountedPath(req, res, urlPath, basePath)) notFound(res);
501 return;
502 }
503
504 const mountedUrl = stripBasePathFromUrl(req.url ?? "/", basePath);
505 const mountedPath = parseUrlPath({ ...req, url: mountedUrl }, res);
506 if (mountedPath === null) return;
507
508 const injectRuntimeConfig = needsRuntimeInjection(injectionOpts);
509 const indexPath = resolve(dirAbs, "index.html");
510
511 if (
512 injectRuntimeConfig &&
513 isGetOrHead(req) &&
514 (mountedPath === "/" || mountedPath === "/index.html")
515 ) {
516 if (await serveInjectedIndexHtml(req, res, indexPath, injectionOpts))
517 return;
518 }
519
520 const mountedReq = Object.create(req);
521 mountedReq.url = mountedUrl;
522
523 staticMiddleware(mountedReq, res, async () => {
524 if (matchesAnyPrefix(mountedPath, rejectPrefixes)) {
525 rejectUnavailable(res);
526 return;
527 }
528
529 if (isGetOrHead(req) && !looksLikeAssetRequest(mountedPath)) {
530 if (await serveInjectedIndexHtml(req, res, indexPath, injectionOpts)) {
531 return;
532 }
533 }
534
535 notFound(res);
536 });
537}
538
539// ─────────────────────────────────────────────────────────────────────────────
540// Server

Callers 1

startStaticServerFunction · 0.85

Calls 12

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

Tested by

no test coverage detected