Skip to content

Commit

Permalink
Merge pull request koajs#225 from dead-horse/issue224-host-confused
Browse files Browse the repository at this point in the history
add request.hostname(getter), fixed koajs#224
  • Loading branch information
tj committed Feb 26, 2014
2 parents d85b757 + 72a9b69 commit c50012a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ delegate(proto, 'request')
.access('url')
.getter('subdomains')
.getter('protocol')
.getter('hostname')
.getter('header')
.getter('secure')
.getter('stale')
Expand Down
21 changes: 18 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ module.exports = {
},

/**
* Parse the "Host" header field hostname
* Parse the "Host" header field host
* and support X-Forwarded-Host when a
* proxy is enabled.
*
* @return {String}
* @return {String} hostname:port
* @api public
*/

Expand All @@ -186,7 +186,7 @@ module.exports = {
var host = proxy && this.get('X-Forwarded-Host');
host = host || this.get('Host');
if (!host) return;
return host.split(/\s*,\s*/)[0].split(':')[0];
return host.split(/\s*,\s*/)[0];
},

/**
Expand All @@ -200,6 +200,21 @@ module.exports = {
this.req.headers.host = val;
},

/**
* Parse the "Host" header field hostname
* and support X-Forwarded-Host when a
* proxy is enabled.
*
* @return {String} hostname
* @api public
*/

get hostname() {
var host = this.host;
if (!host) return;
return host.split(':')[0];
},

/**
* Check if the request is fresh, aka
* Last-Modified and/or the ETag
Expand Down
4 changes: 2 additions & 2 deletions test/request/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
var request = require('../context').request;

describe('req.host', function(){
it('should return host void of port', function(){
it('should return host with port', function(){
var req = request();
req.header.host = 'foo.com:3000';
req.host.should.equal('foo.com');
req.host.should.equal('foo.com:3000');
})

describe('when X-Forwarded-Host is present', function(){
Expand Down
31 changes: 31 additions & 0 deletions test/request/hostname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

var request = require('../context').request;

describe('req.hostname', function(){
it('should return hostname void of port', function(){
var req = request();
req.header.host = 'foo.com:3000';
req.hostname.should.equal('foo.com');
})

describe('when X-Forwarded-Host is present', function(){
describe('and proxy is not trusted', function(){
it('should be ignored', function(){
var req = request();
req.header['x-forwarded-host'] = 'bar.com';
req.header['host'] = 'foo.com';
req.hostname.should.equal('foo.com')
})
})

describe('and proxy is trusted', function(){
it('should be used', function(){
var req = request();
req.app.proxy = true;
req.header['x-forwarded-host'] = 'bar.com, baz.com';
req.header['host'] = 'foo.com';
req.hostname.should.equal('bar.com')
})
})
})
})

0 comments on commit c50012a

Please sign in to comment.