forked from MrSwitch/node-oauth-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
361 lines (294 loc) · 8.48 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// Proxy Server
// -------------
// Proxies requests with the Access-Control-Allow-Origin Header
//
// @author Andrew Dodson
// Heavily takes code design from ConnectJS
var url = require('url');
var http=require('http');
var https=require('https');
var EventEmitter=require('events').EventEmitter;
var request = function(opts, callback){
/*
// Use fiddler?
opts.path = (opts.protocol === 'https:'? "https" : "http" ) + '://' + opts.host + (opts.port?':' + opts.port:'') + opts.path;
if(!opts.headers){
opts.headers = {};
}
opts.headers.host = opts.host;
opts.host = '127.0.0.1';
// opts.host = 'localhost';
opts.port = 8888;
// opts.protocol = null;
/**/
var req = (opts.protocol === 'https:'? https : http ).request(opts, callback);
return req;
};
//
// @param req - Request Object
// @param options || url - Map request to this
// @param res - Response, bind response to this
exports.proxy = function(req, res, options, buffer){
//////////////////////////
// Inherit from events
//////////////////////////
// TODO:
// make this extend the instance
var self = new EventEmitter();
///////////////////////////
// Define where this request is going
///////////////////////////
if(typeof(options)==='string'){
options = url.parse(options);
options.method = req.method;
}
else{
if(!options.method){
options.method = req.method;
}
}
if (options.method === 'DELETE') {
if(!options.headers){
options.headers = {};
}
options.headers['content-length'] = req.headers['content-length'] || '0';
}
options.agent = false;
///////////////////////////////////
// Preflight request
///////////////////////////////////
if( req.method.toUpperCase() === 'OPTIONS' ){
// Response headers
var obj = {
'access-control-allow-origin': '*',
'access-control-allow-methods' : 'OPTIONS, TRACE, GET, HEAD, POST, PUT, DELETE',
'content-length' : 0
// 'Access-Control-Max-Age': 3600, // seconds
};
// Return any headers the client has specified
if(req.headers["access-control-request-headers"]){
obj['access-control-allow-headers'] = req.headers["access-control-request-headers"];
}
res.writeHead(204, "no content", obj);
return res.end();
}
///////////////////////////////////
// Define error handler
///////////////////////////////////
function proxyError(err) {
errState = true;
//
// Emit an `error` event, allowing the application to use custom
// error handling. The error handler should end the response.
//
if (self.emit('proxyError', err, req, res)) {
return;
}
res.writeHead(502, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods' : 'OPTIONS, TRACE, GET, HEAD, POST, PUT'
});
if (req.method !== 'HEAD') {
//
// This NODE_ENV=production behavior is mimics Express and
// Connect.
//if (process.env.NODE_ENV === 'production') {
// res.write('Internal Server Error');
//}
res.write(JSON.stringify({error:err}));
}
try { res.end(); }
catch (ex) { console.error("res.end error: %s", ex.message); }
}
///////////////////////////////////
// Make outbound call
///////////////////////////////////
var _req = request(options, function(_res) {
// Process the `reverseProxy` `response` when it's received.
//
if (req.httpVersion === '1.0') {
if (req.headers.connection){
_res.headers.connection = req.headers.connection;
} else {
_res.headers.connection = 'close';
}
} else if (!_res.headers.connection) {
if (req.headers.connection){
_res.headers.connection = req.headers.connection;
}
else {
_res.headers.connection = 'keep-alive';
}
}
// Remove `Transfer-Encoding` header if client's protocol is HTTP/1.0
// or if this is a DELETE request with no content-length header.
// See: https://github.com/nodejitsu/node-http-proxy/pull/373
if (req.httpVersion === '1.0' || (req.method === 'DELETE' && !req.headers['content-length'])) {
delete _res.headers['transfer-encoding'];
}
//
// When the `reverseProxy` `response` ends, end the
// corresponding outgoing `res` unless we have entered
// an error state. In which case, assume `res.end()` has
// already been called and the 'error' event listener
// removed.
//
var ended = false;
_res.on('close', function () {
if (!ended) { _res.emit('end'); }
});
//
// After reading a chunked response, the underlying socket
// will hit EOF and emit a 'end' event, which will abort
// the request. If the socket was paused at that time,
// pending data gets discarded, truncating the response.
// This code makes sure that we flush pending data.
//
_res.connection.on('end', function () {
if (_res.readable && _res.resume) {
_res.resume();
}
});
_res.on('end', function () {
ended = true;
if (!errState) {
try { res.end(); }
catch (ex) { console.error("res.end error: %s", ex.message); }
// Emit the `end` event now that we have completed proxying
self.emit('end', req, res, _res);
}
});
// Allow observer to modify headers or abort response
try { self.emit('proxyResponse', req, res, _res); }
catch (ex) {
errState = true;
return;
}
// Set the headers of the client response
Object.keys(_res.headers).forEach(function (key) {
res.setHeader(key, _res.headers[key]);
});
res.setHeader("access-control-allow-methods", 'OPTIONS, TRACE, GET, HEAD, POST, PUT');
res.setHeader("access-control-allow-origin", "*");
//
// StatusCode
// Should we supress error codes
//
var suppress_response_codes = url.parse(req.url, true).query.suppress_response_codes;
// Overwrite the nasty ones
res.writeHead( suppress_response_codes ? 200 : _res.statusCode );
//
// Data
//
_res.on('data', function (chunk, encoding) {
if (res.writable) {
// Only pause if the underlying buffers are full,
// *and* the connection is not in 'closing' state.
// Otherwise, the pause will cause pending data to
// be discarded and silently lost.
if (false === res.write(chunk, encoding) && _res.pause && _res.connection.readable) {
_res.pause();
}
}
});
res.on('drain', function() {
if (_res.readable && _res.resume) {
_res.resume();
}
});
});
var errState = false;
///////////////////////////
// Set Listeners to handle errors
///////////////////////////
req.on('error', proxyError);
_req.on('error', proxyError);
req.on('aborted', function(){
_req.abort();
});
_req.on('aborted', function(){
_req.abort();
});
///////////////////////////
// Set Listeners to write data
///////////////////////////
req.on('data', function (chunk, encoding) {
if (errState) {
return;
}
// Writing chunk data doesn not require an encoding parameter
var flushed = _req.write(chunk);
if (flushed){
return;
}
req.pause();
_req.once('drain', function () {
try {
req.resume();
}
catch (er){
console.error("req.resume error: %s", er.message);
}
});
//
// Force the `drain` event in 100ms if it hasn't
// happened on its own.
//
setTimeout(function(){
_req.emit('drain');
},100);
});
//
// When the incoming `req` ends, end the corresponding `reverseProxy`
// request unless we have entered an error state.
//
req.on('end', function () {
if (!errState) {
_req.end();
}
});
//
// Buffer
if(buffer){
return !errState ? buffer.resume() : buffer.destroy();
}
return this;
};
// __Attribution:__ This approach is based heavily on
// [Connect](https://github.com/senchalabs/connect/blob/master/lib/utils.js#L157).
// However, this is not a big leap from the implementation in node-http-proxy < 0.4.0.
// This simply chooses to manage the scope of the events on a new Object literal as opposed to
// [on the HttpProxy instance](https://github.com/nodejitsu/node-http-proxy/blob/v0.3.1/lib/node-http-proxy.js#L154).
//
exports.buffer = function (obj) {
var events = [],
onData,
onEnd;
obj.on('data', onData = function (data, encoding) {
events.push(['data', data, encoding]);
});
obj.on('end', onEnd = function (data, encoding) {
events.push(['end', data, encoding]);
});
return {
end: function () {
obj.removeListener('data', onData);
obj.removeListener('end', onEnd);
},
destroy: function () {
this.end();
this.resume = function () {
console.error("Cannot resume buffer after destroying it.");
};
onData = onEnd = events = obj = null;
},
resume: function () {
this.end();
for (var i = 0, len = events.length; i < len; i++) {
obj.emit.apply(obj, events[i]);
}
}
};
};