Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: fix first body chunk fast case for UTF-16 #12747

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
// this at a lower level and in a more general way.
if (!this._headerSent) {
if (typeof data === 'string' &&
encoding !== 'hex' &&
encoding !== 'base64') {
(encoding === 'utf8' || encoding === 'latin1' || !encoding)) {
data = this._header + data;
} else {
this.output.unshift(this._header);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
const common = require('../common');

// Regression test for https://github.com/nodejs/node/issues/11788.

const assert = require('assert');
const http = require('http');
const net = require('net');

for (const enc of ['utf8', 'utf16le', 'latin1', 'UTF-8']) {
const server = http.createServer(common.mustCall((req, res) => {
res.setHeader('content-type', `text/plain; charset=${enc}`);
res.write('helloworld', enc);
res.end();
})).listen(0);

server.on('listening', common.mustCall(() => {
const buffers = [];
const socket = net.connect(server.address().port);
socket.write('GET / HTTP/1.0\r\n\r\n');
socket.on('data', (data) => buffers.push(data));
socket.on('end', common.mustCall(() => {
const received = Buffer.concat(buffers);
const headerEnd = received.indexOf('\r\n\r\n', 'utf8');
assert.notStrictEqual(headerEnd, -1);

const header = received.toString('utf8', 0, headerEnd).split(/\r\n/g);
const body = received.toString(enc, headerEnd + 4);

assert.strictEqual(header[0], 'HTTP/1.1 200 OK');
assert.strictEqual(header[1], `content-type: text/plain; charset=${enc}`);
assert.strictEqual(body, 'helloworld');
server.close();
}));
}));
}