This will add the capability for handling gzipped content to the
http connector. On one hand, the response is handled with buffers instead of assuming it is a string, and then depending on the presence of the "content-encoding" header, will try to uncompress the response or return it as it is. Either way, it will try to handle the final result as an utf8 encoded string
This commit is contained in:
@ -16,6 +16,7 @@ var _ = require('../utils');
|
||||
var qs = require('querystring');
|
||||
var ForeverAgent = require('./_custom_agent');
|
||||
var ConnectionAbstract = require('../connection');
|
||||
var zlib = require('zlib');
|
||||
|
||||
/**
|
||||
* Connector used to talk to an elasticsearch node via HTTP
|
||||
@ -123,8 +124,9 @@ HttpConnector.prototype.request = function (params, cb) {
|
||||
var request;
|
||||
var response;
|
||||
var status = 0;
|
||||
var headers;
|
||||
var headers = {};
|
||||
var log = this.log;
|
||||
var buffers = [];
|
||||
|
||||
var reqParams = this.makeReqParams(params);
|
||||
|
||||
@ -144,7 +146,20 @@ HttpConnector.prototype.request = function (params, cb) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(err, response, status, headers);
|
||||
response = Buffer.concat(buffers);
|
||||
if (headers['content-encoding'] && headers['content-encoding'].match(/gzip/i)) {
|
||||
zlib.unzip(response, function(gzErr, uncompressedResponse) {
|
||||
if(gzErr) {
|
||||
err = gzErr;
|
||||
response = response.toString('binary');
|
||||
} else {
|
||||
response = uncompressedResponse.toString('utf8');
|
||||
}
|
||||
cb(err, response, status, headers);
|
||||
});
|
||||
} else {
|
||||
cb(err, response.toString('utf8'), status, headers);
|
||||
}
|
||||
}
|
||||
}, this);
|
||||
|
||||
@ -152,11 +167,10 @@ HttpConnector.prototype.request = function (params, cb) {
|
||||
incoming = _incoming;
|
||||
status = incoming.statusCode;
|
||||
headers = incoming.headers;
|
||||
incoming.setEncoding('utf8');
|
||||
response = '';
|
||||
|
||||
buffers = [];
|
||||
incoming.on('data', function (d) {
|
||||
response += d;
|
||||
buffers.push(new Buffer(d));
|
||||
});
|
||||
|
||||
incoming.on('error', cleanUp);
|
||||
|
||||
Reference in New Issue
Block a user