request.js
This file
Path
lib/request.js
364 lines · 7.5 KiB · JavaScript
Last changed by
refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee
History of this fileTake it
The walk stopped after 200 commits: lines older than 4106bae point at it.
generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
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};generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 25–35why, 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');Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b
line 36why, one step back36 * req.get('Content-Type');generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 37–39why, one step back37 * // => "text/plain"38 * 39 * req.header('content-type');Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b
line 40why, one step back40 * req.get('content-type');generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 41–42why, one step back41 * // => "text/plain"42 * Removed `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f
line 43why, one step back43 * req.header('Something');generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 44–50why, one step back44 * // => undefined45 * 46 * @param {String} name47 * @return {String} 48 * @api public49 */50Added `req.get()` alias of `req.header()`TJ Holowaychukbefore adoption27e696b
lines 51–52why, one step back51req.header = 52req.get = function(name){generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 53–56why, one step back53 switch (name = name.toLowerCase()) {54 case 'referer':55 case 'referrer':56 return this.headers.referrerRemoved `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f
line 57why, one step back57 || this.headers.referer;generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 58why, one step back58 default:Removed `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f
line 59why, one step back59 return this.headers[name];generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 60–63why, one step back60 }61};6263/**mergedTj Holowaychukbefore adoption547d18c
lines 64–65why, one step back64 * Check if the given `type` is acceptable,65 * otherwise you should respond with 406 "Not Acceptable".generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 66–89why, one step back66 *67 * Examples:68 * 69 * // Accept: text/html70 * req.accepts('html');71 * // => true72 *73 * // Accept: text/*; application/json74 * req.accepts('html');75 * req.accepts('text/html');76 * req.accepts('text/plain');77 * req.accepts('application/json');78 * // => true79 *80 * req.accepts('image/png');81 * req.accepts('png');82 * // => false83 *84 * @param {String} type85 * @return {Boolean}86 * @api public87 */8889req.accepts = function(type){mergedTj Holowaychukbefore adoption547d18c
lines 90–91why, one step back90 return utils.accepts(type, this.header('Accept'));91};generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 92why, one step back92mergedTj Holowaychukbefore adoption547d18c
lines 93–100why, one step back93/**94 * Check if the given `charset` is acceptable,95 * otherwise you should respond with 406 "Not Acceptable".96 *97 * @param {String} charset98 * @return {Boolean}99 * @api public100 */generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 101why, one step back101mergedTj Holowaychukbefore adoption547d18c
lines 102–107why, one step back102req.acceptsCharset = function(charset){103 var accepted = this.acceptedCharsets;104 return accepted.length105 ? ~accepted.indexOf(charset)106 : true;107};generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 108why, one step back108mergedTj Holowaychukbefore adoption547d18c
lines 109–116why, one step back109/**110 * Check if the given `lang` is acceptable,111 * otherwise you should respond with 406 "Not Acceptable".112 *113 * @param {String} lang114 * @return {Boolean}115 * @api public116 */generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 117why, one step back117mergedTj Holowaychukbefore adoption547d18c
lines 118–122why, one step back118req.acceptsLanguage = function(lang){119 var accepted = this.acceptedLanguages;120 return accepted.length121 ? ~accepted.indexOf(lang)122 : true;generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 123why, one step back123};mergedTj Holowaychukbefore adoption547d18c
lines 124–197why, one step back124125/**126 * Return an array of Accepted media types127 * ordered from highest quality to lowest.128 *129 * Examples:130 *131 * [ { value: 'application/json',132 * quality: 1,133 * type: 'application',134 * subtype: 'json' },135 * { value: 'text/html',136 * quality: 0.5,137 * type: 'text',138 * subtype: 'html' } ]139 *140 * @return {Array}141 * @api public142 */143144req.__defineGetter__('accepted', function(){145 var accept = this.header('Accept');146 return accept147 ? utils.parseAccept(accept)148 : [];149});150151/**152 * Return an array of Accepted languages153 * ordered from highest quality to lowest.154 *155 * Examples:156 *157 * Accept-Language: en;q=.5, en-us158 * ['en-us', 'en']159 *160 * @return {Array}161 * @api public162 */163164req.__defineGetter__('acceptedLanguages', function(){165 var accept = this.header('Accept-Language');166 return accept167 ? utils168 .parseQuality(accept)169 .map(function(obj){170 return obj.value;171 })172 : [];173});174175/**176 * Return an array of Accepted charsets177 * ordered from highest quality to lowest.178 *179 * Examples:180 *181 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8182 * ['unicode-1-1', 'iso-8859-5']183 *184 * @return {Array}185 * @api public186 */187188req.__defineGetter__('acceptedCharsets', function(){189 var accept = this.header('Accept-Charset');190 return accept191 ? utils192 .parseQuality(accept)193 .map(function(obj){194 return obj.value;195 })196 : [];197});generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 198–201why, one step back198199/**200 * Return the value of param `name` when present or `defaultValue`.201 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 202why, one step back202 * - Checks body params, ex: id=12, {"id":12}generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 203–205why, one step back203 * - Checks route placeholders, ex: _/user/:id_204 * - Checks query string params, ex: ?id=12205 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 206why, one step back206 * To utilize request bodies, `req.body`generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 207why, one step back207 * should be an object. This can be done by usingreq.body takes precedenceTj Holowaychukbefore adoptione49c482
line 208why, one step back208 * the `connect.bodyParser()` middleware.generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 209–216why, one step back209 *210 * @param {String} name211 * @param {Mixed} defaultValue212 * @return {String}213 * @api public214 */215216req.param = function(name, defaultValue){req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 217–220why, one step back217 // req.body218 if (this.body && undefined !== this.body[name]) return this.body[name];219220 // route paramsgenerated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 221–226why, one step back221 if (this.params222 && this.params.hasOwnProperty(name)223 && undefined !== this.params[name]) {224 return this.params[name]; 225 }226req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 227–228why, one step back227 // query-string228 if (undefined !== this.query[name]) return this.query[name]; generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 229–241why, one step back229230 return defaultValue;231};232233/**234 * Check if the incoming request contains the "Content-Type" 235 * header field, and it contains the give mime `type`.236 *237 * Examples:238 *239 * // With Content-Type: text/html; charset=utf-8240 * req.is('html');241 * req.is('text/html');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 242why, one step back242 * req.is('text/*');generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 243–247why, one step back243 * // => true244 * 245 * // When Content-Type is application/json246 * req.is('json');247 * req.is('application/json');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 248why, one step back248 * req.is('application/*');generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 249–257why, one step back249 * // => true250 * 251 * req.is('html');252 * // => false253 * 254 * Now within our route callbacks, we can use to to assert content types255 * such as "image/jpeg", "image/png", etc.256 * 257 * app.post('/image/upload', function(req, res, next){Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 258why, one step back258 * if (req.is('image/*')) {generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 259–270why, one step back259 * // do something260 * } else {261 * next();262 * }263 * });264 * 265 * @param {String} type266 * @return {Boolean}267 * @api public268 */269270req.is = function(type){Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 271–273why, one step back271 var ct = this.headers['content-type'];272 if (!ct) return false;273 ct = ct.split(';')[0];generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 274–276why, one step back274 if (!~type.indexOf('/')) type = mime.lookup(type);275 if (~type.indexOf('*')) {276 type = type.split('/');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 277–280why, one step back277 ct = ct.split('/');278 if ('*' == type[0] && type[1] == ct[1]) return true;279 if ('*' == type[1] && type[0] == ct[0]) return true;280 return false;generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 281why, one step back281 }Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 282why, one step back282 return !! ~ct.indexOf(type);generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 283–285why, one step back283};284285/**Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 286–291why, one step back286 * Return the protocol string "http" or "https"287 * when requested with TLS. When the "trust proxy" 288 * setting is enabled the "X-Forwarded-Proto" header289 * field will be trusted. If you're running behind290 * a reverse proxy that supplies https for you this291 * may be enabled.Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 292–296why, one step back292 *293 * @return {String}294 * @api public295 */296Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 297–298why, one step back297req.__defineGetter__('protocol', function(trustProxy){298 var trustProxy = this.app.settings['trust proxy'];Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 299–301why, one step back299 return this.secure300 ? 'https'301 : trustProxyAdded "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 302why, one step back302 ? (this.get('X-Forwarded-Proto') || 'http')Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
line 303why, one step back303 : 'http';Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 304why, one step back304});Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 305–306why, one step back305306/**generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 307–313why, one step back307 * Short-hand for `req.connection.encrypted`.308 *309 * @return {Boolean}310 * @api public311 */312313req.__defineGetter__('secure', function(){Added X-Forwarded-Proto support to `res.redirect()`. Closes #927Tj Holowaychukbefore adoption5757f87
line 314why, one step back314 return this.connection.encrypted;generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 315–342why, one step back315});316317/**318 * Short-hand for `require('url').parse(req.url).pathname`.319 *320 * @return {String}321 * @api public322 */323324req.__defineGetter__('path', function(){325 return parse(this.url).pathname;326});327328/**329 * Check if the request is fresh, aka330 * Last-Modified and/or the ETag331 * still match.332 *333 * @return {Boolean}334 * @api public335 */336337req.__defineGetter__('fresh', function(){338 return ! this.stale;339});340341/**342 * Check if the request is stale, akadocsTJ Holowaychukbefore adoption6ab61d0
line 343why, one step back343 * "Last-Modified" and / or the "ETag" for thegenerated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 344–360why, one step back344 * resource has changed.345 *346 * @return {Boolean}347 * @api public348 */349350req.__defineGetter__('stale', function(){351 return connect.utils.modified(this, this.res);352});353354/**355 * Check if the request was an _XMLHttpRequest_.356 *357 * @return {Boolean}358 * @api public359 */360Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 361why, one step back361req.__defineGetter__('xhr', function(){refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee
lines 362–363why, one step back362 var val = this.get('X-Requested-With') || '';363 return 'xmlhttprequest' == val.toLowerCase();Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 364why, one step back364});