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
Provenance
- Commit
a4159a7- Tree
e561a2d- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
0e40813
4 files changed+34 added−7 removed
modified:examples/app.js+2 added-5 removed
12 unmodified lines13141516171718192021212223242212 unmodified linesget('/chat', function(){ render('chat.haml.html')})
post('/chat', function(){put('/chat', function(){ p(Express.server.request.body) p(param('message'))})
put('/chat', function(){ p(Express.server.request.uri.params)})
run()No newline at end of fileNo newline at end of filemodified:lib/express/core.js+7 added-2 removed
231 unmodified lines232 * 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} request4 unmodified lines245 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 linesmodified:lib/express/helpers.js+15 added
114 unmodified lines115 */116 117 exports.param = function(key) {118 return Express.router.params[key] ||119+ Express.server.request.uri.post[key] ||120 Express.server.request.uri.params[key]121 }122 123 /**95 unmodified lines219 return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')220 }221 222 /**223+ * Decode values in _params_.224+ *225+ * @param {hash} params226+ * @see parseNestedParams()227+ * @api private228+ */229+230+function decode(params) {231+ for (var key in params)232+ params[key] = decodeURIComponent(params[key]).replace(/\+/g, ' ')233+}234+235+/**236 * Parse nested _params_.237 *238 * @param {hash} params239 * @return {hash}2 unmodified lines242 */243 244 var 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 linesmodified:spec/spec.helpers.js+10 added
17 unmodified lines18 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