Skip to content

Commit

Permalink
if body not decode, dont touch it #26
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Mar 11, 2014
1 parent 0609a3b commit a881cbd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
19 changes: 13 additions & 6 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ exports.request = function (url, args, callback) {
var headers = JSON.stringify(res && res.headers || {});
err.message += ', ' + options.method + ' ' + url + ' ' + statusCode
+ '\nheaders: ' + headers;
err.res = res;
if (res) {
err.res = {
statusCode: res.statusCode,
headers: res.headers,
aborted: res.aborted,
};
}
err.data = data;
}
cb(err, data, res);
Expand Down Expand Up @@ -264,13 +270,13 @@ exports.request = function (url, args, callback) {
}

var decodeContent = function (res, body, cb) {
var encoding = res.headers['content-encoding'];
if (body.length === 0 || !enableGzip) {
return cb(null, body);
return cb(null, body, encoding);
}

var encoding = res.headers['content-encoding'];
if (!encoding || encoding.toLowerCase() !== 'gzip') {
return cb(null, body);
return cb(null, body, encoding);
}

debug('gunzip %d length body', body.length);
Expand Down Expand Up @@ -367,12 +373,13 @@ exports.request = function (url, args, callback) {
return;
}

decodeContent(res, body, function (err, data) {
decodeContent(res, body, function (err, data, encoding) {
if (err) {
return done(err, body, res);
}

if (args.dataType === 'json') {
// if body not decode, dont touch it
if (!encoding && args.dataType === 'json') {
try {
data = size > 0 ? JSON.parse(data) : null;
} catch (e) {
Expand Down
26 changes: 24 additions & 2 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
var should = require('should');
var http = require('http');
var https = require('https');
var zlib = require('zlib');
var querystring = require('querystring');
var urlutil = require('url');
var KeepAliveAgent = require('agentkeepalive');
Expand Down Expand Up @@ -781,16 +782,37 @@ describe('urllib.test.js', function () {

describe('gzip content', function () {
it('should auto accept and decode gzip response content', function (done) {
urllib.request('http://r.cnpmjs.org/npm',
urllib.request('http://r.cnpmjs.org/byte',
{dataType: 'json', gzip: true, timeout: 10000}, function (err, data, res) {
should.not.exist(err);
data.name.should.equal('npm');
data.name.should.equal('byte');
res.should.have.header('content-encoding', 'gzip');
res.should.have.header('content-type', 'application/json');
done();
});
});

it('should auto accept and custom decode gzip response content', function (done) {
urllib.request('http://r.cnpmjs.org/byte', {
dataType: 'json', gzip: true, timeout: 10000,
headers: {
'accept-encoding': 'gzip'
}
}, function (err, data, res) {
should.not.exist(err);
data.should.be.a.Buffer;
data.length.should.above(0);
res.should.have.header('content-encoding', 'gzip');
res.should.have.header('content-type', 'application/json');
zlib.gunzip(data, function (err, buf) {
should.not.exist(err);
buf.should.be.a.Buffer;
JSON.parse(buf).name.should.equal('byte');
done();
});
});
});

it('should redirect and gzip', function (done) {
urllib.request('http://dist.cnpmjs.org/v0.10.1/SHASUMS.txt',
{followRedirect: true, gzip: true, timeout: 10000}, function (err, data, res) {
Expand Down

0 comments on commit a881cbd

Please sign in to comment.