Skip to content

Commit 6f9cb98

Browse files
marco-ippolitotargos
authored andcommitted
http: unify header treatment
PR-URL: #46528 Fixes: #46395 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 2eb8875 commit 6f9cb98

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

lib/_http_outgoing.js

+13
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ function isCookieField(s) {
100100
return s.length === 6 && StringPrototypeToLowerCase(s) === 'cookie';
101101
}
102102

103+
function isContentDispositionField(s) {
104+
return s.length === 19 && StringPrototypeToLowerCase(s) === 'content-disposition';
105+
}
106+
103107
function OutgoingMessage() {
104108
Stream.call(this);
105109

@@ -569,6 +573,15 @@ function _storeHeader(firstLine, headers) {
569573
function processHeader(self, state, key, value, validate) {
570574
if (validate)
571575
validateHeaderName(key);
576+
577+
// If key is content-disposition and there is content-length
578+
// encode the value in latin1
579+
// https://www.rfc-editor.org/rfc/rfc6266#section-4.3
580+
// Refs: https://github.com/nodejs/node/pull/46528
581+
if (isContentDispositionField(key) && self._contentLength) {
582+
value = Buffer.from(value, 'latin1');
583+
}
584+
572585
if (ArrayIsArray(value)) {
573586
if (
574587
(value.length < 2 || !isCookieField(key)) &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
const common = require('../common');
3+
const http = require('http');
4+
const assert = require('assert');
5+
6+
const nonUtf8Header = 'bår';
7+
const nonUtf8ToLatin1 = Buffer.from(nonUtf8Header).toString('latin1');
8+
9+
{
10+
const server = http.createServer(common.mustCall((req, res) => {
11+
res.writeHead(200, [
12+
'content-disposition',
13+
Buffer.from(nonUtf8Header).toString('binary'),
14+
]);
15+
res.end('hello');
16+
}));
17+
18+
server.listen(0, common.mustCall(() => {
19+
http.get({ port: server.address().port }, (res) => {
20+
assert.strictEqual(res.statusCode, 200);
21+
assert.strictEqual(res.headers['content-disposition'], nonUtf8ToLatin1);
22+
res.resume().on('end', common.mustCall(() => {
23+
server.close();
24+
}));
25+
});
26+
}));
27+
}
28+
29+
{
30+
const server = http.createServer(common.mustCall((req, res) => {
31+
res.writeHead(200, [
32+
'Content-Length', '5',
33+
'content-disposition',
34+
Buffer.from(nonUtf8Header).toString('binary'),
35+
]);
36+
res.end('hello');
37+
}));
38+
39+
server.listen(0, common.mustCall(() => {
40+
http.get({ port: server.address().port }, (res) => {
41+
assert.strictEqual(res.statusCode, 200);
42+
assert.strictEqual(res.headers['content-disposition'], nonUtf8ToLatin1);
43+
res.resume().on('end', common.mustCall(() => {
44+
server.close();
45+
}));
46+
});
47+
}));
48+
}

0 commit comments

Comments
 (0)