forked from AurelienGasser/http-folder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·151 lines (125 loc) · 4.08 KB
/
server.js
File metadata and controls
executable file
·151 lines (125 loc) · 4.08 KB
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
#!/usr/bin/env node
const http = require('http');
const fs = require('fs');
const path = require('path');
const ROOT_DIR = process.argv[2] || process.env.HTTP_FOLDER_ROOT_DIR || __dirname
const PORT = process.argv[3] || process.env.HTTP_FOLDER_PORT || 8080;
const httpServer = http.createServer(requestHandler);
httpServer.listen(PORT, () => { console.log(`Serving ${ROOT_DIR} on port ${PORT}`) });
async function requestHandler(req, res) {
const { method, url } = req;
console.log(method, url)
try {
if (url.endsWith('/')) {
return dir(req, res);
}
if (method == "GET") {
return downloadFile(req, res);
}
if (method == "POST") {
return uploadFile(req, res);
}
if (method == "DELETE") {
return deleteFile(req, res);
}
return error(req, res, "Bad Request");
} catch (err) {
return error(req, res, err.message);
}
}
function error(req, res, message) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.write(message);
res.end();
}
function isPathSafe(filePath) {
const normalizedPath = path.normalize(filePath);
return normalizedPath.startsWith(ROOT_DIR);
}
async function dir(req, res) {
const dirPath = req.url === '/' ? ROOT_DIR : path.join(ROOT_DIR, req.url);
if (!isPathSafe(dirPath)) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.write('Access Denied');
res.end();
return;
}
try {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
const files = entries
.filter(entry => !entry.name.startsWith('.'))
.map(entry => entry.isDirectory() ? entry.name + '/' : entry.name);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(files));
res.end();
} catch (err) {
console.log(err);
return error(req, res, err.message);
}
}
async function downloadFile(req, res) {
let file = path.join(ROOT_DIR, req.url);
// Check if path attempts to traverse above ROOT_DIR
const normalizedPath = path.normalize(file);
if (!normalizedPath.startsWith(ROOT_DIR)) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.write('Access Denied');
res.end();
return;
}
try {
const content = await fs.promises.readFile(file);
res.writeHead(200, { 'Content-Type': 'application/octet-stream' });
res.write(content);
res.end();
} catch (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.write('File Not Found');
res.end();
}
}
async function uploadFile(req, res) {
let file = path.join(ROOT_DIR, req.url);
if (!isPathSafe(file)) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.write('Access Denied');
res.end();
return;
}
let dir = path.dirname(file);
try {
await fs.promises.mkdir(dir, { recursive: true });
const writeStream = fs.createWriteStream(file);
req.pipe(writeStream);
await new Promise((resolve, reject) => {
req.on('end', resolve);
req.on('error', reject);
});
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Uploaded successfully');
res.end();
} catch (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.write('Failed to create directory');
res.end();
}
}
async function deleteFile(req, res) {
const file = path.join(ROOT_DIR, req.url);
if (!isPathSafe(file)) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.write('Access Denied');
res.end();
return;
}
try {
await fs.promises.unlink(file);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Deleted succesfully');
res.end();
} catch (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.write('File Not Found');
res.end();
}
}