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

Function handleStatic

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

Source from the content-addressed store, hash-verified

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