forked from hapijs/hapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
executable file
·150 lines (108 loc) · 5.06 KB
/
proxy.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
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
147
148
149
150
// Load modules
var Nipple = require('nipple');
var Hoek = require('hoek');
var Defaults = require('./defaults');
var Schema = require('./schema');
// Declare internals
var internals = {};
exports.handler = function (route, options) {
Schema.assert('proxy handler', options, route.path);
Hoek.assert(!route.settings.payload || ((route.settings.payload.output === 'data' || route.settings.payload.output === 'stream') && !route.settings.payload.parse), 'Cannot proxy if payload is parsed or if output is not stream or data');
var settings = Hoek.applyToDefaults(Defaults.proxy, options);
settings.mapUri = options.mapUri || internals.mapUri(options.protocol, options.host, options.port, options.uri);
if (options.rejectUnauthorized !== undefined) {
settings.rejectUnauthorized = options.rejectUnauthorized;
}
if (settings.ttl === 'upstream') {
settings._upstreamTtl = true;
}
return function (request, reply) {
settings.mapUri(request, function (err, uri, headers) {
if (err) {
return reply(err);
}
var req = request.raw.req;
var options = {
headers: {},
payload: request.payload,
redirects: settings.redirects,
timeout: settings.timeout,
rejectUnauthorized: settings.rejectUnauthorized // in case maxSockets is not specified
};
var protocol = uri.split(':', 1)[0];
options.agent = protocol === 'http' ? request.server._agents.http :
(settings.rejectUnauthorized === false ? request.server._agents.insecureAgent : request.server._agents.https);
var bind = request.route.bind || request._route.env.bind || null;
if (settings.passThrough) {
options.headers = Hoek.clone(req.headers);
delete options.headers.host;
if (settings.acceptEncoding === false) { // Defaults to true
delete options.headers['accept-encoding'];
}
}
if (headers) {
Hoek.merge(options.headers, headers);
}
if (settings.xforward &&
request.info.remoteAddress &&
request.info.remotePort) {
options.headers['x-forwarded-for'] = (options.headers['x-forwarded-for'] ? options.headers['x-forwarded-for'] + ',' : '') + request.info.remoteAddress;
options.headers['x-forwarded-port'] = (options.headers['x-forwarded-port'] ? options.headers['x-forwarded-port'] + ',' : '') + request.info.remotePort;
options.headers['x-forwarded-proto'] = (options.headers['x-forwarded-proto'] ? options.headers['x-forwarded-proto'] + ',' : '') + protocol;
}
var contentType = req.headers['content-type'];
if (contentType) {
options.headers['content-type'] = contentType;
}
// Send request
Nipple.request(request.method, uri, options, function (err, res) {
var ttl = null;
if (err) {
if (settings.onResponse) {
return settings.onResponse.call(bind, err, res, request, reply, settings, ttl);
}
return reply(err);
}
if (settings._upstreamTtl) {
var cacheControlHeader = res.headers['cache-control'];
if (cacheControlHeader) {
var cacheControl = Nipple.parseCacheControl(cacheControlHeader);
if (cacheControl) {
ttl = cacheControl['max-age'] * 1000;
}
}
}
if (settings.onResponse) {
return settings.onResponse.call(bind, null, res, request, reply, settings, ttl);
}
return reply(res)
.ttl(ttl)
.passThrough(settings.passThrough || false); // Default to false
});
});
};
};
internals.mapUri = function (protocol, host, port, uri) {
if (uri) {
return function (request, next) {
if (uri.indexOf('{') === -1) {
return next(null, uri);
}
var address = uri.replace(/{protocol}/g, request.server.info.protocol)
.replace(/{host}/g, request.server.info.host)
.replace(/{port}/g, request.server.info.port)
.replace(/{path}/g, request.url.path);
return next(null, address);
};
}
if (protocol &&
protocol[protocol.length - 1] !== ':') {
protocol += ':';
}
protocol = protocol || 'http:';
port = port || (protocol === 'http:' ? 80 : 443);
var baseUrl = protocol + '//' + host + ':' + port;
return function (request, next) {
return next(null, baseUrl + request.path + (request.url.search || ''));
};
};