request.js
This file
Path
lib/request.js
407 lines · 8.6 KiB · JavaScript
Last changed by
refactored req.param()TJ Holowaychukbefore adoption99b2e0f
History of this fileTake it
The walk stopped after 200 commits: lines older than 6368ab4 point at it.
ocdTJ Holowaychukbefore adoption6368ab4
lines 1–8why, one step back12/**3 * Module dependencies.4 */56var http = require('http')7 , utils = require('./utils')8 , connect = require('connect')Perf: memoize parsed urlsTJ Holowaychukbefore adoption73092c9
line 9why, one step back9 , parse = connect.utils.parseUrlocdTJ Holowaychukbefore adoption6368ab4
lines 10–23why, one step back10 , mime = require('mime');1112/**13 * Request prototype.14 */1516var req = exports = module.exports = {17 __proto__: http.IncomingMessage.prototype18};1920/**21 * Return request header.22 *23 * The `Referrer` header field is special-cased,doc typoTJ Holowaychukbefore adoptionb5cee82
line 24why, one step back24 * both `Referrer` and `Referer` are interchangeable.ocdTJ Holowaychukbefore adoption6368ab4
lines 25–35why, one step back25 *26 * Examples:27 *28 * req.get('Content-Type');29 * // => "text/plain"30 * 31 * req.get('content-type');32 * // => "text/plain"33 * 34 * req.get('Something');35 * // => undefineddocsTJ Holowaychukbefore adoption27b1b5e
lines 36–38why, one step back36 *37 * Aliased as `req.header()`.38 *ocdTJ Holowaychukbefore adoption6368ab4
lines 39–43why, one step back39 * @param {String} name40 * @return {String} 41 * @api public42 */43Added `res.header()` and `req.header()` aliases for BCTJ Holowaychukbefore adoption5fa2f89
lines 44–45why, one step back44req.get = 45req.header = function(name){ocdTJ Holowaychukbefore adoption6368ab4
lines 46–56why, one step back46 switch (name = name.toLowerCase()) {47 case 'referer':48 case 'referrer':49 return this.headers.referrer50 || this.headers.referer;51 default:52 return this.headers[name];53 }54};5556/**docsTJ Holowaychukbefore adoption4e2677f
lines 57–59why, one step back57 * Check if the given `type(s)` is acceptable, returning58 * the best match when true, otherwise `undefined`, in which59 * case you should respond with 406 "Not Acceptable".ocdTJ Holowaychukbefore adoption6368ab4
line 60why, one step back60 *docsTJ Holowaychukbefore adoption365a98d
lines 61–66why, one step back61 * The `type` value may be a single mime type string62 * such as "application/json", the extension name63 * such as "json", a comma-delimted list such as "json, html, text/plain",64 * or an array `["json", "html", "text/plain"]`. When a list65 * or array is given the _best_ match, if any is returned.66 *ocdTJ Holowaychukbefore adoption6368ab4
lines 67–70why, one step back67 * Examples:68 * 69 * // Accept: text/html70 * req.accepts('html');docsTJ Holowaychukbefore adoption365a98d
line 71why, one step back71 * // => "html"ocdTJ Holowaychukbefore adoption6368ab4
line 72why, one step back72 *typoTJ Holowaychukbefore adoptionba570a9
line 73why, one step back73 * // Accept: text/*, application/jsonocdTJ Holowaychukbefore adoption6368ab4
line 74why, one step back74 * req.accepts('html');docsTJ Holowaychukbefore adoption365a98d
line 75why, one step back75 * // => "html"ocdTJ Holowaychukbefore adoption6368ab4
line 76why, one step back76 * req.accepts('text/html');docsTJ Holowaychukbefore adoption365a98d
lines 77–79why, one step back77 * // => "text/html"78 * req.accepts('json, text');79 * // => "json"ocdTJ Holowaychukbefore adoption6368ab4
line 80why, one step back80 * req.accepts('application/json');docsTJ Holowaychukbefore adoption365a98d
line 81why, one step back81 * // => "application/json"ocdTJ Holowaychukbefore adoption6368ab4
line 82why, one step back82 *typoTJ Holowaychukbefore adoptionba570a9
line 83why, one step back83 * // Accept: text/*, application/jsonocdTJ Holowaychukbefore adoption6368ab4
lines 84–85why, one step back84 * req.accepts('image/png');85 * req.accepts('png');docsTJ Holowaychukbefore adoption365a98d
line 86why, one step back86 * // => undefinedocdTJ Holowaychukbefore adoption6368ab4
line 87why, one step back87 *typoTJ Holowaychukbefore adoptionba570a9
line 88why, one step back88 * // Accept: text/*;q=.5, application/jsonAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 89why, one step back89 * req.accepts(['html', 'json']);docsTJ Holowaychukbefore adoption365a98d
lines 90–91why, one step back90 * req.accepts('html, json');91 * // => "json"Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
lines 92–93why, one step back92 *93 * @param {String|Array} type(s)docsTJ Holowaychukbefore adoption4e2677f
line 94why, one step back94 * @return {String}ocdTJ Holowaychukbefore adoption6368ab4
lines 95–211why, one step back95 * @api public96 */9798req.accepts = function(type){99 return utils.accepts(type, this.get('Accept'));100};101102/**103 * Check if the given `charset` is acceptable,104 * otherwise you should respond with 406 "Not Acceptable".105 *106 * @param {String} charset107 * @return {Boolean}108 * @api public109 */110111req.acceptsCharset = function(charset){112 var accepted = this.acceptedCharsets;113 return accepted.length114 ? ~accepted.indexOf(charset)115 : true;116};117118/**119 * Check if the given `lang` is acceptable,120 * otherwise you should respond with 406 "Not Acceptable".121 *122 * @param {String} lang123 * @return {Boolean}124 * @api public125 */126127req.acceptsLanguage = function(lang){128 var accepted = this.acceptedLanguages;129 return accepted.length130 ? ~accepted.indexOf(lang)131 : true;132};133134/**135 * Return an array of Accepted media types136 * ordered from highest quality to lowest.137 *138 * Examples:139 *140 * [ { value: 'application/json',141 * quality: 1,142 * type: 'application',143 * subtype: 'json' },144 * { value: 'text/html',145 * quality: 0.5,146 * type: 'text',147 * subtype: 'html' } ]148 *149 * @return {Array}150 * @api public151 */152153req.__defineGetter__('accepted', function(){154 var accept = this.get('Accept');155 return accept156 ? utils.parseAccept(accept)157 : [];158});159160/**161 * Return an array of Accepted languages162 * ordered from highest quality to lowest.163 *164 * Examples:165 *166 * Accept-Language: en;q=.5, en-us167 * ['en-us', 'en']168 *169 * @return {Array}170 * @api public171 */172173req.__defineGetter__('acceptedLanguages', function(){174 var accept = this.get('Accept-Language');175 return accept176 ? utils177 .parseQuality(accept)178 .map(function(obj){179 return obj.value;180 })181 : [];182});183184/**185 * Return an array of Accepted charsets186 * ordered from highest quality to lowest.187 *188 * Examples:189 *190 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8191 * ['unicode-1-1', 'iso-8859-5']192 *193 * @return {Array}194 * @api public195 */196197req.__defineGetter__('acceptedCharsets', function(){198 var accept = this.get('Accept-Charset');199 return accept200 ? utils201 .parseQuality(accept)202 .map(function(obj){203 return obj.value;204 })205 : [];206});207208/**209 * Return the value of param `name` when present or `defaultValue`.210 *211 * - Checks route placeholders, ex: _/user/:id_Changed `req.param()` to check route firstTJ Holowaychukbefore adoptionebcb1ca
line 212why, one step back212 * - Checks body params, ex: id=12, {"id":12}ocdTJ Holowaychukbefore adoption6368ab4
lines 213–225why, one step back213 * - Checks query string params, ex: ?id=12214 *215 * To utilize request bodies, `req.body`216 * should be an object. This can be done by using217 * the `connect.bodyParser()` middleware.218 *219 * @param {String} name220 * @param {Mixed} defaultValue221 * @return {String}222 * @api public223 */224225req.param = function(name, defaultValue){refactored req.param()TJ Holowaychukbefore adoption99b2e0f
lines 226–231why, one step back226 var params = this.params || {};227 var body = this.body || {};228 var query = this.query || {};229 if (null != params[name]) return params[name];230 if (null != body[name]) return body[name];231 if (null != query[name]) return query[name];ocdTJ Holowaychukbefore adoption6368ab4
lines 232–287why, one step back232 return defaultValue;233};234235/**236 * Check if the incoming request contains the "Content-Type" 237 * header field, and it contains the give mime `type`.238 *239 * Examples:240 *241 * // With Content-Type: text/html; charset=utf-8242 * req.is('html');243 * req.is('text/html');244 * req.is('text/*');245 * // => true246 * 247 * // When Content-Type is application/json248 * req.is('json');249 * req.is('application/json');250 * req.is('application/*');251 * // => true252 * 253 * req.is('html');254 * // => false255 * 256 * @param {String} type257 * @return {Boolean}258 * @api public259 */260261req.is = function(type){262 var ct = this.get('Content-Type');263 if (!ct) return false;264 ct = ct.split(';')[0];265 if (!~type.indexOf('/')) type = mime.lookup(type);266 if (~type.indexOf('*')) {267 type = type.split('/');268 ct = ct.split('/');269 if ('*' == type[0] && type[1] == ct[1]) return true;270 if ('*' == type[1] && type[0] == ct[0]) return true;271 return false;272 }273 return !! ~ct.indexOf(type);274};275276/**277 * Return the protocol string "http" or "https"278 * when requested with TLS. When the "trust proxy" 279 * setting is enabled the "X-Forwarded-Proto" header280 * field will be trusted. If you're running behind281 * a reverse proxy that supplies https for you this282 * may be enabled.283 *284 * @return {String}285 * @api public286 */287populate changelogTJ Holowaychukbefore adoptionabe5cc7
line 288why, one step back288req.__defineGetter__('protocol', function(){Fixed: use app.get() for all core settingsTJ Holowaychukbefore adoption8f87400
line 289why, one step back289 var trustProxy = this.app.get('trust proxy');Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 290why, one step back290 return this.connection.encryptedocdTJ Holowaychukbefore adoption6368ab4
lines 291–297why, one step back291 ? 'https'292 : trustProxy293 ? (this.get('X-Forwarded-Proto') || 'http')294 : 'http';295});296297/**Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 298why, one step back298 * Short-hand for:ocdTJ Holowaychukbefore adoption6368ab4
line 299why, one step back299 *Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
lines 300–301why, one step back300 * req.protocol == 'https'301 *ocdTJ Holowaychukbefore adoption6368ab4
lines 302–306why, one step back302 * @return {Boolean}303 * @api public304 */305306req.__defineGetter__('secure', function(){Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 307why, one step back307 return 'https' == this.protocol;ocdTJ Holowaychukbefore adoption6368ab4
lines 308–310why, one step back308});309310/**Added `req.ip`TJ Holowaychukbefore adoptiond84d0b6
lines 311–323why, one step back311 * Return the remote address, or when312 * "trust proxy" is `true` return313 * the upstream addr.314 *315 * @return {String}316 * @api public317 */318319req.__defineGetter__('ip', function(){320 return this.ips[0] || this.connection.remoteAddress;321});322323/**Added `req.ips`TJ Holowaychukbefore adoptiondd468fb
lines 324–326why, one step back324 * When "trust proxy" is `true`, parse325 * the "X-Forwarded-For" ip address list.326 *reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c
line 327why, one step back327 * For example if the value were "client, proxy1, proxy2"Changed: dont reverse `req.ips`TJ Holowaychukbefore adoptionb694ba2
line 328why, one step back328 * you would receive the array `["client", "proxy1", "proxy2"]`reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c
lines 329–330why, one step back329 * where "proxy2" is the furthest down-stream.330 *Added `req.ips`TJ Holowaychukbefore adoptiondd468fb
lines 331–335why, one step back331 * @return {Array}332 * @api public333 */334335req.__defineGetter__('ips', function(){misc refactorTJ Holowaychukbefore adoption910dae1
line 336why, one step back336 var trustProxy = this.app.get('trust proxy');req.ipds doesnt need "trust proxy"TJ Holowaychukbefore adoption6d0f9a3
line 337why, one step back337 var val = this.get('X-Forwarded-For');Fixed setting check for req.ipsTJ Holowaychukbefore adoptione3cbac2
line 338why, one step back338 return trustProxy && valChanged: dont reverse `req.ips`TJ Holowaychukbefore adoptionb694ba2
line 339why, one step back339 ? val.split(/ *, */)Added `req.ips`TJ Holowaychukbefore adoptiondd468fb
lines 340–343why, one step back340 : [];341});342343/**Added `req.subdomains`TJ Holowaychukbefore adoption4e33245
lines 344–360why, one step back344 * Return subdomains as an array.345 *346 * For example "tobi.ferrets.example.com"347 * would provide `["ferrets", "tobi"]`.348 *349 * @return {Array}350 * @api public351 */352353req.__defineGetter__('subdomains', function(){354 return this.get('Host')355 .split('.')356 .slice(0, -2)357 .reverse();358});359360/**Added new dox API docsTJ Holowaychukbefore adoption7e81cad
line 361why, one step back361 * Short-hand for `url.parse(req.url).pathname`.ocdTJ Holowaychukbefore adoption6368ab4
lines 362–367why, one step back362 *363 * @return {String}364 * @api public365 */366367req.__defineGetter__('path', function(){Perf: memoize parsed urlsTJ Holowaychukbefore adoption73092c9
line 368why, one step back368 return parse(this).pathname;ocdTJ Holowaychukbefore adoption6368ab4
lines 369–407why, one step back369});370371/**372 * Check if the request is fresh, aka373 * Last-Modified and/or the ETag374 * still match.375 *376 * @return {Boolean}377 * @api public378 */379380req.__defineGetter__('fresh', function(){381 return ! this.stale;382});383384/**385 * Check if the request is stale, aka386 * "Last-Modified" and / or the "ETag" for the387 * resource has changed.388 *389 * @return {Boolean}390 * @api public391 */392393req.__defineGetter__('stale', function(){394 return connect.utils.modified(this, this.res);395});396397/**398 * Check if the request was an _XMLHttpRequest_.399 *400 * @return {Boolean}401 * @api public402 */403404req.__defineGetter__('xhr', function(){405 var val = this.get('X-Requested-With') || '';406 return 'xmlhttprequest' == val.toLowerCase();407});