Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / vhost / index.js
1 /*!
2  * vhost
3  * Copyright(c) 2014 Jonathan Ong
4  * Copyright(c) 2014-2015 Douglas Christopher Wilson
5  * MIT Licensed
6  */
7
8 'use strict'
9
10 /**
11  * Module exports.
12  * @public
13  */
14
15 module.exports = vhost
16
17 /**
18  * Module variables.
19  * @private
20  */
21
22 var asteriskRegExp = /\*/g
23 var asteriskReplace = '([^\.]+)'
24 var endAnchoredRegExp = /(?:^|[^\\])(?:\\\\)*\$$/
25 var escapeRegExp = /([.+?^=!:${}()|\[\]\/\\])/g
26 var escapeReplace = '\\$1'
27
28 /**
29  * Create a vhost middleware.
30  *
31  * @param {string|RegExp} hostname
32  * @param {function} handle
33  * @return {Function}
34  * @public
35  */
36
37 function vhost(hostname, handle) {
38   if (!hostname) {
39     throw new TypeError('argument hostname is required')
40   }
41
42   if (!handle) {
43     throw new TypeError('argument handle is required')
44   }
45
46   if (typeof handle !== 'function') {
47     throw new TypeError('argument handle must be a function')
48   }
49
50   // create regular expression for hostname
51   var regexp = hostregexp(hostname)
52
53   return function vhost(req, res, next) {
54     var vhostdata = vhostof(req, regexp)
55
56     if (!vhostdata) {
57       return next()
58     }
59
60     // populate
61     req.vhost = vhostdata
62
63     // handle
64     handle(req, res, next)
65   }
66 }
67
68 /**
69  * Get hostname of request.
70  *
71  * @param (object} req
72  * @return {string}
73  * @private
74  */
75
76 function hostnameof(req) {
77   var host = req.headers.host
78
79   if (!host) {
80     return
81   }
82
83   var offset = host[0] === '['
84     ? host.indexOf(']') + 1
85     : 0
86   var index = host.indexOf(':', offset)
87
88   return index !== -1
89     ? host.substring(0, index)
90     : host
91 }
92
93 /**
94  * Determine if object is RegExp.
95  *
96  * @param (object} val
97  * @return {boolean}
98  * @private
99  */
100
101 function isregexp(val) {
102   return Object.prototype.toString.call(val) === '[object RegExp]'
103 }
104
105 /**
106  * Generate RegExp for given hostname value.
107  *
108  * @param (string|RegExp} val
109  * @private
110  */
111
112 function hostregexp(val) {
113   var source = !isregexp(val)
114     ? String(val).replace(escapeRegExp, escapeReplace).replace(asteriskRegExp, asteriskReplace)
115     : val.source
116
117   // force leading anchor matching
118   if (source[0] !== '^') {
119     source = '^' + source
120   }
121
122   // force trailing anchor matching
123   if (!endAnchoredRegExp.test(source)) {
124     source += '$'
125   }
126
127   return new RegExp(source, 'i')
128 }
129
130 /**
131  * Get the vhost data of the request for RegExp
132  *
133  * @param (object} req
134  * @param (RegExp} regexp
135  * @return {object}
136  * @private
137  */
138
139 function vhostof(req, regexp) {
140   var host = req.headers.host
141   var hostname = hostnameof(req)
142
143   if (!hostname) {
144     return
145   }
146
147   var match = regexp.exec(hostname)
148
149   if (!match) {
150     return
151   }
152
153   var obj = Object.create(null)
154
155   obj.host = host
156   obj.hostname = hostname
157   obj.length = match.length - 1
158
159   for (var i = 1; i < match.length; i++) {
160     obj[i - 1] = match[i]
161   }
162
163   return obj
164 }