Skip to main content
Module

x/opine/src/middleware/bodyParser/urlencoded.ts

Fast, minimalist web framework for Deno ported from ExpressJS.
Go to Latest
File
/*! * Port of body-parser (https://github.com/expressjs/body-parser) for Deno. * * Licensed as follows: * * (The MIT License) * * Copyright (c) 2014 Jonathan Ong <me@jongleberry.com> * Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.com> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * 'Software'), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */
import { createError } from "../../../deps.ts";import { read } from "./read.ts";import { getCharset } from "./getCharset.ts";import { Request, Response, NextFunction } from "../../types.ts";import { hasBody } from "../../../deps.ts";import { typeChecker } from "./typeChecker.ts";
/** * Create a middleware to parse urlencoded bodies. * * @param {object} [options] * @return {function} * @public */export function urlencoded(options: any = {}) { const inflate = options.inflate !== false; const type = options.type || "application/x-www-form-urlencoded"; const verify = options.verify || false;
if (verify !== false && typeof verify !== "function") { throw new TypeError("option verify must be function"); }
// create the appropriate type checking function const shouldParse = typeof type !== "function" ? typeChecker(type) : type;
function parse(buf: string) { return buf.length ? Object.fromEntries( new URLSearchParams(decodeURIComponent(buf.replace(/\+/g, " "))) .entries(), ) : {}; }
return function urlencodedParser( req: Request, res: Response, next: NextFunction, ) { if ((req as any)._isParsed) { return next(); }
// skip requests without bodies if ( !hasBody(req.headers) || parseInt(req.headers.get("content-length") || "") === 0 ) { (req as any).parsedBody = Object.fromEntries( new URLSearchParams().entries(), ); return next(); }
// determine if request should be parsed if (!shouldParse(req)) { return next(); }
// assert charset const charset = getCharset(req) || "utf-8"; if (charset !== "utf-8") { return next( createError( 415, 'unsupported charset "' + charset.toUpperCase() + '"', ), ); }
// read read(req, res, next, parse, { encoding: charset, inflate: inflate, verify: verify, }); };}