Support IP address host in req.subdomains
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2308
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent d07c063 · Browse files · 3 files·+59 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+59 added−1 removed
- tests:+54 added−0 removed
- other:+5 added−1 removed
Provenance
- Commit
2de6514- Tree
6ce6490- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
d07c063
3 files changed+59 added−1 removed
modified:lib/request.js+4 added-1 removed
11 unmodified lines12 , parseRange = require('range-parser')13 , parse = require('parseurl')14 , proxyaddr = require('proxy-addr')15 , mime = connect.mime;16+var isIP = require('net').isIP;17 18 /**19 * Request prototype.20 */433 unmodified lines454 455 req.__defineGetter__('subdomains', function(){456 var host = this.host;457 var offset = this.app.get('subdomain offset');457- var subdomains = host.split('.').reverse();458+ var subdomains = !isIP(host)459+ ? host.split('.').reverse()460+ : [host];461 462 return subdomains.slice(offset);463 });464 91 unmodified linesmodified:test/req.subdomains.js+54 added
15 unmodified lines1617181920212223242526272829303132333435363738394041424344454647484955 unmodified lines10510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914032 unmodified lines15 unmodified lines .get('/') .set('Host', 'tobi.ferrets.example.com') .expect(["ferrets","tobi"], done); })
it('should work with IPv4 address', function(done){ var app = express();
app.use(function(req, res){ res.send(req.subdomains); });
request(app) .get('/') .set('Host', '127.0.0.1') .expect([], done); })
it('should work with IPv6 address', function(done){ var app = express();
app.use(function(req, res){ res.send(req.subdomains); });
request(app) .get('/') .set('Host', '[::1]') .expect([], done); }) })
describe('otherwise', function(){ it('should return an empty array', function(done){55 unmodified lines .get('/') .set('Host', 'tobi.ferrets.sub.example.com') .expect(["com","example","sub","ferrets","tobi"], done); })
it('should return an array with the whole IPv4', function (done) { var app = express(); app.set('subdomain offset', 0);
app.use(function(req, res){ res.send(req.subdomains); });
request(app) .get('/') .set('Host', '127.0.0.1') .expect(['127.0.0.1'], done); })
it('should return an array with the whole 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', '[::1]') .expect(['[::1]'], done); }) })
describe('when present', function(){ it('should return an array', function(done){32 unmodified linesmodified:History.md+1 added
1 3.x2 ===3 4 * Support `X-Forwarded-Host` in `req.subdomains`5+ * Support IP address host in `req.subdomains`6 * deps: connect@2.26.07 - deps: body-parser@~1.8.18 - deps: compression@~1.1.09 - deps: connect-timeout@~1.3.01741 unmodified lines