Skip to content

Commit c4f4595

Browse files
ijseSpaceK33z
authored andcommitted
Improve getFilenameFromUrl to work with proxy requests (#80)
Fixes #79
1 parent 7a20ccd commit c4f4595

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

lib/GetFilenameFromUrl.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
var pathJoin = require("./PathJoin");
2+
var urlParse = require("url").parse;
23

34
function getFilenameFromUrl(publicPath, outputPath, url) {
4-
// publicPrefix is the folder our bundle should be in
5-
var localPrefix = publicPath || "/";
6-
if(url.indexOf(localPrefix) !== 0) {
7-
if(/^(https?:)?\/\//.test(localPrefix)) {
8-
localPrefix = "/" + localPrefix.replace(/^(https?:)?\/\/[^\/]+\//, "");
9-
// fast exit if another directory requested
10-
if(url.indexOf(localPrefix) !== 0) return false;
11-
} else return false;
5+
var filename;
6+
7+
// localPrefix is the folder our bundle should be in
8+
var localPrefix = urlParse(publicPath || "/");
9+
var urlObject = urlParse(url);
10+
11+
// publicPath has the hostname that is not the same as request url's, should fail
12+
if(localPrefix.hostname !== null && urlObject.hostname !== null &&
13+
localPrefix.hostname !== urlObject.hostname) {
14+
return false;
15+
}
16+
17+
// publicPath is not in url, so it should fail
18+
if(publicPath && localPrefix.hostname === urlObject.hostname && url.indexOf(publicPath) !== 0) {
19+
return false;
1220
}
13-
// get filename from request
14-
var filename = url.substr(localPrefix.length);
15-
if(filename.indexOf("?") >= 0) {
16-
filename = filename.substr(0, filename.indexOf("?"));
21+
22+
// strip localPrefix from the start of url
23+
if(urlObject.pathname.indexOf(localPrefix.pathname) === 0) {
24+
filename = urlObject.pathname.substr(localPrefix.pathname.length);
25+
}
26+
27+
if(!urlObject.hostname && localPrefix.hostname &&
28+
url.indexOf(localPrefix.path) !== 0) {
29+
return false;
1730
}
31+
// and if not match, use outputPath as filename
1832
return filename ? pathJoin(outputPath, filename) : outputPath;
33+
1934
}
2035

2136
module.exports = getFilenameFromUrl;

test/GetFilenameFromUrl.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ describe("GetFilenameFromUrl", function() {
4949
outputPath: "/a",
5050
publicPath: "/",
5151
expected: "/a/more/complex/path.js",
52+
}, {
53+
url: "/more/complex/path.js",
54+
outputPath: "/a",
55+
publicPath: "/complex",
56+
expected: false,
5257
}, {
5358
url: "c.js",
5459
outputPath: "/dist",
@@ -64,6 +69,11 @@ describe("GetFilenameFromUrl", function() {
6469
outputPath: "/",
6570
publicPath: "http://localhost/foo/",
6671
expected: false,
72+
}, {
73+
url: "http://test.domain/test/sample.js",
74+
outputPath: "/",
75+
publicPath: "/test/",
76+
expected: "/sample.js"
6777
}
6878
];
6979
results.forEach(testUrl);

0 commit comments

Comments
 (0)