Skip to content
Myr

Decoding param values and supporting urlencoded request bodies

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent 0e40813 · Browse files · 4 files+34 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+34 added−7 removed

  • tests:+10 added−0 removed
  • other:+24 added−7 removed

4 files+34 added−7 removed

modified:examples/app.js+2 added-5 removed
modified:lib/express/core.js+7 added-2 removed
231 unmodified lines
232 * Route the given _request_ and _response_.233 *234 * - Defaults response headers to {}235 * - Normalizes the request path to prep for matching routes236 * - Parses urlencoded request bodies237 * - Invokes Express.router.route() to do its job238 * - Responds immediately if the route returns a body string239 *240 * @param {object} request
4 unmodified lines
245 route: function(request, response){246 var body 247 response.headers = {}248 this.request = request, this.response = response248 this.request.uri.path = normalizePath(this.request.uri.path)249 this.request.uri.params = parseNestedParams(this.request.uri.params)249 this.request.uri.path = normalizePath(request.uri.path)250 this.request.uri.params = parseNestedParams(request.uri.params)251 this.request.uri.post = 252 header('content-type') == 'application/x-www-form-urlencoded' ?253 parseParams(request.body) :254 {}255 trigger('request')256 if (typeof (body = Express.router.route(request)) == 'string')257 halt(200, body)258 },
35 unmodified lines
modified:lib/express/helpers.js+15 added
114 unmodified lines
115 */116117exports.param = function(key) {118 return Express.router.params[key] ||119 Express.server.request.uri.post[key] ||120 Express.server.request.uri.params[key]121}122123/**
95 unmodified lines
219 return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')220}221222/**223 * Decode values in _params_.224 *225 * @param {hash} params226 * @see parseNestedParams()227 * @api private228 */229230function decode(params) {231 for (var key in params)232 params[key] = decodeURIComponent(params[key]).replace(/\+/g, ' ')233}234235/**236 * Parse nested _params_.237 *238 * @param {hash} params239 * @return {hash}
2 unmodified lines
242 */243244var parseNestedParams = exports.parseNestedParams = function(params) {245 var parts, key246 decode(params)247 for (key in params)248 if (parts = key.split('['))249 if (parts.length > 1)250 for (var i = 0, prop = params, len = parts.length; i < len; ++i) {
39 unmodified lines
modified:spec/spec.helpers.js+10 added
17 unmodified lines
18 it 'should parse several levels of nesting'19 var user = { user: { name: 'tj', email: { primary: 'tj@vision-media.ca' }}}20 parseParams('user[name]=tj&user[email][primary]=tj@vision-media.ca').should.eql user21 end22 23 it 'should convert + to literal space'24 parseParams('foo=bar+baz++that').should.eql { foo: 'bar baz that' }25 parseParams('user[name]=tj+holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}26 end27 28 it 'should decode hex literals'29 parseParams('foo=bar%20baz%20%20that').should.eql { foo: 'bar baz that' }30 parseParams('user[name]=tj%20holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}31 end32 end33 34 describe 'toArray()'35 describe 'when given an array'
176 unmodified lines