mirror of
https://github.com/sbrl/powahroot.git
synced 2024-12-03 11:13:01 +00:00
Add parse post data middleware example
This commit is contained in:
parent
4979c04bee
commit
d72cb648df
2 changed files with 44 additions and 0 deletions
|
@ -6,3 +6,4 @@ logo-large.png
|
|||
docs.css
|
||||
build
|
||||
.gitmodules
|
||||
examples/
|
||||
|
|
43
examples/parse_post_data.mjs
Normal file
43
examples/parse_post_data.mjs
Normal file
|
@ -0,0 +1,43 @@
|
|||
"use strict";
|
||||
|
||||
import RouterContext from 'powahroot/Server/RouterContext.mjs';
|
||||
//import { middleware_catch_errors } from './monitor.mjs';
|
||||
|
||||
// Maximum body size in characters
|
||||
const max_request_body_length = 128 * 1024; // 128K
|
||||
|
||||
|
||||
function depostify(postdata) {
|
||||
return postdata.split("&").reduce(function (decoded, chunk) {
|
||||
chunk = chunk.split("=").map(decodeURIComponent);
|
||||
decoded[chunk[0]] = chunk[1];
|
||||
return decoded;
|
||||
}, {})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses URL-encoded POST data out into the context.env.post_data variable.
|
||||
* @param {RouterContext} context The context object.
|
||||
* @param {Function} next Function to invoke the next middleware item
|
||||
*/
|
||||
async function middleware_parse_post(context, next) {
|
||||
let raw_post_data = "";
|
||||
context.request.on("data", async (chunk) => {
|
||||
if(raw_post_data.length + chunk.length > max_request_body_length) {
|
||||
context.send.plain(413, "Error: Request payload was too large.");
|
||||
return;
|
||||
}
|
||||
raw_post_data += chunk;
|
||||
});
|
||||
|
||||
context.request.on("end", async () => {
|
||||
context.env.post_data = depostify(raw_post_data);
|
||||
// Institute another error-catching safety net. This is needed because
|
||||
// we're in an event here, which breaks the async chain.
|
||||
//await middleware_catch_errors(context, next);
|
||||
await next();
|
||||
});
|
||||
}
|
||||
|
||||
export default middleware_parse_post;
|
Loading…
Reference in a new issue