|
1 | 1 | var pathJoin = require("./PathJoin");
|
| 2 | +var urlParse = require("url").parse; |
2 | 3 |
|
3 | 4 | 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; |
12 | 20 | }
|
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; |
17 | 30 | }
|
| 31 | + // and if not match, use outputPath as filename |
18 | 32 | return filename ? pathJoin(outputPath, filename) : outputPath;
|
| 33 | + |
19 | 34 | }
|
20 | 35 |
|
21 | 36 | module.exports = getFilenameFromUrl;
|
0 commit comments