request.js
This file
Path
lib/request.js
370 lines · 7.6 KiB · JavaScript
Last changed by
Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
History of this fileTake it
The walk stopped after 200 commits: lines older than 17ccd55 point at it.
upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 1–16why, one step back12/*!3 * Express - request4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var http = require('http')13 , utils = require('./utils')14 , connect = require('connect')15 , parse = require('url').parse16 , mime = require('mime');no longer manipulate the req/res protos directlyTj Holowaychukbefore adoptiond249868
lines 17–24why, one step back1718/**19 * Request prototype.20 */2122var req = exports = module.exports = {23 __proto__: http.IncomingMessage.prototype24};upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 25–82why, one step back2526/**27 * Return request header or optional default.28 *29 * The `Referrer` header field is special-cased,30 * both `Referrer` and `Referer` will yield are31 * interchangeable.32 *33 * Examples:34 *35 * req.header('Content-Type');36 * // => "text/plain"37 * 38 * req.header('content-type');39 * // => "text/plain"40 * 41 * req.header('Accept');42 * // => undefined43 * 44 * req.header('Accept', 'text/html');45 * // => "text/html"46 *47 * @param {String} name48 * @param {String} defaultValue49 * @return {String} 50 * @api public51 */5253req.header = function(name, defaultValue){54 switch (name = name.toLowerCase()) {55 case 'referer':56 case 'referrer':57 return this.headers.referrer58 || this.headers.referer59 || defaultValue;60 default:61 return this.headers[name] || defaultValue;62 }63};6465/**66 * Get `field`'s `param` value, defaulting to ''.67 *68 * Examples:69 *70 * req.get('content-disposition', 'filename');71 * // => "something.png"72 *73 * req.get('content-disposition', 'rawr');74 * // => ""75 *76 * @param {String} field77 * @param {String} param78 * @return {String}79 * @api public80 */8182req.get = function(field, param){mergedTj Holowaychukbefore adoption547d18c
line 83why, one step back83 // TODO: rename... add tests, make .get() .header()upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 84–91why, one step back84 var val = this.header(field);85 if (!val) return '';86 var regexp = new RegExp(param + ' *= *(?:"([^"]+)"|([^;]+))', 'i');87 if (!regexp.exec(val)) return '';88 return RegExp.$1 || RegExp.$2;89};9091/**mergedTj Holowaychukbefore adoption547d18c
lines 92–93why, one step back92 * Check if the given `type` is acceptable,93 * otherwise you should respond with 406 "Not Acceptable".upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 94–117why, one step back94 *95 * Examples:96 * 97 * // Accept: text/html98 * req.accepts('html');99 * // => true100 *101 * // Accept: text/*; application/json102 * req.accepts('html');103 * req.accepts('text/html');104 * req.accepts('text/plain');105 * req.accepts('application/json');106 * // => true107 *108 * req.accepts('image/png');109 * req.accepts('png');110 * // => false111 *112 * @param {String} type113 * @return {Boolean}114 * @api public115 */116117req.accepts = function(type){mergedTj Holowaychukbefore adoption547d18c
lines 118–119why, one step back118 return utils.accepts(type, this.header('Accept'));119};upgrade shouldTj Holowaychukbefore adoption17ccd55
line 120why, one step back120mergedTj Holowaychukbefore adoption547d18c
lines 121–128why, one step back121/**122 * Check if the given `charset` is acceptable,123 * otherwise you should respond with 406 "Not Acceptable".124 *125 * @param {String} charset126 * @return {Boolean}127 * @api public128 */upgrade shouldTj Holowaychukbefore adoption17ccd55
line 129why, one step back129mergedTj Holowaychukbefore adoption547d18c
lines 130–135why, one step back130req.acceptsCharset = function(charset){131 var accepted = this.acceptedCharsets;132 return accepted.length133 ? ~accepted.indexOf(charset)134 : true;135};upgrade shouldTj Holowaychukbefore adoption17ccd55
line 136why, one step back136mergedTj Holowaychukbefore adoption547d18c
lines 137–144why, one step back137/**138 * Check if the given `lang` is acceptable,139 * otherwise you should respond with 406 "Not Acceptable".140 *141 * @param {String} lang142 * @return {Boolean}143 * @api public144 */upgrade shouldTj Holowaychukbefore adoption17ccd55
line 145why, one step back145mergedTj Holowaychukbefore adoption547d18c
lines 146–150why, one step back146req.acceptsLanguage = function(lang){147 var accepted = this.acceptedLanguages;148 return accepted.length149 ? ~accepted.indexOf(lang)150 : true;upgrade shouldTj Holowaychukbefore adoption17ccd55
line 151why, one step back151};mergedTj Holowaychukbefore adoption547d18c
lines 152–225why, one step back152153/**154 * Return an array of Accepted media types155 * ordered from highest quality to lowest.156 *157 * Examples:158 *159 * [ { value: 'application/json',160 * quality: 1,161 * type: 'application',162 * subtype: 'json' },163 * { value: 'text/html',164 * quality: 0.5,165 * type: 'text',166 * subtype: 'html' } ]167 *168 * @return {Array}169 * @api public170 */171172req.__defineGetter__('accepted', function(){173 var accept = this.header('Accept');174 return accept175 ? utils.parseAccept(accept)176 : [];177});178179/**180 * Return an array of Accepted languages181 * ordered from highest quality to lowest.182 *183 * Examples:184 *185 * Accept-Language: en;q=.5, en-us186 * ['en-us', 'en']187 *188 * @return {Array}189 * @api public190 */191192req.__defineGetter__('acceptedLanguages', function(){193 var accept = this.header('Accept-Language');194 return accept195 ? utils196 .parseQuality(accept)197 .map(function(obj){198 return obj.value;199 })200 : [];201});202203/**204 * Return an array of Accepted charsets205 * ordered from highest quality to lowest.206 *207 * Examples:208 *209 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8210 * ['unicode-1-1', 'iso-8859-5']211 *212 * @return {Array}213 * @api public214 */215216req.__defineGetter__('acceptedCharsets', function(){217 var accept = this.header('Accept-Charset');218 return accept219 ? utils220 .parseQuality(accept)221 .map(function(obj){222 return obj.value;223 })224 : [];225});upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 226–229why, one step back226227/**228 * Return the value of param `name` when present or `defaultValue`.229 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 230why, one step back230 * - Checks body params, ex: id=12, {"id":12}upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 231–233why, one step back231 * - Checks route placeholders, ex: _/user/:id_232 * - Checks query string params, ex: ?id=12233 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 234why, one step back234 * To utilize request bodies, `req.body`upgrade shouldTj Holowaychukbefore adoption17ccd55
line 235why, one step back235 * should be an object. This can be done by usingreq.body takes precedenceTj Holowaychukbefore adoptione49c482
line 236why, one step back236 * the `connect.bodyParser()` middleware.upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 237–244why, one step back237 *238 * @param {String} name239 * @param {Mixed} defaultValue240 * @return {String}241 * @api public242 */243244req.param = function(name, defaultValue){req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 245–248why, one step back245 // req.body246 if (this.body && undefined !== this.body[name]) return this.body[name];247248 // route paramsupgrade shouldTj Holowaychukbefore adoption17ccd55
lines 249–254why, one step back249 if (this.params250 && this.params.hasOwnProperty(name)251 && undefined !== this.params[name]) {252 return this.params[name]; 253 }254req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 255–256why, one step back255 // query-string256 if (undefined !== this.query[name]) return this.query[name]; upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 257–269why, one step back257258 return defaultValue;259};260261/**262 * Check if the incoming request contains the "Content-Type" 263 * header field, and it contains the give mime `type`.264 *265 * Examples:266 *267 * // With Content-Type: text/html; charset=utf-8268 * req.is('html');269 * req.is('text/html');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 270why, one step back270 * req.is('text/*');upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 271–275why, one step back271 * // => true272 * 273 * // When Content-Type is application/json274 * req.is('json');275 * req.is('application/json');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 276why, one step back276 * req.is('application/*');upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 277–285why, one step back277 * // => true278 * 279 * req.is('html');280 * // => false281 * 282 * Now within our route callbacks, we can use to to assert content types283 * such as "image/jpeg", "image/png", etc.284 * 285 * app.post('/image/upload', function(req, res, next){Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 286why, one step back286 * if (req.is('image/*')) {upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 287–298why, one step back287 * // do something288 * } else {289 * next();290 * }291 * });292 * 293 * @param {String} type294 * @return {Boolean}295 * @api public296 */297298req.is = function(type){Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 299–301why, one step back299 var ct = this.headers['content-type'];300 if (!ct) return false;301 ct = ct.split(';')[0];upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 302–304why, one step back302 if (!~type.indexOf('/')) type = mime.lookup(type);303 if (~type.indexOf('*')) {304 type = type.split('/');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 305–308why, one step back305 ct = ct.split('/');306 if ('*' == type[0] && type[1] == ct[1]) return true;307 if ('*' == type[1] && type[0] == ct[0]) return true;308 return false;upgrade shouldTj Holowaychukbefore adoption17ccd55
line 309why, one step back309 }Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 310why, one step back310 return !! ~ct.indexOf(type);upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 311–320why, one step back311};312313/**314 * Short-hand for `req.connection.encrypted`.315 *316 * @return {Boolean}317 * @api public318 */319320req.__defineGetter__('secure', function(){Added X-Forwarded-Proto support to `res.redirect()`. Closes #927Tj Holowaychukbefore adoption5757f87
line 321why, one step back321 return this.connection.encrypted;upgrade shouldTj Holowaychukbefore adoption17ccd55
lines 322–367why, one step back322});323324/**325 * Short-hand for `require('url').parse(req.url).pathname`.326 *327 * @return {String}328 * @api public329 */330331req.__defineGetter__('path', function(){332 return parse(this.url).pathname;333});334335/**336 * Check if the request is fresh, aka337 * Last-Modified and/or the ETag338 * still match.339 *340 * @return {Boolean}341 * @api public342 */343344req.__defineGetter__('fresh', function(){345 return ! this.stale;346});347348/**349 * Check if the request is stale, aka350 * Last-Modified and/or the ETag for the351 * resource has changed.352 *353 * @return {Boolean}354 * @api public355 */356357req.__defineGetter__('stale', function(){358 return connect.utils.modified(this, this.res);359});360361/**362 * Check if the request was an _XMLHttpRequest_.363 *364 * @return {Boolean}365 * @api public366 */367Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 368why, one step back368req.__defineGetter__('xhr', function(){case sensitivity testTj Holowaychukbefore adoption1a5636b
line 369why, one step back369 return 'xmlhttprequest' == this.header('X-Requested-With', '').toLowerCase();Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 370why, one step back370});