Utilizing nodes parseQuery() util. Closes #123
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Introduces a failing Request#param() spec, yet to be fixed.
authored by visionmedia · committed by visionmedia · parent 9a2178d · Browse files · 5 files·+8 added−88 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+8 added−88 removed
- tests:+2 added−28 removed
- other:+6 added−60 removed
Provenance
- Commit
8953450- Tree
3a0bf8b- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
9a2178d
5 files changed+8 added−88 removed
modified:lib/express/plugins/body-decoder.js+3 added-1 removed
1234567895 unmodified lines151617181719202122
// Express - BodyDecoder - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
var queryString = require('querystring')
exports.BodyDecoder = Plugin.extend({ on: { /**5 unmodified lines request: function(event) { var request = event.request if (request.header('content-type') && request.header('content-type').indexOf('application/x-www-form-urlencoded') > -1) request.params.post = parseParams(request.body) request.params.post = queryString.parseQuery(request.body) } }})No newline at end of fileNo newline at end of filemodified:lib/express/helpers.js+1 added-58 removed
90 unmodified lines9192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511529590 unmodified lines
exports.escapeRegexp = function(string, chars) { var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\') return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')}
/** * Decode values in _params_. * * @param {hash} params * @see parseNestedParams() * @api private */
function decode(params) { for (var key in params) params[key] = decodeURIComponent(params[key]).replace(/\+/g, ' ')}
/** * Parse nested _params_. * * @param {hash} params * @return {hash} * @see parseParams() * @api public */
exports.parseNestedParams = function(params) { var parts, key decode(params) for (key in params) if (parts = key.split('[')) if (parts.length > 1) for (var i = 0, prop = params, len = parts.length; i < len; ++i) { var name = parts[i].replace(']', '') if (i == len - 1) prop[name] = params[key], prop = params, delete params[key] else prop = prop[name] = prop[name] || {} } return params}
/** * Parse params _str_ into a nested hash. * * @param {string} str * @return {hash} * @api public */
exports.parseParams = function(str) { if (typeof str !== 'string') return return exports.parseNestedParams($(str.split('&')).reduce({}, function(params, pair){ pair = pair.split('=') params[pair[0]] = pair[1] return params }))}}No newline at end of filemodified:lib/express/request.js+2 added-1 removed
5 unmodified lines6 */7 8 var StaticFile = require('express/static').File,9 statusBodies = require('http').STATUS_CODES,10+ queryString = require('querystring'),11 url = require('url')12 13 // --- InvalidStatusCode14 52 unmodified lines67 this.response = response68 this.url = url.parse(this.url)69 this.url.pathname = exports.normalizePath(this.url.pathname)70 this.params = { 70- get: parseParams(this.url.query) || {}, 71+ get: this.url.query ? queryString.parseQuery(this.url.query) : {}, 72 post: {}, 73 path: {}74 }75 this.plugins = $(Express.plugins).map(function(plugin){189 unmodified linesmodified:spec/spec.helpers.js-27 removed
1 2 describe 'Express'3- describe 'parseParams()'4- it 'should parse simple query string key / value pairs'5- parseParams('foo=bar').should.eql { foo: 'bar' }6- parseParams('foo=bar&baz=1').should.eql { foo: 'bar', baz: '1' }7- end8- 9- it 'should parse named nested params'10- var user = { user: { name: 'tj', email: 'tj@vision-media.ca' }}11- parseParams('user[name]=tj&user[email]=tj@vision-media.ca').should.eql user12- end13- 14- it 'should parse several levels of nesting'15- var user = { user: { name: 'tj', email: { primary: 'tj@vision-media.ca' }}}16- parseParams('user[name]=tj&user[email][primary]=tj@vision-media.ca').should.eql user17- end18- 19- it 'should convert + to literal space'20- parseParams('foo=bar+baz++that').should.eql { foo: 'bar baz that' }21- parseParams('user[name]=tj+holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}22- end23- 24- it 'should decode hex literals'25- parseParams('foo=bar%20baz%20%20that').should.eql { foo: 'bar baz that' }26- parseParams('user[name]=tj%20holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}27- end28- end29- 3 describe 'toArray()'4 describe 'when given an array'5 it 'should return the array'6 toArray([1,2,3]).should.eql [1,2,3]42 unmodified linesmodified:spec/spec.request.js+2 added-1 removed
152 unmodified lines153 get('/user/12').body.should.eql '12'154 get('/product/ipod').body.should.eql 'ipod'155 end156 157- it 'should access request.url.params'157+ it 'should work with a query string'158 get('/user', function(){159 return this.param('page') || 'First page'160 })161 get('/user').body.should.eql 'First page'162 get('/user?page=2').body.should.eql '2'163+ get('/user?foo[]=bar&page=5').body.should.eql '5'164 end165 166 it 'should work with splats'167 get('/:path/*.*', function(path, file, ext){7 unmodified lines