Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions lib/node-pusher.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down