-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-server.js
309 lines (267 loc) · 7.67 KB
/
test-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
var fs = require('fs');
var http = require('http');
var process = require('process');
var config = {
staticFiles: {
path: '.',
reloadInterval: 1000, // * 60 * 5,
logReload: false,
extensionMap: {
html: 'text/html',
css: 'text/css',
js: 'text/javascript',
glsl: 'text/x-glsl',
},
},
http: {
port: 8000,
},
};
var util = {};
util.isObject = function(scrutinent) {
var type = typeof scrutinent;
return type === 'function' || (type === 'object' && !!scrutinent);
}
util.extend = function(destination, source) {
for (var key in source) {
destination[key] = source[key];
}
return destination;
};
util.deepExtend = function(destination, source) {
for (var key in source) {
if (util.isObject(destination[key]) && util.isObject(source[key])) {
destination[key] = util.deepExtend(destination[key], source[key]);
} else {
destination[key] = source[key];
}
}
return destination;
};
util.logErrors = function(inner) {
try {
return inner.apply(this, Array.prototype.slice.call(arguments, 1));
} catch(e) {
console.log(e);
}
};
var staticFiles = {};
function logNotPlainOrDirectory(path) {
console.log(
'Not a plain file or directory "'
+ path
+ '"; treating as absent.');
};
function logNotDirectory(path) {
console.log(
'Not a directory "'
+ path
+ '"; leaving in prior state.');
};
function logNoReadAccess(path) {
console.log(
'No read access to "'
+ path
+ '"; treating as absent.');
};
function logReadError(path, error) {
console.log(
'Error ('
+ error
+ ') while reading "'
+ path
+ '"; leaving in prior state.');
};
function isIgnoredFilename(filename) {
return /^\./.test(filename);
};
staticFiles.load = function(config, state) {
var basePath = config.path || '';
var reloadInterval = config.reloadInterval || 30000;
var shouldLogReload = config.logReload;
state = state || {};
if (!state.resourceData) {
state.resourceData = {};
}
if (!state.extensionMap) {
state.extensionMap = [];
for(var extension in config.extensionMap) {
state.extensionMap.push(new RegExp('\\.' + extension + '$'));
state.extensionMap.push(config.extensionMap[extension]);
}
}
var log;
if (shouldLogReload) {
logReload = console.log;
} else {
logReload = function() {};
}
logReload('Loading static files from "' + basePath + '".');
var newResources = {};
var outstandingItems = 0;
var maybeDone = function() {
outstandingItems--;
if (outstandingItems > 0) return;
for (var subpath in state.resourceData) {
if (newResources[subpath] === undefined) {
console.log('Discarded resource "' + subpath + '".');
delete state.resourceData[subpath];
}
}
for (var subpath in newResources) {
state.resourceData[subpath] = newResources[subpath];
}
logReload('Done loading static files; will reload again in '
+ reloadInterval + 'ms.');
setTimeout(staticFiles.load, reloadInterval, config, state);
}
var populate = function(fullPath, subpath, timestamp, error, data) {
if (!error) {
var contentType = 'text/html';
for (var i = 0; i < state.extensionMap.length; i += 2) {
if (state.extensionMap[i].test(subpath)) {
contentType = state.extensionMap[i+1];
break;
}
}
newResources[subpath] = {
timestamp: timestamp,
data: data,
contentType: contentType,
};
logReload('Loaded resource "' + subpath + '" as MIME type ' + contentType + '.');
} else {
newResources[subpath] = state.resourceData[subpath];
logReadError(fullPath, error);
}
maybeDone();
};
var loadOne = function(subpath, error) {
var fullPath = basePath + subpath;
if (!error) {
var stat = fs.lstatSync(fullPath);
if (stat.isFile()) {
var timestamp = stat.mtime;
var oldTimestamp =
state.resourceData[subpath] &&
state.resourceData[subpath].timestamp;
if (!oldTimestamp || timestamp.getTime() > oldTimestamp.getTime()) {
fs.readFile(fullPath,
populate.bind(this, fullPath, subpath, timestamp));
} else {
newResources[subpath] = state.resourceData[subpath];
maybeDone();
}
} else if (stat.isDirectory()) {
var items = fs.readdirSync(fullPath);
for (var i in items) {
var item = items[i];
if (!isIgnoredFilename(item)) {
var itemSubpath = subpath + '/' + item;
var itemFullPath = basePath + itemSubpath;
outstandingItems++;
fs.access(itemFullPath, fs.R_OK, loadOne.bind(this, itemSubpath));
}
}
maybeDone();
} else {
logNotPlainOrDirectory(fullPath);
maybeDone();
}
} else {
console.log(error);
logNoReadAccess(fullPath);
maybeDone();
}
};
outstandingItems++;
fs.access(basePath, fs.R_OK, function(error) {
if (!error) {
var stat = fs.lstatSync(basePath);
if (stat.isDirectory()) {
var items = fs.readdirSync(basePath);
for (var i in items) {
var item = items[i];
if (!isIgnoredFilename(item)) {
var itemSubpath = '/' + item;
var itemFullPath = basePath + itemSubpath;
outstandingItems++;
fs.access(itemFullPath, fs.R_OK, loadOne.bind(this, itemSubpath));
}
}
} else {
logNotDirectory(basePath);
}
} else {
prefixesToRemove.push('/');
logNoReadAccess(basePath);
}
maybeDone();
});
};
var methods = {};
function Server(config) {
this.config = config;
this.resources = {};
this.resources.staticFiles = {};
staticFiles.load(config.staticFiles, this.resources.staticFiles);
httpServer = http.createServer();
for (var event in methods) {
httpServer.on(event, util.logErrors.bind(
this, methods[event], this.config.http));
}
httpServer.listen(this.config.http.port);
if (this.config.process && this.config.process.user !== undefined) {
process.setuid(this.config.process.user);
}
console.log('Running as ' + process.getuid());
}
Server.prototype.giveUp = function(response) {
response.writeHead(500, {
'Content-Length': 0,
'Content-Type': 'text/html',
});
}
Server.prototype.tryResource = function(response, status, resourceName) {
var resource = this.resources.staticFiles.resourceData[resourceName];
if (resource) {
console.log('Found "' + resourceName + '".');
response.writeHead(status, {
'Content-Length': resource.data.length,
'Content-Type': resource.contentType,
});
response.write(resource.data);
} else if (status == 200) {
console.log('Didn\'t find "' + resourceName + '".');
this.tryResource(response, 404, '/404');
} else {
console.log('Oh noes.');
this.giveUp(response);
}
}
methods.request = function(config, request, response) {
try {
if (request.method == 'GET') {
if (!/^\//.test(request.url)) {
this.tryResource(response, 404, '/404');
} else if (/\/$/.test(request.url)) {
this.tryResource(response, 200, request.url + 'index.html');
} else if (/\/index\.html$/.test(request.url)) {
response.writeHead(301, {
'Location': request.url.replace(/\/index\.html$/, '/'),
});
} else {
this.tryResource(response, 200, request.url);
}
}
} finally {
if (!response.headersSent) {
this.giveUp(response);
}
response.end();
}
}
methods.listening = function(config) {
console.log('Listening on port ' + config.port);
}
new Server(config);