-
Notifications
You must be signed in to change notification settings - Fork 1
/
hapi.js
55 lines (43 loc) · 1.16 KB
/
hapi.js
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
var assign = require('lodash/assign');
var extensions = assign({}, require('./index'));
var hapi = extensions.hapi = assign({}, extensions.hapi);
function trim(v) {
return v.replace(/^\s+|\s+$/ig, '');
}
hapi.forwarded = function hapiForwarded(request, type, hop) {
type = type.toLowerCase();
hop = hop || 0;
var headers = request.headers || {};
var list = (headers['x-forwarded-' + type] || '')
.split(',')
.map(trim);
return list[hop];
};
hapi.proto = function hapiProto(request) {
return hapi.forwarded(request, 'proto') ||
request.connection.info.protocol;
};
hapi.host = function hapiHost(request) {
return hapi.forwarded(request, 'host') ||
request.info.host;
};
hapi.requested = function hapiRequested(request) {
var host = hapi.host(request);
var proto = hapi.proto(request);
return [
proto,
'://',
host,
request.url.path
].join('');
};
hapi.baseUrl = function hapiBaseUrl(request) {
var host = hapi.host(request);
var proto = hapi.proto(request);
return [
proto,
'://',
host
].join('');
};
module.exports = extensions;