Skip to content
Myr

request.js

This file

Path

lib/request.js

460 lines · 10.6 KiB · JavaScript

Last changed by

Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534

History of this file

Take it

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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');

Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 8–110why, one step back
8var typeis = require('type-is');
9var http = require('http');10var fresh = require('fresh');11var parseRange = require('range-parser');12var parse = require('parseurl');13var proxyaddr = require('proxy-addr');1415/**16 * Request prototype.17 */1819var req = exports = module.exports = {20 __proto__: http.IncomingMessage.prototype21};2223/**24 * Return request header.25 *26 * The `Referrer` header field is special-cased,27 * both `Referrer` and `Referer` are interchangeable.28 *29 * Examples:30 *31 * req.get('Content-Type');32 * // => "text/plain"33 *34 * req.get('content-type');35 * // => "text/plain"36 *37 * req.get('Something');38 * // => undefined39 *40 * Aliased as `req.header()`.41 *42 * @param {String} name43 * @return {String}44 * @api public45 */4647req.get =48req.header = function(name){49 switch (name = name.toLowerCase()) {50 case 'referer':51 case 'referrer':52 return this.headers.referrer53 || this.headers.referer;54 default:55 return this.headers[name];56 }57};5859/**60 * To do: update docs.61 *62 * Check if the given `type(s)` is acceptable, returning63 * the best match when true, otherwise `undefined`, in which64 * case you should respond with 406 "Not Acceptable".65 *66 * The `type` value may be a single mime type string67 * such as "application/json", the extension name68 * such as "json", a comma-delimted list such as "json, html, text/plain",69 * an argument list such as `"json", "html", "text/plain"`,70 * or an array `["json", "html", "text/plain"]`. When a list71 * or array is given the _best_ match, if any is returned.72 *73 * Examples:74 *75 * // Accept: text/html76 * req.accepts('html');77 * // => "html"78 *79 * // Accept: text/*, application/json80 * req.accepts('html');81 * // => "html"82 * req.accepts('text/html');83 * // => "text/html"84 * req.accepts('json, text');85 * // => "json"86 * req.accepts('application/json');87 * // => "application/json"88 *89 * // Accept: text/*, application/json90 * req.accepts('image/png');91 * req.accepts('png');92 * // => undefined93 *94 * // Accept: text/*;q=.5, application/json95 * req.accepts(['html', 'json']);96 * req.accepts('html', 'json');97 * req.accepts('html, json');98 * // => "json"99 *100 * @param {String|Array} type(s)101 * @return {String}102 * @api public103 */104105req.accepts = function(){106 var accept = accepts(this);107 return accept.types.apply(accept, arguments);108};109110/**

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

line 112why, one step back
112 *

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

line 126why, one step back
126/**

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

line 143why, one step back
143/**

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

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

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 159–247why, one step back
159
160/**161 * Parse Range header field,162 * capping to the given `size`.163 *164 * Unspecified ranges such as "0-" require165 * knowledge of your resource length. In166 * the case of a byte range this is of course167 * the total number of bytes. If the Range168 * header field is not given `null` is returned,169 * `-1` when unsatisfiable, `-2` when syntactically invalid.170 *171 * NOTE: remember that ranges are inclusive, so172 * for example "Range: users=0-3" should respond173 * with 4 users when available, not 3.174 *175 * @param {Number} size176 * @return {Array}177 * @api public178 */179180req.range = function(size){181 var range = this.get('Range');182 if (!range) return;183 return parseRange(size, range);184};185186/**187 * Return the value of param `name` when present or `defaultValue`.188 *189 * - Checks route placeholders, ex: _/user/:id_190 * - Checks body params, ex: id=12, {"id":12}191 * - Checks query string params, ex: ?id=12192 *193 * To utilize request bodies, `req.body`194 * should be an object. This can be done by using195 * the `bodyParser()` middleware.196 *197 * @param {String} name198 * @param {Mixed} [defaultValue]199 * @return {String}200 * @api public201 */202203req.param = function(name, defaultValue){204 var params = this.params || {};205 var body = this.body || {};206 var query = this.query || {};207 if (null != params[name] && params.hasOwnProperty(name)) return params[name];208 if (null != body[name]) return body[name];209 if (null != query[name]) return query[name];210 return defaultValue;211};212213/**214 * Check if the incoming request contains the "Content-Type"215 * header field, and it contains the give mime `type`.216 *217 * Examples:218 *219 * // With Content-Type: text/html; charset=utf-8220 * req.is('html');221 * req.is('text/html');222 * req.is('text/*');223 * // => true224 *225 * // When Content-Type is application/json226 * req.is('json');227 * req.is('application/json');228 * req.is('application/*');229 * // => true230 *231 * req.is('html');232 * // => false233 *234 * @param {String} type235 * @return {Boolean}236 * @api public237 */238239req.is = function(types){240 if (!Array.isArray(types)) types = [].slice.call(arguments);241 return typeis(this, types);242};243244/**245 * Return the protocol string "http" or "https"246 * when requested with TLS. When the "trust proxy"247 * setting trusts the socket address, the

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 251–257why, one step back
251 * If you're running behind a reverse proxy that
252 * supplies https for you this may be enabled.253 *254 * @return {String}255 * @api public256 */257

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

line 265why, one step back
265 return proto;

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 266–269why, one step back
266 }
267268 // Note: X-Forwarded-Proto is normally only ever a269 // single value, but this is to be safe.

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoption11c74d7

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 284–296why, one step back
284 return 'https' == this.protocol;
285});286287/**288 * Return the remote address from the trusted proxy.289 *290 * The is the remote address on the socket unless291 * "trust proxy" is set.292 *293 * @return {String}294 * @api public295 */296

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 298–313why, one step back
298 var trust = this.app.get('trust proxy fn');
299 return proxyaddr(this, trust);300});301302/**303 * When "trust proxy" is set, trusted proxy addresses + client.304 *305 * For example if the value were "client, proxy1, proxy2"306 * you would receive the array `["client", "proxy1", "proxy2"]`307 * where "proxy2" is the furthest down-stream and "proxy1" and308 * "proxy2" were trusted.309 *310 * @return {Array}311 * @api public312 */313

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 315–334why, one step back
315 var trust = this.app.get('trust proxy fn');
316 var addrs = proxyaddr.all(this, trust);317 return addrs.slice(1).reverse();318});319320/**321 * Return subdomains as an array.322 *323 * Subdomains are the dot-separated parts of the host before the main domain of324 * the app. By default, the domain of the app is assumed to be the last two325 * parts of the host. This can be changed by setting "subdomain offset".326 *327 * For example, if the domain is "tobi.ferrets.example.com":328 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.329 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.330 *331 * @return {Array}332 * @api public333 */334

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534

lines 336–339why, one step back
336 var hostname = this.hostname;
337338 if (!hostname) return [];339

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

Merge tag '3.17.1'Douglas Christopher Wilsonbefore adoption3dca534

lines 341–345why, one step back
341 var subdomains = !isIP(hostname)
342 ? hostname.split('.').reverse()343 : [hostname];344345 return subdomains.slice(offset);

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 346–354why, one step back
346});
347348/**349 * Short-hand for `url.parse(req.url).pathname`.350 *351 * @return {String}352 * @api public353 */354

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 356–359why, one step back
356 return parse(this).pathname;
357});358359/**

Add req.hostnameDouglas Christopher Wilsonbefore adoptione6eeec3

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 361–369why, one step back
361 *
362 * When the "trust proxy" setting trusts the socket363 * address, the "X-Forwarded-Host" header field will364 * be trusted.365 *366 * @return {String}367 * @api public368 */369

Add req.hostnameDouglas Christopher Wilsonbefore adoptione6eeec3

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 371–389why, one step back
371 var trust = this.app.get('trust proxy fn');
372 var host = this.get('X-Forwarded-Host');373374 if (!host || !trust(this.connection.remoteAddress)) {375 host = this.get('Host');376 }377378 if (!host) return;379380 // IPv6 literal support381 var offset = host[0] === '['382 ? host.indexOf(']') + 1383 : 0;384 var index = host.indexOf(':', offset);385386 return ~index387 ? host.substring(0, index)388 : host;389});

Add req.hostnameDouglas Christopher Wilsonbefore adoptione6eeec3

lines 390–395why, one step back
390
391// TODO: change req.host to return host in next major392393defineGetter(req, 'host', deprecate.function(function host(){394 return this.hostname;395}, 'req.host: Use req.hostname instead'));

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 396–405why, one step back
396
397/**398 * Check if the request is fresh, aka399 * Last-Modified and/or the ETag400 * still match.401 *402 * @return {Boolean}403 * @api public404 */405

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 407–429why, one step back
407 var method = this.method;
408 var s = this.res.statusCode;409410 // GET or HEAD for weak freshness validation only411 if ('GET' != method && 'HEAD' != method) return false;412413 // 2xx or 304 as per rfc2616 14.26414 if ((s >= 200 && s < 300) || 304 == s) {415 return fresh(this.headers, this.res._headers);416 }417418 return false;419});420421/**422 * Check if the request is stale, aka423 * "Last-Modified" and / or the "ETag" for the424 * resource has changed.425 *426 * @return {Boolean}427 * @api public428 */429

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

lines 431–440why, one step back
431 return !this.fresh;
432});433434/**435 * Check if the request was an _XMLHttpRequest_.436 *437 * @return {Boolean}438 * @api public439 */440

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption9e5a758

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

Replace __defineGetter__ with Object.definePropertyGuy Ellis Monsterbefore adoption746044b

lines 445–460why, one step back
445
446/**447 * Helper function for creating a getter on an object.448 *449 * @param {Object} obj450 * @param {String} name451 * @param {Function} getter452 * @api private453 */454function defineGetter(obj, name, getter) {455 Object.defineProperty(obj, name, {456 configurable: true,457 enumerable: true,458 get: getter459 });460};