diff --git a/lib/request.js b/lib/request.js index 68243f52b6d..4f8614b7e87 100644 --- a/lib/request.js +++ b/lib/request.js @@ -385,6 +385,10 @@ defineGetter(req, 'subdomains', function subdomains() { if (!hostname) return []; + // Strip the optional DNS root label so fully-qualified domain names + // produce the same subdomain list as their non-root-qualified form. + hostname = hostname.replace(/\.$/, ''); + var offset = this.app.get('subdomain offset'); var subdomains = !isIP(hostname) ? hostname.split('.').reverse() diff --git a/test/req.subdomains.js b/test/req.subdomains.js index e5600f2eb56..a93b40e681c 100644 --- a/test/req.subdomains.js +++ b/test/req.subdomains.js @@ -19,6 +19,19 @@ describe('req', function(){ .expect(200, ['ferrets', 'tobi'], done); }) + it('should ignore a trailing root dot', function(done){ + var app = express(); + + app.use(function(req, res){ + res.send(req.subdomains); + }); + + request(app) + .get('/') + .set('Host', 'tobi.ferrets.example.com.') + .expect(200, ['ferrets', 'tobi'], done); + }) + it('should work with IPv4 address', function(done){ var app = express();