Skip to content
Closed
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
11 changes: 8 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,14 @@ defineGetter(req, 'subdomains', function subdomains() {
if (!hostname) return [];

var offset = this.app.get('subdomain offset');
var subdomains = !isIP(hostname)
? hostname.split('.').reverse()
: [hostname];
// An IPv6 literal keeps its brackets (e.g. "[::ffff:127.0.0.1]"), which
// net.isIP() rejects, so detect it by the brackets and keep it as a single
// unit instead of splitting it on its dots.
var subdomains = hostname[0] === '[' && hostname[hostname.length - 1] === ']'
? [hostname]
: !isIP(hostname)
? hostname.split('.').reverse()
: [hostname];

return subdomains.slice(offset);
});
Expand Down
27 changes: 27 additions & 0 deletions test/req.subdomains.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ describe('req', function(){
.set('Host', '[::1]')
.expect(200, [], done);
})

it('should work with a dotted IPv6 address', function(done){
var app = express();

app.use(function(req, res){
res.send(req.subdomains);
});

request(app)
.get('/')
.set('Host', '[::ffff:127.0.0.1]')
.expect(200, [], done);
})
})

describe('otherwise', function(){
Expand Down Expand Up @@ -135,6 +148,20 @@ describe('req', function(){
.set('Host', '[::1]')
.expect(200, ['[::1]'], done);
})

it('should return an array with the whole dotted IPv6', function (done) {
var app = express();
app.set('subdomain offset', 0);

app.use(function(req, res){
res.send(req.subdomains);
});

request(app)
.get('/')
.set('Host', '[::ffff:127.0.0.1]')
.expect(200, ['[::ffff:127.0.0.1]'], done);
})
})

describe('when present', function(){
Expand Down