Skip to content
Myr

request.js

This file

Path

lib/request.js

454 lines · 10.4 KiB · JavaScript

Last changed by

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

History of this file

Take it

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 1–5why, one step back
1/**
2 * Module dependencies.3 */45var accepts = require('accepts');

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

line 6why, one step back
6var deprecate = require('depd')('express');

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 7–11why, one step back
7var typeis = require('type-is');
8var http = require('http');9var fresh = require('fresh');10var parseRange = require('range-parser');11var parse = require('parseurl');

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

line 12why, one step back
12var proxyaddr = require('proxy-addr');

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 13–109why, one step back
13
14/**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/**59 * To do: update docs.60 *61 * 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 */103104req.accepts = function(){105 var accept = accepts(this);106 return accept.types.apply(accept, arguments);107};108109/**

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

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

line 111why, one step back
111 *

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

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 113–121why, one step back
113 * @return {Boolean}
114 * @api public115 */116117req.acceptsEncodings = function(){118 var accept = accepts(this);119 return accept.encodings.apply(accept, arguments);120};121

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

lines 122–124why, one step back
122req.acceptsEncoding = deprecate.function(req.acceptsEncodings,
123 'req.acceptsEncoding: Use acceptsEncodings instead');124

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

line 125why, one step back
125/**

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

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 127–128why, one step back
127 * 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 back
129 * @param {String} ...charset

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 130–138why, one step back
130 * @return {Boolean}
131 * @api public132 */133134req.acceptsCharsets = function(){135 var accept = accepts(this);136 return accept.charsets.apply(accept, arguments);137};138

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

lines 139–141why, one step back
139req.acceptsCharset = deprecate.function(req.acceptsCharsets,
140 'req.acceptsCharset: Use acceptsCharsets instead');141

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

line 142why, one step back
142/**

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

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 144–145why, one step back
144 * 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 back
146 * @param {String} ...lang

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 147–154why, one step back
147 * @return {Boolean}
148 * @api public149 */150151req.acceptsLanguages = function(){152 var accept = accepts(this);153 return accept.languages.apply(accept, arguments);154};

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

lines 155–157why, one step back
155
156req.acceptsLanguage = deprecate.function(req.acceptsLanguages,157 'req.acceptsLanguage: Use acceptsLanguages instead');

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 158–245why, one step back
158
159/**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 using194 * the `bodyParser()` middleware.195 *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 */237238req.is = function(types){239 if (!Array.isArray(types)) types = [].slice.call(arguments);240 return typeis(this, types);241};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

line 246why, one step back
246 * setting trusts the socket address, the

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

lines 247–249why, one step back
247 * "X-Forwarded-Proto" header field will be trusted
248 * and used if present.249 *

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 250–251why, one step back
250 * If you're running behind a reverse proxy that
251 * supplies https for you this may be enabled.

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 252–256why, one step back
252 *
253 * @return {String}254 * @api public255 */256

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

lines 258–260why, one step back
258 var proto = this.connection.encrypted
259 ? 'https'260 : 'http';

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 261–263why, one step back
261 var trust = this.app.get('trust proxy fn');
262263 if (!trust(this.connection.remoteAddress)) {

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

line 264why, one step back
264 return proto;

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 265–268why, one step back
265 }
266267 // Note: X-Forwarded-Proto is normally only ever a268 // single value, but this is to be safe.

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 270–281why, one step back
270 return proto.split(/\s*,\s*/)[0];
271});272273/**274 * Short-hand for:275 *276 * req.protocol == 'https'277 *278 * @return {Boolean}279 * @api public280 */281

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 283–286why, one step back
283 return 'https' == this.protocol;
284});285286/**

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

line 287why, one step back
287 * Return the remote address from the trusted proxy.

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

line 288why, one step back
288 *

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 289–291why, one step back
289 * The is the remote address on the socket unless
290 * "trust proxy" is set.291 *

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 292–295why, one step back
292 * @return {String}
293 * @api public294 */295

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 297–298why, one step back
297 var trust = this.app.get('trust proxy fn');
298 return proxyaddr(this, trust);

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 299–301why, one step back
299});
300301/**

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

line 302why, one step back
302 * When "trust proxy" is set, trusted proxy addresses + client.

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 303–305why, one step back
303 *
304 * For example if the value were "client, proxy1, proxy2"305 * you would receive the array `["client", "proxy1", "proxy2"]`

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 306–307why, one step back
306 * where "proxy2" is the furthest down-stream and "proxy1" and
307 * "proxy2" were trusted.

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 308–312why, one step back
308 *
309 * @return {Array}310 * @api public311 */312

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 314–316why, one step back
314 var trust = this.app.get('trust proxy fn');
315 var addrs = proxyaddr.all(this, trust);316 return addrs.slice(1).reverse();

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 317–333why, one step back
317});
318319/**320 * Return subdomains as an array.321 *322 * Subdomains are the dot-separated parts of the host before the main domain of323 * the app. By default, the domain of the app is assumed to be the last two324 * parts of the host. This can be changed by setting "subdomain offset".325 *326 * For example, if the domain is "tobi.ferrets.example.com":327 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.328 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.329 *330 * @return {Array}331 * @api public332 */333

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

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

Add req.hostnameDouglas Christopher Wilsonbefore adoptione6eeec3

line 336why, one step back
336 return (this.hostname || '')

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 337–348why, one step back
337 .split('.')
338 .reverse()339 .slice(offset);340});341342/**343 * Short-hand for `url.parse(req.url).pathname`.344 *345 * @return {String}346 * @api public347 */348

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 350–353why, one step back
350 return parse(this).pathname;
351});352353/**

Add req.hostnameDouglas Christopher Wilsonbefore adoptione6eeec3

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

line 355why, one step back
355 *

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 356–359why, one step back
356 * When the "trust proxy" setting trusts the socket
357 * address, the "X-Forwarded-Host" header field will358 * be trusted.359 *

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 360–363why, one step back
360 * @return {String}
361 * @api public362 */363

Add req.hostnameDouglas Christopher Wilsonbefore adoptione6eeec3

line 364why, one step back
364defineGetter(req, 'hostname', function hostname(){

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 365–371why, one step back
365 var trust = this.app.get('trust proxy fn');
366 var host = this.get('X-Forwarded-Host');367368 if (!host || !trust(this.connection.remoteAddress)) {369 host = this.get('Host');370 }371

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

line 372why, one step back
372 if (!host) return;

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 373–374why, one step back
373
374 // IPv6 literal support

Merge branch '4.1.x'Douglas Christopher Wilsonbefore adoption0bbbc84

lines 375–378why, one step back
375 var offset = host[0] === '['
376 ? host.indexOf(']') + 1377 : 0;378 var index = host.indexOf(':', offset);

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

line 379why, one step back
379

Merge branch '4.1.x'Douglas Christopher Wilsonbefore adoption0bbbc84

lines 380–382why, one step back
380 return ~index
381 ? host.substring(0, index)382 : host;

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

line 383why, one step back
383});

Add req.hostnameDouglas Christopher Wilsonbefore adoptione6eeec3

lines 384–389why, one step back
384
385// TODO: change req.host to return host in next major386387defineGetter(req, 'host', deprecate.function(function host(){388 return this.hostname;389}, 'req.host: Use req.hostname instead'));

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 390–399why, one step back
390
391/**392 * Check if the request is fresh, aka393 * Last-Modified and/or the ETag394 * still match.395 *396 * @return {Boolean}397 * @api public398 */399

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 401–423why, one step back
401 var method = this.method;
402 var s = this.res.statusCode;403404 // GET or HEAD for weak freshness validation only405 if ('GET' != method && 'HEAD' != method) return false;406407 // 2xx or 304 as per rfc2616 14.26408 if ((s >= 200 && s < 300) || 304 == s) {409 return fresh(this.headers, this.res._headers);410 }411412 return false;413});414415/**416 * Check if the request is stale, aka417 * "Last-Modified" and / or the "ETag" for the418 * resource has changed.419 *420 * @return {Boolean}421 * @api public422 */423

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 425–434why, one step back
425 return !this.fresh;
426});427428/**429 * Check if the request was an _XMLHttpRequest_.430 *431 * @return {Boolean}432 * @api public433 */434

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

tests: remove unused image fixtureDouglas Christopher Wilsonbefore adoption5c3852b

lines 436–438why, one step back
436 var val = this.get('X-Requested-With') || '';
437 return 'xmlhttprequest' == val.toLowerCase();438});

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

lines 439–454why, one step back
439
440/**441 * Helper function for creating a getter on an object.442 *443 * @param {Object} obj444 * @param {String} name445 * @param {Function} getter446 * @api private447 */448function defineGetter(obj, name, getter) {449 Object.defineProperty(obj, name, {450 configurable: true,451 enumerable: true,452 get: getter453 });454};