request.js
This file
Path
lib/request.js
415 lines · 8.7 KiB · JavaScript
Last changed by
Added `req.ips`TJ Holowaychukbefore adoptiondd468fb
History of this fileTake it
The walk stopped after 200 commits: lines older than 0e39597 point at it.
Added res.attachment() testsTj Holowaychukbefore adoption0e39597
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};Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 25–26why, one step back2526/**doc typoTJ Holowaychukbefore adoptionfe27989
line 27why, one step back27 * Return request header.Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 28–34why, one step back28 *29 * The `Referrer` header field is special-cased,30 * both `Referrer` and `Referer` will yield are31 * interchangeable.32 *33 * Examples:34 *Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b
line 35why, one step back35 * req.get('Content-Type');Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 36–37why, one step back36 * // => "text/plain"37 * Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b
line 38why, one step back38 * req.get('content-type');Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 39–40why, one step back39 * // => "text/plain"40 * Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 41why, one step back41 * req.get('Something');Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 42–48why, one step back42 * // => undefined43 * 44 * @param {String} name45 * @return {String} 46 * @api public47 */48Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b
line 49why, one step back49req.get = function(name){Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 50–53why, one step back50 switch (name = name.toLowerCase()) {51 case 'referer':52 case 'referrer':53 return this.headers.referrerRemoved `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f
line 54why, one step back54 || this.headers.referer;Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 55why, one step back55 default:Removed `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f
line 56why, one step back56 return this.headers[name];Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 57–60why, one step back57 }58};5960/**docsTJ Holowaychukbefore adoption4e2677f
lines 61–63why, one step back61 * Check if the given `type(s)` is acceptable, returning62 * the best match when true, otherwise `undefined`, in which63 * case you should respond with 406 "Not Acceptable".Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 64why, one step back64 *docsTJ Holowaychukbefore adoption365a98d
lines 65–70why, one step back65 * The `type` value may be a single mime type string66 * such as "application/json", the extension name67 * such as "json", a comma-delimted list such as "json, html, text/plain",68 * or an array `["json", "html", "text/plain"]`. When a list69 * or array is given the _best_ match, if any is returned.70 *Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 71–74why, one step back71 * Examples:72 * 73 * // Accept: text/html74 * req.accepts('html');docsTJ Holowaychukbefore adoption365a98d
line 75why, one step back75 * // => "html"Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 76why, one step back76 *typoTJ Holowaychukbefore adoptionba570a9
line 77why, one step back77 * // Accept: text/*, application/jsonAdded res.attachment() testsTj Holowaychukbefore adoption0e39597
line 78why, one step back78 * req.accepts('html');docsTJ Holowaychukbefore adoption365a98d
line 79why, one step back79 * // => "html"Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 80why, one step back80 * req.accepts('text/html');docsTJ Holowaychukbefore adoption365a98d
lines 81–83why, one step back81 * // => "text/html"82 * req.accepts('json, text');83 * // => "json"Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 84why, one step back84 * req.accepts('application/json');docsTJ Holowaychukbefore adoption365a98d
line 85why, one step back85 * // => "application/json"Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 86why, one step back86 *typoTJ Holowaychukbefore adoptionba570a9
line 87why, one step back87 * // Accept: text/*, application/jsonAdded res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 88–89why, one step back88 * req.accepts('image/png');89 * req.accepts('png');docsTJ Holowaychukbefore adoption365a98d
line 90why, one step back90 * // => undefinedAdded res.attachment() testsTj Holowaychukbefore adoption0e39597
line 91why, one step back91 *typoTJ Holowaychukbefore adoptionba570a9
line 92why, one step back92 * // Accept: text/*;q=.5, application/jsonAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 93why, one step back93 * req.accepts(['html', 'json']);docsTJ Holowaychukbefore adoption365a98d
lines 94–95why, one step back94 * req.accepts('html, json');95 * // => "json"Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
lines 96–97why, one step back96 *97 * @param {String|Array} type(s)docsTJ Holowaychukbefore adoption4e2677f
line 98why, one step back98 * @return {String}Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 99–102why, one step back99 * @api public100 */101102req.accepts = function(type){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 103why, one step back103 return utils.accepts(type, this.get('Accept'));mergedTj Holowaychukbefore adoption547d18c
line 104why, one step back104};Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 105why, one step back105mergedTj Holowaychukbefore adoption547d18c
lines 106–113why, one step back106/**107 * Check if the given `charset` is acceptable,108 * otherwise you should respond with 406 "Not Acceptable".109 *110 * @param {String} charset111 * @return {Boolean}112 * @api public113 */Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 114why, one step back114mergedTj Holowaychukbefore adoption547d18c
lines 115–120why, one step back115req.acceptsCharset = function(charset){116 var accepted = this.acceptedCharsets;117 return accepted.length118 ? ~accepted.indexOf(charset)119 : true;120};Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 121why, one step back121mergedTj Holowaychukbefore adoption547d18c
lines 122–129why, one step back122/**123 * Check if the given `lang` is acceptable,124 * otherwise you should respond with 406 "Not Acceptable".125 *126 * @param {String} lang127 * @return {Boolean}128 * @api public129 */Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 130why, one step back130mergedTj Holowaychukbefore adoption547d18c
lines 131–135why, one step back131req.acceptsLanguage = function(lang){132 var accepted = this.acceptedLanguages;133 return accepted.length134 ? ~accepted.indexOf(lang)135 : true;Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 136why, one step back136};mergedTj Holowaychukbefore adoption547d18c
lines 137–157why, one step back137138/**139 * Return an array of Accepted media types140 * ordered from highest quality to lowest.141 *142 * Examples:143 *144 * [ { value: 'application/json',145 * quality: 1,146 * type: 'application',147 * subtype: 'json' },148 * { value: 'text/html',149 * quality: 0.5,150 * type: 'text',151 * subtype: 'html' } ]152 *153 * @return {Array}154 * @api public155 */156157req.__defineGetter__('accepted', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 158why, one step back158 var accept = this.get('Accept');mergedTj Holowaychukbefore adoption547d18c
lines 159–177why, one step back159 return accept160 ? utils.parseAccept(accept)161 : [];162});163164/**165 * Return an array of Accepted languages166 * ordered from highest quality to lowest.167 *168 * Examples:169 *170 * Accept-Language: en;q=.5, en-us171 * ['en-us', 'en']172 *173 * @return {Array}174 * @api public175 */176177req.__defineGetter__('acceptedLanguages', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 178why, one step back178 var accept = this.get('Accept-Language');mergedTj Holowaychukbefore adoption547d18c
lines 179–201why, one step back179 return accept180 ? utils181 .parseQuality(accept)182 .map(function(obj){183 return obj.value;184 })185 : [];186});187188/**189 * Return an array of Accepted charsets190 * ordered from highest quality to lowest.191 *192 * Examples:193 *194 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8195 * ['unicode-1-1', 'iso-8859-5']196 *197 * @return {Array}198 * @api public199 */200201req.__defineGetter__('acceptedCharsets', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 202why, one step back202 var accept = this.get('Accept-Charset');mergedTj Holowaychukbefore adoption547d18c
lines 203–210why, one step back203 return accept204 ? utils205 .parseQuality(accept)206 .map(function(obj){207 return obj.value;208 })209 : [];210});Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 211–214why, one step back211212/**213 * Return the value of param `name` when present or `defaultValue`.214 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 215why, one step back215 * - Checks body params, ex: id=12, {"id":12}Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 216–218why, one step back216 * - Checks route placeholders, ex: _/user/:id_217 * - Checks query string params, ex: ?id=12218 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 219why, one step back219 * To utilize request bodies, `req.body`Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 220why, one step back220 * should be an object. This can be done by usingreq.body takes precedenceTj Holowaychukbefore adoptione49c482
line 221why, one step back221 * the `connect.bodyParser()` middleware.Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 222–229why, one step back222 *223 * @param {String} name224 * @param {Mixed} defaultValue225 * @return {String}226 * @api public227 */228229req.param = function(name, defaultValue){req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 230–233why, one step back230 // req.body231 if (this.body && undefined !== this.body[name]) return this.body[name];232233 // route paramsAdded res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 234–239why, one step back234 if (this.params235 && this.params.hasOwnProperty(name)236 && undefined !== this.params[name]) {237 return this.params[name]; 238 }239req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 240–241why, one step back240 // query-string241 if (undefined !== this.query[name]) return this.query[name]; Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 242–254why, one step back242243 return defaultValue;244};245246/**247 * Check if the incoming request contains the "Content-Type" 248 * header field, and it contains the give mime `type`.249 *250 * Examples:251 *252 * // With Content-Type: text/html; charset=utf-8253 * req.is('html');254 * req.is('text/html');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 255why, one step back255 * req.is('text/*');Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 256–260why, one step back256 * // => true257 * 258 * // When Content-Type is application/json259 * req.is('json');260 * req.is('application/json');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 261why, one step back261 * req.is('application/*');Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 262–270why, one step back262 * // => true263 * 264 * req.is('html');265 * // => false266 * 267 * Now within our route callbacks, we can use to to assert content types268 * such as "image/jpeg", "image/png", etc.269 * 270 * app.post('/image/upload', function(req, res, next){Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 271why, one step back271 * if (req.is('image/*')) {Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 272–283why, one step back272 * // do something273 * } else {274 * next();275 * }276 * });277 * 278 * @param {String} type279 * @return {Boolean}280 * @api public281 */282283req.is = function(type){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 284why, one step back284 var ct = this.get('Content-Type');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 285–286why, one step back285 if (!ct) return false;286 ct = ct.split(';')[0];Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 287–289why, one step back287 if (!~type.indexOf('/')) type = mime.lookup(type);288 if (~type.indexOf('*')) {289 type = type.split('/');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 290–293why, one step back290 ct = ct.split('/');291 if ('*' == type[0] && type[1] == ct[1]) return true;292 if ('*' == type[1] && type[0] == ct[0]) return true;293 return false;Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 294why, one step back294 }Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 295why, one step back295 return !! ~ct.indexOf(type);Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 296–298why, one step back296};297298/**Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 299–304why, one step back299 * Return the protocol string "http" or "https"300 * when requested with TLS. When the "trust proxy" 301 * setting is enabled the "X-Forwarded-Proto" header302 * field will be trusted. If you're running behind303 * a reverse proxy that supplies https for you this304 * may be enabled.Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 305–309why, one step back305 *306 * @return {String}307 * @api public308 */309Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 310–311why, one step back310req.__defineGetter__('protocol', function(trustProxy){311 var trustProxy = this.app.settings['trust proxy'];Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 312why, one step back312 return this.connection.encryptedAdded `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 313–314why, one step back313 ? 'https'314 : trustProxyAdded "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 315why, one step back315 ? (this.get('X-Forwarded-Proto') || 'http')Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
line 316why, one step back316 : 'http';Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 317why, one step back317});Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 318–319why, one step back318319/**Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 320why, one step back320 * Short-hand for:Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 321why, one step back321 *Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
lines 322–323why, one step back322 * req.protocol == 'https'323 *Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 324–328why, one step back324 * @return {Boolean}325 * @api public326 */327328req.__defineGetter__('secure', function(){Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 329why, one step back329 return 'https' == this.protocol;Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 330–332why, one step back330});331332/**Added `req.ips`TJ Holowaychukbefore adoptiondd468fb
lines 333–351why, one step back333 * When "trust proxy" is `true`, parse334 * the "X-Forwarded-For" ip address list.335 *336 * @return {Array}337 * @api public338 */339340req.__defineGetter__('ips', function(){341 var trustProxy = this.app.settings['trust proxy']342 , field = this.get('X-Forwarded-For');343344 return trustProxy345 ? field346 ? field.split(/ *, */)347 : []348 : [];349});350351/**Added `req.subdomains`TJ Holowaychukbefore adoption4e33245
lines 352–368why, one step back352 * Return subdomains as an array.353 *354 * For example "tobi.ferrets.example.com"355 * would provide `["ferrets", "tobi"]`.356 *357 * @return {Array}358 * @api public359 */360361req.__defineGetter__('subdomains', function(){362 return this.get('Host')363 .split('.')364 .slice(0, -2)365 .reverse();366});367368/**Added res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 369–393why, one step back369 * Short-hand for `require('url').parse(req.url).pathname`.370 *371 * @return {String}372 * @api public373 */374375req.__defineGetter__('path', function(){376 return parse(this.url).pathname;377});378379/**380 * Check if the request is fresh, aka381 * Last-Modified and/or the ETag382 * still match.383 *384 * @return {Boolean}385 * @api public386 */387388req.__defineGetter__('fresh', function(){389 return ! this.stale;390});391392/**393 * Check if the request is stale, akadocsTJ Holowaychukbefore adoption6ab61d0
line 394why, one step back394 * "Last-Modified" and / or the "ETag" for theAdded res.attachment() testsTj Holowaychukbefore adoption0e39597
lines 395–412why, one step back395 * resource has changed.396 *397 * @return {Boolean}398 * @api public399 */400401req.__defineGetter__('stale', function(){402 return connect.utils.modified(this, this.res);403});404405/**406 * Check if the request was an _XMLHttpRequest_.407 *408 * @return {Boolean}409 * @api public410 */411412req.__defineGetter__('xhr', function(){refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee
lines 413–414why, one step back413 var val = this.get('X-Requested-With') || '';414 return 'xmlhttprequest' == val.toLowerCase();Added res.attachment() testsTj Holowaychukbefore adoption0e39597
line 415why, one step back415});