request.js
This file
Path
lib/request.js
445 lines · 10.2 KiB · JavaScript
Last changed by
add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
History of this fileTake it
The walk stopped after 200 commits: lines older than 34c83d7 point at it.
3.4.7Jonathan Ongbefore adoption34c83d7
lines 1–4why, one step back1/**2 * Module dependencies.3 */4refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
line 5why, one step back5var accepts = require('accepts');add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
line 6why, one step back6var deprecate = require('depd')('express');refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
line 7why, one step back7var typeis = require('type-is');refactor to use basic-authJonathan Ongbefore adoption3cf7b2e
lines 8–10why, one step back8var http = require('http');9var fresh = require('fresh');10var parseRange = require('range-parser');use parseurlJonathan Ongbefore adoption7693aa5
line 11why, one step back11var parse = require('parseurl');Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 12why, one step back12var proxyaddr = require('proxy-addr');3.4.7Jonathan Ongbefore adoption34c83d7
lines 13–58why, one step back1314/**15 * Request prototype.16 */1718var req = exports = module.exports = {19 __proto__: http.IncomingMessage.prototype20};2122/**23 * Return request header.24 *25 * The `Referrer` header field is special-cased,26 * both `Referrer` and `Referer` are interchangeable.27 *28 * Examples:29 *30 * req.get('Content-Type');31 * // => "text/plain"32 *33 * req.get('content-type');34 * // => "text/plain"35 *36 * req.get('Something');37 * // => undefined38 *39 * Aliased as `req.header()`.40 *41 * @param {String} name42 * @return {String}43 * @api public44 */4546req.get =47req.header = function(name){48 switch (name = name.toLowerCase()) {49 case 'referer':50 case 'referrer':51 return this.headers.referrer52 || this.headers.referer;53 default:54 return this.headers[name];55 }56};5758/**refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
lines 59–60why, one step back59 * To do: update docs.60 *3.4.7Jonathan Ongbefore adoption34c83d7
lines 61–103why, 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".64 *65 * 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 * an argument list such as `"json", "html", "text/plain"`,69 * or an array `["json", "html", "text/plain"]`. When a list70 * or array is given the _best_ match, if any is returned.71 *72 * Examples:73 *74 * // Accept: text/html75 * req.accepts('html');76 * // => "html"77 *78 * // Accept: text/*, application/json79 * req.accepts('html');80 * // => "html"81 * req.accepts('text/html');82 * // => "text/html"83 * req.accepts('json, text');84 * // => "json"85 * req.accepts('application/json');86 * // => "application/json"87 *88 * // Accept: text/*, application/json89 * req.accepts('image/png');90 * req.accepts('png');91 * // => undefined92 *93 * // Accept: text/*;q=.5, application/json94 * req.accepts(['html', 'json']);95 * req.accepts('html', 'json');96 * req.accepts('html, json');97 * // => "json"98 *99 * @param {String|Array} type(s)100 * @return {String}101 * @api public102 */103refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
lines 104–106why, one step back104req.accepts = function(){105 var accept = accepts(this);106 return accept.types.apply(accept, arguments);3.4.7Jonathan Ongbefore adoption34c83d7
lines 107–109why, one step back107};108109/**add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
line 110why, one step back110 * Check if the given `encoding`s are accepted.3.4.7Jonathan Ongbefore adoption34c83d7
line 111why, one step back111 *add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
line 112why, one step back112 * @param {String} ...encoding3.4.7Jonathan Ongbefore adoption34c83d7
lines 113–116why, one step back113 * @return {Boolean}114 * @api public115 */116refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
lines 117–119why, one step back117req.acceptsEncodings = function(){118 var accept = accepts(this);119 return accept.encodings.apply(accept, arguments);3.4.7Jonathan Ongbefore adoption34c83d7
lines 120–121why, one step back120};121add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
lines 122–124why, one step back122req.acceptsEncoding = deprecate.function(req.acceptsEncodings,123 'req.acceptsEncoding: Use acceptsEncodings instead');1243.4.7Jonathan Ongbefore adoption34c83d7
line 125why, one step back125/**add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
line 126why, one step back126 * Check if the given `charset`s are acceptable,3.4.7Jonathan Ongbefore adoption34c83d7
lines 127–128why, one step back127 * otherwise you should respond with 406 "Not Acceptable".128 *add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
line 129why, one step back129 * @param {String} ...charset3.4.7Jonathan Ongbefore adoption34c83d7
lines 130–133why, one step back130 * @return {Boolean}131 * @api public132 */133refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
lines 134–136why, one step back134req.acceptsCharsets = function(){135 var accept = accepts(this);136 return accept.charsets.apply(accept, arguments);3.4.7Jonathan Ongbefore adoption34c83d7
lines 137–138why, one step back137};138add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
lines 139–141why, one step back139req.acceptsCharset = deprecate.function(req.acceptsCharsets,140 'req.acceptsCharset: Use acceptsCharsets instead');1413.4.7Jonathan Ongbefore adoption34c83d7
line 142why, one step back142/**add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
line 143why, one step back143 * Check if the given `lang`s are acceptable,3.4.7Jonathan Ongbefore adoption34c83d7
lines 144–145why, one step back144 * otherwise you should respond with 406 "Not Acceptable".145 *add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
line 146why, one step back146 * @param {String} ...lang3.4.7Jonathan Ongbefore adoption34c83d7
lines 147–150why, one step back147 * @return {Boolean}148 * @api public149 */150some jshint cleanupJonathan Ongbefore adoption5572897
line 151why, one step back151req.acceptsLanguages = function(){refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
lines 152–153why, one step back152 var accept = accepts(this);153 return accept.languages.apply(accept, arguments);3.4.7Jonathan Ongbefore adoption34c83d7
line 154why, one step back154};add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1
lines 155–157why, one step back155156req.acceptsLanguage = deprecate.function(req.acceptsLanguages,157 'req.acceptsLanguage: Use acceptsLanguages instead');3.4.7Jonathan Ongbefore adoption34c83d7
lines 158–193why, one step back158159/**160 * Parse Range header field,161 * capping to the given `size`.162 *163 * Unspecified ranges such as "0-" require164 * knowledge of your resource length. In165 * the case of a byte range this is of course166 * the total number of bytes. If the Range167 * header field is not given `null` is returned,168 * `-1` when unsatisfiable, `-2` when syntactically invalid.169 *170 * NOTE: remember that ranges are inclusive, so171 * for example "Range: users=0-3" should respond172 * with 4 users when available, not 3.173 *174 * @param {Number} size175 * @return {Array}176 * @api public177 */178179req.range = function(size){180 var range = this.get('Range');181 if (!range) return;182 return parseRange(size, range);183};184185/**186 * Return the value of param `name` when present or `defaultValue`.187 *188 * - Checks route placeholders, ex: _/user/:id_189 * - Checks body params, ex: id=12, {"id":12}190 * - Checks query string params, ex: ?id=12191 *192 * To utilize request bodies, `req.body`193 * should be an object. This can be done by usingremove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 194why, one step back194 * the `bodyParser()` middleware.3.4.7Jonathan Ongbefore adoption34c83d7
lines 195–237why, one step back195 *196 * @param {String} name197 * @param {Mixed} [defaultValue]198 * @return {String}199 * @api public200 */201202req.param = function(name, defaultValue){203 var params = this.params || {};204 var body = this.body || {};205 var query = this.query || {};206 if (null != params[name] && params.hasOwnProperty(name)) return params[name];207 if (null != body[name]) return body[name];208 if (null != query[name]) return query[name];209 return defaultValue;210};211212/**213 * Check if the incoming request contains the "Content-Type"214 * header field, and it contains the give mime `type`.215 *216 * Examples:217 *218 * // With Content-Type: text/html; charset=utf-8219 * req.is('html');220 * req.is('text/html');221 * req.is('text/*');222 * // => true223 *224 * // When Content-Type is application/json225 * req.is('json');226 * req.is('application/json');227 * req.is('application/*');228 * // => true229 *230 * req.is('html');231 * // => false232 *233 * @param {String} type234 * @return {Boolean}235 * @api public236 */237refactor req.is and req.accepts*Jonathan Ongbefore adoptioncec0c06
lines 238–240why, one step back238req.is = function(types){239 if (!Array.isArray(types)) types = [].slice.call(arguments);240 return typeis(this, types);3.4.7Jonathan Ongbefore adoption34c83d7
lines 241–245why, one step back241};242243/**244 * Return the protocol string "http" or "https"245 * when requested with TLS. When the "trust proxy"Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 246–249why, one step back246 * setting trusts the socket address, the247 * "X-Forwarded-Proto" header field will be trusted.248 * If you're running behind a reverse proxy that249 * supplies https for you this may be enabled.3.4.7Jonathan Ongbefore adoption34c83d7
lines 250–254why, one step back250 *251 * @return {String}252 * @api public253 */254Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 255why, one step back255defineGetter(req, 'protocol', function protocol(){Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 256–265why, one step back256 var trust = this.app.get('trust proxy fn');257258 if (!trust(this.connection.remoteAddress)) {259 return this.connection.encrypted260 ? 'https'261 : 'http';262 }263264 // Note: X-Forwarded-Proto is normally only ever a265 // single value, but this is to be safe.3.4.7Jonathan Ongbefore adoption34c83d7
lines 266–278why, one step back266 var proto = this.get('X-Forwarded-Proto') || 'http';267 return proto.split(/\s*,\s*/)[0];268});269270/**271 * Short-hand for:272 *273 * req.protocol == 'https'274 *275 * @return {Boolean}276 * @api public277 */278Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 279why, one step back279defineGetter(req, 'secure', function secure(){3.4.7Jonathan Ongbefore adoption34c83d7
lines 280–283why, one step back280 return 'https' == this.protocol;281});282283/**Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 284why, one step back284 * Return the remote address from the trusted proxy.3.4.7Jonathan Ongbefore adoption34c83d7
line 285why, one step back285 *Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 286–288why, one step back286 * The is the remote address on the socket unless287 * "trust proxy" is set.288 *3.4.7Jonathan Ongbefore adoption34c83d7
lines 289–292why, one step back289 * @return {String}290 * @api public291 */292Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 293why, one step back293defineGetter(req, 'ip', function ip(){Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 294–295why, one step back294 var trust = this.app.get('trust proxy fn');295 return proxyaddr(this, trust);3.4.7Jonathan Ongbefore adoption34c83d7
lines 296–298why, one step back296});297298/**Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 299why, one step back299 * When "trust proxy" is set, trusted proxy addresses + client.3.4.7Jonathan Ongbefore adoption34c83d7
lines 300–302why, one step back300 *301 * For example if the value were "client, proxy1, proxy2"302 * you would receive the array `["client", "proxy1", "proxy2"]`Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 303–304why, one step back303 * where "proxy2" is the furthest down-stream and "proxy1" and304 * "proxy2" were trusted.3.4.7Jonathan Ongbefore adoption34c83d7
lines 305–309why, one step back305 *306 * @return {Array}307 * @api public308 */309Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 310why, one step back310defineGetter(req, 'ips', function ips() {Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 311–313why, one step back311 var trust = this.app.get('trust proxy fn');312 var addrs = proxyaddr.all(this, trust);313 return addrs.slice(1).reverse();3.4.7Jonathan Ongbefore adoption34c83d7
lines 314–330why, one step back314});315316/**317 * Return subdomains as an array.318 *319 * Subdomains are the dot-separated parts of the host before the main domain of320 * the app. By default, the domain of the app is assumed to be the last two321 * parts of the host. This can be changed by setting "subdomain offset".322 *323 * For example, if the domain is "tobi.ferrets.example.com":324 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.325 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.326 *327 * @return {Array}328 * @api public329 */330Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 331why, one step back331defineGetter(req, 'subdomains', function subdomains() {3.4.7Jonathan Ongbefore adoption34c83d7
lines 332–345why, one step back332 var offset = this.app.get('subdomain offset');333 return (this.host || '')334 .split('.')335 .reverse()336 .slice(offset);337});338339/**340 * Short-hand for `url.parse(req.url).pathname`.341 *342 * @return {String}343 * @api public344 */345Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 346why, one step back346defineGetter(req, 'path', function path() {3.4.7Jonathan Ongbefore adoption34c83d7
lines 347–352why, one step back347 return parse(this).pathname;348});349350/**351 * Parse the "Host" header field hostname.352 *Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 353–356why, one step back353 * When the "trust proxy" setting trusts the socket354 * address, the "X-Forwarded-Host" header field will355 * be trusted.356 *3.4.7Jonathan Ongbefore adoption34c83d7
lines 357–360why, one step back357 * @return {String}358 * @api public359 */360Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 361why, one step back361defineGetter(req, 'host', function host(){Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 362–368why, one step back362 var trust = this.app.get('trust proxy fn');363 var host = this.get('X-Forwarded-Host');364365 if (!host || !trust(this.connection.remoteAddress)) {366 host = this.get('Host');367 }3683.4.7Jonathan Ongbefore adoption34c83d7
line 369why, one step back369 if (!host) return;Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 370–371why, one step back370371 // IPv6 literal supportMerge branch '4.1.x'Douglas Christopher Wilsonbefore adoption0bbbc84
lines 372–375why, one step back372 var offset = host[0] === '['373 ? host.indexOf(']') + 1374 : 0;375 var index = host.indexOf(':', offset);Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 376why, one step back376Merge branch '4.1.x'Douglas Christopher Wilsonbefore adoption0bbbc84
lines 377–379why, one step back377 return ~index378 ? host.substring(0, index)379 : host;3.4.7Jonathan Ongbefore adoption34c83d7
lines 380–390why, one step back380});381382/**383 * Check if the request is fresh, aka384 * Last-Modified and/or the ETag385 * still match.386 *387 * @return {Boolean}388 * @api public389 */390Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 391why, one step back391defineGetter(req, 'fresh', function(){3.4.7Jonathan Ongbefore adoption34c83d7
lines 392–414why, one step back392 var method = this.method;393 var s = this.res.statusCode;394395 // GET or HEAD for weak freshness validation only396 if ('GET' != method && 'HEAD' != method) return false;397398 // 2xx or 304 as per rfc2616 14.26399 if ((s >= 200 && s < 300) || 304 == s) {400 return fresh(this.headers, this.res._headers);401 }402403 return false;404});405406/**407 * Check if the request is stale, aka408 * "Last-Modified" and / or the "ETag" for the409 * resource has changed.410 *411 * @return {Boolean}412 * @api public413 */414Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 415why, one step back415defineGetter(req, 'stale', function stale(){3.4.7Jonathan Ongbefore adoption34c83d7
lines 416–425why, one step back416 return !this.fresh;417});418419/**420 * Check if the request was an _XMLHttpRequest_.421 *422 * @return {Boolean}423 * @api public424 */425Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
line 426why, one step back426defineGetter(req, 'xhr', function xhr(){3.4.7Jonathan Ongbefore adoption34c83d7
lines 427–429why, one step back427 var val = this.get('X-Requested-With') || '';428 return 'xmlhttprequest' == val.toLowerCase();429});Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b
lines 430–445why, one step back430431/**432 * Helper function for creating a getter on an object.433 *434 * @param {Object} obj435 * @param {String} name436 * @param {Function} getter437 * @api private438 */439function defineGetter(obj, name, getter) {440 Object.defineProperty(obj, name, {441 configurable: true,442 enumerable: true,443 get: getter444 });445};