From b1a14f1a73bfb851dac1fe20b31a62a55b53437a Mon Sep 17 00:00:00 2001 From: Adam Lau Date: Wed, 7 Sep 2016 11:23:39 +0800 Subject: [PATCH] fix: subdomains should be [] if the host is an ip (#807) * fix: subdomains should be [] if the host is an ip Fixes #775 --- lib/request.js | 5 ++++- test/request/subdomains.js | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/request.js b/lib/request.js index c4a5ccf..830b21b 100644 --- a/lib/request.js +++ b/lib/request.js @@ -5,6 +5,7 @@ * Module dependencies. */ +var net = require('net'); var contentType = require('content-type'); var stringify = require('url').format; var parse = require('parseurl'); @@ -417,7 +418,9 @@ module.exports = { get subdomains() { var offset = this.app.subdomainOffset; - return (this.host || '') + var host = this.host || ''; + if (net.isIP(host)) return []; + return host .split('.') .reverse() .slice(offset); diff --git a/test/request/subdomains.js b/test/request/subdomains.js index 8df5be5..39b4b6b 100644 --- a/test/request/subdomains.js +++ b/test/request/subdomains.js @@ -14,8 +14,14 @@ describe('req.subdomains', function(){ req.subdomains.should.eql(['tobi']); }) - describe('with no host present', function(){ + it('should work with no host present', function(){ var req = request(); req.subdomains.should.eql([]); }) + + it('should check if the host is an ip address', function(){ + var req = request(); + req.header.host = '127.0.0.1'; + req.subdomains.should.eql([]); + }); })