Skip to content

Commit

Permalink
add gunzip error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hefangshi committed Jul 6, 2016
1 parent aee4001 commit 669524a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/ext/protocol/httpProtocolBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,17 @@ HttpProtocolBase.prototype._request = function(config, callback) {
statusCodeError.statusCode = res.statusCode;
req.emit('error', statusCodeError);
}
var stream = res;
// 添加 gzip与deflate 处理
switch (config.headers ? res.headers['content-encoding'] : '') {
// or, just use zlib.createUnzip() to handle both cases
case 'gzip':
res.pipe(zlib.createGunzip()).pipe(response);
break;
case 'deflate':
res.pipe(zlib.createInflate()).pipe(response);
break;
default:
res.pipe(response);
break;
var contentEncoding = res.headers['content-encoding'];
if (contentEncoding === 'gzip' || contentEncoding === 'deflate') {
stream = contentEncoding === 'gzip' ? zlib.createGunzip() : zlib.createInflate();
stream.on('error', function(error) {
response.emit('error', error);
});
res.pipe(stream);
}
stream.pipe(response);
callback && callback(response);
response.emit('extras', {
headers: res.headers,
Expand Down
59 changes: 59 additions & 0 deletions test/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,65 @@ describe('http protocol', function () {
done();
});
});

it('should unzip gzip content', function (done) {
var getTest = require('./protocol/http_protocol_get_test.js');
// start a http server for get
var server = getTest.createServer();
var httpProtocol = new HttpProtocol();
var context = HttpProtocol.normalizeConfig(getTest.service);
context.path = '/gzip';
context.disableGzip = false;
util.merge(context, getTest.request);
httpProtocol.talk(context, function (res) {
res.on('data', function (data) {
server.close();
data.toString().should.be.equal('hear you');
done();
});
});
});

it('should inflate deflate content', function (done) {
var getTest = require('./protocol/http_protocol_get_test.js');
// start a http server for get
var server = getTest.createServer();
var httpProtocol = new HttpProtocol();
var context = HttpProtocol.normalizeConfig(getTest.service);
context.path = '/deflate';
context.disableGzip = false;
util.merge(context, getTest.request);
httpProtocol.talk(context, function (res) {
res.on('data', function (data) {
server.close();
data.toString().should.be.equal('hear you');
done();
});
});
});

it('should handle unzip error', function (done) {
var getTest = require('./protocol/http_protocol_get_test.js');
// start a http server for get
var server = getTest.createServer();
var httpProtocol = new HttpProtocol();
var context = HttpProtocol.normalizeConfig(getTest.service);
context.path = '/gziperror';
context.disableGzip = false;
util.merge(context, getTest.request);
httpProtocol.talk(context, function (res) {
res.on('data', function (data) {
server.close();
data.toString().should.not.be.ok;
done();
});
res.on('error', function (err) {
server.close();
err.message.should.be.match(/unexpected end of file/);
done();
});
});
});
});
});

Expand Down
50 changes: 50 additions & 0 deletions test/protocol/http_protocol_get_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var http = require('http');
var url = require('url');
var urlencode = require('urlencode');
var zlib = require('zlib');

module.exports.__defineGetter__('service', function () {
return {
Expand Down Expand Up @@ -111,6 +112,55 @@ module.exports.createServer = function () {
response.write(content);
response.end();
}
else if (pathname === '/gzip') {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip'
});
var content = 'hear you';
if (info.query) {
info.query = urlencode.parse(info.query);
if (info.query.name) {
content += ' ' + info.query.name;
}
}
zlib.gzip(content, function (err, compressed) {
response.write(compressed);
response.end();
});
} else if (pathname === '/gziperror') {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip'
});
var content = 'hear you';
if (info.query) {
info.query = urlencode.parse(info.query);
if (info.query.name) {
content += ' ' + info.query.name;
}
}
zlib.gzip(content, function (err, compressed) {
response.write(compressed.slice(0, compressed.length / 2));
response.end();
});
} else if (pathname === '/deflate') {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Encoding': 'deflate'
});
var content = 'hear you';
if (info.query) {
info.query = urlencode.parse(info.query);
if (info.query.name) {
content += ' ' + info.query.name;
}
}
zlib.deflate(content, function (err, compressed) {
response.write(compressed);
response.end();
});
}
else if (pathname === '/error') {
response.writeHead(503, {
'Content-Type': 'text/plain'
Expand Down

0 comments on commit 669524a

Please sign in to comment.