forked from villadora/express-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
191 lines (149 loc) · 5.04 KB
/
index.js
File metadata and controls
191 lines (149 loc) · 5.04 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var assert = require('assert');
var util = require('util');
var url = require('url');
var http = require('http');
var https = require('https');
var is = require('type-is');
var getRawBody = require('raw-body');
require('buffer');
module.exports = function proxy(host, options) {
assert(host, 'Host should not be empty');
options = options || {};
var port = 80;
var ishttps = /^https/.test(host);
if (typeof host == 'string') {
var mc = host.match(/^(https?:\/\/)/);
if (mc) {
host = host.substring(mc[1].length);
}
var h = host.split(':');
if (h[1] === '443') {
ishttps = true;
}
host = h[0];
port = h[1] || (ishttps ? 443 : 80);
port = String.prototype.replace.call(port, '/', '');
}
port = options.port || port;
/**
* intercept(targetResponse, data, res, req, function(err, json));
*/
var intercept = options.intercept;
var decorateRequest = options.decorateRequest;
var forwardPath = options.forwardPath;
var filter = options.filter;
var limit = options.limit || '1mb';
var preserveHostHdr = options.preserveHostHdr;
return function handleProxy(req, res, next) {
if (filter && !filter(req, res)) return next();
var headers = options.headers || {};
var path;
path = forwardPath ? forwardPath(req, res) : url.parse(req.url).path;
var skipHdrs = [ 'connection', 'content-length' ];
if (!preserveHostHdr) {
skipHdrs.push('host');
}
var hds = extend(headers, req.headers, skipHdrs);
hds.connection = 'close';
// var hasRequestBody = 'content-type' in req.headers || 'transfer-encoding' in req.headers;
getRawBody(req, {
length: req.headers['content-length'],
limit: limit
}, function(err, bodyContent) {
if (err) return next(err);
var reqOpt = {
hostname: (typeof host == 'function') ? host(req) : host.toString(),
port: port,
headers: hds,
method: req.method,
path: path,
bodyContent: bodyContent,
params: req.params
};
if (decorateRequest)
reqOpt = decorateRequest(reqOpt, req) || reqOpt;
bodyContent = reqOpt.bodyContent;
delete reqOpt.bodyContent;
delete reqOpt.params;
if (typeof bodyContent == 'string')
reqOpt.headers['content-length'] = Buffer.byteLength(bodyContent);
else if (Buffer.isBuffer(bodyContent)) // Buffer
reqOpt.headers['content-length'] = bodyContent.length;
var chunks = [];
var realRequest = (ishttps ? https : http).request(reqOpt, function(rsp) {
var rspData = null;
rsp.on('data', function(chunk) {
chunks.push(chunk);
});
rsp.on('end', function() {
var totalLength = chunks.reduce(function(len, buf) {
return len + buf.length;
}, 0);
var rspData = Buffer.concat(chunks, totalLength);
if (intercept) {
intercept(rsp, rspData, req, res, function(err, rspd, sent) {
if (err) {
return next(err);
}
var encode = 'utf8';
if (rsp.headers && rsp.headers['content-type']) {
var contentType = rsp.headers['content-type'];
if (/charset=/.test(contentType)) {
var attrs = contentType.split(';').map(function(str) { return str.trim(); });
for(var i = 0, len = attrs.length; i < len; i++) {
var attr = attrs[i];
if (/charset=/.test(attr)) {
// encode = attr.split('=')[1];
break;
}
}
}
}
if (typeof rspd == 'string')
rspd = new Buffer(rspd, encode);
if (!Buffer.isBuffer(rspd)) {
next(new Error("intercept should return string or buffer as data"));
}
if (!res.headersSent)
res.set('content-length', rspd.length);
else if (rspd.length != rspData.length) {
next(new Error("'Content-Length' is already sent, the length of response data can not be changed"));
}
if (!sent) {
res.send(rspd);
}
});
} else {
res.send(rspData);
}
});
rsp.on('error', function(e) {
next(e);
});
if (!res.headersSent) { // if header is not set yet
res.status(rsp.statusCode);
for (var p in rsp.headers) {
if (p == 'transfer-encoding')
continue;
res.set(p, rsp.headers[p]);
}
}
});
realRequest.on('error', function(e) {
next(e);
});
if (bodyContent.length) {
realRequest.write(bodyContent);
}
realRequest.end();
});
};
};
function extend(obj, source, skips) {
if (!source) return obj;
for (var prop in source) {
if (!skips || skips.indexOf(prop) == -1)
obj[prop] = source[prop];
}
return obj;
}