Skip to content
Myr

request.js

This file

Path

lib/request.js

489 lines · 11.1 KiB · JavaScript

Last changed by

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

History of this file

Take it

The walk stopped after 200 commits: lines older than 94f10c2 point at it.

docs: add license commentsDouglas Christopher Wilsonbefore adoption95ad276

lines 1–8why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */8

perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f

lines 9–10why, one step back
9'use strict';
10

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 11–12why, one step back
11/**
12 * Module dependencies.

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 13why, one step back
13 * @private

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 14–54why, one step back
14 */
1516var accepts = require('accepts');17var deprecate = require('depd')('express');18var isIP = require('net').isIP;19var typeis = require('type-is');20var http = require('http');21var fresh = require('fresh');22var parseRange = require('range-parser');23var parse = require('parseurl');24var proxyaddr = require('proxy-addr');2526/**27 * Request prototype.28 */2930var req = exports = module.exports = {31 __proto__: http.IncomingMessage.prototype32};3334/**35 * Return request header.36 *37 * The `Referrer` header field is special-cased,38 * both `Referrer` and `Referer` are interchangeable.39 *40 * Examples:41 *42 * req.get('Content-Type');43 * // => "text/plain"44 *45 * req.get('content-type');46 * // => "text/plain"47 *48 * req.get('Something');49 * // => undefined50 *51 * Aliased as `req.header()`.52 *53 * @param {String} name54 * @return {String}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 55why, one step back
55 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 56–58why, one step back
56 */
5758req.get =

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

lines 59–62why, one step back
59req.header = function header(name) {
60 var lc = name.toLowerCase();6162 switch (lc) {

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 63–67why, one step back
63 case 'referer':
64 case 'referrer':65 return this.headers.referrer66 || this.headers.referer;67 default:

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 68why, one step back
68 return this.headers[lc];

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 69–78why, one step back
69 }
70};7172/**73 * To do: update docs.74 *75 * Check if the given `type(s)` is acceptable, returning76 * the best match when true, otherwise `undefined`, in which77 * case you should respond with 406 "Not Acceptable".78 *

Fix typos in req.accepts jsdocElvin Yungbefore adoption9adfc2d

lines 79–81why, one step back
79 * The `type` value may be a single MIME type string
80 * such as "application/json", an extension name81 * such as "json", a comma-delimited list such as "json, html, text/plain",

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 82–83why, one step back
82 * an argument list such as `"json", "html", "text/plain"`,
83 * or an array `["json", "html", "text/plain"]`. When a list

Fix typos in req.accepts jsdocElvin Yungbefore adoption9adfc2d

line 84why, one step back
84 * or array is given, the _best_ match, if any is returned.

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 85–113why, one step back
85 *
86 * Examples:87 *88 * // Accept: text/html89 * req.accepts('html');90 * // => "html"91 *92 * // Accept: text/*, application/json93 * req.accepts('html');94 * // => "html"95 * req.accepts('text/html');96 * // => "text/html"97 * req.accepts('json, text');98 * // => "json"99 * req.accepts('application/json');100 * // => "application/json"101 *102 * // Accept: text/*, application/json103 * req.accepts('image/png');104 * req.accepts('png');105 * // => undefined106 *107 * // Accept: text/*;q=.5, application/json108 * req.accepts(['html', 'json']);109 * req.accepts('html', 'json');110 * req.accepts('html, json');111 * // => "json"112 *113 * @param {String|Array} type(s)

docs: update return types for req.accept*()Alexander Mareninbefore adoptionfce3d14

line 114why, one step back
114 * @return {String|Array|Boolean}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 115why, one step back
115 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 116–126why, one step back
116 */
117118req.accepts = function(){119 var accept = accepts(this);120 return accept.types.apply(accept, arguments);121};122123/**124 * Check if the given `encoding`s are accepted.125 *126 * @param {String} ...encoding

docs: update return types for req.accept*()Alexander Mareninbefore adoptionfce3d14

line 127why, one step back
127 * @return {String|Array}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 128why, one step back
128 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 129–143why, one step back
129 */
130131req.acceptsEncodings = function(){132 var accept = accepts(this);133 return accept.encodings.apply(accept, arguments);134};135136req.acceptsEncoding = deprecate.function(req.acceptsEncodings,137 'req.acceptsEncoding: Use acceptsEncodings instead');138139/**140 * Check if the given `charset`s are acceptable,141 * otherwise you should respond with 406 "Not Acceptable".142 *143 * @param {String} ...charset

docs: update return types for req.accept*()Alexander Mareninbefore adoptionfce3d14

line 144why, one step back
144 * @return {String|Array}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 145why, one step back
145 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 146–160why, one step back
146 */
147148req.acceptsCharsets = function(){149 var accept = accepts(this);150 return accept.charsets.apply(accept, arguments);151};152153req.acceptsCharset = deprecate.function(req.acceptsCharsets,154 'req.acceptsCharset: Use acceptsCharsets instead');155156/**157 * Check if the given `lang`s are acceptable,158 * otherwise you should respond with 406 "Not Acceptable".159 *160 * @param {String} ...lang

docs: update return types for req.accept*()Alexander Mareninbefore adoptionfce3d14

line 161why, one step back
161 * @return {String|Array}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 162why, one step back
162 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 163–189why, one step back
163 */
164165req.acceptsLanguages = function(){166 var accept = accepts(this);167 return accept.languages.apply(accept, arguments);168};169170req.acceptsLanguage = deprecate.function(req.acceptsLanguages,171 'req.acceptsLanguage: Use acceptsLanguages instead');172173/**174 * Parse Range header field,175 * capping to the given `size`.176 *177 * Unspecified ranges such as "0-" require178 * knowledge of your resource length. In179 * the case of a byte range this is of course180 * the total number of bytes. If the Range181 * header field is not given `null` is returned,182 * `-1` when unsatisfiable, `-2` when syntactically invalid.183 *184 * NOTE: remember that ranges are inclusive, so185 * for example "Range: users=0-3" should respond186 * with 4 users when available, not 3.187 *188 * @param {Number} size189 * @return {Array}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 190why, one step back
190 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 191–212why, one step back
191 */
192193req.range = function(size){194 var range = this.get('Range');195 if (!range) return;196 return parseRange(size, range);197};198199/**200 * Return the value of param `name` when present or `defaultValue`.201 *202 * - Checks route placeholders, ex: _/user/:id_203 * - Checks body params, ex: id=12, {"id":12}204 * - Checks query string params, ex: ?id=12205 *206 * To utilize request bodies, `req.body`207 * should be an object. This can be done by using208 * the `bodyParser()` middleware.209 *210 * @param {String} name211 * @param {Mixed} [defaultValue]212 * @return {String}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 213why, one step back
213 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 214–215why, one step back
214 */
215

Deprecate req.param()Douglas Christopher Wilsonbefore adoption4db51fb

line 216why, one step back
216req.param = function param(name, defaultValue) {

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 217–219why, one step back
217 var params = this.params || {};
218 var body = this.body || {};219 var query = this.query || {};

Deprecate req.param()Douglas Christopher Wilsonbefore adoption4db51fb

lines 220–225why, one step back
220
221 var args = arguments.length === 1222 ? 'name'223 : 'name, default';224 deprecate('req.param(' + args + '): Use req.params, req.body, or req.query instead');225

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 226–228why, one step back
226 if (null != params[name] && params.hasOwnProperty(name)) return params[name];
227 if (null != body[name]) return body[name];228 if (null != query[name]) return query[name];

Deprecate req.param()Douglas Christopher Wilsonbefore adoption4db51fb

line 229why, one step back
229

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 230–253why, one step back
230 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');242 * req.is('text/*');243 * // => true244 *245 * // When Content-Type is application/json246 * req.is('json');247 * req.is('application/json');248 * req.is('application/*');249 * // => true250 *251 * req.is('html');252 * // => false253 *

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

lines 254–256why, one step back
254 * @param {String|Array} types...
255 * @return {String|false|null}256 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 257–258why, one step back
257 */
258

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

lines 259–270why, one step back
259req.is = function is(types) {
260 var arr = types;261262 // support flattened arguments263 if (!Array.isArray(types)) {264 arr = new Array(arguments.length);265 for (var i = 0; i < arr.length; i++) {266 arr[i] = arguments[i];267 }268 }269270 return typeis(this, arr);

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 271–283why, one step back
271};
272273/**274 * Return the protocol string "http" or "https"275 * when requested with TLS. When the "trust proxy"276 * setting trusts the socket address, the277 * "X-Forwarded-Proto" header field will be trusted278 * and used if present.279 *280 * If you're running behind a reverse proxy that281 * supplies https for you this may be enabled.282 *283 * @return {String}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 284why, one step back
284 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 285–292why, one step back
285 */
286287defineGetter(req, 'protocol', function protocol(){288 var proto = this.connection.encrypted289 ? 'https'290 : 'http';291 var trust = this.app.get('trust proxy fn');292

Merge tag '3.20.1'Douglas Christopher Wilsonbefore adoptioncd6df76

line 293why, one step back
293 if (!trust(this.connection.remoteAddress, 0)) {

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 294–308why, one step back
294 return proto;
295 }296297 // Note: X-Forwarded-Proto is normally only ever a298 // single value, but this is to be safe.299 proto = this.get('X-Forwarded-Proto') || proto;300 return proto.split(/\s*,\s*/)[0];301});302303/**304 * Short-hand for:305 *306 * req.protocol == 'https'307 *308 * @return {Boolean}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 309why, one step back
309 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 310–312why, one step back
310 */
311312defineGetter(req, 'secure', function secure(){

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 313why, one step back
313 return this.protocol === 'https';

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 314–322why, one step back
314});
315316/**317 * Return the remote address from the trusted proxy.318 *319 * The is the remote address on the socket unless320 * "trust proxy" is set.321 *322 * @return {String}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 323why, one step back
323 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 324–339why, one step back
324 */
325326defineGetter(req, 'ip', function ip(){327 var trust = this.app.get('trust proxy fn');328 return proxyaddr(this, trust);329});330331/**332 * When "trust proxy" is set, trusted proxy addresses + client.333 *334 * For example if the value were "client, proxy1, proxy2"335 * you would receive the array `["client", "proxy1", "proxy2"]`336 * where "proxy2" is the furthest down-stream and "proxy1" and337 * "proxy2" were trusted.338 *339 * @return {Array}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 340why, one step back
340 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 341–360why, one step back
341 */
342343defineGetter(req, 'ips', function ips() {344 var trust = this.app.get('trust proxy fn');345 var addrs = proxyaddr.all(this, trust);346 return addrs.slice(1).reverse();347});348349/**350 * Return subdomains as an array.351 *352 * Subdomains are the dot-separated parts of the host before the main domain of353 * the app. By default, the domain of the app is assumed to be the last two354 * parts of the host. This can be changed by setting "subdomain offset".355 *356 * For example, if the domain is "tobi.ferrets.example.com":357 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.358 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.359 *360 * @return {Array}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 361why, one step back
361 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 362–380why, one step back
362 */
363364defineGetter(req, 'subdomains', function subdomains() {365 var hostname = this.hostname;366367 if (!hostname) return [];368369 var offset = this.app.get('subdomain offset');370 var subdomains = !isIP(hostname)371 ? hostname.split('.').reverse()372 : [hostname];373374 return subdomains.slice(offset);375});376377/**378 * Short-hand for `url.parse(req.url).pathname`.379 *380 * @return {String}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 381why, one step back
381 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 382–395why, one step back
382 */
383384defineGetter(req, 'path', function path() {385 return parse(this).pathname;386});387388/**389 * Parse the "Host" header field to a hostname.390 *391 * When the "trust proxy" setting trusts the socket392 * address, the "X-Forwarded-Host" header field will393 * be trusted.394 *395 * @return {String}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 396why, one step back
396 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 397–402why, one step back
397 */
398399defineGetter(req, 'hostname', function hostname(){400 var trust = this.app.get('trust proxy fn');401 var host = this.get('X-Forwarded-Host');402

Merge tag '3.20.1'Douglas Christopher Wilsonbefore adoptioncd6df76

line 403why, one step back
403 if (!host || !trust(this.connection.remoteAddress, 0)) {

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 404–414why, one step back
404 host = this.get('Host');
405 }406407 if (!host) return;408409 // IPv6 literal support410 var offset = host[0] === '['411 ? host.indexOf(']') + 1412 : 0;413 var index = host.indexOf(':', offset);414

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 415why, one step back
415 return index !== -1

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 416–431why, one step back
416 ? host.substring(0, index)
417 : host;418});419420// TODO: change req.host to return host in next major421422defineGetter(req, 'host', deprecate.function(function host(){423 return this.hostname;424}, 'req.host: Use req.hostname instead'));425426/**427 * Check if the request is fresh, aka428 * Last-Modified and/or the ETag429 * still match.430 *431 * @return {Boolean}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 432why, one step back
432 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 433–443why, one step back
433 */
434435defineGetter(req, 'fresh', function(){436 var method = this.method;437 var s = this.res.statusCode;438439 // GET or HEAD for weak freshness validation only440 if ('GET' != method && 'HEAD' != method) return false;441442 // 2xx or 304 as per rfc2616 14.26443 if ((s >= 200 && s < 300) || 304 == s) {

Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801

line 444why, one step back
444 return fresh(this.headers, (this.res._headers || {}));

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 445–455why, one step back
445 }
446447 return false;448});449450/**451 * Check if the request is stale, aka452 * "Last-Modified" and / or the "ETag" for the453 * resource has changed.454 *455 * @return {Boolean}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 456why, one step back
456 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 457–466why, one step back
457 */
458459defineGetter(req, 'stale', function stale(){460 return !this.fresh;461});462463/**464 * Check if the request was an _XMLHttpRequest_.465 *466 * @return {Boolean}

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 467why, one step back
467 * @public

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 468–471why, one step back
468 */
469470defineGetter(req, 'xhr', function xhr(){471 var val = this.get('X-Requested-With') || '';

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 472why, one step back
472 return val.toLowerCase() === 'xmlhttprequest';

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 473–480why, one step back
473});
474475/**476 * Helper function for creating a getter on an object.477 *478 * @param {Object} obj479 * @param {String} name480 * @param {Function} getter

perf: remove argument reassignments in request prototypeDouglas Christopher Wilsonbefore adoptiond414a2d

line 481why, one step back
481 * @private

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 482–489why, one step back
482 */
483function defineGetter(obj, name, getter) {484 Object.defineProperty(obj, name, {485 configurable: true,486 enumerable: true,487 get: getter488 });489};