diff --git a/lib/proxy.js b/lib/proxy.js index 7aaf018..736602e 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -9,7 +9,6 @@ var http = require('http') , zlib = require('zlib') , sniparse = require('./sniparse'); -require('bufferjs/concat'); require('bufferjs/add-chunk'); require('./regexp-escape'); @@ -136,10 +135,11 @@ module.exports.createProxyServer = function (opts) { var isHtml = (proxyResponse.headers['content-type'] && proxyResponse.headers['content-type'].toLowerCase().indexOf("html") != -1); + var writeResponse = function (shouldBuffer) { - var buffer = undefined, bufferLength = 0; + var buffer = undefined, buffers = [], bufferLength = 0; if (shouldBuffer) { - bufferLength = ~~(proxyResponse.headers['content-length'] || 0); + bufferLength = 0;//~~(proxyResponse.headers['content-length'] || 0); delete proxyResponse.headers['content-length']; buffer = new Buffer(0); } @@ -150,8 +150,10 @@ module.exports.createProxyServer = function (opts) { }); proxyResponse.on('data', function (chunk) { + bufferLength += chunk.length; if (shouldBuffer) { - buffer = Buffer.concat(buffer, chunk); + // buffer = Buffer.concat(buffer, chunk); + buffers.push(chunk); } else { response.write(chunk); } @@ -193,6 +195,8 @@ module.exports.createProxyServer = function (opts) { emitOrRun('interceptResponseContent', function () { writeResponse(newBuffer); }, newBuffer, proxyResponse, isSsl, charset, writeResponse); }; + + buffer = Buffer.concat(buffers, bufferLength); switch (responseEncoding) { case 'gzip': zlib.gunzip(buffer, setupIntercept); @@ -340,7 +344,7 @@ module.exports.createProxyServer = function (opts) { socket.write('HTTP/1.0 200 Connection established\r\n\r\n'); } } catch (error) { - emitter.emit('error', error, 'httpsSocketConnect'); + emitter.emit('error', error, 'httpsSocket'); } }); @@ -355,11 +359,6 @@ module.exports.createProxyServer = function (opts) { } }); - clientSocket.on('error', function (error) { - socket.end(); - emitter.emit('error', error, 'httpsClientSocket'); - }); - clientSocket.on('end', function () { socket.end(); }); socket.on('data', function (data) { @@ -372,12 +371,6 @@ module.exports.createProxyServer = function (opts) { emitter.emit('error', error, 'httpsClientSocketData'); } }); - - socket.on('error', function (error) { - clientSocket.end(); - emitter.emit('error', error, 'httpsSocket'); - }); - socket.on('end', function () { clientSocket.end(); }); }; diff --git a/package.json b/package.json index 0f5112b..8bd6fff 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "homepage": "https://github.com/axiak/filternet", "bugs": "https://github.com/axiak/filternet/issues", "dependencies": { - "bufferjs": ">= 1.0.0" + "bufferjs": ">1.0.0" }, "main": "index", "license": "BSD",