Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/\.$/, '');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A regular expression is going to be slower than something like:

Suggested change
hostname = hostname.replace(/\.$/, '');
if (hostname[hostname.length - 1] === '.') {
hostname = hostname.slice(0, -1);
}


var offset = this.app.get('subdomain offset');
var subdomains = !isIP(hostname)
? hostname.split('.').reverse()
Expand Down
13 changes: 13 additions & 0 deletions test/req.subdomains.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down