Fix req.host for IPv6 literals

fixes #2102
This commit is contained in:
Douglas Christopher Wilson 2014-05-07 14:07:59 -04:00
parent d5815922ca
commit c99fa6a192
3 changed files with 51 additions and 1 deletions

View File

@ -1,3 +1,8 @@
3.5.x
=====
* fix `req.host` for IPv6 literals
3.5.2 / 2014-04-24
==================

View File

@ -476,7 +476,13 @@ req.__defineGetter__('host', function(){
var host = trustProxy && this.get('X-Forwarded-Host');
host = host || this.get('Host');
if (!host) return;
return host.split(':')[0];
var offset = host[0] === '['
? host.indexOf(']') + 1
: 0;
var index = host.indexOf(':', offset);
return ~index
? host.substring(0, index)
: host;
});
/**

View File

@ -18,6 +18,19 @@ describe('req', function(){
.expect('example.com', done);
})
it('should strip port number', function(done){
var app = express();
app.use(function(req, res){
res.end(req.host);
});
request(app)
.post('/')
.set('Host', 'example.com:3000')
.expect('example.com', done);
})
it('should return undefined otherwise', function(done){
var app = express();
@ -30,5 +43,31 @@ describe('req', function(){
.post('/')
.expect('undefined', done);
})
it('should work with IPv6 Host', function(done){
var app = express();
app.use(function(req, res){
res.end(req.host);
});
request(app)
.post('/')
.set('Host', '[::1]')
.expect('[::1]', done);
})
it('should work with IPv6 Host and port', function(done){
var app = express();
app.use(function(req, res){
res.end(req.host);
});
request(app)
.post('/')
.set('Host', '[::1]:3000')
.expect('[::1]', done);
})
})
})