123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- "use strict";
- const path = require("path");
- const {
- parse
- } = require("url");
- const querystring = require("querystring");
- const getPaths = require("./getPaths");
- const memorize = require("./memorize");
- const memoizedParse = memorize(parse, undefined, value => {
- if (value.pathname) {
-
- value.pathname = decode(value.pathname);
- }
- return value;
- });
- const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
- function decode(input) {
- return querystring.unescape(input);
- }
- function getFilenameFromUrl(context, url, extra = {}) {
- const {
- options
- } = context;
- const paths = getPaths(context);
-
- let foundFilename;
-
- let urlObject;
- try {
-
- urlObject = memoizedParse(url, false, true);
- } catch (_ignoreError) {
- return;
- }
- for (const {
- publicPath,
- outputPath,
- assetsInfo
- } of paths) {
-
- let filename;
-
- let publicPathObject;
- try {
- publicPathObject = memoizedParse(publicPath !== "auto" && publicPath ? publicPath : "/", false, true);
- } catch (_ignoreError) {
-
- continue;
- }
- const {
- pathname
- } = urlObject;
- const {
- pathname: publicPathPathname
- } = publicPathObject;
- if (pathname && pathname.startsWith(publicPathPathname)) {
-
- if (pathname.includes("\0")) {
-
- extra.errorCode = 400;
- return;
- }
-
- if (UP_PATH_REGEXP.test(path.normalize(`./${pathname}`))) {
-
- extra.errorCode = 403;
- return;
- }
-
-
-
-
- filename = path.join(outputPath, pathname.slice(publicPathPathname.length));
- try {
-
- extra.stats = context.outputFileSystem.statSync(filename);
- } catch (_ignoreError) {
-
- continue;
- }
- if (extra.stats.isFile()) {
- foundFilename = filename;
-
- if (assetsInfo) {
- const assetInfo = assetsInfo.get(pathname.slice(publicPathObject.pathname.length));
-
- extra.immutable = assetInfo ? assetInfo.immutable : false;
- }
- break;
- } else if (extra.stats.isDirectory() && (typeof options.index === "undefined" || options.index)) {
- const indexValue = typeof options.index === "undefined" || typeof options.index === "boolean" ? "index.html" : options.index;
- filename = path.join(filename, indexValue);
- try {
-
- extra.stats = context.outputFileSystem.statSync(filename);
- } catch (__ignoreError) {
-
- continue;
- }
- if (extra.stats.isFile()) {
- foundFilename = filename;
- break;
- }
- }
- }
- }
-
- return foundFilename;
- }
- module.exports = getFilenameFromUrl;
|