~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to docs/example-server/node_modules/body-parser/lib/types/urlencoded.js

  • Committer: Roberto Alsina
  • Date: 2014-09-05 14:57:17 UTC
  • mto: (91.179.25 automatic)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: roberto.alsina@canonical.com-20140905145717-0ufcsv27w25i1nnu
added example app server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
 * body-parser
 
3
 * MIT Licensed
 
4
 */
 
5
 
 
6
/**
 
7
 * Module dependencies.
 
8
 */
 
9
 
 
10
var bytes = require('bytes')
 
11
var deprecate = require('depd')('body-parser')
 
12
var read = require('../read')
 
13
var typer = require('media-typer')
 
14
var typeis = require('type-is')
 
15
 
 
16
/**
 
17
 * Module exports.
 
18
 */
 
19
 
 
20
module.exports = urlencoded
 
21
 
 
22
/**
 
23
 * Cache of parser modules.
 
24
 */
 
25
 
 
26
var parsers = Object.create(null)
 
27
 
 
28
/**
 
29
 * Create a middleware to parse urlencoded bodies.
 
30
 *
 
31
 * @param {object} [options]
 
32
 * @return {function}
 
33
 * @api public
 
34
 */
 
35
 
 
36
function urlencoded(options){
 
37
  options = options || {};
 
38
 
 
39
  // notice because option default will flip in next major
 
40
  if (options.extended === undefined) {
 
41
    deprecate('urlencoded: explicitly specify "extended: true" for extended parsing')
 
42
  }
 
43
 
 
44
  var extended = options.extended !== false
 
45
  var inflate = options.inflate !== false
 
46
  var limit = typeof options.limit !== 'number'
 
47
    ? bytes(options.limit || '100kb')
 
48
    : options.limit
 
49
  var type = options.type || 'urlencoded'
 
50
  var verify = options.verify || false
 
51
 
 
52
  if (verify !== false && typeof verify !== 'function') {
 
53
    throw new TypeError('option verify must be function')
 
54
  }
 
55
 
 
56
  var queryparse = extended
 
57
    ? parser('qs')
 
58
    : parser('querystring')
 
59
 
 
60
  function parse(body) {
 
61
    return body.length
 
62
      ? queryparse(body)
 
63
      : {}
 
64
  }
 
65
 
 
66
  return function urlencodedParser(req, res, next) {
 
67
    if (req._body) return next();
 
68
    req.body = req.body || {}
 
69
 
 
70
    if (!typeis(req, type)) return next();
 
71
 
 
72
    var charset = typer.parse(req).parameters.charset || 'utf-8'
 
73
    if (charset.toLowerCase() !== 'utf-8') {
 
74
      var err = new Error('unsupported charset')
 
75
      err.status = 415
 
76
      next(err)
 
77
      return
 
78
    }
 
79
 
 
80
    // read
 
81
    read(req, res, next, parse, {
 
82
      encoding: charset,
 
83
      inflate: inflate,
 
84
      limit: limit,
 
85
      verify: verify
 
86
    })
 
87
  }
 
88
}
 
89
 
 
90
/**
 
91
 * Get parser for module name dynamically.
 
92
 *
 
93
 * @param {string} name
 
94
 * @return {function}
 
95
 * @api private
 
96
 */
 
97
 
 
98
function parser(name) {
 
99
  var mod = parsers[name]
 
100
 
 
101
  if (mod) {
 
102
    return mod.parse
 
103
  }
 
104
 
 
105
  // load module
 
106
  mod = parsers[name] = require(name)
 
107
 
 
108
  return mod.parse
 
109
}