From f3a5779102f743b922e5a190922b5918cd89b7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Tue, 22 Dec 2015 19:27:57 +0100 Subject: [PATCH] Fix a bug when an incorrect protocol is called, for exemple htp instead of http --- lib/tools/weightChecker/weightChecker.js | 196 ++++++++++++----------- 1 file changed, 102 insertions(+), 94 deletions(-) diff --git a/lib/tools/weightChecker/weightChecker.js b/lib/tools/weightChecker/weightChecker.js index ea2f472..747ece5 100644 --- a/lib/tools/weightChecker/weightChecker.js +++ b/lib/tools/weightChecker/weightChecker.js @@ -408,113 +408,121 @@ var WeightChecker = function() { var statusCode; - request(requestOptions) + try { - .on('response', function(res) { - - // Raw headers were added in NodeJS v0.12 - // (https://github.com/joyent/node/issues/4844), but let's - // reconstruct them for backwards compatibility. - var rawHeaders = ('HTTP/' + res.httpVersion + ' ' + res.statusCode + - ' ' + http.STATUS_CODES[res.statusCode] + '\r\n'); - Object.keys(res.headers).forEach(function(headerKey) { - rawHeaders += headerKey + ': ' + res.headers[headerKey] + '\r\n'; - }); - rawHeaders += '\r\n'; + request(requestOptions) - var uncompressedSize = 0; // size after uncompression - var bodySize = 0; // bytes size over the wire - var body = ''; // plain text body (after uncompressing gzip/deflate) - var isCompressed = false; + .on('response', function(res) { + + // Raw headers were added in NodeJS v0.12 + // (https://github.com/joyent/node/issues/4844), but let's + // reconstruct them for backwards compatibility. + var rawHeaders = ('HTTP/' + res.httpVersion + ' ' + res.statusCode + + ' ' + http.STATUS_CODES[res.statusCode] + '\r\n'); + Object.keys(res.headers).forEach(function(headerKey) { + rawHeaders += headerKey + ': ' + res.headers[headerKey] + '\r\n'; + }); + rawHeaders += '\r\n'; - function tally() { + var uncompressedSize = 0; // size after uncompression + var bodySize = 0; // bytes size over the wire + var body = ''; // plain text body (after uncompressing gzip/deflate) + var isCompressed = false; - if (statusCode !== 200) { - callback({code: statusCode}); - return; - } + function tally() { - var result = { - body: body, - headersSize: Buffer.byteLength(rawHeaders, 'utf8'), - bodySize: bodySize, - isCompressed: isCompressed, - uncompressedSize: uncompressedSize - }; - - callback(null, result); - } - - switch (res.headers['content-encoding']) { - case 'gzip': - - var gzip = zlib.createGunzip(); - - gzip.on('data', function (data) { - body += data; - uncompressedSize += data.length; - }).on('end', function () { - isCompressed = true; - tally(); - }).on('error', function(err) { - debug('Error while decoding %s', requestOptions.url); - debug(err); - callback(err); - }); - - res.on('data', function (data) { - bodySize += data.length; - }).pipe(gzip); - - break; - case 'deflate': - res.setEncoding('utf8'); - - var deflate = zlib.createInflate(); - - deflate.on('data', function (data) { - body += data; - uncompressedSize += data.length; - }).on('end', function () { - isCompressed = true; - tally(); - }).on('error', function(err) { - debug('Error while decoding %s', requestOptions.url); - debug(err); - callback(err); - }); - - res.on('data', function (data) { - bodySize += data.length; - }).pipe(deflate); - - break; - default: - if (contentType === 'image/jpeg' || contentType === 'image/png') { - res.setEncoding('binary'); + if (statusCode !== 200) { + callback({code: statusCode}); + return; } - res.on('data', function (data) { - body += data; - uncompressedSize += data.length; - bodySize += data.length; - }).on('end', function () { - tally(); - }); + var result = { + body: body, + headersSize: Buffer.byteLength(rawHeaders, 'utf8'), + bodySize: bodySize, + isCompressed: isCompressed, + uncompressedSize: uncompressedSize + }; - break; - } - }) + callback(null, result); + } - .on('response', function(response) { - statusCode = response.statusCode; - }) + switch (res.headers['content-encoding']) { + case 'gzip': - .on('error', function(err) { + var gzip = zlib.createGunzip(); + + gzip.on('data', function (data) { + body += data; + uncompressedSize += data.length; + }).on('end', function () { + isCompressed = true; + tally(); + }).on('error', function(err) { + debug('Error while decoding %s', requestOptions.url); + debug(err); + callback(err); + }); + + res.on('data', function (data) { + bodySize += data.length; + }).pipe(gzip); + + break; + case 'deflate': + res.setEncoding('utf8'); + + var deflate = zlib.createInflate(); + + deflate.on('data', function (data) { + body += data; + uncompressedSize += data.length; + }).on('end', function () { + isCompressed = true; + tally(); + }).on('error', function(err) { + debug('Error while decoding %s', requestOptions.url); + debug(err); + callback(err); + }); + + res.on('data', function (data) { + bodySize += data.length; + }).pipe(deflate); + + break; + default: + if (contentType === 'image/jpeg' || contentType === 'image/png') { + res.setEncoding('binary'); + } + + res.on('data', function (data) { + body += data; + uncompressedSize += data.length; + bodySize += data.length; + }).on('end', function () { + tally(); + }); + + break; + } + }) + + .on('response', function(response) { + statusCode = response.statusCode; + }) + + .on('error', function(err) { + debug('Error while downloading %s', requestOptions.url); + debug(err); + callback(err); + }); + + } catch(err) { debug('Error while downloading %s', requestOptions.url); debug(err); callback(err); - }); + } } return {