/usr/lib/nodejs/vhost/index.js is in node-vhost 3.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | /*!
* vhost
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014 Douglas Christopher Wilson
* MIT Licensed
*/
/**
* Create a vhost middleware.
*
* @param {string|RegExp} hostname
* @param {function} handle
* @return {Function}
* @api public
*/
module.exports = function vhost(hostname, handle) {
if (!hostname) {
throw new TypeError('argument hostname is required')
}
if (!handle) {
throw new TypeError('argument handle is required')
}
if (typeof handle !== 'function') {
throw new TypeError('argument handle must be a function')
}
// create regular expression for hostname
var regexp = hostregexp(hostname)
return function vhost(req, res, next){
var vhostdata = vhostof(req, regexp)
if (!vhostdata) {
return next()
}
// populate
req.vhost = vhostdata
// handle
handle(req, res, next)
};
};
/**
* Get hostname of request.
*
* @param (object} req
* @return {string}
* @api private
*/
function hostnameof(req){
var host = req.headers.host
if (!host) {
return
}
var offset = host[0] === '['
? host.indexOf(']') + 1
: 0
var index = host.indexOf(':', offset)
return index !== -1
? host.substring(0, index)
: host
}
/**
* Determine if object is RegExp.
*
* @param (object} val
* @return {boolean}
* @api private
*/
function isregexp(val){
return Object.prototype.toString.call(val) === '[object RegExp]'
}
/**
* Generate RegExp for given hostname value.
*
* @param (string|RegExp} val
* @api private
*/
function hostregexp(val){
var source = !isregexp(val)
? String(val).replace(/([.+?^=!:${}()|\[\]\/\\])/g, '\\$1').replace(/\*/g, '([^\.]+)')
: val.source
// force leading anchor matching
if (source[0] !== '^') {
source = '^' + source
}
// force trailing anchor matching
source = source.replace(/(\\*)(.)$/, function(s, b, c){
return c !== '$' || b.length % 2 === 1
? s + '$'
: s
})
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
}
|