summaryrefslogtreecommitdiff
path: root/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/express/node_modules/connect/lib/middleware/urlencoded.js')
-rw-r--r--node_modules/express/node_modules/connect/lib/middleware/urlencoded.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js b/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js
new file mode 100644
index 0000000..cceafc0
--- /dev/null
+++ b/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js
@@ -0,0 +1,78 @@
+
+/*!
+ * Connect - urlencoded
+ * Copyright(c) 2010 Sencha Inc.
+ * Copyright(c) 2011 TJ Holowaychuk
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var utils = require('../utils')
+ , _limit = require('./limit')
+ , qs = require('qs');
+
+/**
+ * noop middleware.
+ */
+
+function noop(req, res, next) {
+ next();
+}
+
+/**
+ * Urlencoded:
+ *
+ * Parse x-ww-form-urlencoded request bodies,
+ * providing the parsed object as `req.body`.
+ *
+ * Options:
+ *
+ * - `limit` byte limit disabled by default
+ *
+ * @param {Object} options
+ * @return {Function}
+ * @api public
+ */
+
+exports = module.exports = function(options){
+ options = options || {};
+
+ var limit = options.limit
+ ? _limit(options.limit)
+ : noop;
+
+ return function urlencoded(req, res, next) {
+ if (req._body) return next();
+ req.body = req.body || {};
+
+ if (!utils.hasBody(req)) return next();
+
+ // check Content-Type
+ if ('application/x-www-form-urlencoded' != utils.mime(req)) return next();
+
+ // flag as parsed
+ req._body = true;
+
+ // parse
+ limit(req, res, function(err){
+ if (err) return next(err);
+ var buf = '';
+ req.setEncoding('utf8');
+ req.on('data', function(chunk){ buf += chunk });
+ req.on('end', function(){
+ try {
+ req.body = buf.length
+ ? qs.parse(buf, options)
+ : {};
+ next();
+ } catch (err){
+ err.body = buf;
+ next(err);
+ }
+ });
+ });
+ }
+};