MCPcopy
hub / github.com/http-party/http-server / statFile

Function statFile

lib/core/index.js:347–425  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

345
346
347 function statFile() {
348 try {
349 fs.stat(file, (err, stat) => {
350 if (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) {
351 if (req.statusCode === 404) {
352 // This means we're already trying ./404.html and can not find it.
353 // So send plain text response with 404 status code
354 status[404](res, next);
355 } else if (!path.extname(parsed.pathname).length && defaultExt) {
356 // If there is no file extension in the path and we have a default
357 // extension try filename and default extension combination before rendering 404.html.
358 middleware({
359 url: `${parsed.pathname}.${defaultExt}${(parsed.search) ? parsed.search : ''}`,
360 headers: req.headers,
361 }, res, next);
362 } else {
363 // Try to serve default ./404.html
364 const rawUrl = (handleError ? `/${path.join(baseDir, `404.${defaultExt}`)}` : req.url);
365 const encodedUrl = ensureUriEncoded(rawUrl);
366 middleware({
367 url: encodedUrl,
368 headers: req.headers,
369 statusCode: 404,
370 }, res, next);
371 }
372 } else if (err) {
373 status[500](res, next, { error: err });
374 } else if (stat.isDirectory()) {
375 if (!autoIndex && !opts.showDir) {
376 status[404](res, next);
377 return;
378 }
379
380
381 // 302 to / if necessary
382 if (!pathname.match(/\/$/)) {
383 res.statusCode = 302;
384 const q = parsed.query ? `?${parsed.query}` : '';
385 res.setHeader(
386 'location',
387 ensureUriEncoded(`${parsed.pathname}/${q}`)
388 );
389 res.end();
390 return;
391 }
392
393 if (autoIndex) {
394 middleware({
395 url: urlJoin(
396 encodeURIComponent(pathname),
397 `/index.${defaultExt}`
398 ),
399 headers: req.headers,
400 }, res, (autoIndexError) => {
401 if (autoIndexError) {
402 status[500](res, next, { error: autoIndexError });
403 return;
404 }

Callers 3

tryServeWithGzipFunction · 0.85
tryServeWithBrotliFunction · 0.85
index.jsFile · 0.85

Calls 2

ensureUriEncodedFunction · 0.85
serveFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…