-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
420 lines (338 loc) · 9.66 KB
/
server.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
var http = require('http');
var https = require('https');
var fs = require('fs');
var config = {
//single backend only at this moment
backend: {
host: "localhost",
port: 8080
},
https: {
enabled: true,
port: 443,
keyFile: "kiwsy.key",
certFile: "kiwsy.crt"
},
http: {
enabled: true,
port: 80
}
};
/*
A class for handling cache.
Support defining ttl per cache object.
There are 2 cache maps, 1 for complete response object and 1 for partial response object.
*/
var cachee = {
cacheMap: {},
partialMap: {},
defaultTTL: 120 * 1000,
set: function (hashKey, cache){
this.cacheMap[hashKey] = cache;
},
get: function (hashKey){
if (! this.cacheMap.hasOwnProperty(hashKey)){
return null;
}
var cache = this.cacheMap[hashKey];
if (cache == null){
return null;
}
var now = +new Date;
var ttl = this.defaultTTL;
if (cache.hasOwnProperty("ttl")){
ttl = cache.ttl;
}
if (now - cache.updateTime > ttl){
this.purge(hashKey);
return null;
}
return cache;
},
purge: function (hashKey){
this.cacheMap[hashKey] = null;
delete this.cacheMap[hashKey];
},
setPartial: function (hashKey, cache){
this.partialMap[hashKey] = cache;
},
getPartial: function (hashKey){
if (! this.partialMap.hasOwnProperty(hashKey)){
return null;
}
return this.partialMap[hashKey];
},
purgePartial: function (hashKey){
this.partialMap[hashKey] = null;
delete this.partialMap[hashKey];
}
};
/*
A class for handling the request queue.
*/
var queuee = {
queueMap: {},
notEmpty: function (hashKey){
return this.queueMap.hasOwnProperty(hashKey) && this.queueMap[hashKey].length > 0;
},
clear: function (hashKey){
if (! this.queueMap.hasOwnProperty(hashKey)){
return;
}
this.queueMap[hashKey].length = 0;
},
add: function (hashKey, req, res){
if (! this.queueMap.hasOwnProperty(hashKey)){
this.queueMap[hashKey] = [];
}
this.queueMap[hashKey].push({
req: req,
res: res
});
},
each: function (hashKey, cb){
if (! this.queueMap.hasOwnProperty(hashKey)){
return;
}
if (! cb){
return;
}
var queue = this.queueMap[hashKey];
for (var i = 0, l = queue.length; i < l; i++){
cb(queue[i]);
}
}
};
//A handy function for cloning object. To be used for modifying headers without polluting the original headers.
function clone(obj) {
if(obj === null || typeof(obj) !== 'object' || 'isActiveClone' in obj)
return obj;
var temp = obj.constructor(); // changed
for(var key in obj) {
if(Object.prototype.hasOwnProperty.call(obj, key)) {
obj['isActiveClone'] = null;
temp[key] = clone(obj[key]);
delete obj['isActiveClone'];
}
}
return temp;
}
/*
Create a request to the backend and perform the callbacks accordingly.
*/
function forwardRequest(req, res, headerCome, dataChunkCome, done){
var proxyReq = http.request({
hostname: config.backend.host,
port: config.backend.port,
method: req.method,
path: req.url,
headers: req.headers,
}, function (proxyRes){
var hashKey = hash(req);
var responseObj = {
statusCode: proxyRes.statusCode,
statusMessage: proxyRes.statusMessage,
headers: proxyRes.headers,
rawBody: [],
body: null
};
//process headers
for (var k in responseObj.headers){
var newK = k.toLowerCase();
if (k != newK){
responseObj.headers[newK] = responseObj.headers[k];
delete responseObj.headers[k];
}
}
delete responseObj.headers['transfer-encoding'];
responseObj.headers['connection'] = 'Close';
cachee.setPartial(hashKey, responseObj);
headerCome(req, res, responseObj);
proxyRes.on('data', function (chunk) {
responseObj.rawBody.push(chunk);
dataChunkCome(req, res, chunk);
});
proxyRes.on('end', function() {
responseObj.body = Buffer.concat(responseObj.rawBody);
responseObj.updateTime = +new Date;
cachee.purgePartial(hashKey);
done(req, res, responseObj);
queuee.clear(hashKey);
})
});
var bodyLength = 0;
req.on("data", function (data){
bodyLength += data.length;
//so large, fuck you
if (bodyLength > 1e6){
req.connection.destroy();
return;
}
proxyReq.write(data);
})
req.on("end", function (){
proxyReq.end();
});
}
//The hash function for producing the hash key of the cache for a request.
function hash(req){
var host = req.headers.host;
var url = req.url;
return host + url;
}
//Purge the cache. Only exact matching is supported at this moment.
function purge(req, res){
var hashKey = hash(req);
cachee.purge(hashKey);
res.writeHead(200, "Purged");
res.end("Purged");
}
/*
The 3 functions, headerComeAndWriteToRes, dataChunkComeAndWriteToRes and doneButNoCache
handle the case that the request is not cached and forwarded to the backend directly.
They are used as the callbacks of forwardRequest().
*/
function headerComeAndWriteToRes(req, res, responseObj){
res.writeHead(responseObj.statusCode, responseObj.statusMessage, responseObj.headers);
}
function dataChunkComeAndWriteToRes(req, res, chunk){
res.write(chunk);
}
function doneButNoCache(req, res, responseObj){
res.end();
}
/*
The 3 functions, headerComeAndWriteToQueue, dataChunkComeAndWriteToQueue and doneAndKeepCache
handle the case that the request is cached.
When data arrives, they forward the data to all the queued requests.
At the end, it writes the response object to cache such that it can be re-used later.
*/
function headerComeAndWriteToQueue(req, res, responseObj){
var hashKey = hash(req);
queuee.each(hashKey, function (obj){
obj.res.writeHead(responseObj.statusCode, responseObj.statusMessage, responseObj.headers);
});
}
function dataChunkComeAndWriteToQueue(req, res, chunk){
var hashKey = hash(req);
queuee.each(hashKey, function (obj){
obj.res.write(chunk);
});
}
function doneAndKeepCache(req, res, responseObj){
var hashKey = hash(req);
responseObj.ttl = 120 * 1000;
cachee.set(hashKey, responseObj);
queuee.each(hashKey, function (obj){
obj.res.end();
});
}
/*
When there are simultaneous requests to the same URL, the one that comes first will be forwarded to the backend.
Then latter one will stay in the queue. If there is any partial responded data, this function will return it to the latter request.
*/
function writeExistingResponseObj(hashKey, req, res){
var responseObj = cachee.getPartial(hashKey);
if (responseObj == null){
return;
}
res.writeHead(responseObj.statusCode, responseObj.statusMessage, responseObj.headers);
//TODO is there a case that the rawBody grows more items before the loop ends?
for (var i = 0, l = responseObj.rawBody.length; i < l; i++){
res.write(responseObj.rawBody[i]);
}
}
/*
Respond the responseObj. The responseObj should be a complete response object.
Here you may modify the headers, the body or whatever you can think of.
** Note that the partial object is not passed through this function. The modification you made here will not affect the partial object.
*/
function respond(req, res, responseObj, hit){
if (hit){
var headers = clone(responseObj.headers);
headers['x-nodish-hit'] = '1';
res.writeHead(responseObj.statusCode, responseObj.statusMessage, headers);
}else{
res.writeHead(responseObj.statusCode, responseObj.statusMessage, responseObj.headers);
}
res.end(responseObj.body);
}
/*
As the name suggested, it first checks if it is cached or not (and also if it is expired or not).
If cache is available, simply responds with the cached object.
Otherwise, forward the request to the backend and cache it such that it can be re-used next time.
It also handles simultaneous requests to the same URL.
i.e. When there are 2 requests requesting the same URL, only 1 request will be forwarded to backend (the one comes first).
When the latter request comes, all the responded data from the former request will be sent to it.
*/
function useCacheIfExist(req, res){
var hashKey = hash(req);
var cache = cachee.get(hashKey);
if (cache == null){
var needToRequestAgain = false;
if (! queuee.notEmpty(hashKey)){
needToRequestAgain = true;
}
writeExistingResponseObj(hashKey, req, res);
queuee.add(hashKey, req, res);
if (needToRequestAgain){
forwardRequest(req, res, headerComeAndWriteToQueue, dataChunkComeAndWriteToQueue, doneAndKeepCache);
}
}else{
respond(req, res, cache, true);
}
};
var recv = function (req, res){
var host = req.headers.host;
var url = req.url;
if (host == "kiwsy.com"){
var urlMatch = false;
urlMatch = urlMatch || (url == "/");
if (!urlMatch){
urlMatch = urlMatch || (url.substr(0, 8) == "/wp-json");
}
if (!urlMatch){
urlMatch = urlMatch || (url.substr(0, 19) == "/wp-content/uploads");
}
if (!urlMatch){
urlMatch = urlMatch || (new RegExp("^/20[0-9][0-9]/").test(url));
}
if (urlMatch){
req.headers.cookie = "";
useCacheIfExist(req, res);
return true;
}
}
return false;
};
var requestHandler = function(req, res) {
var handled = false;
var method = req.method.toLowerCase();
//Handle GET and PURGE requests only
//Others like POST, PUT blablabla are forwarded directly
if (method == "get"){
handled = recv(req, res);
}else if (method == "purge"){
purge(req, res);
handled = true;
}
if (! handled){
forwardRequest(req, res, headerComeAndWriteToRes, dataChunkComeAndWriteToRes, doneButNoCache);
}
};
if (config.https.enabled){
https.createServer({
key: fs.readFileSync(config.https.keyFile),
cert: fs.readFileSync(config.https.certFile)
}, requestHandler).listen(config.https.port);
}
if (config.http.enabled){
http.createServer(requestHandler).listen(config.http.port);
}
console.log("Nodish started");
if (config.http.enabled){
console.log("\tListening on " + config.http.port + " for HTTP");
}
if (config.https.enabled){
console.log("\tListening on " + config.https.port + " for HTTPS");
}