Skip to content

Commit

Permalink
Fix body parsing for x-www-form-urlencoded and empty
Browse files Browse the repository at this point in the history
Form data posted with application/x-www-form-urlencoded
was ignored. This has been addressed and tested, and the body
is now populated.

The default handler now shows the content type received so
that users can understand what they are sending in via curl
or other mechanisms.

When no content-type was given, the template did not attempt to
do any form of parsing. This has been updated, so set a plain
text body type when the sender doesn't provide one.

Tested with node and express.js with various content-types.

A tech-debt item is that the body parser is now considered
deprecated, and a new parser needs to be moved to.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Feb 10, 2021
1 parent 668ebe7 commit 4d5d8bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions template/node12/function/handler.js
Expand Up @@ -2,11 +2,11 @@

module.exports = async (event, context) => {
const result = {
'status': 'Received input: ' + JSON.stringify(event.body)
'body': JSON.stringify(event.body),
'content-type': event.headers["content-type"]
}

return context
.status(200)
.succeed(result)
}

45 changes: 30 additions & 15 deletions template/node12/index.js
Expand Up @@ -9,18 +9,39 @@ const app = express()
const handler = require('./function/handler');
const bodyParser = require('body-parser')

const defaultMaxSize = '100kb' // body-parser default

app.disable('x-powered-by');

const rawLimit = process.env.MAX_RAW_SIZE || defaultMaxSize
const jsonLimit = process.env.MAX_JSON_SIZE || defaultMaxSize

app.use(function addDefaultContentType(req, res, next) {
// When no content-type is given, the body element is set to
// nil, and has been a source of contention for new users.

if(!req.headers['content-type']) {
req.headers['content-type'] = "text/plain"
}
next()
})

if (process.env.RAW_BODY === 'true') {
var rawLimit = process.env.MAX_RAW_SIZE || '100kb' // body-parser default
app.use(bodyParser.raw({ type: '*/*' , limit: rawLimit }))
} else {
var jsonLimit = process.env.MAX_JSON_SIZE || '100kb' //body-parser default
app.use(bodyParser.json({ limit: jsonLimit}));
app.use(bodyParser.raw()); // "Content-Type: application/octet-stream"
app.use(bodyParser.text({ type : "text/*" }));
app.use(bodyParser.json({ limit: jsonLimit}));
app.use(bodyParser.urlencoded({ extended: true }));
}

const isArray = (a) => {
return (!!a) && (a.constructor === Array);
};

const isObject = (a) => {
return (!!a) && (a.constructor === Object);
};

class FunctionEvent {
constructor(req) {
this.body = req.body;
Expand Down Expand Up @@ -70,8 +91,8 @@ class FunctionContext {
}
}

var middleware = async (req, res) => {
let cb = (err, functionResult) => {
const middleware = async (req, res) => {
const cb = (err, functionResult) => {
if (err) {
console.error(err);

Expand All @@ -89,8 +110,8 @@ var middleware = async (req, res) => {
}
};

let fnEvent = new FunctionEvent(req);
let fnContext = new FunctionContext(cb);
const fnEvent = new FunctionEvent(req);
const fnContext = new FunctionContext(cb);

Promise.resolve(handler(fnEvent, fnContext, cb))
.then(res => {
Expand All @@ -113,13 +134,7 @@ app.options('/*', middleware);
const port = process.env.http_port || 3000;

app.listen(port, () => {
console.log(`OpenFaaS Node.js listening on port: ${port}`)
console.log(`node12 listening on port: ${port}`)
});

let isArray = (a) => {
return (!!a) && (a.constructor === Array);
};

let isObject = (a) => {
return (!!a) && (a.constructor === Object);
};

0 comments on commit 4d5d8bb

Please sign in to comment.