request.js
This file
Path
lib/request.js
514 lines · 11.4 KiB · JavaScript
Last changed by
lib: use req.socket over deprecated req.connection (#6705)Sebastian Beltranbefore adoption89f198c
History of this fileTake it
The walk stopped after 200 commits: lines older than d40dc65 point at it.
Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 1–10why, one step back1/*!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';104.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 11–12why, one step back11/**12 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 13why, one step back13 * @private4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 14–16why, one step back14 */1516var accepts = require('accepts');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 17why, one step back17var isIP = require('node:net').isIP;4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
line 18why, one step back18var typeis = require('type-is');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 19why, one step back19var http = require('node:http');4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 20–26why, one step back20var fresh = require('fresh');21var parseRange = require('range-parser');22var parse = require('parseurl');23var proxyaddr = require('proxy-addr');2425/**26 * Request prototype.Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 27why, one step back27 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 28–29why, one step back28 */29Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 30why, one step back30var req = Object.create(http.IncomingMessage.prototype)4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 31–32why, one step back3132/**Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 33–39why, one step back33 * Module exports.34 * @public35 */3637module.exports = req3839/**4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 40–59why, one step back40 * Return request header.41 *42 * The `Referrer` header field is special-cased,43 * both `Referrer` and `Referer` are interchangeable.44 *45 * Examples:46 *47 * req.get('Content-Type');48 * // => "text/plain"49 *50 * req.get('content-type');51 * // => "text/plain"52 *53 * req.get('Something');54 * // => undefined55 *56 * Aliased as `req.header()`.57 *58 * @param {String} name59 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 60why, one step back60 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 61–63why, one step back61 */6263req.get =Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 64why, one step back64req.header = function header(name) {Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 65–72why, one step back65 if (!name) {66 throw new TypeError('name argument is required to req.get');67 }6869 if (typeof name !== 'string') {70 throw new TypeError('name must be a string to req.get');71 }72Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 73–75why, one step back73 var lc = name.toLowerCase();7475 switch (lc) {4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 76–80why, one step back76 case 'referer':77 case 'referrer':78 return this.headers.referrer79 || this.headers.referer;80 default:Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 81why, one step back81 return this.headers[lc];4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 82–91why, one step back82 }83};8485/**86 * To do: update docs.87 *88 * Check if the given `type(s)` is acceptable, returning89 * the best match when true, otherwise `undefined`, in which90 * case you should respond with 406 "Not Acceptable".91 *Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 92–94why, one step back92 * The `type` value may be a single MIME type string93 * such as "application/json", an extension name94 * such as "json", a comma-delimited list such as "json, html, text/plain",4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 95–96why, one step back95 * an argument list such as `"json", "html", "text/plain"`,96 * or an array `["json", "html", "text/plain"]`. When a listMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 97why, one step back97 * or array is given, the _best_ match, if any is returned.4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 98–126why, one step back98 *99 * Examples:100 *101 * // Accept: text/html102 * req.accepts('html');103 * // => "html"104 *105 * // Accept: text/*, application/json106 * req.accepts('html');107 * // => "html"108 * req.accepts('text/html');109 * // => "text/html"110 * req.accepts('json, text');111 * // => "json"112 * req.accepts('application/json');113 * // => "application/json"114 *115 * // Accept: text/*, application/json116 * req.accepts('image/png');117 * req.accepts('png');118 * // => undefined119 *120 * // Accept: text/*;q=.5, application/json121 * req.accepts(['html', 'json']);122 * req.accepts('html', 'json');123 * req.accepts('html, json');124 * // => "json"125 *126 * @param {String|Array} type(s)Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 127–128why, one step back127 * @return {String|Array|Boolean}128 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 129–139why, one step back129 */130131req.accepts = function(){132 var accept = accepts(this);133 return accept.types.apply(accept, arguments);134};135136/**137 * Check if the given `encoding`s are accepted.138 *139 * @param {String} ...encodingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 140–141why, one step back140 * @return {String|Array}141 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 142–153why, one step back142 */143144req.acceptsEncodings = function(){145 var accept = accepts(this);146 return accept.encodings.apply(accept, arguments);147};148149/**150 * Check if the given `charset`s are acceptable,151 * otherwise you should respond with 406 "Not Acceptable".152 *153 * @param {String} ...charsetMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 154–155why, one step back154 * @return {String|Array}155 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 156–167why, one step back156 */157158req.acceptsCharsets = function(){159 var accept = accepts(this);160 return accept.charsets.apply(accept, arguments);161};162163/**164 * Check if the given `lang`s are acceptable,165 * otherwise you should respond with 406 "Not Acceptable".166 *167 * @param {String} ...langMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 168–169why, one step back168 * @return {String|Array}169 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 170–171why, one step back170 */171Refactor: simplify `acceptsLanguages` implementation using spread operator (#6137)Ayoub Mabroukbefore adoption3dc9699
lines 172–173why, one step back172req.acceptsLanguages = function(...languages) {173 return accepts(this).languages(...languages);4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 174–176why, one step back174};175176/**Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 177why, one step back177 * Parse Range header field, capping to the given `size`.4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
line 178why, one step back178 *Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 179–182why, one step back179 * Unspecified ranges such as "0-" require knowledge of your resource length. In180 * the case of a byte range this is of course the total number of bytes. If the181 * Range header field is not given `undefined` is returned, `-1` when unsatisfiable,182 * and `-2` when syntactically invalid.4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
line 183why, one step back183 *Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 184–186why, one step back184 * When ranges are returned, the array has a "type" property which is the type of185 * range that is required (most commonly, "bytes"). Each array element is an object186 * with a "start" and "end" property for the portion of the range.4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
line 187why, one step back187 *Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 188–197why, one step back188 * The "combine" option can be set to `true` and overlapping & adjacent ranges189 * will be combined into a single range.190 *191 * NOTE: remember that ranges are inclusive, so for example "Range: users=0-3"192 * should respond with 4 users when available, not 3.193 *194 * @param {number} size195 * @param {object} [options]196 * @param {boolean} [options.combine=false]197 * @return {number|array}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 198why, one step back198 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 199–200why, one step back199 */200Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 201why, one step back201req.range = function range(size, options) {4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 202–203why, one step back202 var range = this.get('Range');203 if (!range) return;Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 204why, one step back204 return parseRange(size, range, options);4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 205–207why, one step back205};206207/**Change req.query to a getterDouglas Christopher Wilsonbefore adoptiondcc4eaa
lines 208–228why, one step back208 * Parse the query string of `req.url`.209 *210 * This uses the "query parser" setting to parse the raw211 * string into an object.212 *213 * @return {String}214 * @api public215 */216217defineGetter(req, 'query', function query(){218 var queryparse = this.app.get('query parser fn');219220 if (!queryparse) {221 // parsing is disabled222 return Object.create(null);223 }224225 var querystring = parse(this).query;226227 return queryparse(querystring);228});4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 229–231why, one step back229230/**231 * Check if the incoming request contains the "Content-Type"Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 232why, one step back232 * header field, and it contains the given mime `type`.4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 233–250why, one step back233 *234 * Examples:235 *236 * // With Content-Type: text/html; charset=utf-8237 * req.is('html');238 * req.is('text/html');239 * req.is('text/*');240 * // => true241 *242 * // When Content-Type is application/json243 * req.is('json');244 * req.is('application/json');245 * req.is('application/*');246 * // => true247 *248 * req.is('html');249 * // => false250 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 251–253why, one step back251 * @param {String|Array} types...252 * @return {String|false|null}253 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 254–255why, one step back254 */255Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 256–267why, one step back256req.is = function is(types) {257 var arr = types;258259 // support flattened arguments260 if (!Array.isArray(types)) {261 arr = new Array(arguments.length);262 for (var i = 0; i < arr.length; i++) {263 arr[i] = arguments[i];264 }265 }266267 return typeis(this, arr);4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 268–280why, one step back268};269270/**271 * Return the protocol string "http" or "https"272 * when requested with TLS. When the "trust proxy"273 * setting trusts the socket address, the274 * "X-Forwarded-Proto" header field will be trusted275 * and used if present.276 *277 * If you're running behind a reverse proxy that278 * supplies https for you this may be enabled.279 *280 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 281why, one step back281 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 282–284why, one step back282 */283284defineGetter(req, 'protocol', function protocol(){lib: use req.socket over deprecated req.connection (#6705)Sebastian Beltranbefore adoption89f198c
line 285why, one step back285 var proto = this.socket.encrypted4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 286–289why, one step back286 ? 'https'287 : 'http';288 var trust = this.app.get('trust proxy fn');289lib: use req.socket over deprecated req.connection (#6705)Sebastian Beltranbefore adoption89f198c
line 290why, one step back290 if (!trust(this.socket.remoteAddress, 0)) {4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 291–295why, one step back291 return proto;292 }293294 // Note: X-Forwarded-Proto is normally only ever a295 // single value, but this is to be safe.Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 296–301why, one step back296 var header = this.get('X-Forwarded-Proto') || proto297 var index = header.indexOf(',')298299 return index !== -1300 ? header.substring(0, index).trim()301 : header.trim()4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 302–306why, one step back302});303304/**305 * Short-hand for:306 *Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 307why, one step back307 * req.protocol === 'https'4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 308–309why, one step back308 *309 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 310why, one step back310 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 311–313why, one step back311 */312313defineGetter(req, 'secure', function secure(){Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 314why, one step back314 return this.protocol === 'https';4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 315–323why, one step back315});316317/**318 * Return the remote address from the trusted proxy.319 *320 * The is the remote address on the socket unless321 * "trust proxy" is set.322 *323 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 324why, one step back324 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 325–340why, one step back325 */326327defineGetter(req, 'ip', function ip(){328 var trust = this.app.get('trust proxy fn');329 return proxyaddr(this, trust);330});331332/**333 * When "trust proxy" is set, trusted proxy addresses + client.334 *335 * For example if the value were "client, proxy1, proxy2"336 * you would receive the array `["client", "proxy1", "proxy2"]`337 * where "proxy2" is the furthest down-stream and "proxy1" and338 * "proxy2" were trusted.339 *340 * @return {Array}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 341why, one step back341 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 342–346why, one step back342 */343344defineGetter(req, 'ips', function ips() {345 var trust = this.app.get('trust proxy fn');346 var addrs = proxyaddr.all(this, trust);Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 347–352why, one step back347348 // reverse the order (to farthest -> closest)349 // and remove socket address350 addrs.reverse().pop()351352 return addrs4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 353–366why, one step back353});354355/**356 * Return subdomains as an array.357 *358 * Subdomains are the dot-separated parts of the host before the main domain of359 * the app. By default, the domain of the app is assumed to be the last two360 * parts of the host. This can be changed by setting "subdomain offset".361 *362 * For example, if the domain is "tobi.ferrets.example.com":363 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.364 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.365 *366 * @return {Array}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 367why, one step back367 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 368–386why, one step back368 */369370defineGetter(req, 'subdomains', function subdomains() {371 var hostname = this.hostname;372373 if (!hostname) return [];374375 var offset = this.app.get('subdomain offset');376 var subdomains = !isIP(hostname)377 ? hostname.split('.').reverse()378 : [hostname];379380 return subdomains.slice(offset);381});382383/**384 * Short-hand for `url.parse(req.url).pathname`.385 *386 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 387why, one step back387 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 388–394why, one step back388 */389390defineGetter(req, 'path', function path() {391 return parse(this).pathname;392});393394/**Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296
line 395why, one step back395 * Parse the "Host" header field to a host.4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 396–401why, one step back396 *397 * When the "trust proxy" setting trusts the socket398 * address, the "X-Forwarded-Host" header field will399 * be trusted.400 *401 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 402why, one step back402 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 403–404why, one step back403 */404Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296
line 405why, one step back405defineGetter(req, 'host', function host(){4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
line 406why, one step back406 var trust = this.app.get('trust proxy fn');Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296
line 407why, one step back407 var val = this.get('X-Forwarded-Host');4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
line 408why, one step back408lib: use req.socket over deprecated req.connection (#6705)Sebastian Beltranbefore adoption89f198c
line 409why, one step back409 if (!val || !trust(this.socket.remoteAddress, 0)) {Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296
line 410why, one step back410 val = this.get('Host');Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 411–414why, one step back411 } else if (val.indexOf(',') !== -1) {412 // Note: X-Forwarded-Host is normally only ever a413 // single value, but this is to be safe.414 val = val.substring(0, val.indexOf(',')).trimRight()4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 415–416why, one step back415 }416Change req.host to return hostDouglas Christopher Wilsonbefore adoption085a296
lines 417–433why, one step back417 return val || undefined;418});419420/**421 * Parse the "Host" header field to a hostname.422 *423 * When the "trust proxy" setting trusts the socket424 * address, the "X-Forwarded-Host" header field will425 * be trusted.426 *427 * @return {String}428 * @api public429 */430431defineGetter(req, 'hostname', function hostname(){432 var host = this.host;4334.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 434–441why, one step back434 if (!host) return;435436 // IPv6 literal support437 var offset = host[0] === '['438 ? host.indexOf(']') + 1439 : 0;440 var index = host.indexOf(':', offset);441Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 442why, one step back442 return index !== -14.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 443–448why, one step back443 ? host.substring(0, index)444 : host;445});446447/**448 * Check if the request is fresh, akaMerge branch '5.0' into 5-mergeWes Toddbefore adoptionf9256ef
line 449why, one step back449 * Last-Modified or the ETag4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 450–452why, one step back450 * still match.451 *452 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 453why, one step back453 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 454–457why, one step back454 */455456defineGetter(req, 'fresh', function(){457 var method = this.method;Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 458–459why, one step back458 var res = this.res459 var status = res.statusCode4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 460–461why, one step back460461 // GET or HEAD for weak freshness validation onlyMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 462why, one step back462 if ('GET' !== method && 'HEAD' !== method) return false;4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 463–464why, one step back463464 // 2xx or 304 as per rfc2616 14.26Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 465–469why, one step back465 if ((status >= 200 && status < 300) || 304 === status) {466 return fresh(this.headers, {467 'etag': res.get('ETag'),468 'last-modified': res.get('Last-Modified')469 })4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 470–480why, one step back470 }471472 return false;473});474475/**476 * Check if the request is stale, aka477 * "Last-Modified" and / or the "ETag" for the478 * resource has changed.479 *480 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 481why, one step back481 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 482–491why, one step back482 */483484defineGetter(req, 'stale', function stale(){485 return !this.fresh;486});487488/**489 * Check if the request was an _XMLHttpRequest_.490 *491 * @return {Boolean}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 492why, one step back492 * @public4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 493–496why, one step back493 */494495defineGetter(req, 'xhr', function xhr(){496 var val = this.get('X-Requested-With') || '';Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 497why, one step back497 return val.toLowerCase() === 'xmlhttprequest';4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 498–505why, one step back498});499500/**501 * Helper function for creating a getter on an object.502 *503 * @param {Object} obj504 * @param {String} name505 * @param {Function} getterMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 506why, one step back506 * @private4.10.0Douglas Christopher Wilsonbefore adoptiond40dc65
lines 507–513why, one step back507 */508function defineGetter(obj, name, getter) {509 Object.defineProperty(obj, name, {510 configurable: true,511 enumerable: true,512 get: getter513 });Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 514why, one step back514}