From e4be2f5a369451d68ff359ff168cd049831e0e03 Mon Sep 17 00:00:00 2001 From: Gilbert Roulot Date: Fri, 15 Jun 2012 11:29:00 +0200 Subject: [PATCH] Adds host and port options. Adds SSL support. host and port of the API server can now be given as options. SSL can also be requested in the options. --- lib/node-pusher.js | 55 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/node-pusher.js b/lib/node-pusher.js index 1b6f29b..8fb7b0f 100644 --- a/lib/node-pusher.js +++ b/lib/node-pusher.js @@ -1,6 +1,7 @@ module.exports = (function() { var crypto = require('crypto'); var http = require('http'); + var https = require('https'); var Pusher = function(options) { this.options = options; @@ -45,21 +46,55 @@ module.exports = (function() { path = path + '?' + queryString + '&auth_signature=' + signature; - var client = http.createClient(80, this.domain); - var request = client.request('POST', path, { - 'host': this.domain, - 'content-type': 'application/json', - 'content-length': new Buffer(requestBody).toString('binary').length - }); + var domain; + if (this.options.host) { + domain = this.options.host + } else { + domain = this.domain + } + var port; + if (this.options.port) { + port = this.options.port + } else { + port = this.options.ssl ? 443 : 80; + } + + var options = { + 'method': 'POST', + 'path': path, + 'host': domain, + 'port': port, + 'headers': { + 'content-type': 'application/json', + 'content-length': new Buffer(requestBody).toString('binary').length + } + } + + var request; + if (this.options.ssl) { + // Verify server certificate with our CAs + options['rejectUnauthorized'] = true + options.agent = new https.Agent(options); + request = https.request(options); + } else { + request = http.request(options); + } if(callback) { - client.addListener('error', function(error) { - callback(error, request, null); + var response = ''; + request.on('data', function(data) { + response += data; + if (response.length > 1e4) { + // No slanger server will ever reply more than a short line of text + request.connection.destroy(); + } }); - - request.addListener('response', function(response) { + request.on('end', function() { callback(null, request, response); }); + request.on('error', function(error) { + callback(error, request, null); + }); } request.write(requestBody);