-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstream-file-server-md5.js
51 lines (45 loc) · 1.44 KB
/
stream-file-server-md5.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
var util = require("util"),
http = require("http"),
url = require("url"),
path = require("path"),
crypto = require("crypto"),
fs = require("fs");
var dir = process.argv[2] || './public';
var port = parseInt(process.argv[3]) || 8080;
util.log('Serving files from ' + dir + ', port is ' + port);
http.createServer(function(request, response) {
var te = request.headers.te;
console.log('te: ' + te);
var sendTrailer = te && te.match('trailers');
console.log('Send Trailer: ' + sendTrailer);
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), dir, uri);
path.exists(filename, function(exists) {
if(exists) {
var hash = crypto.createHash('md5');
f = fs.createReadStream(filename);
f.on('open', function() {
var headers = { 'Content-Type': 'text/plain' };
if (sendTrailer) headers.trailer = 'Content-MD5'
response.writeHead(200, headers);
});
f.on('data', function(chunk) {
hash.update(chunk);
response.write(chunk);
});
f.on('error', function(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
});
f.on('end', function() {
response.addTrailers({'Content-MD5': hash.digest('base64')});
response.end();
});
} else {
response.writeHead(404);
response.end();
}
});
}).listen(port);
util.log("Server running at http://localhost:8080/");