forked from drewzboto/grunt-connect-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
109 lines (96 loc) · 3.11 KB
/
utils.js
File metadata and controls
109 lines (96 loc) · 3.11 KB
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
'use strict';
var utils = module.exports;
var httpProxy = require('http-proxy');
var _ = require('lodash');
var proxies = [];
var rewrite = function (req) {
return function (rule) {
if (rule.from.test(req.url)) {
req.url = req.url.replace(rule.from, rule.to);
}
};
};
utils.registerProxy = function(proxy) {
proxies.push(proxy);
};
utils.proxies = function() {
return proxies;
};
utils.reset = function() {
proxies = [];
};
utils.validateRewrite = function (rule) {
if (!rule ||
typeof rule.from === 'undefined' ||
typeof rule.to === 'undefined' ||
typeof rule.from !== 'string' ||
typeof rule.to !== 'string') {
return false;
}
return true;
};
utils.processRewrites = function (rewrites) {
var rules = [];
Object.keys(rewrites || {}).forEach(function (from) {
var rule = {
from: from,
to: rewrites[from]
};
if (utils.validateRewrite(rule)) {
rule.from = new RegExp(rule.from);
rules.push(rule);
utils.log.writeln('Rewrite rule created for: [' + rule.from + ' -> ' + rule.to + '].');
} else {
utils.log.error('Invalid rule');
}
});
return rules;
};
utils.matchContext = function(context, url) {
var contexts = context;
var matched = false;
if (!_.isArray(contexts)) {
contexts = [contexts];
}
contexts.forEach(function(testContext) {
if (url.lastIndexOf(testContext, 0) === 0) {
matched = true;
}
});
return matched;
};
utils.interceptCookiePaths = function(res, rewritePath) {
var _setHeader = res.setHeader.bind(res);
res.setHeader = function(name, value) {
if (name && value && name.toLowerCase() === 'set-cookie') {
var fixedCookie = value.toString().replace(/Path=([^;]*)(;|$)/gi, function(match, path, end){
return 'Path=' + rewritePath + end;
});
return _setHeader('Set-Cookie', fixedCookie);
} else {
return _setHeader(name, value);
}
};
};
utils.proxyRequest = function (req, res, next) {
var proxied = false;
proxies.forEach(function(proxy) {
if (!proxied && req && utils.matchContext(proxy.config.context, req.url)) {
if (proxy.config.rules.length) {
proxy.config.rules.forEach(rewrite(req));
}
if(proxy.config.rewriteCookiePath) {
utils.interceptCookiePaths(res, proxy.config.rewriteCookiePath);
}
proxy.server.proxyRequest(req, res);
// proxying twice would cause the writing to a response header that is already sent. Bad config!
proxied = true;
var source = req.originalUrl;
var target = (proxy.server.target.https ? 'https://' : 'http://') + proxy.server.target.host + ':' + proxy.server.target.port + req.url;
utils.log.verbose.writeln('Proxied request: ' + source + ' -> ' + target + '\n' + JSON.stringify(req.headers, true, 2));
}
});
if (!proxied) {
next();
}
};