request.js
This file
Path
lib/request.js
418 lines · 8.8 KiB · JavaScript
Last changed by
docsTJ Holowaychukbefore adoption27b1b5e
History of this fileTake it
The walk stopped after 200 commits: lines older than fc1e849 point at it.
fixed headTj Holowaychukbefore adoptionfc1e849
lines 1–26why, 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');1718/**19 * Request prototype.20 */2122var req = exports = module.exports = {23 __proto__: http.IncomingMessage.prototype24};2526/**doc typoTJ Holowaychukbefore adoptionfe27989
line 27why, one step back27 * Return request header.fixed headTj Holowaychukbefore adoptionfc1e849
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');fixed headTj Holowaychukbefore adoptionfc1e849
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');fixed headTj Holowaychukbefore adoptionfc1e849
lines 39–40why, one step back39 * // => "text/plain"40 * Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 41why, one step back41 * req.get('Something');fixed headTj Holowaychukbefore adoptionfc1e849
line 42why, one step back42 * // => undefineddocsTJ Holowaychukbefore adoption27b1b5e
lines 43–45why, one step back43 *44 * Aliased as `req.header()`.45 *fixed headTj Holowaychukbefore adoptionfc1e849
lines 46–50why, one step back46 * @param {String} name47 * @return {String} 48 * @api public49 */50Added `res.header()` and `req.header()` aliases for BCTJ Holowaychukbefore adoption5fa2f89
lines 51–52why, one step back51req.get = 52req.header = function(name){fixed headTj Holowaychukbefore adoptionfc1e849
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;fixed headTj Holowaychukbefore adoptionfc1e849
line 58why, one step back58 default:Removed `req.header()` defaultValue supportTJ Holowaychukbefore adoptionbfba98f
line 59why, one step back59 return this.headers[name];fixed headTj Holowaychukbefore adoptionfc1e849
lines 60–63why, one step back60 }61};6263/**docsTJ Holowaychukbefore adoption4e2677f
lines 64–66why, one step back64 * Check if the given `type(s)` is acceptable, returning65 * the best match when true, otherwise `undefined`, in which66 * case you should respond with 406 "Not Acceptable".fixed headTj Holowaychukbefore adoptionfc1e849
line 67why, one step back67 *docsTJ Holowaychukbefore adoption365a98d
lines 68–73why, one step back68 * The `type` value may be a single mime type string69 * such as "application/json", the extension name70 * such as "json", a comma-delimted list such as "json, html, text/plain",71 * or an array `["json", "html", "text/plain"]`. When a list72 * or array is given the _best_ match, if any is returned.73 *fixed headTj Holowaychukbefore adoptionfc1e849
lines 74–77why, one step back74 * Examples:75 * 76 * // Accept: text/html77 * req.accepts('html');docsTJ Holowaychukbefore adoption365a98d
line 78why, one step back78 * // => "html"fixed headTj Holowaychukbefore adoptionfc1e849
line 79why, one step back79 *typoTJ Holowaychukbefore adoptionba570a9
line 80why, one step back80 * // Accept: text/*, application/jsonfixed headTj Holowaychukbefore adoptionfc1e849
line 81why, one step back81 * req.accepts('html');docsTJ Holowaychukbefore adoption365a98d
line 82why, one step back82 * // => "html"fixed headTj Holowaychukbefore adoptionfc1e849
line 83why, one step back83 * req.accepts('text/html');docsTJ Holowaychukbefore adoption365a98d
lines 84–86why, one step back84 * // => "text/html"85 * req.accepts('json, text');86 * // => "json"fixed headTj Holowaychukbefore adoptionfc1e849
line 87why, one step back87 * req.accepts('application/json');docsTJ Holowaychukbefore adoption365a98d
line 88why, one step back88 * // => "application/json"fixed headTj Holowaychukbefore adoptionfc1e849
line 89why, one step back89 *typoTJ Holowaychukbefore adoptionba570a9
line 90why, one step back90 * // Accept: text/*, application/jsonfixed headTj Holowaychukbefore adoptionfc1e849
lines 91–92why, one step back91 * req.accepts('image/png');92 * req.accepts('png');docsTJ Holowaychukbefore adoption365a98d
line 93why, one step back93 * // => undefinedfixed headTj Holowaychukbefore adoptionfc1e849
line 94why, one step back94 *typoTJ Holowaychukbefore adoptionba570a9
line 95why, one step back95 * // Accept: text/*;q=.5, application/jsonAdded array support. Closes #1053TJ Holowaychukbefore adoption298899d
line 96why, one step back96 * req.accepts(['html', 'json']);docsTJ Holowaychukbefore adoption365a98d
lines 97–98why, one step back97 * req.accepts('html, json');98 * // => "json"Added array support. Closes #1053TJ Holowaychukbefore adoption298899d
lines 99–100why, one step back99 *100 * @param {String|Array} type(s)docsTJ Holowaychukbefore adoption4e2677f
line 101why, one step back101 * @return {String}fixed headTj Holowaychukbefore adoptionfc1e849
lines 102–105why, one step back102 * @api public103 */104105req.accepts = function(type){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 106why, one step back106 return utils.accepts(type, this.get('Accept'));mergedTj Holowaychukbefore adoption547d18c
line 107why, one step back107};fixed headTj Holowaychukbefore adoptionfc1e849
line 108why, one step back108mergedTj Holowaychukbefore adoption547d18c
lines 109–116why, one step back109/**110 * Check if the given `charset` is acceptable,111 * otherwise you should respond with 406 "Not Acceptable".112 *113 * @param {String} charset114 * @return {Boolean}115 * @api public116 */fixed headTj Holowaychukbefore adoptionfc1e849
line 117why, one step back117mergedTj Holowaychukbefore adoption547d18c
lines 118–123why, one step back118req.acceptsCharset = function(charset){119 var accepted = this.acceptedCharsets;120 return accepted.length121 ? ~accepted.indexOf(charset)122 : true;123};fixed headTj Holowaychukbefore adoptionfc1e849
line 124why, one step back124mergedTj Holowaychukbefore adoption547d18c
lines 125–132why, one step back125/**126 * Check if the given `lang` is acceptable,127 * otherwise you should respond with 406 "Not Acceptable".128 *129 * @param {String} lang130 * @return {Boolean}131 * @api public132 */fixed headTj Holowaychukbefore adoptionfc1e849
line 133why, one step back133mergedTj Holowaychukbefore adoption547d18c
lines 134–138why, one step back134req.acceptsLanguage = function(lang){135 var accepted = this.acceptedLanguages;136 return accepted.length137 ? ~accepted.indexOf(lang)138 : true;fixed headTj Holowaychukbefore adoptionfc1e849
line 139why, one step back139};mergedTj Holowaychukbefore adoption547d18c
lines 140–160why, one step back140141/**142 * Return an array of Accepted media types143 * ordered from highest quality to lowest.144 *145 * Examples:146 *147 * [ { value: 'application/json',148 * quality: 1,149 * type: 'application',150 * subtype: 'json' },151 * { value: 'text/html',152 * quality: 0.5,153 * type: 'text',154 * subtype: 'html' } ]155 *156 * @return {Array}157 * @api public158 */159160req.__defineGetter__('accepted', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 161why, one step back161 var accept = this.get('Accept');mergedTj Holowaychukbefore adoption547d18c
lines 162–180why, one step back162 return accept163 ? utils.parseAccept(accept)164 : [];165});166167/**168 * Return an array of Accepted languages169 * ordered from highest quality to lowest.170 *171 * Examples:172 *173 * Accept-Language: en;q=.5, en-us174 * ['en-us', 'en']175 *176 * @return {Array}177 * @api public178 */179180req.__defineGetter__('acceptedLanguages', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 181why, one step back181 var accept = this.get('Accept-Language');mergedTj Holowaychukbefore adoption547d18c
lines 182–204why, one step back182 return accept183 ? utils184 .parseQuality(accept)185 .map(function(obj){186 return obj.value;187 })188 : [];189});190191/**192 * Return an array of Accepted charsets193 * ordered from highest quality to lowest.194 *195 * Examples:196 *197 * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8198 * ['unicode-1-1', 'iso-8859-5']199 *200 * @return {Array}201 * @api public202 */203204req.__defineGetter__('acceptedCharsets', function(){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 205why, one step back205 var accept = this.get('Accept-Charset');mergedTj Holowaychukbefore adoption547d18c
lines 206–213why, one step back206 return accept207 ? utils208 .parseQuality(accept)209 .map(function(obj){210 return obj.value;211 })212 : [];213});fixed headTj Holowaychukbefore adoptionfc1e849
lines 214–217why, one step back214215/**216 * Return the value of param `name` when present or `defaultValue`.217 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 218why, one step back218 * - Checks body params, ex: id=12, {"id":12}fixed headTj Holowaychukbefore adoptionfc1e849
lines 219–221why, one step back219 * - Checks route placeholders, ex: _/user/:id_220 * - Checks query string params, ex: ?id=12221 *req.body takes precedenceTj Holowaychukbefore adoptione49c482
line 222why, one step back222 * To utilize request bodies, `req.body`fixed headTj Holowaychukbefore adoptionfc1e849
line 223why, one step back223 * should be an object. This can be done by usingreq.body takes precedenceTj Holowaychukbefore adoptione49c482
line 224why, one step back224 * the `connect.bodyParser()` middleware.fixed headTj Holowaychukbefore adoptionfc1e849
lines 225–232why, one step back225 *226 * @param {String} name227 * @param {Mixed} defaultValue228 * @return {String}229 * @api public230 */231232req.param = function(name, defaultValue){req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 233–236why, one step back233 // req.body234 if (this.body && undefined !== this.body[name]) return this.body[name];235236 // route paramsfixed headTj Holowaychukbefore adoptionfc1e849
lines 237–242why, one step back237 if (this.params238 && this.params.hasOwnProperty(name)239 && undefined !== this.params[name]) {240 return this.params[name]; 241 }242req.body takes precedenceTj Holowaychukbefore adoptione49c482
lines 243–244why, one step back243 // query-string244 if (undefined !== this.query[name]) return this.query[name]; fixed headTj Holowaychukbefore adoptionfc1e849
lines 245–257why, one step back245246 return defaultValue;247};248249/**250 * Check if the incoming request contains the "Content-Type" 251 * header field, and it contains the give mime `type`.252 *253 * Examples:254 *255 * // With Content-Type: text/html; charset=utf-8256 * req.is('html');257 * req.is('text/html');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 258why, one step back258 * req.is('text/*');fixed headTj Holowaychukbefore adoptionfc1e849
lines 259–263why, one step back259 * // => true260 * 261 * // When Content-Type is application/json262 * req.is('json');263 * req.is('application/json');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 264why, one step back264 * req.is('application/*');fixed headTj Holowaychukbefore adoptionfc1e849
lines 265–273why, one step back265 * // => true266 * 267 * req.is('html');268 * // => false269 * 270 * Now within our route callbacks, we can use to to assert content types271 * such as "image/jpeg", "image/png", etc.272 * 273 * app.post('/image/upload', function(req, res, next){Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 274why, one step back274 * if (req.is('image/*')) {fixed headTj Holowaychukbefore adoptionfc1e849
lines 275–286why, one step back275 * // do something276 * } else {277 * next();278 * }279 * });280 * 281 * @param {String} type282 * @return {Boolean}283 * @api public284 */285286req.is = function(type){Removed `req.header(field)`TJ Holowaychukbefore adoption85d6964
line 287why, one step back287 var ct = this.get('Content-Type');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 288–289why, one step back288 if (!ct) return false;289 ct = ct.split(';')[0];fixed headTj Holowaychukbefore adoptionfc1e849
lines 290–292why, one step back290 if (!~type.indexOf('/')) type = mime.lookup(type);291 if (~type.indexOf('*')) {292 type = type.split('/');Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
lines 293–296why, one step back293 ct = ct.split('/');294 if ('*' == type[0] && type[1] == ct[1]) return true;295 if ('*' == type[1] && type[0] == ct[0]) return true;296 return false;fixed headTj Holowaychukbefore adoptionfc1e849
line 297why, one step back297 }Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9
line 298why, one step back298 return !! ~ct.indexOf(type);fixed headTj Holowaychukbefore adoptionfc1e849
lines 299–301why, one step back299};300301/**Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
lines 302–307why, one step back302 * Return the protocol string "http" or "https"303 * when requested with TLS. When the "trust proxy" 304 * setting is enabled the "X-Forwarded-Proto" header305 * field will be trusted. If you're running behind306 * a reverse proxy that supplies https for you this307 * may be enabled.Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 308–312why, one step back308 *309 * @return {String}310 * @api public311 */312populate changelogTJ Holowaychukbefore adoptionabe5cc7
line 313why, one step back313req.__defineGetter__('protocol', function(){Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 314why, one step back314 var trustProxy = this.app.settings['trust proxy'];Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 315why, one step back315 return this.connection.encryptedAdded `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 316–317why, one step back316 ? 'https'317 : trustProxyAdded "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 318why, one step back318 ? (this.get('X-Forwarded-Proto') || 'http')Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
line 319why, one step back319 : 'http';Added "trust proxy" settingTJ Holowaychukbefore adoption4d87efc
line 320why, one step back320});Added `req.protocol()`TJ Holowaychukbefore adoption2ba343d
lines 321–322why, one step back321322/**Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 323why, one step back323 * Short-hand for:fixed headTj Holowaychukbefore adoptionfc1e849
line 324why, one step back324 *Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
lines 325–326why, one step back325 * req.protocol == 'https'326 *fixed headTj Holowaychukbefore adoptionfc1e849
lines 327–331why, one step back327 * @return {Boolean}328 * @api public329 */330331req.__defineGetter__('secure', function(){Added X-Forwarded-Proto support to `req.secure`TJ Holowaychukbefore adoption04ecf04
line 332why, one step back332 return 'https' == this.protocol;fixed headTj Holowaychukbefore adoptionfc1e849
lines 333–335why, one step back333});334335/**Added `req.ips`TJ Holowaychukbefore adoptiondd468fb
lines 336–338why, one step back336 * When "trust proxy" is `true`, parse337 * the "X-Forwarded-For" ip address list.338 *reverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c
lines 339–342why, one step back339 * For example if the value were "client, proxy1, proxy2"340 * you would receive the array `["proxy2", "proxy1", "client"]`341 * where "proxy2" is the furthest down-stream.342 *Added `req.ips`TJ Holowaychukbefore adoptiondd468fb
lines 343–347why, one step back343 * @return {Array}344 * @api public345 */346347req.__defineGetter__('ips', function(){req.ipds doesnt need "trust proxy"TJ Holowaychukbefore adoption6d0f9a3
lines 348–349why, one step back348 var val = this.get('X-Forwarded-For');349 return valreverse req.ips arrayTJ Holowaychukbefore adoption54c3d5c
line 350why, one step back350 ? val.split(/ *, */).reverse()Added `req.ips`TJ Holowaychukbefore adoptiondd468fb
lines 351–354why, one step back351 : [];352});353354/**Added `req.subdomains`TJ Holowaychukbefore adoption4e33245
lines 355–371why, one step back355 * Return subdomains as an array.356 *357 * For example "tobi.ferrets.example.com"358 * would provide `["ferrets", "tobi"]`.359 *360 * @return {Array}361 * @api public362 */363364req.__defineGetter__('subdomains', function(){365 return this.get('Host')366 .split('.')367 .slice(0, -2)368 .reverse();369});370371/**Added new dox API docsTJ Holowaychukbefore adoption7e81cad
line 372why, one step back372 * Short-hand for `url.parse(req.url).pathname`.fixed headTj Holowaychukbefore adoptionfc1e849
lines 373–396why, one step back373 *374 * @return {String}375 * @api public376 */377378req.__defineGetter__('path', function(){379 return parse(this.url).pathname;380});381382/**383 * Check if the request is fresh, aka384 * Last-Modified and/or the ETag385 * still match.386 *387 * @return {Boolean}388 * @api public389 */390391req.__defineGetter__('fresh', function(){392 return ! this.stale;393});394395/**396 * Check if the request is stale, akadocsTJ Holowaychukbefore adoption6ab61d0
line 397why, one step back397 * "Last-Modified" and / or the "ETag" for thefixed headTj Holowaychukbefore adoptionfc1e849
lines 398–415why, one step back398 * resource has changed.399 *400 * @return {Boolean}401 * @api public402 */403404req.__defineGetter__('stale', function(){405 return connect.utils.modified(this, this.res);406});407408/**409 * Check if the request was an _XMLHttpRequest_.410 *411 * @return {Boolean}412 * @api public413 */414415req.__defineGetter__('xhr', function(){refactored req.xhrTJ Holowaychukbefore adoptionb04f3ee
lines 416–417why, one step back416 var val = this.get('X-Requested-With') || '';417 return 'xmlhttprequest' == val.toLowerCase();fixed headTj Holowaychukbefore adoptionfc1e849
line 418why, one step back418});