-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrosswordHTTPServer.js
30 lines (27 loc) · 1.37 KB
/
crosswordHTTPServer.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
var http = require('http');
var fs = require('fs');
module.exports.server = http.createServer(function (req, res) {
//Finding required file from request url and headers
fileReq = req.url;
if(req.headers["user-agent"].substr(0,5)=="Links" || req.headers["user-agent"].substr(0,4)=="Lynx") fileReq="/links_support";
if(req.headers.referer)
if(req.headers.referer.split("/").reverse()[0]=="admin")
fileReq="/admin"+fileReq;
if(fileReq=="/") fileReq="/crossword.html";
if(fileReq=="/admin") fileReq="/crossword.html";
if(fileReq.search("\\\.\\\.") != -1) fileReq="/sdgbhjhfj";
//Those rare files that are to be sent
if(fileReq=="/serverConfig.js" || fileReq=="/admin/serverConfig.js") fileReq="/../serverConfig.js";
//Fetching and sending file
exists = fs.existsSync('static'+fileReq);
if(!exists){
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<html><head><title>Oops!</title></head><body>Sorry, but that file doesn\'t exist. (Or you\'re trying to access a file that we specifically blocked you from accesing. Not cool.)</body></html>');
return;
}
data = fs.readFileSync('static'+fileReq,'utf8');
extension=fileReq.substr(fileReq.lastIndexOf("."));
res.writeHead(200, {'Content-Type': extension==".html"?'text/html':extension==".css"?'text/css':extension==".js"?'text/javascript':'text/plain'});
res.write(data);
res.end();
});