Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ const getHeaders = async (handlers, config, current, absolutePath, stats) => {
// if that's not working, it will save the file. But to be clear: This
// only happens if it cannot find a appropiate value.
'Content-Disposition': contentDisposition(base, {
type: 'inline'
type: 'inline',
// Node.js rejects non-ASCII header values on some responses, so
// the plain `filename` parameter must stay ASCII. The full name
// goes into `filename*`.
fallback: base.replace(/[^\x20-\x7e]/g, '?')
}),
'Accept-Ranges': 'bytes'
};
Expand Down
28 changes: 28 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Native
const path = require('path');
const os = require('os');

// Packages
const listen = require('test-listen');
Expand Down Expand Up @@ -1442,3 +1443,30 @@ test('prefer directory index.html over .html sibling when both 1.3/ and 1.3.html
expect(response.status).toBe(200);
expect(text).toBe(content);
});

test('serve a file whose name has non-ASCII characters', async () => {
const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'serve-handler-'));
const names = ['bras\u00e3o.txt', 'DALL\u00b7E.png', '\u65e5\u672c\u8a9e.txt'];

try {
await Promise.all(names.map(name => fs.writeFile(path.join(directory, name), 'content')));

const url = await getUrl({
'public': directory
});

for (const name of names) {
const response = await fetch(`${url}/${encodeURIComponent(name)}`, {
timeout: 2000
});

expect(response.status).toBe(200);
expect(await response.text()).toBe('content');

const disposition = response.headers.get('content-disposition');
expect(disposition).toContain(`filename*=UTF-8''${encodeURIComponent(name)}`);
}
} finally {
await fs.remove(directory);
}
});