diff --git a/front/src/views/rule.html b/front/src/views/rule.html index 39bb18b..7301e85 100644 --- a/front/src/views/rule.html +++ b/front/src/views/rule.html @@ -369,6 +369,18 @@ +
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.
The latest versions of all major browsers are now compatible. The difficulty is on the server side, where technologies are not quite ready yet.
", + "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", diff --git a/lib/metadata/scoreProfileGeneric.json b/lib/metadata/scoreProfileGeneric.json index 9c2f68f..a65a931 100644 --- a/lib/metadata/scoreProfileGeneric.json +++ b/lib/metadata/scoreProfileGeneric.json @@ -96,6 +96,7 @@ "serverConfig": { "label": "Server config", "policies": { + "http2": 2, "closedConnections": 2, "cachingNotSpecified": 1, "cachingDisabled": 1, diff --git a/lib/runner.js b/lib/runner.js index e5fa3c7..977355c 100644 --- a/lib/runner.js +++ b/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 diff --git a/lib/tools/isHttp2.js b/lib/tools/isHttp2.js new file mode 100644 index 0000000..0af9753 --- /dev/null +++ b/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(); \ No newline at end of file diff --git a/package.json b/package.json index c8a75c8..35e8866 100644 --- a/package.json +++ b/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", diff --git a/test/core/isHttp2Test.js b/test/core/isHttp2Test.js new file mode 100644 index 0000000..1ace12e --- /dev/null +++ b/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'); + }); + +});