request.js
This file
Path
lib/request.js
378 lines · 7.7 KiB · JavaScript
Last changed by
Added `req.subdomains`TJ Holowaychukbefore adoption4e33245
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bee32c point at it.
removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
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};removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 25–26why, one step back2526/**doc typoTJ Holowaychukbefore adoptionfe27989
line 27why, one step back27 * Return request header.removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
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');removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
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');removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 39–40why, one step back39 * // => "text/plain"40 * Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 41why, one step back41 * req.get('Something');removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
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){removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
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;removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 55why, one step back55 default:Removed `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f
line 56why, one step back56 return this.headers[name];removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 57–60why, one step back57 }58};5960/**mergedTj Holowaychukbefore adoption547d18c
lines 61–62why, one step back61 * Check if the given `type` is acceptable,62 * otherwise you should respond with 406 "Not Acceptable".removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 63–86why, one step back63 *64 * Examples:65 * 66 * // Accept: text/html67 * req.accepts('html');68 * // => true69 *70 * // Accept: text/*; application/json71 * req.accepts('html');72 * req.accepts('text/html');73 * req.accepts('text/plain');74 * req.accepts('application/json');75 * // => true76 *77 * req.accepts('image/png');78 * req.accepts('png');79 * // => false80 *81 * @param {String} type82 * @return {Boolean}83 * @api public84 */8586req.accepts = function(type){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 87why, one step back87 return utils.accepts(type, this.get('Accept'));mergedTj Holowaychukbefore adoption547d18c
line 88why, one step back88};removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 89why, one step back89mergedTj Holowaychukbefore adoption547d18c
lines 90–97why, one step back90/**91 * Check if the given `charset` is acceptable,92 * otherwise you should respond with 406 "Not Acceptable".93 *94 * @param {String} charset95 * @return {Boolean}96 * @api public97 */removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 98why, one step back98mergedTj Holowaychukbefore adoption547d18c
lines 99–104why, one step back99req.acceptsCharset = function(charset){100 var accepted = this.acceptedCharsets;101 return accepted.length102 ? ~accepted.indexOf(charset)103 : true;104};removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 105why, one step back105mergedTj Holowaychukbefore adoption547d18c
lines 106–113why, one step back106/**107 * Check if the given `lang` is acceptable,108 * otherwise you should respond with 406 "Not Acceptable".109 *110 * @param {String} lang111 * @return {Boolean}112 * @api public113 */removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 114why, one step back114mergedTj Holowaychukbefore adoption547d18c
lines 115–119why, one step back115req.acceptsLanguage = function(lang){116 var accepted = this.acceptedLanguages;117 return accepted.length118 ? ~accepted.indexOf(lang)119 : true;removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 120why, one step back120};mergedTj Holowaychukbefore adoption547d18c
lines 121–141why, one step back121122/**123 * Return an array of Accepted media types124 * ordered from highest quality to lowest.125 *126 * Examples:127 *128 * [ { value: 'application/json',129 * quality: 1,130 * type: 'application',131 * subtype: 'json' },132 * { value: 'text/html',133 * quality: 0.5,134 * type: 'text',135 * subtype: 'html' } ]136 *137 * @return {Array}138 * @api public139 */140141req.__defineGetter__('accepted', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 142why, one step back142 var accept = this.get('Accept');mergedTj Holowaychukbefore adoption547d18c
lines 143–161why, one step back143 return accept144 ? utils.parseAccept(accept)145 : [];146});147148/**149 * Return an array of Accepted languages150 * ordered from highest quality to lowest.151 *152 * Examples:153 *154 * Accept-Language: en;q=.5, en-us155 * ['en-us', 'en']156 *157 * @return {Array}158 * @api public159 */160161req.__defineGetter__('acceptedLanguages', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 162why, one step back162 var accept = this.get('Accept-Language');mergedTj Holowaychukbefore adoption547d18c
lines 163–185why, one step back163 return accept164 ? utils165 .parseQuality(accept)166 .map(function(obj){167 return obj.value;168 })169 : [];170});171172/**173 * Return an array of Accepted charsets174 * ordered from highest quality to lowest.175 *176 * Examples:177 *178 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8179 * ['unicode-1-1', 'iso-8859-5']180 *181 * @return {Array}182 * @api public183 */184185req.__defineGetter__('acceptedCharsets', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 186why, one step back186 var accept = this.get('Accept-Charset');mergedTj Holowaychukbefore adoption547d18c
lines 187–194why, one step back187 return accept188 ? utils189 .parseQuality(accept)190 .map(function(obj){191 return obj.value;192 })193 : [];194});removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 195–198why, one step back195196/**197 * Return the value of param `name` when present or `defaultValue`.198 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 199why, one step back199 * - Checks body params, ex: id=12, {"id":12}removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 200–202why, one step back200 * - Checks route placeholders, ex: _/user/:id_201 * - Checks query string params, ex: ?id=12202 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 203why, one step back203 * To utilize request bodies, `req.body`removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 204why, one step back204 * should be an object. This can be done by usingreq.body takes precedenceTj Holowaychukbefore adoptione49c482
line 205why, one step back205 * the `connect.bodyParser()` middleware.removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 206–213why, one step back206 *207 * @param {String} name208 * @param {Mixed} defaultValue209 * @return {String}210 * @api public211 */212213req.param = function(name, defaultValue){req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 214–217why, one step back214 // req.body215 if (this.body && undefined !== this.body[name]) return this.body[name];216217 // route paramsremoved a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 218–223why, one step back218 if (this.params219 && this.params.hasOwnProperty(name)220 && undefined !== this.params[name]) {221 return this.params[name]; 222 }223req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 224–225why, one step back224 // query-string225 if (undefined !== this.query[name]) return this.query[name]; removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 226–238why, one step back226227 return defaultValue;228};229230/**231 * Check if the incoming request contains the "Content-Type" 232 * header field, and it contains the give mime `type`.233 *234 * Examples:235 *236 * // With Content-Type: text/html; charset=utf-8237 * req.is('html');238 * req.is('text/html');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 239why, one step back239 * req.is('text/*');removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 240–244why, one step back240 * // => true241 * 242 * // When Content-Type is application/json243 * req.is('json');244 * req.is('application/json');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 245why, one step back245 * req.is('application/*');removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 246–254why, one step back246 * // => true247 * 248 * req.is('html');249 * // => false250 * 251 * Now within our route callbacks, we can use to to assert content types252 * such as "image/jpeg", "image/png", etc.253 * 254 * app.post('/image/upload', function(req, res, next){Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 255why, one step back255 * if (req.is('image/*')) {removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 256–267why, one step back256 * // do something257 * } else {258 * next();259 * }260 * });261 * 262 * @param {String} type263 * @return {Boolean}264 * @api public265 */266267req.is = function(type){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 268why, one step back268 var ct = this.get('Content-Type');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 269–270why, one step back269 if (!ct) return false;270 ct = ct.split(';')[0];removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 271–273why, one step back271 if (!~type.indexOf('/')) type = mime.lookup(type);272 if (~type.indexOf('*')) {273 type = type.split('/');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 274–277why, one step back274 ct = ct.split('/');275 if ('*' == type[0] && type[1] == ct[1]) return true;276 if ('*' == type[1] && type[0] == ct[0]) return true;277 return false;removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 278why, one step back278 }Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 279why, one step back279 return !! ~ct.indexOf(type);removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 280–282why, one step back280};281282/**Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 283–288why, one step back283 * Return the protocol string "http" or "https"284 * when requested with TLS. When the "trust proxy" 285 * setting is enabled the "X-Forwarded-Proto" header286 * field will be trusted. If you're running behind287 * a reverse proxy that supplies https for you this288 * may be enabled.Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 289–293why, one step back289 *290 * @return {String}291 * @api public292 */293Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 294–295why, one step back294req.__defineGetter__('protocol', function(trustProxy){295 var trustProxy = this.app.settings['trust proxy'];Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 296–298why, one step back296 return this.secure297 ? 'https'298 : trustProxyAdded "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 299why, one step back299 ? (this.get('X-Forwarded-Proto') || 'http')Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
line 300why, one step back300 : 'http';Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 301why, one step back301});Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 302–303why, one step back302303/**removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 304–310why, one step back304 * Short-hand for `req.connection.encrypted`.305 *306 * @return {Boolean}307 * @api public308 */309310req.__defineGetter__('secure', function(){Added X-Forwarded-Proto support to `res.redirect()`. Closes #927Tj Holowaychukbefore adoption5757f87
line 311why, one step back311 return this.connection.encrypted;removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 312–314why, one step back312});313314/**Added `req.subdomains`TJ Holowaychukbefore adoption4e33245
lines 315–331why, one step back315 * Return subdomains as an array.316 *317 * For example "tobi.ferrets.example.com"318 * would provide `["ferrets", "tobi"]`.319 *320 * @return {Array}321 * @api public322 */323324req.__defineGetter__('subdomains', function(){325 return this.get('Host')326 .split('.')327 .slice(0, -2)328 .reverse();329});330331/**removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 332–356why, one step back332 * Short-hand for `require('url').parse(req.url).pathname`.333 *334 * @return {String}335 * @api public336 */337338req.__defineGetter__('path', function(){339 return parse(this.url).pathname;340});341342/**343 * Check if the request is fresh, aka344 * Last-Modified and/or the ETag345 * still match.346 *347 * @return {Boolean}348 * @api public349 */350351req.__defineGetter__('fresh', function(){352 return ! this.stale;353});354355/**356 * Check if the request is stale, akadocsTJ Holowaychukbefore adoption6ab61d0
line 357why, one step back357 * "Last-Modified" and / or the "ETag" for theremoved a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 358–374why, one step back358 * resource has changed.359 *360 * @return {Boolean}361 * @api public362 */363364req.__defineGetter__('stale', function(){365 return connect.utils.modified(this, this.res);366});367368/**369 * Check if the request was an _XMLHttpRequest_.370 *371 * @return {Boolean}372 * @api public373 */374Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 375why, one step back375req.__defineGetter__('xhr', function(){refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee
lines 376–377why, one step back376 var val = this.get('X-Requested-With') || '';377 return 'xmlhttprequest' == val.toLowerCase();Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 378why, one step back378});