Skip to content

Commit

Permalink
http: unify header treatment
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Feb 6, 2023
1 parent 2dacd07 commit a2a954b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
// `this._header` can be null if OutgoingMessage is used without a proper Socket
// See: /test/parallel/test-http-outgoing-message-inheritance.js
if (typeof data === 'string' &&
(encoding === 'utf8' || encoding === 'latin1' || !encoding)) {
(encoding === 'utf8' || encoding === 'latin1')) {
data = this._header + data;
} else {
const header = this._header;
Expand Down
48 changes: 48 additions & 0 deletions test/parallel/test-http-server-non-utf8-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');

const nonUtf8Header = 'bår';
const nonUtf8ToLatin1 = Buffer.from(nonUtf8Header).toString('latin1');

{
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200, [
'foo',
Buffer.from(nonUtf8Header).toString('binary'),
]);
res.end('hello');
}));

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, (res) => {
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers.foo, nonUtf8ToLatin1);
res.resume().on('end', common.mustCall(() => {
server.close();
}));
});
}));
}

{
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200, [
'Content-Length', '5',
'foo',
Buffer.from(nonUtf8Header).toString('binary'),
]);
res.end('hello');
}));

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, (res) => {
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers.foo, nonUtf8ToLatin1);
res.resume().on('end', common.mustCall(() => {
server.close();
}));
});
}));
}
6 changes: 2 additions & 4 deletions test/parallel/test-http-server-response-standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ let firstChunk = true;
const ws = new Writable({
write: common.mustCall((chunk, encoding, callback) => {
if (firstChunk) {
assert(chunk.toString().endsWith('hello world'));
assert(chunk.toString().startsWith('HTTP/1.1 200 OK'));
firstChunk = false;
} else {
assert.strictEqual(chunk.length, 0);
}
setImmediate(callback);
}, 2)
}, 3)
});

res.assignSocket(ws);
Expand Down

0 comments on commit a2a954b

Please sign in to comment.