Skip to content

Commit e606cf1

Browse files
stephenmartindaleshellscape
authored andcommitted
Excluded outputPath from URI escaping to fix #297. (#303)
* Excluded outputPath from URI escaping to fix #297. * Resolved linting issues. * Moved Windows-specific test case to the Windows-specific test case block. * Removed `.only` from test fixture.
1 parent ac17265 commit e606cf1

File tree

2 files changed

+66
-30
lines changed

2 files changed

+66
-30
lines changed

lib/util.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ module.exports = {
6363
const localPrefix = parse(publicPath || '/', false, true);
6464
const urlObject = parse(url);
6565
let filename;
66-
let result = '';
6766

6867
// publicPath has the hostname that is not the same as request url's, should fail
6968
if (localPrefix.hostname !== null && urlObject.hostname !== null &&
@@ -89,24 +88,31 @@ module.exports = {
8988

9089
let uri = outputPath;
9190

91+
/* istanbul ignore if */
92+
if (process.platform === 'win32') {
93+
// Path Handling for Microsoft Windows
94+
if (filename) {
95+
uri = urlJoin((outputPath || ''), querystring.unescape(filename));
96+
97+
if (!pathabs.win32(uri)) {
98+
uri = `/${uri}`;
99+
}
100+
}
101+
102+
return uri;
103+
}
104+
105+
// Path Handling for all other operating systems
92106
if (filename) {
93107
uri = urlJoin((outputPath || ''), filename);
94108

95-
if (!pathabs.posix(uri) && !pathabs.win32(uri)) {
109+
if (!pathabs.posix(uri)) {
96110
uri = `/${uri}`;
97111
}
98112
}
99113

100114
// if no matches, use outputPath as filename
101-
result = querystring.unescape(uri);
102-
103-
// fixes #284, on windows path spaces should resolve to %20
104-
/* istanbul ignore if */
105-
if (process.platform === 'win32') {
106-
result = result.replace(/\s/g, '%20');
107-
}
108-
109-
return result;
115+
return querystring.unescape(uri);
110116
},
111117

112118
handleRangeHeaders(content, req, res) {

test/tests/util.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,7 @@ describe('GetFilenameFromUrl', () => {
121121
url: '/pathname%20with%20spaces.js',
122122
outputPath: '/',
123123
publicPath: '/',
124-
// ref #284 - windows paths should persist %20
125-
expected: isWindows ? '/pathname%20with%20spaces.js' : '/pathname with spaces.js'
126-
},
127-
{
128-
url: '/test/windows.txt',
129-
outputPath: 'c:\\foo',
130-
publicPath: '/test',
131-
// this is weird, but it's legal-ish, and what URI parsing produces
132-
expected: 'c://\\foo/windows.txt'
124+
expected: '/pathname with spaces.js'
133125
},
134126
{
135127
url: '/js/sample.js',
@@ -279,17 +271,55 @@ describe('GetFilenameFromUrl', () => {
279271
});
280272
}
281273

274+
// Explicit Tests for Microsoft Windows
282275
if (isWindows) {
283-
// explicit test for #284
284-
const test = {
285-
url: '/test/windows.txt',
286-
outputPath: 'C:\\My%20Path\\wwwroot',
287-
publicPath: '/test',
288-
expected: 'C://\\My%20Path\\wwwroot/windows.txt'
289-
};
276+
const windowsTests = [
277+
{
278+
url: '/test/windows.txt',
279+
outputPath: 'c:\\foo',
280+
publicPath: '/test',
281+
// this is weird, but it's legal-ish, and what URI parsing produces
282+
expected: 'c://\\foo/windows.txt'
283+
},
284+
// Tests for #284
285+
{
286+
url: '/test/windows.txt',
287+
outputPath: 'C:\\My%20Path\\wwwroot',
288+
publicPath: '/test',
289+
expected: 'C://\\My%20Path\\wwwroot/windows.txt'
290+
},
291+
{
292+
url: '/test/windows%202.txt',
293+
outputPath: 'C:\\My%20Path\\wwwroot',
294+
publicPath: '/test',
295+
expected: 'C://\\My%20Path\\wwwroot/windows 2.txt'
296+
},
297+
// Tests for #297
298+
{
299+
url: '/test/windows.txt',
300+
outputPath: 'C:\\My Path\\wwwroot',
301+
publicPath: '/test',
302+
expected: 'C://\\My Path\\wwwroot/windows.txt'
303+
},
304+
{
305+
url: '/test/windows%202.txt',
306+
outputPath: 'C:\\My Path\\wwwroot',
307+
publicPath: '/test',
308+
expected: 'C://\\My Path\\wwwroot/windows 2.txt'
309+
},
310+
// Tests for #284 & #297
311+
{
312+
url: '/windows%20test/test%20%26%20test%20%26%20%2520.txt',
313+
outputPath: 'C:\\My %20 Path\\wwwroot',
314+
publicPath: '/windows%20test',
315+
expected: 'C://\\My %20 Path\\wwwroot/test & test & %20.txt'
316+
}
317+
];
290318

291-
it(`windows: should process ${test.url} -> ${test.expected}`, () => {
292-
testUrl(test);
293-
});
319+
for (const test of windowsTests) {
320+
it(`windows: should process ${test.url} -> ${test.expected}`, () => {
321+
testUrl(test);
322+
});
323+
}
294324
}
295325
});

0 commit comments

Comments
 (0)