Skip to content

Commit

Permalink
Include req.vhost.host
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 9, 2014
1 parent c36f6d5 commit 5230f14
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -34,7 +34,8 @@ corresponding to each wildcard (or capture group if RegExp object provided) and
`hostname` that was matched.

```js
// for match of "foo.bar.example.com" against "*.*.example.com":
// for match of "foo.bar.example.com:8080" against "*.*.example.com":
req.vhost.host === 'foo.bar.example.com:8080'
req.vhost.hostname === 'foo.bar.example.com'
req.vhost.length === 2
req.vhost[0] === 'foo'
Expand Down
69 changes: 39 additions & 30 deletions index.js
Expand Up @@ -42,20 +42,14 @@ module.exports = function vhost(hostname, server){
var regexp = hostregexp(hostname)

return function vhost(req, res, next){
var hostname = hostnameof(req)
var vhostdata = vhostof(req, regexp)

if (!hostname) {
return next()
}

var match = regexp.exec(hostname)

if (!match) {
if (!vhostdata) {
return next()
}

// populate
req.vhost = data(match)
req.vhost = vhostdata

// handle
handle(req, res, next)
Expand Down Expand Up @@ -84,27 +78,6 @@ function createHandle(server){
throw new TypeError('argument server is unsupported')
}

/**
* Create the value for req.vhost from a RegExp match.
*
* @param (object} match
* @return {object}
* @api private
*/

function data(match){
var obj = Object.create(null)

obj.hostname = match.input
obj.length = match.length - 1

for (var i = 1; i < match.length; i++) {
obj[i - 1] = match[i]
}

return obj
}

/**
* Get hostname of request.
*
Expand Down Expand Up @@ -168,3 +141,39 @@ function hostregexp(val){

return new RegExp(source, 'i')
}

/**
* Get the vhost data of the request for RegExp
*
* @param (object} req
* @param (RegExp} regexp
* @return {object}
* @api private
*/

function vhostof(req, regexp){
var host = req.headers.host
var hostname = hostnameof(req)

if (!hostname) {
return
}

var match = regexp.exec(hostname)

if (!match) {
return
}

var obj = Object.create(null)

obj.host = host
obj.hostname = hostname
obj.length = match.length - 1

for (var i = 1; i < match.length; i++) {
obj[i - 1] = match[i]
}

return obj
}
4 changes: 2 additions & 2 deletions test/test.js
Expand Up @@ -167,7 +167,7 @@ describe('vhost(hostname, server)', function(){
request(app)
.get('/')
.set('Host', 'user-bob.foo.com:8080')
.expect(200, '[["0","bob"],["1","foo"],["hostname","user-bob.foo.com"],["length",2]]', done)
.expect(200, '[["0","bob"],["1","foo"],["host","user-bob.foo.com:8080"],["hostname","user-bob.foo.com"],["length",2]]', done)
})
})

Expand Down Expand Up @@ -210,7 +210,7 @@ describe('vhost(hostname, server)', function(){
request(app)
.get('/')
.set('Host', 'user-bob.foo.com:8080')
.expect(200, '[["0","bob"],["1","foo"],["hostname","user-bob.foo.com"],["length",2]]', done)
.expect(200, '[["0","bob"],["1","foo"],["host","user-bob.foo.com:8080"],["hostname","user-bob.foo.com"],["length",2]]', done)
})
})

Expand Down

0 comments on commit 5230f14

Please sign in to comment.