From a8853f675412c4e2309e28cdf1b642b95115b074 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Thu, 25 May 2017 14:11:50 +0200 Subject: [PATCH] Fixes #6 - Support HTTPS. --- cometd-nodejs-client.js | 23 ++++++++++++++---- test/https.js | 42 +++++++++++++++++++++++++++++++++ test/tls/private.pem | 52 +++++++++++++++++++++++++++++++++++++++++ test/tls/public.pem | 33 ++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 test/https.js create mode 100644 test/tls/private.pem create mode 100644 test/tls/public.pem diff --git a/cometd-nodejs-client.js b/cometd-nodejs-client.js index 4bfefe4..56743ee 100644 --- a/cometd-nodejs-client.js +++ b/cometd-nodejs-client.js @@ -1,7 +1,8 @@ module.exports = { adapt: function() { var url = require('url'); - var http = require('http'); + var httpc = require('http'); + var https = require('https'); global.window = {}; @@ -12,11 +13,18 @@ module.exports = { window.console.debug = window.console.log; // Fields shared by all XMLHttpRequest instances. - var _agent = new http.Agent({ + var _agentc = new httpc.Agent({ + keepAlive: true + }); + var _agents = new https.Agent({ keepAlive: true }); var _cookieStore = {}; + function _secure(uri) { + return /^https/i.test(uri.protocol); + } + // Bare minimum XMLHttpRequest implementation that works with CometD. window.XMLHttpRequest = function() { var _config; @@ -30,7 +38,7 @@ module.exports = { this.open = function(method, uri) { _config = url.parse(uri); _config.method = method; - _config.agent = _agent; + _config.agent = _secure(_config) ? _agents : _agentc; _config.headers = {}; this.readyState = window.XMLHttpRequest.OPENED; }; @@ -59,6 +67,7 @@ module.exports = { } var self = this; + var http = _secure(_config) ? https : httpc; _request = http.request(_config, function(response) { var success = false; self.status = response.statusCode; @@ -105,10 +114,10 @@ module.exports = { }); }); ['abort', 'aborted', 'error'].forEach(function(event) { - _request.on(event, function() { + _request.on(event, function(x) { self.readyState = window.XMLHttpRequest.DONE; if (self.onerror) { - self.onerror(); + self.onerror(x); } }); }); @@ -123,6 +132,10 @@ module.exports = { _request.abort(); } }; + + this._config = function() { + return _config; + }; }; window.XMLHttpRequest.UNSENT = 0; window.XMLHttpRequest.OPENED = 1; diff --git a/test/https.js b/test/https.js new file mode 100644 index 0000000..0b680b5 --- /dev/null +++ b/test/https.js @@ -0,0 +1,42 @@ +var assert = require('assert'); +var cometd = require('..'); +var https = require('https'); +var fs = require('fs'); + +describe('https', function() { + var _server; + + beforeEach(function() { + cometd.adapt(); + }); + + afterEach(function() { + if (_server) { + _server.close(); + } + }); + + it('supports https', function(done) { + var options = { + key: fs.readFileSync('test/tls/private.pem'), + cert: fs.readFileSync('test/tls/public.pem') + }; + _server = https.createServer(options, function(request, response) { + response.end(); + }); + _server.listen(0, 'localhost', function() { + var port = _server.address().port; + console.log('listening on localhost:' + port); + var uri = 'https://localhost:' + port; + var xhr = new window.XMLHttpRequest(); + xhr.open('GET', uri + '/'); + // Allow self-signed certificates. + xhr._config().rejectUnauthorized = false; + xhr.onload = function() { + assert.strictEqual(xhr.status, 200); + done(); + }; + xhr.send(); + }); + }); +}); diff --git a/test/tls/private.pem b/test/tls/private.pem new file mode 100644 index 0000000..3702194 --- /dev/null +++ b/test/tls/private.pem @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCneixWOMofJCzc +VoFtpHxLg/QyHTqyPAfP1jfXrUos4qRuNJn85I0tQ2HYAWxIaZYdYB80Ls4i1VoU +u3aug53yL0rihjltFHxh/VpiGDNWJZ+l8buHILljGDlWDWqrd0tElxM99r8lQLxn +pQF6vQmeXuVRyNkBpb4G/yV5w18bQAg/bPr8RJVKsAX5x0iHvYhb6tMvHq34jm0w +KnrH9HmTuLrQeV5ATADixM20WDw82sflwEF6T51IFz5cZtNBq/OQfaj4WMJBUItI +nOx9A3md89P0i0GsvuNgJY0z5yo2bxBvLLUUFTE72i2JBApIX+CllQfktEG3+Hux +5AhB1HxaV4kuDA2ZWiihCkWfkc+V61GU52GtKT4qBM6WY9MGiyWlENDcXJU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1hyNmxJsuk/qT0yuDb992Rb0xpsMzoY4FSmodq8phY5wjR5UPl+YcQjryknU3D +HM4/MF1OPZNeqWP+Kw6hrl+likTCmDSspV0P7FHUPYddfRmYXT3RB/MYY6V7kXs5 +BQASDC2aUsXI5Bli7+us9XF0FavyQujA43uplNMQGlPNBP23dhUfJAU4xYItLv2V +A+kI5yo54hfcS+TC337F40Fx99Pc6IZdQBtTV6ekqoVDXprc4ABvWtQunESfaLsN +yQKCAQEAnmrKlts5QPaaDCkJZGehI2kuufLLa3qL7GyQzu+eVVJmpb43baSsFIQN +kqO8+ftx03wnhKzleV8x+ieiZL7+PobkdqB+nnhZrgXdLjTIpRVYx63zU+uBy3Wq +N91jr/i+D1uGuBf4vi5CHRZlnE2UNgNcab8fM6XuQNdrNNSIryDUn1/0hnzgf4Bd +VBsnKOs0s8HnEPFbs3qRWuL97t6YaEU0sv2iztXK5/Q/A6GRVpzLCJrmlw7D5+2C +/5DM2QAnK8vRzLk8yBNnWx6HTFLh778ZY+znjlUXYeei1y1jYegOTZ80uBfd3j0y ++av2Juj0AUfUiWC86T5Vh5m/zJ2fbQ== +-----END PRIVATE KEY----- diff --git a/test/tls/public.pem b/test/tls/public.pem new file mode 100644 index 0000000..686e71b --- /dev/null +++ b/test/tls/public.pem @@ -0,0 +1,33 @@ +-----BEGIN CERTIFICATE----- +MIIFnzCCA4egAwIBAgIJAM37vtiKVTNhMA0GCSqGSIb3DQEBCwUAMGUxCzAJBgNV +BAYTAlVTMQswCQYDVQQIDAJORTEOMAwGA1UEBwwFT21haGExFDASBgNVBAoMC1dl +YnRpZGUgTExDMQ8wDQYDVQQLDAZDb21ldEQxEjAQBgNVBAMMCWxvY2FsaG9zdDAg +Fw0xNzA1MjUxMTI1NThaGA8yMTE3MDUwMTExMjU1OFowZTELMAkGA1UEBhMCVVMx +CzAJBgNVBAgMAk5FMQ4wDAYDVQQHDAVPbWFoYTEUMBIGA1UECgwLV2VidGlkZSBM +TEMxDzANBgNVBAsMBkN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