/*
 * webhook.js: Transport for logging to remote http endpoints ( POST / RECEIVE webhooks )
 *
 * (C) 2011 Marak Squires
 * MIT LICENCE
 *
 */

var events = require('events'),
    http = require('http'),
    util = require('util'),
    common = require('../common'),
    Transport = require('./transport').Transport; 

//
// ### function WebHook (options)
// #### @options {Object} Options for this instance.
// Constructor function for the Console transport object responsible
// for making arbitrary HTTP requests whenever log messages and metadata
// are received.
//
var Webhook = exports.Webhook = function (options) {
  Transport.call(this, options);

  this.name   = 'webhook'; 
  this.host   = options.host   || 'localhost';
  this.port   = options.port   || 8080;
  this.method = options.method || 'POST';
  this.path   = options.path   || '/winston-log';

  if (options.auth) {
    //
    // TODO: add http basic auth options for outgoing HTTP requests
    //     
  }
  
  if (options.ssl) {
    //
    //  TODO: add ssl support for outgoing HTTP requests
    //
  }  
};

//
// Inherit from `winston.Transport`.
//
util.inherits(Webhook, Transport);

//
// Expose the name of this Transport on the prototype
//
Webhook.prototype.name = 'webhook';

//
// ### function log (level, msg, [meta], callback)
// #### @level {string} Level at which to log the message.
// #### @msg {string} Message to log
// #### @meta {Object} **Optional** Additional metadata to attach
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Webhook.prototype.log = function (level, msg, meta, callback) {
  if (this.silent) {
    return callback(null, true);
  }
  
  var self = this,
      message = common.clone(meta),
      options,
      req;
      
  message.level = level;
  message.message = msg;

  // Prepare options for outgoing HTTP request
  options = {
    host: this.host,
    port: this.port,
    path: this.path,
    method: this.method,
    headers: { 'Content-Type': 'application/json' }
  };
  
  // Perform HTTP logging request
  req = http.request(options, function (res) { 
    //
    // No callback on request, fire and forget about the response
    //
    self.emit('logged');
  }); 

  req.on('error', function (err) {
    //
    // Propagate the `error` back up to the `Logger` that this
    // instance belongs to.
    //
    self.emit('error', err);
  });
  
  //
  // Write logging event to the outgoing request body
  //
  // jsonMessage is currently conforming to JSON-RPC v1.0, 
  // but without the unique id since there is no anticipated response 
  // see: http://en.wikipedia.org/wiki/JSON-RPC
  // 
  req.write(JSON.stringify({ 
    method: 'log', 
    params: { 
      timestamp: common.timestamp(), 
      msg: msg, 
      level: level, 
      meta: meta 
    } 
  }));
  
  req.end();
  
  // Always return true, regardless of any errors
  callback(null, true);
};

@@ -194,24 +194,54 @@
 re('http'),%0A
+    https = require('https'),%0A
     util = r
@@ -1035,80 +1035,159 @@
     
-//%0A    //  TODO: add ssl support for outgoing HTTP requests%0A    //
+this.ssl      = %7B%7D;%0A    this.ssl.key  = options.ssl.key  %7C%7C null;%0A    this.ssl.cert = options.ssl.cert %7C%7C null;%0A    this.ssl.ca   = options.ssl.ca;
 %0A  %7D
-  
 %0A%7D;%0A
@@ -2187,16 +2187,135 @@
 ' %7D%0A  %7D;
+%0A%0A  if (self.ssl) %7B%0A    options.ca = this.ssl.ca;%0A    options.key = this.ssl.key;%0A    options.cert = this.ssl.cert;%0A  %7D
 %0A  %0A  //
@@ -2352,20 +2352,41 @@
   req = 
-http
+(self.ssl ? https : http)
 .request
@@ -2411,17 +2411,16 @@
  (res) %7B
- 
 %0A    //%0A

Add SSL support in webhook transport