-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.js
executable file
·131 lines (113 loc) · 4.31 KB
/
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
/*jslint onevar: true, undef: true, newcap: true, nomen: true, regexp: true, plusplus: true, bitwise: true, node: true, indent: 4, white: false */
/// Usage: node static_server.js PORT
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
port = process.argv[2] || 9864; /// Defaults to port 9864
function parse_range(headers, total)
{
var match;
var start;
var end;
if (headers && headers.range && typeof headers.range === "string" && total) {
match = headers.range.match(/bytes=(\d+)(?:-(\d+))?/) || [];
start = match[1] && match[1] >= 0 ? Number(match[1]) : 0;
end = match[2] && match[2] > start && match[2] < total ? Number(match[2]) : total - 1;
return {
start: start,
end: end
};
}
}
function get_mime(filename)
{
var ext = path.extname(filename);
if (ext === ".html" || ext === ".htm") {
return "text/html";
} else if (ext === ".css") {
return "text/css";
} else if (ext === ".js") {
return "application/javascript";
} else if (ext === ".png") {
return "image/png";
} else if (ext === ".jpg" || ext === ".jpeg") {
return "image/jpeg";
} else if (ext === ".gif") {
return "image/gif";
} else if (ext === ".pdf") {
return "application/pdf";
} else if (ext === ".webp") {
return "image/webp";
} else if (ext === ".txt") {
return "text/plain";
} else if (ext === ".svg") {
return "image/svg+xml";
} else if (ext === ".xml") {
return "application/xml";
} else if (ext === ".bin") {
return "application/octet-stream";
} else if (ext === ".ttf") {
return "application/x-font-ttf";
} else if (ext === ".woff") {
return "application/font-woff";
}
}
/// Start the server.
http.createServer(function (request, response)
{
var cwd = process.cwd(),
filename,
uri = url.parse(request.url).pathname;
filename = path.join(cwd, uri);
/// Make sure the URI is valid and withing the current working directory.
if (uri.indexOf("/../") !== -1 || uri[0] !== "/" || path.relative(cwd, filename).substr(0, 3) === "../") {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
function sendFile(filename)
{
fs.stat(filename, function (err, stats)
{
var responseHeaders = {};
var range;
var streamOptions = {"bufferSize": 4096};
var code = 200;
if (!err && stats.isDirectory()) {
filename += "/index.html";
return sendFile(filename);
}
fs.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
responseHeaders["Content-Type"] = get_mime(filename);
responseHeaders["Content-Length"] = stats.size;
responseHeaders["Accept-Ranges"] = "bytes";
range = parse_range(request.headers, stats.size);
if (range) {
if (range.end <= range.start) {
/// Range not satisfiable.
response.writeHead(416);
response.end();
return;
}
streamOptions.start = range.start;
streamOptions.end = range.end;
responseHeaders["Content-Range"] = "bytes " + range.start + '-' + range.end + '/' + stats.size;
code = 206;
}
response.writeHead(code, responseHeaders);
/// Stream the data out to prevent massive buffers on large files.
fs.createReadStream(filename, streamOptions).pipe(response);
});
});
}
sendFile(filename)
}).listen(parseInt(port, 10));
console.log("http://localhost:" + port + "/\nCTRL + C to shutdown");