-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode-static.js
295 lines (252 loc) · 9.02 KB
/
node-static.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
var fs = require('fs')
, events = require('events')
, buffer = require('buffer')
, http = require('http')
, url = require('url')
, path = require('path')
, mime = require('mime')
, util = require('./node-static/util');
// Current version
var version = [0, 6, 9];
// In-memory file store
var store = {};
var indexStore = {};
Server = function (root, options) {
if (root && (typeof(root) === 'object')) { options = root; root = null }
this.root = path.resolve(root || '.');
this.options = options || {};
this.cache = 3600;
this.defaultHeaders = {};
this.options.headers = this.options.headers || {};
if ('cache' in this.options) {
if (typeof(this.options.cache) === 'number') {
this.cache = this.options.cache;
} else if (! this.options.cache) {
this.cache = false;
}
}
if ('serverInfo' in this.options) {
this.serverInfo = this.options.serverInfo.toString();
} else {
this.serverInfo = 'node-static/' + version.join('.');
}
this.defaultHeaders['server'] = this.serverInfo;
if (this.cache !== false) {
this.defaultHeaders['cache-control'] = 'max-age=' + this.cache;
}
for (var k in this.defaultHeaders) {
this.options.headers[k] = this.options.headers[k] ||
this.defaultHeaders[k];
}
};
Server.prototype.serveDir = function (pathname, req, res, finish) {
var htmlIndex = path.join(pathname, 'index.html'),
that = this;
fs.stat(htmlIndex, function (e, stat) {
if (!e) {
that.respond(null, 200, {}, [htmlIndex], stat, req, res, finish);
} else {
if (pathname in indexStore) {
streamFiles(indexStore[pathname].files);
} else {
// Stream a directory of files as a single file.
fs.readFile(path.join(pathname, 'index.json'), function (e, contents) {
if (e) { return finish(404, {}) }
var index = JSON.parse(contents);
indexStore[pathname] = index;
streamFiles(index.files);
});
}
}
});
function streamFiles(files) {
util.mstat(pathname, files, function (e, stat) {
if (e) { return finish(404, {}) }
that.respond(pathname, 200, {}, files, stat, req, res, finish);
});
}
};
Server.prototype.serveFile = function (pathname, status, headers, req, res) {
var that = this;
var promise = new(events.EventEmitter);
pathname = this.resolve(pathname);
fs.stat(pathname, function (e, stat) {
if (e) {
return promise.emit('error', e);
}
that.respond(null, status, headers, [pathname], stat, req, res, function (status, headers) {
that.finish(status, headers, req, res, promise);
});
});
return promise;
};
Server.prototype.finish = function (status, headers, req, res, promise, callback) {
var result = {
status: status,
headers: headers,
message: http.STATUS_CODES[status]
};
headers['server'] = this.serverInfo;
if (!status || status >= 400) {
if (callback) {
callback(result);
} else {
if (promise.listeners('error').length > 0) {
promise.emit('error', result);
}
else {
res.writeHead(status, headers);
res.end();
}
}
} else {
// Don't end the request here, if we're streaming;
// it's taken care of in `prototype.stream`.
if (status !== 200 || req.method !== 'GET') {
res.writeHead(status, headers);
res.end();
}
callback && callback(null, result);
promise.emit('success', result);
}
};
Server.prototype.servePath = function (pathname, status, headers, req, res, finish) {
var that = this,
promise = new(events.EventEmitter);
pathname = this.resolve(pathname);
// Only allow GET and HEAD requests
if (req.method !== 'GET' && req.method !== 'HEAD') {
finish(405, { 'Allow': 'GET, HEAD' });
return promise;
}
// Make sure we're not trying to access a
// file outside of the root.
if (pathname.indexOf(that.root) === 0) {
fs.stat(pathname, function (e, stat) {
if (e) {
finish(404, {});
} else if (stat.isFile()) { // Stream a single file.
that.respond(null, status, headers, [pathname], stat, req, res, finish);
} else if (stat.isDirectory()) { // Stream a directory of files.
if(req.url[req.url.length-1] !== '/') {
res.writeHead(302, {
'Location': req.url + '/'
});
res.end();
} else {
that.serveDir(pathname, req, res, finish);
}
} else {
finish(400, {});
}
});
} else {
// Forbidden
finish(403, {});
}
return promise;
};
Server.prototype.resolve = function (pathname) {
return path.resolve(path.join(this.root, pathname));
};
Server.prototype.serve = function (req, res, callback) {
var that = this,
promise = new(events.EventEmitter),
pathname;
var finish = function (status, headers) {
that.finish(status, headers, req, res, promise, callback);
};
try {
pathname = decodeURI(url.parse(req.url).pathname);
}
catch(e) {
return process.nextTick(function() {
return finish(400, {});
});
}
process.nextTick(function () {
that.servePath(pathname, 200, {}, req, res, finish).on('success', function (result) {
promise.emit('success', result);
}).on('error', function (err) {
promise.emit('error');
});
});
if (! callback) { return promise }
};
Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
var mtime = Date.parse(stat.mtime),
key = pathname || files[0],
headers = {},
clientETag = req.headers['if-none-match'],
clientMTime = Date.parse(req.headers['if-modified-since']);
// Copy default headers
for (var k in this.options.headers) { headers[k] = this.options.headers[k] }
headers['etag'] = JSON.stringify([stat.ino, stat.size, mtime].join('-'));
headers['date'] = new(Date)().toUTCString();
headers['last-modified'] = new(Date)(stat.mtime).toUTCString();
// Conditional GET
// If the "If-Modified-Since" or "If-None-Match" headers
// match the conditions, send a 304 Not Modified.
if ((clientMTime || clientETag) &&
(!clientETag || clientETag === headers['etag']) &&
(!clientMTime || clientMTime >= mtime)) {
finish(304, headers);
} else {
headers['content-length'] = stat.size;
headers['content-type'] = mime.lookup(files[0]);
'application/octet-stream';
for (var k in _headers) { headers[k] = _headers[k] }
res.writeHead(status, headers);
if (req.method === 'HEAD') {
finish(200, headers);
return;
}
// If the file was cached and it's not older
// than what's on disk, serve the cached version.
if (this.cache && (key in store) &&
store[key].stat.mtime >= stat.mtime) {
res.end(store[key].buffer);
finish(status, headers);
} else {
this.stream(pathname, files, new(buffer.Buffer)(stat.size), res, function (e, buffer) {
if (e) { return finish(500, {}) }
store[key] = {
stat: stat,
buffer: buffer,
timestamp: Date.now()
};
finish(status, headers);
});
}
}
};
Server.prototype.stream = function (pathname, files, buffer, res, callback) {
(function streamFile(files, offset) {
var file = files.shift();
if (file) {
file = file[0] === '/' ? file : path.join(pathname || '.', file);
// Stream the file to the client
fs.createReadStream(file, {
flags: 'r',
mode: 0666
}).on('data', function (chunk) {
chunk.copy(buffer, offset);
offset += chunk.length;
}).on('close', function () {
streamFile(files, offset);
}).on('error', function (err) {
callback(err);
console.error(err);
}).pipe(res, { end: false });
} else {
res.end();
callback(null, buffer, offset);
}
})(files.slice(0), 0);
};
// Exports
exports.Server = Server;
exports.version = version;
exports.mime = mime;
exports.store = store;
exports.indexStore = indexStore;