Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On windows path spaces should resolve to %20 #288

Merged
merged 8 commits into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
const localPrefix = parse(publicPath || '/', false, true);
const urlObject = parse(url);
let filename;
let result = '';

// publicPath has the hostname that is not the same as request url's, should fail
if (localPrefix.hostname !== null && urlObject.hostname !== null &&
Expand Down Expand Up @@ -99,7 +100,15 @@ module.exports = {
}

// if no matches, use outputPath as filename
return querystring.unescape(uri);
result = querystring.unescape(uri);

// fixes #284, on windows path spaces should resolve to %20
/* istanbul ignore if */
if (process.platform === 'win32') {
result = result.replace(/\s/g, '%20');
}

return result;
},

handleRangeHeaders(content, req, res) {
Expand Down
19 changes: 18 additions & 1 deletion test/tests/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const assert = require('assert');
const { getFilenameFromUrl } = require('../../lib/util');

const isWindows = process.platform === 'win32';

function testUrl(options) {
const url = getFilenameFromUrl(options.publicPath, options, options.url);
assert.equal(url, options.expected);
Expand Down Expand Up @@ -119,7 +121,8 @@ describe('GetFilenameFromUrl', () => {
url: '/pathname%20with%20spaces.js',
outputPath: '/',
publicPath: '/',
expected: '/pathname with spaces.js'
// ref #284 - windows paths should persist %20
expected: isWindows ? '/pathname%20with%20spaces.js' : '/pathname with spaces.js'
},
{
url: '/test/windows.txt',
Expand Down Expand Up @@ -275,4 +278,18 @@ describe('GetFilenameFromUrl', () => {
testUrl(test);
});
}

if (isWindows) {
// explicit test for #284
const test = {
url: '/test/windows.txt',
outputPath: 'C:\\My%20Path\\wwwroot',
publicPath: '/test',
expected: 'C://\\My%20Path\\wwwroot/windows.txt'
};

it(`windows: should process ${test.url} -> ${test.expected}`, () => {
testUrl(test);
});
}
});