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
Provenance
- Commit
e6eeec3- Tree
83b208a- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
269dc53
3 files changed+149 added−3 removed
modified:lib/request.js+9 added-3 removed
328 unmodified lines329 */330 331 defineGetter(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 lines347 return parse(this).pathname;348 });349 350 /**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 line357 * @return {String}358 * @api public359 */360 361-defineGetter(req, 'host', function host(){361+defineGetter(req, 'hostname', function hostname(){362 var trust = this.app.get('trust proxy fn');363 var host = this.get('X-Forwarded-Host');364 365 if (!host || !trust(this.connection.remoteAddress)) {12 unmodified lines378 ? host.substring(0, index)379 : host;380 });381 382+// TODO: change req.host to return host in next major383+384+defineGetter(req, 'host', deprecate.function(function host(){385+ return this.hostname;386+}, 'req.host: Use req.hostname instead'));387+388 /**389 * Check if the request is fresh, aka390 * Last-Modified and/or the ETag391 * still match.60 unmodified linesadded:test/req.hostname.js+138 added
1+2+var express = require('../')3+ , request = require('supertest')4+ , assert = require('assert');5+6+describe('req', function(){7+ describe('.hostname', function(){8+ it('should return the Host when present', function(done){9+ var app = express();10+11+ app.use(function(req, res){12+ res.end(req.hostname);13+ });14+15+ request(app)16+ .post('/')17+ .set('Host', 'example.com')18+ .expect('example.com', done);19+ })20+21+ it('should strip port number', function(done){22+ var app = express();23+24+ app.use(function(req, res){25+ res.end(req.hostname);26+ });27+28+ request(app)29+ .post('/')30+ .set('Host', 'example.com:3000')31+ .expect('example.com', done);32+ })33+34+ it('should return undefined otherwise', function(done){35+ var app = express();36+37+ app.use(function(req, res){38+ req.headers.host = null;39+ res.end(String(req.hostname));40+ });41+42+ request(app)43+ .post('/')44+ .expect('undefined', done);45+ })46+47+ it('should work with IPv6 Host', function(done){48+ var app = express();49+50+ app.use(function(req, res){51+ res.end(req.hostname);52+ });53+54+ request(app)55+ .post('/')56+ .set('Host', '[::1]')57+ .expect('[::1]', done);58+ })59+60+ it('should work with IPv6 Host and port', function(done){61+ var app = express();62+63+ app.use(function(req, res){64+ res.end(req.hostname);65+ });66+67+ request(app)68+ .post('/')69+ .set('Host', '[::1]:3000')70+ .expect('[::1]', done);71+ })72+73+ describe('when "trust proxy" is enabled', function(){74+ it('should respect X-Forwarded-Host', function(done){75+ var app = express();76+77+ app.enable('trust proxy');78+79+ app.use(function(req, res){80+ res.end(req.hostname);81+ });82+83+ request(app)84+ .get('/')85+ .set('Host', 'localhost')86+ .set('X-Forwarded-Host', 'example.com:3000')87+ .expect('example.com', done);88+ })89+90+ it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){91+ var app = express();92+93+ app.set('trust proxy', '10.0.0.1');94+95+ app.use(function(req, res){96+ res.end(req.hostname);97+ });98+99+ request(app)100+ .get('/')101+ .set('Host', 'localhost')102+ .set('X-Forwarded-Host', 'example.com')103+ .expect('localhost', done);104+ })105+106+ it('should default to Host', function(done){107+ var app = express();108+109+ app.enable('trust proxy');110+111+ app.use(function(req, res){112+ res.end(req.hostname);113+ });114+115+ request(app)116+ .get('/')117+ .set('Host', 'example.com')118+ .expect('example.com', done);119+ })120+ })121+122+ describe('when "trust proxy" is disabled', function(){123+ it('should ignore X-Forwarded-Host', function(done){124+ var app = express();125+126+ app.use(function(req, res){127+ res.end(req.hostname);128+ });129+130+ 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 lines7 * 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 router1725 unmodified lines