Browse Source

Fix a bug when an incorrect protocol is called, for exemple htp instead of http

Gaël Métais 9 years ago
parent
commit
f3a5779102
1 changed files with 101 additions and 93 deletions
  1. 101 93
      lib/tools/weightChecker/weightChecker.js

+ 101 - 93
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';
-
-            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;
-
-            function tally() {
-
-                if (statusCode !== 200) {
-                    callback({code: statusCode});
-                    return;
-                }
+            request(requestOptions)
 
-                var result = {
-                    body: body,
-                    headersSize: Buffer.byteLength(rawHeaders, 'utf8'),
-                    bodySize: bodySize,
-                    isCompressed: isCompressed,
-                    uncompressedSize: uncompressedSize
-                };
+            .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';
 
-                callback(null, result);
-            }
+                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;
 
-            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);
-                    });
+                function tally() {
 
-                    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);
-                    });
+                    if (statusCode !== 200) {
+                        callback({code: statusCode});
+                        return;
+                    }
 
-                    res.on('data', function (data) {
-                        bodySize += data.length;
-                    }).pipe(deflate);
+                    var result = {
+                        body: body,
+                        headersSize: Buffer.byteLength(rawHeaders, 'utf8'),
+                        bodySize: bodySize,
+                        isCompressed: isCompressed,
+                        uncompressedSize: uncompressedSize
+                    };
 
-                    break;
-                default:
-                    if (contentType === 'image/jpeg' || contentType === 'image/png') {
-                        res.setEncoding('binary');
-                    }
+                    callback(null, result);
+                }
 
-                    res.on('data', function (data) {
-                        body += data;
-                        uncompressedSize += data.length;
-                        bodySize += data.length;
-                    }).on('end', function () {
-                        tally();
-                    });
+                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');
+                        }
+
+                        res.on('data', function (data) {
+                            body += data;
+                            uncompressedSize += data.length;
+                            bodySize += data.length;
+                        }).on('end', function () {
+                            tally();
+                        });
+
+                        break;
+                }
+            })
 
-                    break;
-            }
-        })
+            .on('response', function(response) {
+                statusCode = response.statusCode;
+            })
 
-        .on('response', function(response) {
-            statusCode = response.statusCode;
-        })
+            .on('error', function(err) {
+                debug('Error while downloading %s', requestOptions.url);
+                debug(err);
+                callback(err);
+            });
 
-        .on('error', function(err) {
+        } catch(err) {
             debug('Error while downloading %s', requestOptions.url);
             debug(err);
             callback(err);
-        });
+        }
     }
 
     return {