-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticHandler.js
180 lines (159 loc) · 5.38 KB
/
staticHandler.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var fs = require('fs');
var sys = require('sys');
var mime = require('./mime');
var url = require('url');
var path = require('path');
exports.handle = function (req, res) {
var send = new Sender(req, res);
send.docroot(req.basicServer.module.options.docroot)
.index(['index.html', 'index.htm'])
.send();
}
function Sender(req, res) {
this.req = req;
this.res = res;
this.useIndex = false;
}
Sender.prototype = {
docroot: function (root) {
this.root = root;
this.pathParse();
return this;
},
pathParse: function () {
var path = url.parse(decodeURIComponent(this.req.url)).pathname;
this.path = this.root + path;
return this;
},
index: function (index) {
if (this.path[this.path.length - 1] == '/') {
var i = 0,
idx = [].concat(index);
for (; i < idx.length; i++) {
if (fs.existsSync(this.path + idx[i])) {
this.useIndex = true;
this.path += idx[i];
break;
}
}
}
return this;
},
parseRange: function(len, range) {
var i = range.indexOf('=');
var invalid = false;
if (i == -1) return false;
var arr = range.slice(i + 1).split(',').map(function(item){
var rg = item.split('-');
var start = parseInt(rg[0]);
var end = parseInt(rg[1]);
// --xxx
if (isNaN(start)) {
start = len - end;
end = len - 1;
}
// xxx-
else if (isNaN(end)) {
end = len - 1;
}
if (end > len) end = len - 1;
if (isNaN(start) || isNaN(end) || start > end) invalid = true;
return {start: start, end: end};
});
return invalid ? false : arr;
},
isMelicious: function () {
var realpath = path.normalize(this.path);
return realpath.indexOf(this.root);
},
error: function (code) {
var res = this.res;
res.statusCode = code;
res.end();
},
redirect: function () {
var res = this.res;
var req = this.req;
res.statusCode = 301;
res.setHeader('Location', req.url + '/');
res.end();
},
notModified: function () {
var res = this.res;
res.statusCode = 304;
res.end();
},
isNotModified: function () {
var etagMatches = true;
var notModified = true;
var reqHeader = this.req.headers,
resHeader = this.res._headers,
modifiedSince = reqHeader['if-modified-since'],
noneMatch = reqHeader['if-none-match'],
lastModified = resHeader['last-modified'],
etag = resHeader['etag'],
cc = reqHeader['cache-control'];
// unconditional request
if (!modifiedSince && !noneMatch) return false;
// check for no-cache cache request directive
if (cc && cc.indexOf('no-cache') !== -1) return false;
// parse if-none-match
if (noneMatch) noneMatch = noneMatch.split(/ *, */);
// if-none-match
if (noneMatch) etagMatches = ~noneMatch.indexOf(etag) || '*' == noneMatch[0];
// if-modified-since
if (modifiedSince) {
modifiedSince = new Date(modifiedSince);
lastModified = new Date(lastModified);
notModified = lastModified <= modifiedSince;
}
return !!(etagMatches && notModified);
},
send: function () {
var res = this.res;
var req = this.req;
var that = this;
var options = {};
var melicious = this.isMelicious();
if (melicious == -1 || melicious > 0) return this.error(403);
if (melicious == -2) return this.error(404);
fs.stat(this.path, function (err, stats) {
if (err) return that.error(404);
if (stats.isDirectory()) return that.redirect();
var len = stats.size;
res.statusCode = 200;
res.setHeader('Content-Type', mime.lookup(that.path));
that.setHeader(stats);
if (that.isNotModified()) return that.notModified();
if (req.headers.range) {
var range = that.parseRange(stats.size, req.headers.range);
if (range) {
res.statusCode = 206;
res.setHeader('Content-Range', 'bytes '
+ range[0].start + '-' + range[0].end
+ '/' + stats.size);
options.start = range[0].start;
options.end = range[0].end;
len = range[0].end - range[0].start + 1;
}
}
res.setHeader('Content-Length', len);
var stream = fs.createReadStream(that.path, options);
stream.pipe(res);
stream.on('error', function () {
res.destroy();
});
req.on('close', stream.destroy.bind(stream));
});
},
setHeader: function (stats) {
var req = this.req;
var res = this.res;
res.setHeader('Last-Modified', stats.mtime.toUTCString());
res.setHeader('Etag', etag(stats));
res.setHeader('Date', (new Date).toUTCString());
}
}
function etag(stats) {
return '"' + Number(stats.mtime) + '"';
}