-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
163 lines (139 loc) · 5.88 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
require('log-timestamp');
var http = require('http'),
redis = require('redis'),
underscore = require('underscore'),
crypto = require('crypto'),
redisClient = redis.createClient();
var modifyResponseHeaders, modifyRequestHeader, server, proxyHost, port, cacheEntryTTLinSeconds, runningRequests;
proxyHost = "TODO.FILL.YOUR.HOST.HERE.COM";
port = 80;
cacheEntryTTLinSeconds = 60 * 60 * 24 * 7 * 4;
runningRequests = [];
server = http.createServer(function (req, res) {
var path, requestHeaders, hashBase, md5, cacheKey;
path = req.url;
requestHeaders = underscore.clone(req.headers);
//modifyRequestHeader(requestHeaders);
console.log("Incoming Request: " + req.method + " " + path);
console.dir(requestHeaders);
hashBase = {
method: req.method,
path: path,
accept: req.headers['accept']
};
md5 = crypto.createHash('md5');
md5.update(JSON.stringify(hashBase));
cacheKey = md5.digest('hex');
console.log('Hash: ' + cacheKey);
var headers, statusCode, encoding, bodyChunks, bodyArrays;
redisClient.hgetall(cacheKey, function (err, data) {
function respond(write) {
res.writeHead(statusCode, headers);
var length = bodyChunks.length;
if (length > 0) {
for (var i = 0; i < length; ++i) {
write(i);
}
}
res.end();
}
function respondFromCache() {
var write = function (i) {
var buffer = new Buffer(bodyChunks[i]);
res.write(buffer, encoding);
};
respond(write);
console.log("Responding from cache");
}
function requestFromDbpedia(forwardResponse) {
var dbpediaRequestOptions = {
hostname: "dbpedia.org",
port: 80,
path: path,
headers: requestHeaders,
//method: req.method
method: "GET"
};
runningRequests.push(cacheKey);
console.log("Requesting " + cacheKey + " from remote");
http.get(dbpediaRequestOptions, function (dbpediaResponse) {
function handleRemoteResponse() {
function respondFromRemote() {
var write = function(i) {
res.write(bodyChunks[i], encoding);
};
respond(write);
console.log("Responding from remote");
}
function cacheResponse() {
if(statusCode >= 200 && statusCode <400)
{
var cacheObject = {
headers: JSON.stringify(headers),
statusCode: statusCode,
encoding: encoding,
bodyChunks: JSON.stringify(bodyArrays)
};
redisClient.hmset(cacheKey, cacheObject, function (err, reply) {
if (!err && reply == "OK") {
console.log("Cached " + cacheKey);
redisClient.expire(cacheKey, cacheEntryTTLinSeconds);
}
else {
console.log("ERROR caching " + cacheKey);
}
var runningRequestIndex = runningRequests.indexOf(cacheKey);
if(runningRequestIndex >= 0) {
runningRequests.splice(runningRequestIndex, 1);
console.log("Update Request finished");
}
});
}
else{
console.log("Won't cache a " + statusCode + " response");
};
}
bodyChunks = [];
bodyArrays = [];
dbpediaResponse.on('data', function (chunk) {
bodyChunks.push(chunk);
bodyArrays.push(chunk.toJSON());
});
dbpediaResponse.on("end", function (event) {
headers = dbpediaResponse.headers;
//modifyResponseHeaders(headers);
statusCode = dbpediaResponse.statusCode;
encoding = (headers['content-encoding'] == 'utf8') ? 'utf-8' : 'binary';
if(forwardResponse) respondFromRemote();
cacheResponse();
});
}
handleRemoteResponse();
});
}
if (!data) {
console.log("Cache Miss " + cacheKey);
requestFromDbpedia(true);
} else {
headers = JSON.parse(data.headers);
statusCode = data.statusCode;
encoding = data.encoding;
bodyChunks = JSON.parse(data.bodyChunks);
console.log("Cache Hit " + cacheKey);
console.log(statusCode);
console.dir(headers);
//Update Cache only
if(runningRequests.indexOf(cacheKey) < 0){
console.log("Update Request started");
requestFromDbpedia(false);
}
else {
console.log("Update Request already running");
console.dir(runningRequests);
}
respondFromCache();
}
});
});
console.log("Proxy running on port " + port)
server.listen(port);