-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpserver.js
119 lines (112 loc) · 3.49 KB
/
httpserver.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
const fs = require('fs');
const path = require("path");
const url = require('url');
const settings = require('./config.json');
const mimeTypes = {
"atom": "application/atom+xml",
"bin": "application/octet-stream",
"css": "text/css",
"gif": "image/gif",
"gz": "application/x-gzip",
"htm": "text/html",
"html": "text/html;charset=UTF-8",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "application/javascript",
"json": "application/json;charset=UTF-8",
"mp3": "audio/mpeg",
"mp4": "video/mp4",
"ogg": "audio/ogg",
"ogv": "video/ogg",
"pdf": "application/pdf",
"png": "image/png",
"rss": "application/rss+xml",
"svg": "image/svg+xml",
"txt": "text/plain;charset=UTF-8",
"webm": "video/webm",
"woff": "font/woff",
"woff2": "font/woff2",
"xml": "application/xml', 'text/xml",
"zip": "application/zip"
}
const server = {
serve: (req,res) => {
let uri = url.parse(req.url).pathname;
if(uri == "/") uri = "/index.html";
const filename = path.join(process.cwd() + "/htdocs", uri);
if(req.url.indexOf("..") > -1){
res.writeHead(403, {"Content-Type": "text/html"});
res.write("Request denied for security reasons");
res.end();
return;
}
/*
if(req.socket.remoteAddress != "::ffff:127.0.0.1" && req.socket.remoteAddress != "::1"){
res.writeHead(403, {"Content-Type": "text/html"});
res.write("You must connect to this server from a local address");
res.end();
return;
}
*/
if(settings.verbose) console.log("HTTP GET: " + req.url);
fs.exists(filename, function(exists) {
if(exists){
if(fs.lstatSync(filename).isDirectory()){
if(req.url.slice(-1) != "/"){
res.writeHead(301, {"Content-Type": "text/html", "Location": req.url + "/"});
res.write("I moved\n");
res.end();
}else{
console.log("dir " + filename);
let dhtml = "<html><head></head><body><h2>Index of " + req.url + "</h2><hr>";
let tmplst = [];
fs.readdir(filename, function (err, files) {
files.forEach(function (file) {
dhtml += "<a href=\"" + file + "\">" + file + "</a><br>";
tmplst.push(file);
});
dhtml += "<script>const files = " + JSON.stringify(tmplst) + "</script>";
dhtml += "<hr>BurdIRC</body></html>";
res.writeHead(200, {"Content-Type": "text/html"});
res.write(dhtml + "\n");
res.end();
});
}
}else{
const mime = (mimeTypes[filename.substr(filename.lastIndexOf(".") + 1)] || "text/plain");
if(mime == "image/svg+xml"){
fs.readFile(filename, "binary", function(err, file) {
if(err){
res.writeHead(404, {"Content-Type": 'text/html'});
res.write('<h1>404 not found</h1>'); //write a response to the client
res.end(); //end the response
}else{
res.writeHead(200, {"Content-Type": mime});
res.write(file);
res.end();
}
});
}else{
fs.readFile(filename, "binary", function(err, file) {
if(err){
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(err + "\n");
res.end();
}else{
res.writeHead(200, {"Content-Type": mime});
res.write(file, "binary");
res.end();
}
});
}
}
}else{
res.writeHead(404, {"Content-Type": 'text/html'});
res.write('<h1>404 not found</h1>'); //write a response to the client
res.end(); //end the response
}
});
}
}
module.exports = server;