-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandler.js
211 lines (164 loc) · 6.71 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
/**
* Based on https://github.com/hvnguyen86/cacheTestNodeServer
*/
const url = require('url');
const crypto = require('crypto');
const fs = require('fs');
const captchapng = require('captchapng');
var headerFields = {
"cc":"Cache-Control",
"et":"ETag",
"lm":"Last-Modified",
"ex":"Expires",
"va":"Vary",
"cl" : "Content-Length"
};
var lastModified;
var timeStamps = {};
function requestHandler(req, res){
// console.log(req.url);
// console.log(req.headers);
// console.log("-----")
var responseString = req.headers["x-response-string"] ? req.headers["x-response-string"] : "";
var urlObject = url.parse(req.url,true);
var query = urlObject.query;
if(responseString == "" && urlObject.query.test != "") {
responseString = urlObject.query.test;
}
var queryParamsResponse = parseResponseString(responseString);
var path = urlObject.pathname;
var id = "";
var accept = req.headers["accept"];
var timeStamp = query["ts"];
console.log(queryParamsResponse);
if(req.headers["x-id"]){
id = req.headers["x-id"];
res.setHeader("X-Id",id);
} else if(req.headers["id"]){
id = req.headers["id"];
res.setHeader("Id",id);
} else {
id = new Date().getTime();
}
res.setHeader("Date",new Date(Date.now()).toUTCString());
if(path.indexOf("/rsc") !== -1) {
res.setHeader("X-Forwarded-Header",JSON.stringify(req.headers))
if(queryParamsResponse["st"] >= 500 && queryParamsResponse["st"] <= 505){
res.statusCode = queryParamsResponse["st"];
return res.end("");
}
else if(queryParamsResponse["st"] == 201 ){
res.statusCode = 201;
}
else if(queryParamsResponse["st"] >= 400 && queryParamsResponse["st"] <= 415){
res.statusCode = queryParamsResponse["st"];
return res.end("");
}
if(req.method == "PUT" || req.method == "DELETE" || req.method == "PATCH"){
res.statusCode = 204;
return res.end("");
}
else if(req.method == "POST"){
res.statusCode = 201;
return res.end("");
}
if(req.headers["if-none-match"] && req.headers["if-none-match"] == "123"){
res.statusCode = 304;
return res.end("");
}
if(req.headers["if-modified-since"]){
if(req.headers["if-modified-since"] == lastModified){
res.statusCode = 304;
return res.end("");
}
}
for (var key in queryParamsResponse) {
if (key == "exp"){
var expires = new Date(Date.now() + parseInt(queryParamsResponse[key]) * 1000).toUTCString();
res.setHeader("Expires",expires);
}
else if (key == "lm"){
lastModified = new Date(Date.now() + parseInt(queryParamsResponse[key]) * 1000).toUTCString();
res.setHeader("Last-Modified",lastModified);
}
else if(key == "t"){
continue;
}
else if(key == "sc"){
let path = req.url.substr(0, req.url.indexOf("/rsc"));
res.setHeader("Set-Cookie", "id=" + rand_string(16) + '; path=' + path);
}
else if(key == "xsf"){
res.setHeader("X-Store-Forbidden","This header field is forbidden to store");
}
else if (headerFields[key]){
res.setHeader(headerFields[key],queryParamsResponse[key]);
}
}
}
var body = "Id:"+id;
if(accept == "application/json"){
res.setHeader("Content-Type",accept);
var bodyJson = {};
bodyJson["Id"] = id;
body = JSON.stringify(bodyJson);
}
else if(accept =="application/xml"){
res.setHeader("Content-Type",accept);
body = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><id>'+id+'</id>';
}
else if(accept =="text/css"){
res.setHeader("Content-Type",accept);
body = 'p{font-family: '+id+'}';
}
else if(accept == "image/png"){
res.setHeader("Content-Type",accept);
// use a captcha to send fresh image
var p = new captchapng(10,10, parseInt(Math.random()*9000+1000));
p.color(0, 0, 0, 0);
p.color(80, 80, 80, 255);
var img = p.getBase64();
body = new Buffer(img,'base64');
}
else if(accept == "text/plain"){
res.setHeader("Content-Type","text/plain");
}
return res.end(body);
}
function parseResponseString(responseString){
//TODO parsing with spaces
var responseStringArray = responseString.split(";");
var responseHeaderFields = {};
for (var i = 0; i < responseStringArray.length; i++) {
var headerFieldArray = responseStringArray[i].split(":");
if(headerFieldArray.length == 2){
responseHeaderFields[headerFieldArray[0]] = headerFieldArray[1];
}
}
return responseHeaderFields;
}
function rand_string(n) {
if (n <= 0) {
return '';
}
var rs = '';
try {
rs = crypto.randomBytes(Math.ceil(n/2)).toString('hex').slice(0,n);
/* note: could do this non-blocking, but still might fail */
}
catch(ex) {
/* known exception cause: depletion of entropy info for randomBytes */
console.error('Exception generating random string: ' + ex);
/* weaker random fallback */
rs = '';
var r = n % 8, q = (n-r)/8, i;
for(i = 0; i < q; i++) {
rs += Math.random().toString(16).slice(2);
}
if(r > 0){
rs += Math.random().toString(16).slice(2,i);
}
}
return rs;
}
exports.requestHandler = requestHandler;