request.js
This file
Path
lib/request.js
391 lines · 8.1 KiB · JavaScript
Last changed by
more docs for req.accepts(arr)TJ Holowaychukbefore adoptione4827b8
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/**Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 61why, one step back61 * Check if the given `type(s)` is acceptable,mergedTj Holowaychukbefore adoption547d18c
line 62why, one step back62 * otherwise you should respond with 406 "Not Acceptable".removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 63–76why, 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 *more docs for req.accepts(arr)TJ Holowaychukbefore adoptione4827b8
line 77why, one step back77 * // Accept: text/*; application/jsonremoved a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 78–81why, one step back78 * req.accepts('image/png');79 * req.accepts('png');80 * // => false81 *Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
lines 82–85why, one step back82 * // Accept: text/*; application/json83 * req.accepts(['html', 'json']);84 * // => "html"85 *more docs for req.accepts(arr)TJ Holowaychukbefore adoptione4827b8
line 86why, one step back86 * // Accept: text/*; q=.5, application/jsonAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 87why, one step back87 * req.accepts(['text/html', 'application/json']);more docs for req.accepts(arr)TJ Holowaychukbefore adoptione4827b8
lines 88–92why, one step back88 * // => "application/json"89 *90 * // Accept: text/plain91 * req.accepts(['text/html', 'application/json']);92 * // => undefinedAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
lines 93–95why, one step back93 *94 * @param {String|Array} type(s)95 * @return {Boolean|String}removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 96–99why, one step back96 * @api public97 */9899req.accepts = function(type){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 100why, one step back100 return utils.accepts(type, this.get('Accept'));mergedTj Holowaychukbefore adoption547d18c
line 101why, one step back101};removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 102why, one step back102mergedTj Holowaychukbefore adoption547d18c
lines 103–110why, one step back103/**104 * Check if the given `charset` is acceptable,105 * otherwise you should respond with 406 "Not Acceptable".106 *107 * @param {String} charset108 * @return {Boolean}109 * @api public110 */removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 111why, one step back111mergedTj Holowaychukbefore adoption547d18c
lines 112–117why, one step back112req.acceptsCharset = function(charset){113 var accepted = this.acceptedCharsets;114 return accepted.length115 ? ~accepted.indexOf(charset)116 : true;117};removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 118why, one step back118mergedTj Holowaychukbefore adoption547d18c
lines 119–126why, one step back119/**120 * Check if the given `lang` is acceptable,121 * otherwise you should respond with 406 "Not Acceptable".122 *123 * @param {String} lang124 * @return {Boolean}125 * @api public126 */removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 127why, one step back127mergedTj Holowaychukbefore adoption547d18c
lines 128–132why, one step back128req.acceptsLanguage = function(lang){129 var accepted = this.acceptedLanguages;130 return accepted.length131 ? ~accepted.indexOf(lang)132 : true;removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 133why, one step back133};mergedTj Holowaychukbefore adoption547d18c
lines 134–154why, one step back134135/**136 * Return an array of Accepted media types137 * ordered from highest quality to lowest.138 *139 * Examples:140 *141 * [ { value: 'application/json',142 * quality: 1,143 * type: 'application',144 * subtype: 'json' },145 * { value: 'text/html',146 * quality: 0.5,147 * type: 'text',148 * subtype: 'html' } ]149 *150 * @return {Array}151 * @api public152 */153154req.__defineGetter__('accepted', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 155why, one step back155 var accept = this.get('Accept');mergedTj Holowaychukbefore adoption547d18c
lines 156–174why, one step back156 return accept157 ? utils.parseAccept(accept)158 : [];159});160161/**162 * Return an array of Accepted languages163 * ordered from highest quality to lowest.164 *165 * Examples:166 *167 * Accept-Language: en;q=.5, en-us168 * ['en-us', 'en']169 *170 * @return {Array}171 * @api public172 */173174req.__defineGetter__('acceptedLanguages', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 175why, one step back175 var accept = this.get('Accept-Language');mergedTj Holowaychukbefore adoption547d18c
lines 176–198why, one step back176 return accept177 ? utils178 .parseQuality(accept)179 .map(function(obj){180 return obj.value;181 })182 : [];183});184185/**186 * Return an array of Accepted charsets187 * ordered from highest quality to lowest.188 *189 * Examples:190 *191 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8192 * ['unicode-1-1', 'iso-8859-5']193 *194 * @return {Array}195 * @api public196 */197198req.__defineGetter__('acceptedCharsets', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 199why, one step back199 var accept = this.get('Accept-Charset');mergedTj Holowaychukbefore adoption547d18c
lines 200–207why, one step back200 return accept201 ? utils202 .parseQuality(accept)203 .map(function(obj){204 return obj.value;205 })206 : [];207});removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 208–211why, one step back208209/**210 * Return the value of param `name` when present or `defaultValue`.211 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 212why, one step back212 * - Checks body params, ex: id=12, {"id":12}removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 213–215why, one step back213 * - Checks route placeholders, ex: _/user/:id_214 * - Checks query string params, ex: ?id=12215 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 216why, one step back216 * To utilize request bodies, `req.body`removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 217why, one step back217 * should be an object. This can be done by usingreq.body takes precedenceTj Holowaychukbefore adoptione49c482
line 218why, one step back218 * the `connect.bodyParser()` middleware.removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 219–226why, one step back219 *220 * @param {String} name221 * @param {Mixed} defaultValue222 * @return {String}223 * @api public224 */225226req.param = function(name, defaultValue){req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 227–230why, one step back227 // req.body228 if (this.body && undefined !== this.body[name]) return this.body[name];229230 // route paramsremoved a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 231–236why, one step back231 if (this.params232 && this.params.hasOwnProperty(name)233 && undefined !== this.params[name]) {234 return this.params[name]; 235 }236req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 237–238why, one step back237 // query-string238 if (undefined !== this.query[name]) return this.query[name]; removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 239–251why, one step back239240 return defaultValue;241};242243/**244 * Check if the incoming request contains the "Content-Type" 245 * header field, and it contains the give mime `type`.246 *247 * Examples:248 *249 * // With Content-Type: text/html; charset=utf-8250 * req.is('html');251 * req.is('text/html');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 252why, one step back252 * req.is('text/*');removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 253–257why, one step back253 * // => true254 * 255 * // When Content-Type is application/json256 * req.is('json');257 * req.is('application/json');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 258why, one step back258 * req.is('application/*');removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 259–267why, one step back259 * // => true260 * 261 * req.is('html');262 * // => false263 * 264 * Now within our route callbacks, we can use to to assert content types265 * such as "image/jpeg", "image/png", etc.266 * 267 * app.post('/image/upload', function(req, res, next){Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 268why, one step back268 * if (req.is('image/*')) {removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 269–280why, one step back269 * // do something270 * } else {271 * next();272 * }273 * });274 * 275 * @param {String} type276 * @return {Boolean}277 * @api public278 */279280req.is = function(type){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 281why, one step back281 var ct = this.get('Content-Type');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 282–283why, one step back282 if (!ct) return false;283 ct = ct.split(';')[0];removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 284–286why, one step back284 if (!~type.indexOf('/')) type = mime.lookup(type);285 if (~type.indexOf('*')) {286 type = type.split('/');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 287–290why, one step back287 ct = ct.split('/');288 if ('*' == type[0] && type[1] == ct[1]) return true;289 if ('*' == type[1] && type[0] == ct[0]) return true;290 return false;removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
line 291why, one step back291 }Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 292why, one step back292 return !! ~ct.indexOf(type);removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 293–295why, one step back293};294295/**Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 296–301why, one step back296 * Return the protocol string "http" or "https"297 * when requested with TLS. When the "trust proxy" 298 * setting is enabled the "X-Forwarded-Proto" header299 * field will be trusted. If you're running behind300 * a reverse proxy that supplies https for you this301 * may be enabled.Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 302–306why, one step back302 *303 * @return {String}304 * @api public305 */306Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 307–308why, one step back307req.__defineGetter__('protocol', function(trustProxy){308 var trustProxy = this.app.settings['trust proxy'];Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 309–311why, one step back309 return this.secure310 ? 'https'311 : trustProxyAdded "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 312why, one step back312 ? (this.get('X-Forwarded-Proto') || 'http')Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
line 313why, one step back313 : 'http';Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 314why, one step back314});Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 315–316why, one step back315316/**removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 317–323why, one step back317 * Short-hand for `req.connection.encrypted`.318 *319 * @return {Boolean}320 * @api public321 */322323req.__defineGetter__('secure', function(){Added X-Forwarded-Proto support to `res.redirect()`. Closes #927Tj Holowaychukbefore adoption5757f87
line 324why, one step back324 return this.connection.encrypted;removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 325–327why, one step back325});326327/**Added `req.subdomains`TJ Holowaychukbefore adoption4e33245
lines 328–344why, one step back328 * Return subdomains as an array.329 *330 * For example "tobi.ferrets.example.com"331 * would provide `["ferrets", "tobi"]`.332 *333 * @return {Array}334 * @api public335 */336337req.__defineGetter__('subdomains', function(){338 return this.get('Host')339 .split('.')340 .slice(0, -2)341 .reverse();342});343344/**removed a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 345–369why, one step back345 * Short-hand for `require('url').parse(req.url).pathname`.346 *347 * @return {String}348 * @api public349 */350351req.__defineGetter__('path', function(){352 return parse(this.url).pathname;353});354355/**356 * Check if the request is fresh, aka357 * Last-Modified and/or the ETag358 * still match.359 *360 * @return {Boolean}361 * @api public362 */363364req.__defineGetter__('fresh', function(){365 return ! this.stale;366});367368/**369 * Check if the request is stale, akadocsTJ Holowaychukbefore adoption6ab61d0
line 370why, one step back370 * "Last-Modified" and / or the "ETag" for theremoved a file i didnt mean to add :)Tj Holowaychukbefore adoption6bee32c
lines 371–387why, one step back371 * resource has changed.372 *373 * @return {Boolean}374 * @api public375 */376377req.__defineGetter__('stale', function(){378 return connect.utils.modified(this, this.res);379});380381/**382 * Check if the request was an _XMLHttpRequest_.383 *384 * @return {Boolean}385 * @api public386 */387Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 388why, one step back388req.__defineGetter__('xhr', function(){refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee
lines 389–390why, one step back389 var val = this.get('X-Requested-With') || '';390 return 'xmlhttprequest' == val.toLowerCase();Removed req.isXMLHttpRequestTj Holowaychukbefore adoptionaf46df7
line 391why, one step back391});