From 72a9b6914697a7e6f29cacc507a33450ab48d3d1 Mon Sep 17 00:00:00 2001 From: dead_horse Date: Wed, 26 Feb 2014 14:03:05 +0800 Subject: [PATCH] add request.hostname(getter), fixed #224 make request.host and request.hostname work as node url lib http://nodejs.org/api/url.html#url_url this commit will change older request.host(getter) --- lib/context.js | 1 + lib/request.js | 23 +++++++++++++++++++---- test/request/host.js | 4 ++-- test/request/hostname.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 test/request/hostname.js diff --git a/lib/context.js b/lib/context.js index a0a9cb7..93de857 100644 --- a/lib/context.js +++ b/lib/context.js @@ -179,6 +179,7 @@ delegate(proto, 'request') .access('url') .getter('subdomains') .getter('protocol') + .getter('hostname') .getter('header') .getter('secure') .getter('stale') diff --git a/lib/request.js b/lib/request.js index 991cc2f..b6d330b 100644 --- a/lib/request.js +++ b/lib/request.js @@ -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 */ @@ -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]; }, /** @@ -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 @@ -579,4 +594,4 @@ module.exports = { header: this.header } } -}; \ No newline at end of file +}; diff --git a/test/request/host.js b/test/request/host.js index ee29d2c..7b3a0e9 100644 --- a/test/request/host.js +++ b/test/request/host.js @@ -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(){ diff --git a/test/request/hostname.js b/test/request/hostname.js new file mode 100644 index 0000000..e60b95b --- /dev/null +++ b/test/request/hostname.js @@ -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') + }) + }) + }) +})