Skip to content

Commit

Permalink
chore(gzip) Retry without gzip when response seems broken
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-hilaire committed Jul 27, 2018
1 parent a116947 commit f3c86d8
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,25 @@ Crawler.prototype._buildHttpRequest = function _buildHTTPRequest (options) {
'followAllRedirects', 'maxRedirects','encoding','pool','timeout','proxy','auth','oauth','strictSSL',
'jar','aws','gzip','time','tunnel','proxyHeaderWhiteList','proxyHeaderExclusiveList','localAddress','forever', 'agent'];

request(_.pick.apply(self,[ropts].concat(requestArgs)), function(error,response) {
if (error) {
return self._onContent(error, options);
}

self._onContent(error,options,response);
});
function tryWithOptions(requestOptions, onError) {
request(requestOptions, function(error, response) {
if (!error) {
return self._onContent(error, options, response);
}
onError(error);
});
}

var allRequestOptions = _.pick.apply(self,[ropts].concat(requestArgs));
tryWithOptions(allRequestOptions, function(error) {
if (error.code === 'Z_DATA_ERROR') {
tryWithOptions(_.assign(allRequestOptions, {gzip: false}), function(newError) {
return self._onContent(newError, options);
});
} else {
self._onContent(error, options);
}
});
}

if (options.preRequest && _.isFunction(options.preRequest)) {
Expand Down

0 comments on commit f3c86d8

Please sign in to comment.