Skip to content

Commit 55c95b1

Browse files
committed
http: fix first body chunk fast case for UTF-16
`http.OutgoingMessage` tried to send the first chunk together with the headers by concatenating them together as a string, but the list of encodings for which that works was incorrect. Change it from a blacklist to a whitelist. Fixes: #11788 PR-URL: #12747 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 2bf461e commit 55c95b1

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/_http_outgoing.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
229229
// this at a lower level and in a more general way.
230230
if (!this._headerSent) {
231231
if (typeof data === 'string' &&
232-
encoding !== 'hex' &&
233-
encoding !== 'base64') {
232+
(encoding === 'utf8' || encoding === 'latin1' || !encoding)) {
234233
data = this._header + data;
235234
} else {
236235
this.output.unshift(this._header);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Regression test for https://github.com/nodejs/node/issues/11788.
5+
6+
const assert = require('assert');
7+
const http = require('http');
8+
const net = require('net');
9+
10+
for (const enc of ['utf8', 'utf16le', 'latin1', 'UTF-8']) {
11+
const server = http.createServer(common.mustCall((req, res) => {
12+
res.setHeader('content-type', `text/plain; charset=${enc}`);
13+
res.write('helloworld', enc);
14+
res.end();
15+
})).listen(0);
16+
17+
server.on('listening', common.mustCall(() => {
18+
const buffers = [];
19+
const socket = net.connect(server.address().port);
20+
socket.write('GET / HTTP/1.0\r\n\r\n');
21+
socket.on('data', (data) => buffers.push(data));
22+
socket.on('end', common.mustCall(() => {
23+
const received = Buffer.concat(buffers);
24+
const headerEnd = received.indexOf('\r\n\r\n', 'utf8');
25+
assert.notStrictEqual(headerEnd, -1);
26+
27+
const header = received.toString('utf8', 0, headerEnd).split(/\r\n/g);
28+
const body = received.toString(enc, headerEnd + 4);
29+
30+
assert.strictEqual(header[0], 'HTTP/1.1 200 OK');
31+
assert.strictEqual(header[1], `content-type: text/plain; charset=${enc}`);
32+
assert.strictEqual(body, 'helloworld');
33+
server.close();
34+
}));
35+
}));
36+
}

0 commit comments

Comments
 (0)