Ver Fonte

Merge pull request #135 from gmetais/http2

Add Http2 detection
Gaël Métais há 9 anos atrás
pai
commit
6a9a247a87

+ 12 - 0
front/src/views/rule.html

@@ -369,6 +369,18 @@
         </div>
     </div>
 
+    <div ng-if="policyName === 'http2'">
+        <h3>Protocols advertised by the server</h3>
+        <div class="offendersTable">
+            <div ng-repeat="protocol in rule.offendersObj.list">
+                <div>{{protocol}}</div>
+            </div>
+            <div ng-if="!rule.offendersObj || rule.offendersObj.count == 0">
+                <div>none</div>
+            </div>
+        </div>
+    </div>
+
     <div ng-if="!rule && rule !== null" class="notFound">
         <h2>404</h2>
         Rule "{{policyName}}"" not found

+ 29 - 0
lib/metadata/policies.js

@@ -1044,6 +1044,35 @@ var policies = {
         "isAbnormalThreshold": 30,
         "hasOffenders": true
     },
+    "http2": {
+        "label": "HTTP/2 or SPDY",
+        "message": "<p>HTTP/2 is the latest version of the HTTP protocol and is designed to optimize load speed. SPDY is deprecated but still very well supported.</p><p>The latest versions of all major browsers are now compatible. The difficulty is on the server side, where technologies are not quite ready yet.</p>",
+        "hasOffenders": true,
+        "scoreFn": function(data) {
+            if (!data.toolsResults.http2) {
+                return null;
+            }
+
+            var isHttp2 = data.toolsResults.http2.metrics.http2;
+
+            var result = {
+                value: isHttp2 ? 'Yes' : 'No',
+                score: isHttp2 ? 100 : 0,
+                bad: !isHttp2,
+                abnormal: false,
+                abnormalityScore: 0
+            };
+
+            if (data.toolsResults.http2.offenders) {
+                result.offendersObj = {
+                    count: data.toolsResults.http2.offenders.http2.length,
+                    list: data.toolsResults.http2.offenders.http2
+                };
+            }
+
+            return result;
+        }
+    },
     "cachingDisabled": {
         "tool": "phantomas",
         "label": "Caching disabled",

+ 1 - 0
lib/metadata/scoreProfileGeneric.json

@@ -96,6 +96,7 @@
         "serverConfig": {
             "label": "Server config",
             "policies": {
+                "http2": 2,
                 "closedConnections": 2,
                 "cachingNotSpecified": 1,
                 "cachingDisabled": 1,

+ 6 - 0
lib/runner.js

@@ -5,6 +5,7 @@ var phantomasWrapper        = require('./tools/phantomas/phantomasWrapper');
 var jsExecutionTransformer  = require('./tools/jsExecutionTransformer');
 var colorDiff               = require('./tools/colorDiff');
 var mediaQueriesChecker     = require('./tools/mediaQueriesChecker');
+var isHttp2                 = require('./tools/isHttp2');
 var weightChecker           = require('./tools/weightChecker/weightChecker');
 var rulesChecker            = require('./rulesChecker');
 var scoreCalculator         = require('./scoreCalculator');
@@ -41,6 +42,11 @@ var Runner = function(params) {
 
     })
 
+    .then(function(data) {
+        // Check if HTTP2
+        return isHttp2.check(data);
+    })
+
     .then(function(data) {
 
         // Rules checker

+ 87 - 0
lib/tools/isHttp2.js

@@ -0,0 +1,87 @@
+var debug   = require('debug')('ylt:isHttp2');
+var url     = require('url');
+var Q       = require('q');
+var http2   = require('is-http2');
+
+var isHttp2 = function() {
+    'use strict';
+
+    this.check = function(data) {
+        debug('Starting to check for HTTP2 support...');
+
+        return this.checkHttp2(data)
+
+            .then(function(result) {
+
+                if (result.isHttp2) {
+                    debug('HTTP/2 (or SPDY) is supported');
+
+                    data.toolsResults.http2 = {
+                        metrics: {
+                            http2: true
+                        }
+                    };
+                
+                } else {
+                    debug('HTTP/2 is not supported');
+                    
+                    data.toolsResults.http2 = {
+                        metrics: {
+                            http2: false
+                        }
+                    };
+                }
+
+                // Add the supported protocols as offenders
+                if (result.supportedProtocols) {
+                    debug('Supported protocols: ' + result.supportedProtocols.join(' '));
+                    data.toolsResults.http2.offenders = {
+                        http2: result.supportedProtocols
+                    };
+                }
+
+                debug('End of HTTP2 support check');
+
+                return data;
+            })
+
+            .fail(function() {
+                return data;
+            });
+    };
+
+    this.getParsedUrl = function(data) {
+        return url.parse(data.toolsResults.phantomas.url);
+    };
+
+    this.getProtocol = function(data) {
+        return this.getParsedUrl(data).protocol;
+    };
+
+    this.getDomain = function(data) {
+        return this.getParsedUrl(data).hostname;
+    };
+
+    this.checkHttp2 = function(data) {
+        var deferred = Q.defer();
+
+        var domain = this.getDomain(data);
+
+        // To make is-http2 work, you need to have openssl in a version greater than 1.0.0 installed and available in your $path.
+        http2(domain, {includeSpdy: true})
+
+            .then(function(result) {
+                deferred.resolve(result);
+            })
+            
+            .catch(function(error) {
+                debug('Error while checking HTTP2 support:');
+                debug(error);
+                deferred.reject('Error while checking for HTTP2 support');
+            });
+
+        return deferred.promise;
+    };
+};
+
+module.exports = new isHttp2();

+ 1 - 0
package.json

@@ -39,6 +39,7 @@
     "express": "4.13.3",
     "imagemin": "4.0.0",
     "imagemin-jpegoptim": "4.1.0",
+    "is-http2": "1.0.4",
     "jstoxml": "0.2.3",
     "lwip": "0.0.8",
     "meow": "3.6.0",

+ 48 - 0
test/core/isHttp2Test.js

@@ -0,0 +1,48 @@
+var should = require('chai').should();
+var isHttp2 = require('../../lib/tools/isHttp2');
+
+describe('isHttp2', function() {
+    
+    it('should parse the protocol correctly', function() {
+        isHttp2.getProtocol({
+            toolsResults: {
+                phantomas: {
+                    url: 'http://www.yahoo.com'
+                }
+            }
+        }).should.equal('http:');
+
+
+        isHttp2.getProtocol({
+            toolsResults: {
+                phantomas: {
+                    url: 'https://www.yahoo.com'
+                }
+            }
+        }).should.equal('https:');
+    });
+
+    it('should parse the domain correctly', function() {
+        isHttp2.getDomain({
+            toolsResults: {
+                phantomas: {
+                    url: 'http://www.yahoo.com'
+                }
+            }
+        }).should.equal('www.yahoo.com');
+
+
+        isHttp2.getDomain({
+            toolsResults: {
+                phantomas: {
+                    url: 'https://www.yahoo.com'
+                }
+            }
+        }).should.equal('www.yahoo.com');
+    });
+
+    it('should have a function checkHttp2', function() {
+        isHttp2.should.have.a.property('checkHttp2').that.is.a('function');
+    });
+
+});