Skip to content

Commit 23c1e2f

Browse files
marco-ippolitodanielleadams
authored andcommitted
http: unify header treatment
PR-URL: #46528 Fixes: #46395 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
1 parent cc6deea commit 23c1e2f

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
@@ -101,6 +101,10 @@ function isCookieField(s) {
101101
return s.length === 6 && StringPrototypeToLowerCase(s) === 'cookie';
102102
}
103103

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

@@ -570,6 +574,15 @@ function _storeHeader(firstLine, headers) {
570574
function processHeader(self, state, key, value, validate) {
571575
if (validate)
572576
validateHeaderName(key);
577+
578+
// If key is content-disposition and there is content-length
579+
// encode the value in latin1
580+
// https://www.rfc-editor.org/rfc/rfc6266#section-4.3
581+
// Refs: https://github.com/nodejs/node/pull/46528
582+
if (isContentDispositionField(key) && self._contentLength) {
583+
value = Buffer.from(value, 'latin1');
584+
}
585+
573586
if (ArrayIsArray(value)) {
574587
if (
575588
(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)