-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestHandler.js
284 lines (226 loc) · 8.19 KB
/
RequestHandler.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
var http = require('http');
var util = require('util');
var events = require('events');
var crypto = require('crypto');
function RequestHandler(config, logger) {
"use strict";
events.EventEmitter.call(this);
this._cache = {};
this._config = config;
this._logger = logger;
this._server = http.createServer(this._request.bind(this));
/**
* All header defined here that are present in the response
* from the source will be passed to the response
**/
this._headersToPassToResponse = [
'cache-control',
'expires',
'access-control-allow-origin',
'content-type',
'last-modified',
'content-length'
];
/**
* All headers defined here will be passed over to the source
* if present
*/
this._headersToPassToSource = [
'user-agent',
'accept',
'accept-language',
'accept-encoding'
];
this._server.on('error', (function (err) {
this._logger.error('Failed to bind to specified port (' + err.message + '), aborting...');
process.exit(127);
}).bind(this));
this._server.listen(this._config.port, this._config.ip, (function () {
this.emit('bound');
this._logger.notice('Http proxy up and running, let\'s fetz!');
this._logger.notice('Listening on ' + this._config.ip + ':' + this._config.port);
this._logger.notice('Reading from ' + this._config.sourceServer + ':' + this._config.sourcePort);
}).bind(this));
setInterval(
this._runGarbageCollection.bind(this),
this._config.garbageCollectionInterval
);
setInterval(
this._debugCacheSize.bind(this),
5000
);
}
util.inherits(RequestHandler, events.EventEmitter);
/**
* Logs some information about the current cache status
*/
RequestHandler.prototype._debugCacheSize = function () {
"use strict";
var cachePercent = Object.keys(this._cache).length * 100 / this._config.cacheSize;
this._logger.debug(
'CacheSize',
Object.keys(this._cache).length + ' / ' + this._config.cacheSize + ' (' + cachePercent + '%)'
);
};
/**
* Clean up cache if the size gets to big
*/
RequestHandler.prototype._runGarbageCollection = function () {
"use strict";
if (this._config.cacheSize < Object.keys(this._cache).length) {
var cacheList = {};
var counter = 1;
Object.keys(this._cache).forEach((function (key) {
var cacheKey = this._cache[key].expires + (counter/Object.keys(this._cache).length);
cacheList[cacheKey] = key;
counter++;
}).bind(this));
var sortedList = Object.keys(cacheList);
sortedList.sort();
while (this._config.cacheSize < Object.keys(this._cache).length) {
var value = sortedList.shift();
this._logger.debug('GarbageCollection', 'evicting entry - ' + cacheList[value] + ' (' + value + ')');
delete this._cache[cacheList[value]];
delete cacheList[value];
}
}
};
/**
* Handle the incoming request
*/
RequestHandler.prototype._request = function (request, response) {
"use strict";
this._logger.debug('Request', request.url);
var now = (+new Date())/1000;
if (true === this._cache.hasOwnProperty(request.url)) {
// cach entry found
var cacheEntry = this._cache[request.url];
var statusCode = cacheEntry.statusCode;
var content = cacheEntry.content;
this._logger.debug('Request', 'cache hit: remaining time: ' + (cacheEntry.expires - now));
if (now > cacheEntry.expires) {
// cache expired
delete this._cache[request.url];
this._getContentFromSource(request, response, request.url);
} else {
// cache hit
var etag = crypto.createHash('md5').update(content).digest('hex');
// conditional get (responde with 304 without content when
// content is unchanged)
if ('undefined' != typeof request.headers['if-modified-since']) {
var ifModifiedSince = (+new Date(request.headers['if-modified-since']));
var lastModified = (+new Date(cacheEntry.headers['last-modified']));
var ifNoneMatch = request.headers['if-none-match'];
if (lastModified <= ifModifiedSince // compare nodified times
&& ('undefined' === ifNoneMatch // compare etag if present
|| etag === ifNoneMatch)
) {
// no changes, set 304 and empty response
statusCode = 304;
content = '';
}
}
cacheEntry.headers['x-http-proxy-cache-hit'] = 'yes';
cacheEntry.headers['etag'] = etag;
this._writeOutput(
response,
content,
statusCode,
cacheEntry.headers
);
}
} else {
// no cache entry found
this._logger.debug('Request', 'no cache hit');
this._getContentFromSource(request, response, request.url);
}
};
/**
* Reads a requested resource from the source
*/
RequestHandler.prototype._getContentFromSource = function (request, response, requestUrl) {
"use strict";
var headersToSource = {};
this._headersToPassToSource.forEach(function (key) {
if (true === request.headers.hasOwnProperty(key)) {
headersToSource[key] = request.headers[key];
}
});
var req = http.get({
hostname: this._config.sourceServer,
port: this._config.sourcePort,
path: requestUrl,
headers: headersToSource
}, (function (res) {
if (200 === res.statusCode
|| 404 === res.statusCode
) {
var raw = null;
res.on('data', function (data) {
if (null === raw) {
raw = data;
} else {
raw = Buffer.concat(
[raw, data],
raw.length + data.length
);
}
});
res.on('end', (function (data) {
var output = '';
var expires = ((+new Date())/1000)+900;
var headers = {
'via': 'http-proxy',
'x-http-proxy-cache-hit': 'no'
};
if (null !== raw) {
output = raw.toString();
}
this._headersToPassToResponse.forEach(function (key) {
if (true === res.headers.hasOwnProperty(key)) {
headers[key] = res.headers[key];
}
});
if (404 !== res.statusCode) {
expires = (+new Date(res.headers.expires))/1000;
headers['etag'] = crypto.createHash('md5').update(output).digest('hex');
}
this._cache[requestUrl] = {
'content': output,
'expires': expires,
'statusCode': res.statusCode,
'headers': headers
};
this._writeOutput(response, output, res.statusCode, headers);
}).bind(this));
res.on('abort', function () {
console.log('FUBAR');
});
}
}).bind(this)).on('error', (function (reponse, err) {
this._logger.error('Source request failed: ' + err.message);
reponse.writeHead(504);
reponse.write('Gateway Time-out');
reponse.end();
}).bind(this, response));
setTimeout((function () {
req.abort();
}).bind(this), this._config.sourceTimeout);
};
/**
* Send output to browser
*/
RequestHandler.prototype._writeOutput = function (response, data, statusCode, header) {
"use strict";
response.writeHead(statusCode, header);
response.write(data);
response.end();
};
RequestHandler.prototype.destroy = function () {
"use strict";
this._cache = null;
this._logger.destroy();
this._server.close();
this._server = null;
};
module.exports = RequestHandler;