Skip to content

Instantly share code, notes, and snippets.

@tjfontaine
Last active August 29, 2015 14:06
Show Gist options
  • Save tjfontaine/f3c345d0ab5d847c1c9d to your computer and use it in GitHub Desktop.
Save tjfontaine/f3c345d0ab5d847c1c9d to your computer and use it in GitHub Desktop.
diff --git a/lib/_http_client.js b/lib/_http_client.js
index 929be3f..5495da6 100644
--- a/lib/_http_client.js
+++ b/lib/_http_client.js
@@ -308,11 +308,17 @@ function socketOnData(d) {
var ret = parser.execute(d);
if (ret instanceof Error) {
- debug('parse error');
- freeParser(parser, req, socket);
- socket.destroy();
- req.emit('error', ret);
- req.socket._hadError = true;
+ if (parser.incoming.headers['transfer-encoding'] === 'chunked' && parser.incoming.complete) {
+ debug('chunkRemainingBytes', d.slice(ret.bytesParsed));
+ parser.incoming.emit('chunkRemainingBytes', d.slice(ret.bytesParsed));
+ } else {
+ ret.data = d;
+ debug('parse error');
+ freeParser(parser, req, socket);
+ socket.destroy();
+ req.emit('error', ret);
+ req.socket._hadError = true;
+ }
} else if (parser.incoming && parser.incoming.upgrade) {
// Upgrade or CONNECT
var bytesParsed = ret;
var assert = require('assert');
var net = require('net');
var responseMessage = [
'HTTP/1.1 200 OK',
'Transfer-Encoding: chunked',
'',
'5',
'hello',
'0',
'',
'0',
'',
'',
].join('\r\n');
var expectedResult = 'hello';
var expectedBuff = new Buffer(responseMessage.slice(responseMessage.length - 5));
var responseError = false;
var requestError = false;
var leftOver = false;
var result = '';
var server = net.createServer(function(conn) {
conn.end(responseMessage);
server.close();
}).listen().on('listening', function() {
var address = server.address();
var host = 'http://127.0.0.1:' + address.port;
console.error(host);
var request = require('http').get(host, function(response) {
response.setEncoding('utf8');
response.on('data', function(d) {
result += d;
});
response.on('error', function(err) {
responseError = err;
console.error('response error', err);
console.error(response);
});
// hypothetical interface for 0.12 (maybe 0.10?)
response.on('chunkRemainingBytes', function(buff) {
leftOver = buff;
console.error('chunkRemainingBytes', buff);
});
}).on('error', function(err) {
console.error(err);
leftOver = err;
requestError = err;
var res = this.res;
// backwards compatible way to detect if this is the case
if (res.headers['transfer-encoding'] === 'chunked' &&
res.complete === true &&
err.code === 'HPE_INVALID_CONSTANT') {
console.error('we have trailing bytes', err.bytesParsed);
} else {
console.error('request error', err);
}
});
});
process.on('exit', function() {
assert.strictEqual(result, expectedResult);
assert.strictEqual(responseError, false);
assert.strictEqual(requestError, false);
assert.deepEqual(leftOver, expectedBuff);
});
TJs-MacBook-Pro:~ tjfontaine$ node t.js > /dev/null
{ date: 'Mon, 22 Sep 2014 17:57:13 GMT',
server: 'Server',
'x-amz-id-1': '0JJGTBFE60F74626RMGJ',
'x-amz-id-2': 'YUpIvdWXOZLxDd/CBvVJqLGpHkQ2yuY41cGlG+Pj3z66IffDziRWwYhelH4qqeKk67UW6uStL2w=',
'x-frame-options': 'SAMEORIGIN',
'content-type': 'text/html;charset=UTF-8',
vary: 'Accept-Encoding,User-Agent',
'set-cookie':
[ 'session-id=276-2142866-6459662; Domain=.amazon.in; Expires=Sun, 17-Sep-2034 17:57:13 GMT; Path=/',
'session-id-time=2042128633l; Domain=.amazon.in; Expires=Sun, 17-Sep-2034 17:57:13 GMT; Path=/' ],
'transfer-encoding': 'chunked' }
request error { [Error: Parse Error] bytesParsed: 188, code: 'HPE_INVALID_CONSTANT' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment