Skip to content
Myr

Change req.host to return host

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 8bb013e · Browse files · 3 files+30 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+30 added−13 removed

  • tests:+2 added−2 removed
  • other:+28 added−11 removed

3 files+30 added−13 removed

modified:lib/request.js+22 added-11 removed
355 unmodified lines
356 return parse(this).pathname;357});358359/**360 * Parse the "Host" header field to a hostname.360 * Parse the "Host" header field to a host.361 *362 * When the "trust proxy" setting trusts the socket363 * address, the "X-Forwarded-Host" header field will364 * be trusted.
1 unmodified line
366 * @return {String}367 * @api public368 */369370defineGetter(req, 'hostname', function hostname(){370defineGetter(req, 'host', function host(){371 var trust = this.app.get('trust proxy fn');372 var host = this.get('X-Forwarded-Host');372 var val = this.get('X-Forwarded-Host');373374 if (!host || !trust(this.connection.remoteAddress)) {375 host = this.get('Host');374 if (!val || !trust(this.connection.remoteAddress)) {375 val = this.get('Host');376 }377378 return val || undefined;379});380381/**382 * Parse the "Host" header field to a hostname.383 *384 * When the "trust proxy" setting trusts the socket385 * address, the "X-Forwarded-Host" header field will386 * be trusted.387 *388 * @return {String}389 * @api public390 */391392defineGetter(req, 'hostname', function hostname(){393 var host = this.host;394395 if (!host) return;396397 // IPv6 literal support398 var offset = host[0] === '['
5 unmodified lines
404 ? host.substring(0, index)405 : host;406});407391// 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'));396408/**409 * Check if the request is fresh, aka410 * Last-Modified and/or the ETag411 * still match.
60 unmodified lines
modified:test/req.host.js+2 added-2 removed
modified:History.md+6 added
15.x2===34 * change:5 - `req.host` now returns host (`hostname:port`) - use `req.hostname` for only hostname674.10.1 / 2014-10-288===================910 * Fix handling of URLs containing `://` in the path
2446 unmodified lines