Skip to content

Commit 23c1e2f

Browse files
marco-ippolitodanielleadams
authored andcommitted
http: unify header treatment
PR-URL: #46528Fixes: #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 cc6deea commit 23c1e2f

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎lib/_http_outgoing.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ function isCookieField(s) {
101101
returns.length===6&&StringPrototypeToLowerCase(s)==='cookie';
102102
}
103103

104+
functionisContentDispositionField(s){
105+
returns.length===19&&StringPrototypeToLowerCase(s)==='content-disposition';
106+
}
107+
104108
functionOutgoingMessage(){
105109
Stream.call(this);
106110

@@ -570,6 +574,15 @@ function _storeHeader(firstLine, headers) {
570574
functionprocessHeader(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))&&
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
consthttp=require('http');
4+
constassert=require('assert');
5+
6+
constnonUtf8Header='bår';
7+
constnonUtf8ToLatin1=Buffer.from(nonUtf8Header).toString('latin1');
8+
9+
{
10+
constserver=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+
constserver=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)