PeerSocket.prototype.init = function() { var self = this; this.ws._socket.removeAllListeners('data'); // Remove WebSocket data handler. this.ws._socket.on('end', this.onEnd.bind(this)); this.agent = spdy.createAgent(SpdyAgent, { host: this.appName, port: 80, socket: this.ws._socket, spdy: { plain: true, ssl: false } }); // TODO: Remove this when bug in agent socket removal is fixed. this.agent.maxSockets = 150; this.agent.on('push', this.onPushData.bind(this)); // subscribe to all prev subscriptions this.subscriptions.forEach(this.subscribe.bind(this)); if(this._pingTimer) { clearInterval(this._pingTimer); } this._pingTimer = setInterval(function() { self.agent.ping(function(err) { //TODO: Handle a lack of PONG. }); }, 10 * 1000); };
describe('spdy', function(){ var spdy = require('spdy'); var https = require('https'); var http = require('http'); var agent = spdy.createAgent({ host: '127.0.0.1', port: config.services.spdy.port }); xit('should be listening for spdy', function( next ) { http.get({ host: config.services.spdy.host, path: '/', agent: agent }, function(res) { assert.equal( res.statusCode , 201 ); agent.close(); next() }).end(); }); });
it('should support callbacks', function (done) { PORT++ spdy.createServer(require('spdy-keys'), function (req, res) { new Polyfill(polyfill, req, res).push(function (err) { assert.ifError(err) res.statusCode = 204 res.end() }) }).listen(PORT) var agent = spdy.createAgent({ port: PORT, rejectUnauthorized: false }) https.get({ host: 'localhost', agent: agent }, function (res) { assert.equal(res.statusCode, 204) }) agent.on('push', function (stream) { assert.equal(stream.url, '/polyfill.js') assert.equal(stream.headers['content-encoding'], 'gzip') assert.equal(stream.headers['content-type'], 'application/javascript; charset=UTF-8') assert.equal(stream.headers['vary'], 'User-Agent') assert(stream.headers['cache-control']) assert(stream.headers['etag']) done() }) })
function createOptions(url) { var options = require('url').parse(url) options.plain = options.protocol == 'http:' options.headers = { 'user-agent' : argv.u || 'curl/7.38.0', // Let's make them think we are curl for now 'accept' : argv.a || '*/*', 'accept-encoding' : argv.e || '*' } options.servername = options.hostname if (argv.r == 'spdy') { options.headers['host'] = options.hostname options.agent = spdy.createAgent({ host: options.hostname, port: (options.plain ? 80 : 443), }); } else if (!options.plain) { options.headers[':authority'] = options.hostname } // Proxy present if (argv.p) { options.hostname = options.host = argv.p.split(':')[0] options.port = argv.p.split(':')[1] } return options }
var testPushStreams = function(options, done) { var app = express(); app.use(spdyPush.referrer()); app.use(express.static(path.join(__dirname, 'site'))); var server = spdy.createServer(options, app).listen(0); var port = server.address().port; var protocol = app instanceof https.Server ? 'https' : 'http'; var homepage = protocol + '://localhost:' + port + '/'; var agent = spdy.createAgent({ host: 'localhost', port: port, spdy: options, rejectUnauthorized: false }); var pushedResources = { '/style.css': { complete: deferred(), content: new streamBuffers.WritableStreamBuffer() }, '/script.js': { complete: deferred(), content: new streamBuffers.WritableStreamBuffer() }, '/black_square.png': { complete: deferred(), content: new streamBuffers.WritableStreamBuffer() }, '/blue_square.png': { complete: deferred(), content: new streamBuffers.WritableStreamBuffer() } }; agent.on('push', function (stream) { var pushedResource = pushedResources[stream.url]; pushedResource['content-type'] = stream.headers['content-type']; stream.pipe(pushedResource.content); stream.on('end', function () { pushedResource.complete.resolve(); }); }); sendRequest(agent, '/') .then(sendRequest(agent, '/style.css', { 'referer': homepage })) .then(sendRequest(agent, '/script.js', { 'referer': homepage })) .then(sendRequest(agent, '/black_square.png', { 'referer': homepage })) .then(sendRequest(agent, '/blue_square.png', { 'referer': homepage })) .then(sendRequest(agent, '/')) .then(pushedResources['/style.css'].complete.promise) .then(pushedResources['/script.js'].complete.promise) .then(pushedResources['/black_square.png'].complete.promise) .then(pushedResources['/blue_square.png'].complete.promise) .then(function () { agent.close(); pushedResources['/style.css'].content.getContents().should.have.length(53); pushedResources['/script.js'].content.getContents().should.have.length(32); pushedResources['/black_square.png'].content.getContents().should.have.length(1165); pushedResources['/blue_square.png'].content.getContents().should.have.length(1165); pushedResources['/style.css']['content-type'].should.equal('text/css'); pushedResources['/script.js']['content-type'].should.equal('application/javascript'); pushedResources['/black_square.png']['content-type'].should.equal('image/png'); pushedResources['/blue_square.png']['content-type'].should.equal('image/png'); done(); }); }
it('should have a monitor link for bar formatted correctly for SPDY requests', function(done) { var a = getHttpServer(app); if (!a.address()) a.listen(0); var agent = spdy.createAgent({ host: '127.0.0.1', port: a.address().port, spdy: { plain: true, ssl: false } }); var request = http.get({ host: '127.0.0.1', port: a.address().port, path: url, agent: agent }, function(response) { var buffers = []; response.on('readable', function() { var data; while ((data = response.read()) !== null) { buffers.push(data); } }); response.on('end', function() { var body = JSON.parse(Buffer.concat(buffers)); var fooBar = body.links.filter(function(link) { return link.title === 'foobar'; }); hasLinkRel(fooBar, rels.binaryStream); var parsed = require('url').parse(fooBar[0].href); assert.equal(parsed.protocol, 'http:'); agent.close(); }); response.on('end', done); }).end(); });
fixtures.spdyRequest = function spdyRequest(sh, uri, cb) { var agent = spdy.createAgent(sh.frontend); var req = https.request({ agent: agent, path: uri }, function(res) { var chunks = ''; res.on('readable', function() { chunks += res.read() || ''; }); res.on('end', function() { agent.close(function() { cb(res, chunks); }); }); }); req.end(); };
it('should have monitor log link formatted correctly for SPDY requests', function(done) { var a = getHttpServer(app); if (!a.address()) a.listen(0); var agent = spdy.createAgent({ host: '127.0.0.1', port: a.address().port, spdy: { plain: true, ssl: false } }); var request = http.get({ host: '127.0.0.1', port: a.address().port, path: url, agent: agent }, function(response) { var buffers = []; response.on('readable', function() { var data; while ((data = response.read()) !== null) { buffers.push(data); } }); response.on('end', function() { var body = JSON.parse(Buffer.concat(buffers)); var link = body.links.filter(function(l) { return l.rel.indexOf('monitor') > -1; })[0]; var obj = require('url').parse(link.href, true); assert.equal(obj.protocol, 'http:'); assert(obj.query.topic); agent.close(); }); response.on('end', done); }).end(); });
it('should not have an events link for SPDY requests', function(done) { var a = getHttpServer(app); if (!a.address()) a.listen(0); var agent = spdy.createAgent({ host: '127.0.0.1', port: a.address().port, spdy: { plain: true, ssl: false } }); var request = http.get({ host: '127.0.0.1', port: a.address().port, path: '/', agent: agent }, function(response) { var buffers = []; response.on('readable', function() { var data; while ((data = response.read()) !== null) { buffers.push(data); } }); response.on('end', function() { var body = JSON.parse(Buffer.concat(buffers)); var links = body.links.filter(function(l) { return l.rel.indexOf('http://rels.zettajs.io/events') > -1; }); assert.equal(links.length, 0); agent.close(); }); response.on('end', done); }).end(); });
function pull(done) { agent = spdy.createAgent({ port: port, rejectUnauthorized: false, }) agent.once('error', done) agent.once('push', function (stream) { done(null, stream) }) https.request({ agent: agent, path: '/', }) .once('error', done) .once('response', function (res) { if (res.statusCode !== 204) done(new Error('got status code: ' + res.statusCode)) res.resume() }) .end() }
PeerSocket.prototype._initWs = function(ws) { var self = this; var u = url.parse(ws.upgradeReq.url, true); // parse out connectionId this.ws = ws; this.connectionId = u.query.connectionId; this.ws._socket.removeAllListeners('data'); // Remove WebSocket data handler. this.ws._socket.on('end', function() { clearInterval(self._pingTimer); self.emit('end'); }); this.ws.on('error', function(err) { clearInterval(self._pingTimer); self.emit('error', err); }); this.agent = spdy.createAgent(SpdyAgent, { host: this.name, port: 80, socket: this.ws._socket, spdy: { plain: true, ssl: false } }); // TODO: Remove this when bug in agent socket removal is fixed. this.agent.maxSockets = 150; this.agent.on('push', this.onPushData.bind(this)); this.agent.on('error', function(err) { self.close(); self.emit('error', err); }); };
var spdy = require('spdy'); var http = require('http'); var fs = require('fs'); var agent = spdy.createAgent({ host: '0.0.0.0', rejectUnauthorized:false, key: fs.readFileSync('keys/server.key'), cert: fs.readFileSync('keys/server.crt'), ca: fs.readFileSync('keys/server.csr'), port: 8080 }); http.get({ host:'0.0.0.0', agent: agent }, function(res) { console.log('Spdy res coming in!'); var d = []; res.on('data', function(data) { d += data; }); res.on('end', function() { console.log(d); }); agent.close(); }).end();
//App stuff if (environment.match(/^development/)) { environment = 'development'; } var http = require('http'), https = require('https'), path = require('path'), spdy = require('spdy'); // Use SPDY to talk to backing services. Seems like a great fit, minimise TLS connection // overhead and multiplex requests to the same limited number of domains. https.globalAgent = spdy.createAgent({ host: 'www.google.com', port: 443 }); var rootDir = path.join(__dirname, '..'); var app = require('./appBuilder').getApp(environment, rootDir, argv.REQUIRE_BASE_URL); // Set a port for the app. There are several different ways to do this: // - In production-like environments we fall back to the config/ directory. // - On PaaS products like Heroku, we use the $PORT environment variable that they set. // - When running a Procfile locally (like with Foreman) we need to override $PORT // by setting $SPOTLIGHT_PORT, so that our local Nginx connects to the right place. var port = process.env.SPOTLIGHT_PORT || process.env.PORT || app.get('port'); app.set('port', port); var server = http.createServer(app).listen(port, function () {
fixtures.spdyRequest = function spdyRequest(sh, uri, cb) { var agent = spdy.createAgent(sh.frontend); return fixtures.agentRequest(sh, agent, uri, cb); };
var options = { host: 'localhost', port: 8081, path: '/backbone.js', method: 'GET', rejectUnauthorized: false, key: fs.readFileSync("key/userB.key"), cert: fs.readFileSync("key/userB.crt"), ca: fs.readFileSync("key/ca.crt") }; var agent = spdy.createAgent({ // host: 'ec2-52-36-120-88.us-west-2.compute.amazonaws.com', host: 'localhost', port: 8081, path: '/backbone.js', key: fs.readFileSync("key/userB.key"), cert: fs.readFileSync("key/userB.crt"), ca: fs.readFileSync("key/ca.crt") }); var req = https.request(options, function(res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function(d) { process.stdout.write(d); }); }); req.end();