Skip to content

Commit

Permalink
Add http2 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Litvinovich committed Jun 3, 2019
1 parent 8f48255 commit ced38bc
Show file tree
Hide file tree
Showing 9 changed files with 786 additions and 13 deletions.
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
relativeStaticUrl: "/s",
DEBUG: false,

SPDY_AGENT_DEFAULT_PORT: 443,
WHITELIST_URL: 'https://iframely.com/qa/whitelist.json',
WHITELIST_URL_RELOAD_PERIOD: 60 * 60 * 1000, // will reload WL every hour, if no local files are found in /whitelist folder

Expand Down
11 changes: 9 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('underscore'),
request = require('request'),
spdy = require('spdy'),
urlLib = require('url');

var pluginUtils = require('./loader/utils'),
Expand Down Expand Up @@ -1069,7 +1070,7 @@
var link2 = canonical.replace(/\/+$/, '');

if (link1 === link2 && link.rel.indexOf(CONFIG.R.oembed) == -1) {
// allow the canonical links for oEmbeds, as such mistakes are usually made for OG and Twitter:
// allow the canonical links for oEmbeds, as such mistakes are usually made for OG and Twitter:
// if publisher has oEmbed, he is most likely to have the valid embed codes
link.error = "Removed canonical link";
}
Expand Down Expand Up @@ -1408,6 +1409,12 @@
requestWrapper(options_from_plugin, options, callback);
}

options.agent = spdy.createAgent({
host: urlLib.parse(uri).hostname,
port: CONFIG.SPDY_AGENT_DEFAULT_PORT,
rejectUnauthorized: false
});

var requiredPlugins = pluginsSet.initialPlugins,
pluginsUrlMatches = pluginsSet.pluginsUrlMatches,
usedDomains = pluginsSet.usedDomains,
Expand Down Expand Up @@ -1621,4 +1628,4 @@

exports.getOembed = oembedUtils.getOembed;

})(exports);
})(exports);
29 changes: 29 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var events = require('events');
var request = require('request');
var spdy = require('spdy');
var URL = require('url');
var zlib = require('zlib');
var iconv = require('iconv-lite');
var async = require('async');
Expand Down Expand Up @@ -91,8 +93,15 @@ var getUrl = exports.getUrl = function(url, options) {

var supportGzip = !process.version.match(/^v0\.8/);

var agent = spdy.createAgent({
host: URL.parse(url).hostname,
port: CONFIG.SPDY_AGENT_DEFAULT_PORT,
rejectUnauthorized: false
});

var r = request(prepareRequestOptions({
uri: url,
agent: agent,
method: 'GET',
headers: {
'User-Agent': options.user_agent || CONFIG.USER_AGENT,
Expand Down Expand Up @@ -167,9 +176,15 @@ var getHead = function(url, options) {

process.nextTick(function() {
try {
var agent = spdy.createAgent({
host: URL.parse(url).hostname,
port: CONFIG.SPDY_AGENT_DEFAULT_PORT,
rejectUnauthorized: false
});

var r = request(prepareRequestOptions({
uri: url,
agent: agent,
method: 'HEAD',
headers: {
'User-Agent': CONFIG.USER_AGENT,
Expand Down Expand Up @@ -650,8 +665,15 @@ exports.sendLogToWhitelist = function(uri, meta, oembed, oembedLinks, whitelistR
data.oembed = oembedHref;
}

var agent = spdy.createAgent({
host: URL.parse(CONFIG.WHITELIST_LOG_URL).hostname,
port: CONFIG.SPDY_AGENT_DEFAULT_PORT,
rejectUnauthorized: false
});

request({
uri: CONFIG.WHITELIST_LOG_URL,
agent: agent,
method: 'GET',
qs: data
})
Expand Down Expand Up @@ -823,8 +845,15 @@ var getUriStatus = function(uri, options, cb) {

try {

var agent = spdy.createAgent({
host: URL.parse(uri).hostname,
port: CONFIG.SPDY_AGENT_DEFAULT_PORT,
rejectUnauthorized: false
});

var r = request(prepareRequestOptions({
uri: uri,
agent: agent,
method: 'GET',
headers: {
'User-Agent': CONFIG.USER_AGENT
Expand Down
9 changes: 9 additions & 0 deletions lib/whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
crypto = require('crypto'),
_ = require('underscore'),
request = require('request'),
spdy = require('spdy'),
URL = require('url'),
utils = require('./utils'),
logging = require('../logging');

Expand Down Expand Up @@ -328,8 +330,15 @@

logging.log("Loading whitelist from " + CONFIG.WHITELIST_URL);

var agent = spdy.createAgent({
host: URL.parse(CONFIG.WHITELIST_URL).hostname,
port: CONFIG.SPDY_AGENT_DEFAULT_PORT,
rejectUnauthorized: false
});

var options = {
uri: CONFIG.WHITELIST_URL,
agent: agent,
json: true,
qs: {
domain: CONFIG.baseAppUrl && CONFIG.baseAppUrl.replace(/.+\/\//, ''),
Expand Down
9 changes: 8 additions & 1 deletion modules/tests-ui/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var _ = require('underscore');
var FeedParser = require('feedparser');
var request = require('request');
var spdy = require('spdy');
var async = require('async');
var url = require('url');

Expand Down Expand Up @@ -74,7 +75,13 @@ var fetchFeedUrls = exports.fetchFeedUrls = function(feedUrl, options, cb) {
cb(error, urls);
};

request(feedUrl)
var agent = spdy.createAgent({
host: url.parse(feedUrl, true).hostname,
port: CONFIG.SPDY_AGENT_DEFAULT_PORT,
rejectUnauthorized: false
});

request(feedUrl, {agent: agent})
.pipe(new FeedParser({addmeta: false}))
.on('error', function(error) {
_cb(error);
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
"moment": "2.19.3",
"node-cache": "1.*",
"parse-iso-duration": "1.0.0",
"patch-package": "^6.1.2",
"readabilitySAX": "1.6.1",
"redis": "2.7.1",
"request": "^2.88.0",
"sax": "1.2.2",
"send": "0.16.1",
"spdy": "^4.0.0",
"underscore": "1.8.3"
},
"devDependencies": {
Expand All @@ -55,6 +57,7 @@
"iframely-proxy-plugins": true,
"main": "./lib/core",
"scripts": {
"prepare": "patch-package",
"test": "vows test/main.js --isolate --spec && npm run test-e2e",
"test-e2e": "NODE_ENV=test PORT=8080 mocha --exit test/e2e.js"
},
Expand Down
33 changes: 33 additions & 0 deletions patches/spdy-transport+3.0.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
diff --git a/node_modules/spdy-transport/lib/spdy-transport/protocol/base/utils.js b/node_modules/spdy-transport/lib/spdy-transport/protocol/base/utils.js
index 72a380f..042199e 100644
--- a/node_modules/spdy-transport/lib/spdy-transport/protocol/base/utils.js
+++ b/node_modules/spdy-transport/lib/spdy-transport/protocol/base/utils.js
@@ -45,13 +45,13 @@ exports.addHeaderLine = function addHeaderLine (field, value, dest) {

switch (field) {
// Array headers:
- case 'set-cookie':
- if (dest[field] !== undefined) {
- dest[field].push(value)
- } else {
- dest[field] = [ value ]
- }
- break
+ // case 'set-cookie':
+ // if (dest[field] !== undefined) {
+ // dest[field].push(value)
+ // } else {
+ // dest[field] = [ value ]
+ // }
+ // break

/* eslint-disable max-len */
// list is taken from:
@@ -74,6 +74,7 @@ exports.addHeaderLine = function addHeaderLine (field, value, dest) {
}
break

+ case 'set-cookie':
case 'cookie':
// make semicolon-separated list
if (dest[field] !== undefined) {
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var spdy = require('spdy');
var sysUtils = require('./utils');
var app = require('./app');

Expand All @@ -7,7 +8,7 @@ var server = app.listen(process.env.PORT || CONFIG.port, process.env.HOST || CON
});

if (CONFIG.ssl) {
require('https').createServer(CONFIG.ssl, app).listen(CONFIG.ssl.port);
spdy.createServer(CONFIG.ssl, app).listen(CONFIG.ssl.port);
}

console.log('');
Expand All @@ -24,4 +25,4 @@ if (!CONFIG.DEBUG) {
});
}

module.exports = server;
module.exports = server;
Loading

0 comments on commit ced38bc

Please sign in to comment.