diff --git a/coffee/http-listener.coffee b/coffee/http-listener.coffee index ef6315c..15a999b 100644 --- a/coffee/http-listener.coffee +++ b/coffee/http-listener.coffee @@ -45,19 +45,23 @@ exports = module.exports = ( args ) => module.exports indexEvent = ( event, body, resp ) -> - try if typeof body is 'string' - obj = payload: JSON.parse body + try + obj = qs.parse body + catch err + try + obj = JSON.parse body + catch err + resp.send 400, 'Badly formed event!' + return else - obj = payload: body + obj = body timestamp = ( new Date() ).toISOString() rand = ( Math.floor Math.random() * 10e9 ).toString( 16 ).toUpperCase() obj.event = event obj.eventid = "#{ obj.event }_UTC|#{ timestamp }_#{ rand }" db.pushEvent obj resp.send 200, "Thank you for the event: #{ obj.eventid }" - catch err - resp.send 400, 'Badly formed event!' # Activate a webhook. the body will be JSON parsed, the name of the webhook will # be the event name given to the event object, a timestamp will be added @@ -69,8 +73,6 @@ activateWebHook = ( app, name ) => body += data req.on 'end', -> - console.log body - console.log typeof body indexEvent name, body, resp # This is a hack to quickly allow storing of public accessible data if name is 'uptimestatistics' diff --git a/js/http-listener.js b/js/http-listener.js index 906076b..4fce30f 100644 --- a/js/http-listener.js +++ b/js/http-listener.js @@ -50,26 +50,28 @@ HTTP Listener indexEvent = function(event, body, resp) { var err, obj, rand, timestamp; - try { - if (typeof body === 'string') { - obj = { - payload: JSON.parse(body) - }; - } else { - obj = { - payload: body - }; + if (typeof body === 'string') { + try { + obj = qs.parse(body); + } catch (_error) { + err = _error; + try { + obj = JSON.parse(body); + } catch (_error) { + err = _error; + resp.send(400, 'Badly formed event!'); + return; + } } - timestamp = (new Date()).toISOString(); - rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase(); - obj.event = event; - obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand; - db.pushEvent(obj); - return resp.send(200, "Thank you for the event: " + obj.eventid); - } catch (_error) { - err = _error; - return resp.send(400, 'Badly formed event!'); + } else { + obj = body; } + timestamp = (new Date()).toISOString(); + rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase(); + obj.event = event; + obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand; + db.pushEvent(obj); + return resp.send(200, "Thank you for the event: " + obj.eventid); }; activateWebHook = (function(_this) { @@ -83,8 +85,6 @@ HTTP Listener }); return req.on('end', function() { var fPath; - console.log(body); - console.log(typeof body); indexEvent(name, body, resp); if (name === 'uptimestatistics') { fPath = path.resolve(__dirname, '..', 'webpages', 'public', 'data', 'histochart.json');