Skip to content
Myr

request.js

This file

Path

lib/request.js

479 lines · 10.3 KiB · JavaScript

Last changed by

Remove req.param()Douglas Christopher Wilsonbefore adoption21d52da

History of this file

Take it

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 1–10why, 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 */89'use strict';10

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 13why, one step back
13 * @private

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 14–16why, one step back
14 */
1516var accepts = require('accepts');

Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534

line 17why, one step back
17var isIP = require('net').isIP;

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 18–53why, one step back
18var typeis = require('type-is');
19var http = require('http');20var fresh = require('fresh');21var parseRange = require('range-parser');22var parse = require('parseurl');23var proxyaddr = require('proxy-addr');2425/**26 * Request prototype.27 */2829var req = exports = module.exports = {30 __proto__: http.IncomingMessage.prototype31};3233/**34 * Return request header.35 *36 * The `Referrer` header field is special-cased,37 * both `Referrer` and `Referer` are interchangeable.38 *39 * Examples:40 *41 * req.get('Content-Type');42 * // => "text/plain"43 *44 * req.get('content-type');45 * // => "text/plain"46 *47 * req.get('Something');48 * // => undefined49 *50 * Aliased as `req.header()`.51 *52 * @param {String} name53 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 54why, one step back
54 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 55–57why, one step back
55 */
5657req.get =

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 58–61why, one step back
58req.header = function header(name) {
59 var lc = name.toLowerCase();6061 switch (lc) {

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 62–66why, one step back
62 case 'referer':
63 case 'referrer':64 return this.headers.referrer65 || this.headers.referer;66 default:

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 68–77why, one step back
68 }
69};7071/**72 * To do: update docs.73 *74 * Check if the given `type(s)` is acceptable, returning75 * the best match when true, otherwise `undefined`, in which76 * case you should respond with 406 "Not Acceptable".77 *

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 84–112why, one step back
84 *
85 * Examples:86 *87 * // Accept: text/html88 * req.accepts('html');89 * // => "html"90 *91 * // Accept: text/*, application/json92 * req.accepts('html');93 * // => "html"94 * req.accepts('text/html');95 * // => "text/html"96 * req.accepts('json, text');97 * // => "json"98 * req.accepts('application/json');99 * // => "application/json"100 *101 * // Accept: text/*, application/json102 * req.accepts('image/png');103 * req.accepts('png');104 * // => undefined105 *106 * // Accept: text/*;q=.5, application/json107 * req.accepts(['html', 'json']);108 * req.accepts('html', 'json');109 * req.accepts('html, json');110 * // => "json"111 *112 * @param {String|Array} type(s)

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 113–114why, one step back
113 * @return {String|Array|Boolean}
114 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 115–122why, one step back
115 */
116117req.accepts = function(){118 var accept = accepts(this);119 return accept.types.apply(accept, arguments);120};121122/**

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 123why, one step back
123 * Check if the given `encoding`s are accepted.

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

line 124why, one step back
124 *

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 125why, one step back
125 * @param {String} ...encoding

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 126–127why, one step back
126 * @return {String|Array}
127 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 128–135why, one step back
128 */
129130req.acceptsEncodings = function(){131 var accept = accepts(this);132 return accept.encodings.apply(accept, arguments);133};134135/**

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 136why, one step back
136 * Check if the given `charset`s are acceptable,

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 137–138why, one step back
137 * otherwise you should respond with 406 "Not Acceptable".
138 *

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 139why, one step back
139 * @param {String} ...charset

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 140–141why, one step back
140 * @return {String|Array}
141 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 142–149why, one step back
142 */
143144req.acceptsCharsets = function(){145 var accept = accepts(this);146 return accept.charsets.apply(accept, arguments);147};148149/**

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 150why, one step back
150 * Check if the given `lang`s are acceptable,

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 151–152why, one step back
151 * otherwise you should respond with 406 "Not Acceptable".
152 *

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 153why, one step back
153 * @param {String} ...lang

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 154–155why, one step back
154 * @return {String|Array}
155 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 156–179why, one step back
156 */
157158req.acceptsLanguages = function(){159 var accept = accepts(this);160 return accept.languages.apply(accept, arguments);161};162163/**164 * Parse Range header field,165 * capping to the given `size`.166 *167 * Unspecified ranges such as "0-" require168 * knowledge of your resource length. In169 * the case of a byte range this is of course170 * the total number of bytes. If the Range171 * header field is not given `null` is returned,172 * `-1` when unsatisfiable, `-2` when syntactically invalid.173 *174 * NOTE: remember that ranges are inclusive, so175 * for example "Range: users=0-3" should respond176 * with 4 users when available, not 3.177 *178 * @param {Number} size179 * @return {Array}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 180why, one step back
180 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 181–189why, one step back
181 */
182183req.range = function(size){184 var range = this.get('Range');185 if (!range) return;186 return parseRange(size, range);187};188189/**

Change req.query to a getterDouglas Christopher Wilsonbefore adoptiondcc4eaa

lines 190–210why, one step back
190 * Parse the query string of `req.url`.
191 *192 * This uses the "query parser" setting to parse the raw193 * string into an object.194 *195 * @return {String}196 * @api public197 */198199defineGetter(req, 'query', function query(){200 var queryparse = this.app.get('query parser fn');201202 if (!queryparse) {203 // parsing is disabled204 return Object.create(null);205 }206207 var querystring = parse(this).query;208209 return queryparse(querystring);210});

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 211–232why, one step back
211
212/**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 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 233–235why, one step back
233 * @param {String|Array} types...
234 * @return {String|false|null}235 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 236–237why, one step back
236 */
237

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 238–249why, one step back
238req.is = function is(types) {
239 var arr = types;240241 // support flattened arguments242 if (!Array.isArray(types)) {243 arr = new Array(arguments.length);244 for (var i = 0; i < arr.length; i++) {245 arr[i] = arguments[i];246 }247 }248249 return typeis(this, arr);

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 250–255why, one step back
250};
251252/**253 * Return the protocol string "http" or "https"254 * when requested with TLS. When the "trust proxy"255 * setting trusts the socket address, the

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

lines 256–258why, one step back
256 * "X-Forwarded-Proto" header field will be trusted
257 * and used if present.258 *

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 259–262why, one step back
259 * If you're running behind a reverse proxy that
260 * supplies https for you this may be enabled.261 *262 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 263why, one step back
263 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 264–265why, one step back
264 */
265

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 266why, one step back
266defineGetter(req, 'protocol', function protocol(){

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

lines 267–269why, one step back
267 var proto = this.connection.encrypted
268 ? 'https'269 : 'http';

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 270–271why, one step back
270 var trust = this.app.get('trust proxy fn');
271

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

line 273why, one step back
273 return proto;

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 274–277why, one step back
274 }
275276 // Note: X-Forwarded-Proto is normally only ever a277 // single value, but this is to be safe.

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

line 278why, one step back
278 proto = this.get('X-Forwarded-Proto') || proto;

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 279–287why, one step back
279 return proto.split(/\s*,\s*/)[0];
280});281282/**283 * Short-hand for:284 *285 * req.protocol == 'https'286 *287 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 288why, one step back
288 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 289–290why, one step back
289 */
290

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 291why, one step back
291defineGetter(req, 'secure', function secure(){

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 293–301why, one step back
293});
294295/**296 * Return the remote address from the trusted proxy.297 *298 * The is the remote address on the socket unless299 * "trust proxy" is set.300 *301 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 302why, one step back
302 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 303–304why, one step back
303 */
304

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 305why, one step back
305defineGetter(req, 'ip', function ip(){

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 306–318why, one step back
306 var trust = this.app.get('trust proxy fn');
307 return proxyaddr(this, trust);308});309310/**311 * When "trust proxy" is set, trusted proxy addresses + client.312 *313 * For example if the value were "client, proxy1, proxy2"314 * you would receive the array `["client", "proxy1", "proxy2"]`315 * where "proxy2" is the furthest down-stream and "proxy1" and316 * "proxy2" were trusted.317 *318 * @return {Array}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 319why, one step back
319 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 320–321why, one step back
320 */
321

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 322why, one step back
322defineGetter(req, 'ips', function ips() {

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 323–339why, one step back
323 var trust = this.app.get('trust proxy fn');
324 var addrs = proxyaddr.all(this, trust);325 return addrs.slice(1).reverse();326});327328/**329 * Return subdomains as an array.330 *331 * Subdomains are the dot-separated parts of the host before the main domain of332 * the app. By default, the domain of the app is assumed to be the last two333 * parts of the host. This can be changed by setting "subdomain offset".334 *335 * For example, if the domain is "tobi.ferrets.example.com":336 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.337 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.338 *339 * @return {Array}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 340why, one step back
340 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 341–342why, one step back
341 */
342

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 343why, one step back
343defineGetter(req, 'subdomains', function subdomains() {

Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534

lines 344–347why, one step back
344 var hostname = this.hostname;
345346 if (!hostname) return [];347

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

line 348why, one step back
348 var offset = this.app.get('subdomain offset');

Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534

lines 349–353why, one step back
349 var subdomains = !isIP(hostname)
350 ? hostname.split('.').reverse()351 : [hostname];352353 return subdomains.slice(offset);

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 354–359why, one step back
354});
355356/**357 * Short-hand for `url.parse(req.url).pathname`.358 *359 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 360why, one step back
360 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 361–362why, one step back
361 */
362

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 363why, one step back
363defineGetter(req, 'path', function path() {

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 364–367why, one step back
364 return parse(this).pathname;
365});366367/**

Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296

line 368why, one step back
368 * Parse the "Host" header field to a host.

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 369–374why, one step back
369 *
370 * When the "trust proxy" setting trusts the socket371 * address, the "X-Forwarded-Host" header field will372 * be trusted.373 *374 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 375why, one step back
375 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 376–377why, one step back
376 */
377

Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296

line 378why, one step back
378defineGetter(req, 'host', function host(){

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

line 379why, one step back
379 var trust = this.app.get('trust proxy fn');

Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296

line 380why, one step back
380 var val = this.get('X-Forwarded-Host');

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

line 381why, one step back
381

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296

line 383why, one step back
383 val = this.get('Host');

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 384–385why, one step back
384 }
385

Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296

lines 386–402why, one step back
386 return val || undefined;
387});388389/**390 * Parse the "Host" header field to a hostname.391 *392 * When the "trust proxy" setting trusts the socket393 * address, the "X-Forwarded-Host" header field will394 * be trusted.395 *396 * @return {String}397 * @api public398 */399400defineGetter(req, 'hostname', function hostname(){401 var host = this.host;402

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 403–410why, one step back
403 if (!host) return;
404405 // IPv6 literal support406 var offset = host[0] === '['407 ? host.indexOf(']') + 1408 : 0;409 var index = host.indexOf(':', offset);410

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 412–421why, one step back
412 ? host.substring(0, index)
413 : host;414});415416/**417 * Check if the request is fresh, aka418 * Last-Modified and/or the ETag419 * still match.420 *421 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 422why, one step back
422 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 423–424why, one step back
423 */
424

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 425why, one step back
425defineGetter(req, 'fresh', function(){

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 426–433why, one step back
426 var method = this.method;
427 var s = this.res.statusCode;428429 // GET or HEAD for weak freshness validation only430 if ('GET' != method && 'HEAD' != method) return false;431432 // 2xx or 304 as per rfc2616 14.26433 if ((s >= 200 && s < 300) || 304 == s) {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 435–445why, one step back
435 }
436437 return false;438});439440/**441 * Check if the request is stale, aka442 * "Last-Modified" and / or the "ETag" for the443 * resource has changed.444 *445 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 446why, one step back
446 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 447–448why, one step back
447 */
448

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 449why, one step back
449defineGetter(req, 'stale', function stale(){

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 450–456why, one step back
450 return !this.fresh;
451});452453/**454 * Check if the request was an _XMLHttpRequest_.455 *456 * @return {Boolean}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 457why, one step back
457 * @public

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

lines 458–459why, one step back
458 */
459

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

line 460why, one step back
460defineGetter(req, 'xhr', function xhr(){

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

line 461why, one step back
461 var val = this.get('X-Requested-With') || '';

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

4.4.4Douglas Christopher Wilsonbefore adoptionf498b66

line 463why, one step back
463});

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

lines 464–470why, one step back
464
465/**466 * Helper function for creating a getter on an object.467 *468 * @param {Object} obj469 * @param {String} name470 * @param {Function} getter

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 471why, one step back
471 * @private

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

lines 472–479why, one step back
472 */
473function defineGetter(obj, name, getter) {474 Object.defineProperty(obj, name, {475 configurable: true,476 enumerable: true,477 get: getter478 });479};