
/**
 * Do sending
 */
var executeSender = function(req, sender, body){
    if (sender.type == 'POST') {
        module.exports.executeSenderHTTP(req, sender, body);
    }
    if (sender.type == 'GET') {
        module.exports.executeSenderHTTP(req, sender);
    }
    if (sender.type == 'SOCKET') {
        module.exports.executeSenderSocket(req, sender, body);
    }
}

/**
 * Do HTTP POST sending
 */
var executeSenderHTTP = function(req, sender, body){
    var request = require('request');
    console.log('[SENDER] execute ', sender.type , ' to ', sender.url);
    // Set the headers
    var headers = {
        'User-Agent':       'Super Agent/0.0.1',
        'Content-Type':     'application/x-www-form-urlencoded',
    }

    // Configure the request
    var options = {
        url: sender.url,
        method: sender.type,
        headers: headers,
        data: body
    }

    // Start the request
    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            // Print out the response body
            //console.log(body)
        }
    })
}

/**
 * Do socket sending
 */
var executeSenderSocket = function(req, sender, body){
    console.log('[SOCKET] execute socket ', sender.url);
    if (typeof global.socket != 'undefined') {
        global.socket.emit(sender.url, body);
        console.log('[SOCKET] pushed');
    }
}

/**
 * Loop listeners
 */
var loopListeners = function(listeners, senders, req, method, uri, body){
    var ret = false;
    listeners.forEach(function(listener){
        if (listener.type == method && listener.url == uri)  {
            listener.senders.forEach(function(senderName){
                senders.forEach(function(sender){
                    if (sender.name == senderName) {
                        console.log('[SENDER] matched ', senderName);
                        module.exports.executeSender(req,sender, body);
                        ret =  true;
                    }
                });
            });
        }
    });
    return ret;
}

var writeSettings = function(req, res, file, data, callback){
    var fs = require('fs');
    fs.writeFile(file, data, function(err) {
        if(err) {
            return console.log(err);
        }
        callback();
        console.log("The file was saved!");
    });


}

var requestIsStatic = function(req,res){
    if(req.url.indexOf('.jpg')> 0 || req.url.indexOf('.gif')> 0 || req.url.indexOf('.png')> 0 || req.url.indexOf('.js')> 0 || req.url.indexOf('.css')> 0) {
        return true;
    }
    return false;
}

var serveStatic = function(req,res){
    var path = require('path');
    if (req.url.indexOf('js')> 0){
        var file = './' + req.url;
    }else if (req.url.indexOf('css')> 0){
        var file = './' + req.url;
    }else{
        var file = './' + req.url;
        console.log(file);
    }
    if (path.existsSync(file)) {
        var html = fs.readFileSync(file);
        var mime = require('mime')
        var mimetype = mime.lookup(file);
        res.writeHead(200, {'Content-Type': mimetype});
        res.end(html);
    }else{
        res.writeHead(404, {'Content-Type': 'text/html'});
        res.end('file does not exist');
    }
}


module.exports = {
  executeSenderSocket: executeSenderSocket,
  executeSenderHTTP: executeSenderHTTP,
  loopListeners: loopListeners,
  executeSender: executeSender,
  requestIsStatic: requestIsStatic,
  serveStatic: serveStatic,
  writeSettings: writeSettings
}

@@ -496,98 +496,238 @@
     
-console.log('%5BSENDER%5D execute ', sender.type , ' to ', sender.url);%0A
+var querystring = require('querystring');%0A    var headers = %7B%0A        'Content-Type':     'application/x-www-form-urlencoded',%0A    %7D%0A    if (sender.type == 'POST') %7B%0A   
     
+ 
 //
- Set the
+POST%0A        request.post(%7B%0A          headers:
  headers
 %0A   
@@ -726,179 +726,445 @@
 ders
+,
 %0A    
-var headers = %7B%0A        'User-Agent':       'Super Agent/0.0.1',%0A        'Content-Type':     'application/x-www-form-urlencoded',%0A    %7D%0A%0A    // Configure the request%0A
+      url:     sender.url,%0A          headers: headers,%0A          body:    body%0A        %7D, function(error, response, body)%7B%0A          if (!error && response.statusCode == 200) %7B%0A                console.log('%5BPOSTREQUEST%5D request returned OK');%0A                console.log(body)%0A            %7D else %7B%0A                console.log('%5BPOSTREQUEST%5D request returned NOK: ' , error);%0A            %7D%0A        %7D);%0A    %7Delse%7B%0A%0A        //GET%0A    
     
@@ -1183,24 +1183,28 @@
 = %7B%0A        
+  
+  
 url: sender.
@@ -1200,32 +1200,36 @@
 rl: sender.url,%0A
+    
         method: 
@@ -1241,32 +1241,36 @@
 r.type,%0A        
+    
 headers: headers
@@ -1283,26 +1283,15 @@
     
-data: body
+%7D%0A
 %0A    
-%7D%0A%0A
     
@@ -1311,16 +1311,20 @@
 request%0A
+    
     requ
@@ -1367,24 +1367,28 @@
 se, body) %7B%0A
+    
         if (
@@ -1443,39 +1443,243 @@
     
-// Print out the response body%0A
+    console.log('%5BGETREQUEST%5D request returned OK');%0A                // Print out the response body%0A                console.log(body)%0A            %7D else %7B%0A                console.log('%5BGETREQUEST%5D request returned NOK: ' , error);%0A    
     
@@ -1705,25 +1705,27 @@
 og(body)
-%0A
+;%0A 
         
 %7D%0A    %7D)
@@ -1716,24 +1716,37 @@
         
-%7D%0A    %7D)
+   %7D%0A        %7D)%0A    %7D
 %0A%7D%0A%0A/**%0A

split GET and POST logic