Skip to content

Commit e4b716e

Browse files
committed
http: Support write(data, 'hex')
We were assuming that any string can be concatenated safely to CRLF. However, for hex, base64, or binary encoded writes, this is not the case, and results in sending the incorrect response. An unusual edge case, but certainly a bug.
1 parent 037bcac commit e4b716e

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

lib/http.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -772,15 +772,18 @@ OutgoingMessage.prototype.write = function(chunk, encoding) {
772772

773773
var len, ret;
774774
if (this.chunkedEncoding) {
775-
if (typeof(chunk) === 'string') {
775+
if (typeof(chunk) === 'string' &&
776+
encoding !== 'hex' &&
777+
encoding !== 'base64' &&
778+
encoding !== 'binary') {
776779
len = Buffer.byteLength(chunk, encoding);
777780
chunk = len.toString(16) + CRLF + chunk + CRLF;
778781
ret = this._send(chunk, encoding);
779782
} else {
780-
// buffer
783+
// buffer, or a non-toString-friendly encoding
781784
len = chunk.length;
782785
this._send(len.toString(16) + CRLF);
783-
this._send(chunk);
786+
this._send(chunk, encoding);
784787
ret = this._send(CRLF);
785788
}
786789
} else {

test/simple/test-http-byteswritten.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ var httpServer = http.createServer(function(req, res) {
3535
});
3636
res.writeHead(200, { 'Content-Type': 'text/plain' });
3737

38-
// Write 1mb to cause some requests to buffer
39-
var chunk = new Array(1024).join('A');
38+
// Write 1.5mb to cause some requests to buffer
39+
// Also, mix up the encodings a bit.
40+
var chunk = new Array(1024 + 1).join('7');
4041
for (var i = 0; i < 1024; i++) {
4142
res.write(chunk);
43+
res.write(chunk, 'hex');
4244
}
4345
// Get .bytesWritten while buffer is not empty
4446
assert(res.connection.bytesWritten > 0);

0 commit comments

Comments
 (0)