Skip to content
Myr

Added POST urlencoded body parsing

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

authored by visionmedia · committed by visionmedia · parent b664df9 · Browse files · 4 files+105 added−28 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+105 added−28 removed

  • tests:+12 added−4 removed
  • other:+93 added−24 removed

4 files+105 added−28 removed

modified:lib/express.core.js+69 added-23 removed
56 unmodified lines
57 }58 }59 },60 61 FormBodyDecoder : {62 onRequest : {63 'parse urlencoded bodies' : function() {64 if (Express.requestHeader('Content-Type') == 'application/x-www-form-urlencoded')65 Express.request.uri.params = Express.parseParams(unescape(Express.request.body))66 }67 }68 },69 70 Logger : {71 onRequest : {72 'output log data' : function(){73 puts('"' + Express.request.method + ' ' + Express.request.uri.path +
105 unmodified lines
179 * @api private180 */181 182 callback : function(request, response){183 request.body = ''184 request.setBodyEncoding('utf8')185 request.addListener('body', function(chunk) { request.body += chunk })186 request.addListener('complete', function(){174 Express.home = Express.settings.basepath175 Express.response.body = null176 Express.response.status = 200177 Express.request = request178 request.headers = Express.arrayToHash(request.headers)179 Express.hook('onEarlyRequest')180 request.uri.params = Express.parseNestedParams(request.uri.params)181 request.uri.path = Express.normalizePath(request.uri.path)182 Express.hook('onRequest')183 try { Express.response.body = Express.callRouteFor(request) }184 catch (e) { Express.fail(e) }185 Express.hook('onResponse')186 response.sendHeader(Express.response.status, Express.hashToArray(Express.response.headers))187 response.sendBody(Express.response.body || '')188 response.finish()187 Express.home = Express.settings.basepath188 Express.response.body = null189 Express.response.status = 200190 Express.request = request191 request.headers = Express.arrayToHash(request.headers)192 Express.hook('onEarlyRequest')193 request.uri.params = Express.parseNestedParams(request.uri.params)194 request.uri.path = Express.normalizePath(request.uri.path)195 Express.hook('onRequest')196 try { Express.response.body = Express.callRouteFor(request) }197 catch (e) { Express.fail(e) }198 Express.hook('onResponse')199 response.sendHeader(Express.response.status, Express.hashToArray(Express.response.headers))200 response.sendBody(Express.response.body || '')201 response.finish()202 })203 }204 },205 206 /**
65 unmodified lines
272 273 jsonDecode : function(string) {274 return JSON.parse(string)275 },262 276 277 /**278 * Redirect to _url_.279 *280 * @param {string} url
38 unmodified lines
319 throw new Express.Halt()320 },321 322 /**309 * Sets _name_ header to _value_. When _value_310 * is not present the request header will be returned.323 * Get or set response _name_ header.324 *312 * @param {string} name313 * @param {string} value325 * @param {string} name326 * @param {string} value327 * @return {string}328 * @api public329 */330 331 header : function(name, value) {318 name = name.toLowerCase()319 return value ? this.response.headers[name] = value :320 this.request.headers[name] || this.response.headers[name]332 return value ? 333 this.response.headers[name.toLowerCase()] = value :334 this.response.headers[name.toLowerCase()]335 },336 337 /**338 * Get request header _name_ optionally providing _default_339 * when _name_ is not present.340 *341 * @param {string} name342 * @param {string} default343 * @return {string}344 * @api public345 */346 347 requestHeader : function(name, defaultValue) {348 return this.request.headers[name.toLowerCase()] || defaultValue349 },350 351 /**352 * Return param _key_'s value or _defaultValue_ when353 * the param has not been set.354 *355 * @param {string} key
6 unmodified lines
362 return this.request.uri.params[key] || defaultValue363 },364 365 /**366 * Parse params _string_ into a nested hash.367 *368 * @param {string} string369 * @return {hash}370 * @api public371 */372 373 parseParams : function(string) {374 var params = {}, pairs = string.split('&'), pair375 for (i = 0, len = pairs.length; i < len; ++i) {376 pair = pairs[i].split('=')377 params[pair[0]] = pair[1]378 }379 return this.parseNestedParams(params)380 },381 382 /**383 * Parse _params_, splitting { user[info][name] : 'tj' } into384 * { user : { info : { name : 'tj' }}}385 *386 * @param {hash} params
276 unmodified lines
663 664 use(Express.RedirectHelpers)665 use(Express.ContentLength)666 use(Express.DefaultContentType)667 use(Express.FormBodyDecoder)668 669})()
modified:lib/express.mocks.js+8 added
modified:spec/spec.core.js+12 added-4 removed
8 unmodified lines
9 Express.version.should.match /\d+\.\d+\.\d+/10 end11 end12 13 describe '.parseParams()'14 it 'should parse a string of parameters'15 string = 'user[name]=tj&user[pass]=test&foo=some bar stuff&cookies=awesome'16 params = Express.parseParams(string)17 params.user.name.should.eql 'tj'18 params.user.pass.should.eql 'test'19 params.foo.should.eql 'some bar stuff'20 params.cookies.should.eql 'awesome'21 end22 end23 24 describe '.parseNestedParams()'25 it 'should parse nested params hash provided by node'26 params = { 'foo' : 'bar', 'user[name]' : 'tj', 'user[info][email]' : 'tj@vision-media.ca', 'user[info][city]' : 'Victoria' }27 nested = Express.parseNestedParams(params)
48 unmodified lines
76 end77 78 describe '.header()'79 it 'should set / get headers'69 Express.request = {}70 Express.request.headers = { 'accept' : 'text/plain' }80 Express.header('Content-Type', 'text/html')81 Express.header('Content-Type').should.eql 'text/html'73 Express.header('Accept').should.eql 'text/plain'74 Express.header('accept').should.eql 'text/plain'82 Express.header('content-type').should.eql 'text/html'83 end84 end85 86 describe '.status()'
68 unmodified lines
modified:Readme+16 added-1 removed
17 unmodified lines
18 * Faux method support for RESTful apps19 * Extendable using Express 'Modules'20 * Static file serving21 * View support22 * Non polluting so you can use it with your fav JavaScript libraries :)22 * Routing23 * string matching 24 * regexp with captures 25 * param key substitution
28 unmodified lines
54 'User deleted' :55 'Failed to delete user'56 })57 58 // GET /login59 60 get('login', function() {61 '<form method="post" action="/login"> \62 <input type="text" name="user[name]" /> \63 <input type="password" name="user[pass]" /> \64 <input type="submit" value="Login"/> \65 </form>'66 })67 68 // POST /login6970 post('login', function() {71 'Welcome ' + param('user').name72 })73 74== Public API7576 * Coming Soon77
24 unmodified lines