diff --git a/lib/apis.js b/lib/apis.js index 9b0c915..a81318f 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -494,12 +494,19 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { // Twilio Parameters are often 26 long var bodyParserTwilio = require('body-parser').urlencoded({ limit: '4kb', parameterLimit: 100, extended: false }); - //var bodyParserMailgun = require('body-parser').urlencoded({ limit: '4kb', parameterLimit: 100, extended: false }); - function bodyParserMailgun (req, res, next) { + var bodyParserMailgun = require('body-parser').urlencoded({ limit: '100kb', parameterLimit: 500, extended: false }); + function bodyMultiParserMailgun (req, res, next) { var multiparty = require('multiparty'); var form = new multiparty.Form(); form.parse(req, function (err, fields/*, files*/) { + if (err) { + console.error('Error'); + console.error(err); + res.end("Couldn't parse form"); + return; + } + var body; req.body = req.body || {}; Object.keys(fields).forEach(function (key) { @@ -526,13 +533,15 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { // // Webhook Parsers // - , 'mailgun.urlencoded@daplie.com': function (req, res, next) { - return bodyParserMailgun(req, res, function () { + //, 'mailgun.urlencoded@daplie.com': function (req, res, next) { ... } + , 'mailgun.parsers@daplie.com': function (req, res, next) { + console.log('mailgun parser req.headers'); + console.log(req.headers); + + function verify() { var body = req.body; var mailconf = siteConfig['mailgun.org']; - console.log('mailgun req.headers'); - console.log(req.headers); if (!validateMailgun(mailconf.apiKey, body.timestamp, body.token, body.signature)) { console.error('Request came, but not from Mailgun'); res.send({ error: { message: 'Invalid signature. Are you even Mailgun?' } }); @@ -540,7 +549,17 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { } next(); - }); + } + + if (/urlencoded/.test(req.headers['content-type'])) { + bodyParserMailgun(req, res, verify); + } + else if (/multipart/.test(req.headers['content-type'])) { + bodyMultiParserMailgun(req, res, verify); + } + else { + next(); + } } , 'twilio.urlencoded@daplie.com': function (req, res, next) { // TODO null for res and Promise instead of next?