Skip to content
Myr

Add req.hostname

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

closes #2179

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 269dc53 · Browse files · 3 files+149 added−3 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+149 added−3 removed

  • tests:+138 added−0 removed
  • other:+11 added−3 removed

3 files+149 added−3 removed

modified:lib/request.js+9 added-3 removed
328 unmodified lines
329 */330331defineGetter(req, 'subdomains', function subdomains() {332 var offset = this.app.get('subdomain offset');333 return (this.host || '')333 return (this.hostname || '')334 .split('.')335 .reverse()336 .slice(offset);337});
9 unmodified lines
347 return parse(this).pathname;348});349350/**351 * Parse the "Host" header field hostname.351 * Parse the "Host" header field to a hostname.352 *353 * When the "trust proxy" setting trusts the socket354 * address, the "X-Forwarded-Host" header field will355 * be trusted.
1 unmodified line
357 * @return {String}358 * @api public359 */360361defineGetter(req, 'host', function host(){361defineGetter(req, 'hostname', function hostname(){362 var trust = this.app.get('trust proxy fn');363 var host = this.get('X-Forwarded-Host');364365 if (!host || !trust(this.connection.remoteAddress)) {
12 unmodified lines
378 ? host.substring(0, index)379 : host;380});381382// TODO: change req.host to return host in next major383384defineGetter(req, 'host', deprecate.function(function host(){385 return this.hostname;386}, 'req.host: Use req.hostname instead'));387388/**389 * Check if the request is fresh, aka390 * Last-Modified and/or the ETag391 * still match.
60 unmodified lines
added:test/req.hostname.js+138 added
12var express = require('../')3 , request = require('supertest')4 , assert = require('assert');56describe('req', function(){7 describe('.hostname', function(){8 it('should return the Host when present', function(done){9 var app = express();1011 app.use(function(req, res){12 res.end(req.hostname);13 });1415 request(app)16 .post('/')17 .set('Host', 'example.com')18 .expect('example.com', done);19 })2021 it('should strip port number', function(done){22 var app = express();2324 app.use(function(req, res){25 res.end(req.hostname);26 });2728 request(app)29 .post('/')30 .set('Host', 'example.com:3000')31 .expect('example.com', done);32 })3334 it('should return undefined otherwise', function(done){35 var app = express();3637 app.use(function(req, res){38 req.headers.host = null;39 res.end(String(req.hostname));40 });4142 request(app)43 .post('/')44 .expect('undefined', done);45 })4647 it('should work with IPv6 Host', function(done){48 var app = express();4950 app.use(function(req, res){51 res.end(req.hostname);52 });5354 request(app)55 .post('/')56 .set('Host', '[::1]')57 .expect('[::1]', done);58 })5960 it('should work with IPv6 Host and port', function(done){61 var app = express();6263 app.use(function(req, res){64 res.end(req.hostname);65 });6667 request(app)68 .post('/')69 .set('Host', '[::1]:3000')70 .expect('[::1]', done);71 })7273 describe('when "trust proxy" is enabled', function(){74 it('should respect X-Forwarded-Host', function(done){75 var app = express();7677 app.enable('trust proxy');7879 app.use(function(req, res){80 res.end(req.hostname);81 });8283 request(app)84 .get('/')85 .set('Host', 'localhost')86 .set('X-Forwarded-Host', 'example.com:3000')87 .expect('example.com', done);88 })8990 it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){91 var app = express();9293 app.set('trust proxy', '10.0.0.1');9495 app.use(function(req, res){96 res.end(req.hostname);97 });9899 request(app)100 .get('/')101 .set('Host', 'localhost')102 .set('X-Forwarded-Host', 'example.com')103 .expect('localhost', done);104 })105106 it('should default to Host', function(done){107 var app = express();108109 app.enable('trust proxy');110111 app.use(function(req, res){112 res.end(req.hostname);113 });114115 request(app)116 .get('/')117 .set('Host', 'example.com')118 .expect('example.com', done);119 })120 })121122 describe('when "trust proxy" is disabled', function(){123 it('should ignore X-Forwarded-Host', function(done){124 var app = express();125126 app.use(function(req, res){127 res.end(req.hostname);128 });129130 request(app)131 .get('/')132 .set('Host', 'localhost')133 .set('X-Forwarded-Host', 'evil')134 .expect('localhost', done);135 })136 })137 })138})
modified:History.md+2 added
6 unmodified lines
7 * add `headers` option to `res.sendfile`8 - use to set headers on successful file transfer9 * add `mergeParams` option to `Router`10 - merges `req.params` from parent routes11 * add `req.hostname` -- correct name for what `req.host` returns12 * deprecate things with `depd` module13 * deprecate `req.host` -- use `req.hostname` instead14 * fix behavior when handling request without routes15 * fix handling when `route.all` is only route16 * invoke `router.param()` only when route matches17 * restore `req.params` after invoking router
1725 unmodified lines