[Pkg-javascript-commits] [node-static] 27/151: (api) serveFile takes a status and headers now.
Tonnerre Lombard
tonnerre-guest at moszumanska.debian.org
Tue Jan 7 23:17:56 UTC 2014
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch master
in repository node-static.
commit b616073318ea444a7bc3d15986e4ea2bdd5d00b6
Author: cloudhead <self at cloudhead.net>
Date: Thu Aug 12 11:46:21 2010 -0400
(api) serveFile takes a status and headers now.
Major refactor to keep things clean, and manageable.
---
lib/node-static.js | 206 +++++++++++++++++++++++++++++------------------------
1 file changed, 114 insertions(+), 92 deletions(-)
diff --git a/lib/node-static.js b/lib/node-static.js
index 938d259..d997c0b 100644
--- a/lib/node-static.js
+++ b/lib/node-static.js
@@ -46,122 +46,142 @@ this.Server = function (root, options) {
}
};
-this.Server.prototype.serveFile = function (pathname, req, res, callback) {
- var that = this,
- promise = new(events.EventEmitter);
-
- process.nextTick(function () {
- // Only allow GET and HEAD requests
- if (req.method !== 'GET' && req.method !== 'HEAD') {
- return finish(405, { 'Allow': 'GET, HEAD' });
- }
+this.Server.prototype.serveDir = function (pathname, req, res, finish) {
+ var htmlIndex = path.join(pathname, 'index.html'),
+ that = this;
- pathname = path.normalize(path.join(that.root, pathname));
-
- // Make sure we're not trying to access a
- // file outside of the root.
- if (new(RegExp)('^' + that.root).test(pathname)) {
- fs.stat(pathname, function (e, stat) {
- var htmlIndex = path.join(pathname, 'index.html');
-
- if (e) {
- finish(404, {});
- } else if (stat.isFile()) {
- // Stream a single file.
- that.respond(null, [pathname], stat, req, res, finish);
-
- } else if (stat.isDirectory()) {
- fs.stat(htmlIndex, function (e, stat) {
- if (!e) {
- that.respond(null, [htmlIndex], stat, req, res, finish);
- } else {
- if (pathname in exports.store) {
- util.mstat(pathname, exports.indexStore[pathname].files, function (e, stat) {
- that.respond(pathname, exports.indexStore[pathname].files, stat, req, res, finish);
- });
- } 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);
-
- exports.indexStore[pathname] = index;
-
- util.mstat(pathname, index.files, function (e, stat) {
- that.respond(pathname, index.files, stat, req, res, finish);
- });
- });
- }
- }
- });
- } else {
- finish(400, {});
- }
- });
+ fs.stat(htmlIndex, function (e, stat) {
+ if (!e) {
+ that.respond(null, 200, {}, [htmlIndex], stat, req, res, finish);
} else {
- // Forbidden
- finish(403, {});
+ if (pathname in exports.store) {
+ streamFiles(exports.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);
+ exports.indexStore[pathname] = index;
+ streamFiles(index.files);
+ });
+ }
}
});
+ function streamFiles(files) {
+ util.mstat(pathname, files, function (e, stat) {
+ that.respond(pathname, 200, {}, files, stat, req, res, finish);
+ });
+ }
+};
+this.Server.prototype.serveFile = function (pathname, status, headers, req, res) {
+ var that = this;
+ var promise = new(events.EventEmitter);
+
+ pathname = this.normalize(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;
+};
+this.Server.prototype.finish = function (status, headers, req, res, promise, callback) {
+ var result = {
+ status: status,
+ headers: headers,
+ message: http.STATUS_CODES[status]
+ };
+
+ headers['Server'] = serverInfo;
+
+ if (!status || status >= 400) {
+ if (callback) {
+ callback(result);
+ } else {
+ if (promise.listeners('error').length > 0) {
+ promise.emit('error', result);
+ }
+ 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);
+ }
+};
+
+this.Server.prototype.servePath = function (pathname, status, headers, req, res, finish) {
+ var that = this,
+ promise = new(events.EventEmitter);
- function finish(status, headers) {
- var result = {
- status: status,
- headers: headers,
- message: http.STATUS_CODES[status]
- };
+ pathname = this.normalize(pathname);
- headers['Server'] = serverInfo;
+ // Only allow GET and HEAD requests
+ if (req.method !== 'GET' && req.method !== 'HEAD') {
+ finish(405, { 'Allow': 'GET, HEAD' });
+ return promise;
+ }
- if (status >= 400) {
- if (callback) {
- callback(result);
+ // Make sure we're not trying to access a
+ // file outside of the root.
+ if (new(RegExp)('^' + that.root).test(pathname)) {
+ 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.
+ that.serveDir(pathname, req, res, finish);
} else {
- if (promise.listeners('error').length > 0) {
- promise.emit('error', result);
- }
- 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();
+ finish(400, {});
}
- callback && callback(null, result);
- promise.emit('success', result);
- }
+ });
+ } else {
+ // Forbidden
+ finish(403, {});
}
+ return promise;
+};
+this.Server.prototype.normalize = function (pathname) {
+ return path.normalize(path.join(this.root, pathname));
};
this.Server.prototype.serve = function (req, res, callback) {
var that = this,
promise = new(events.EventEmitter);
- process.nextTick(function () {
- var file = url.parse(req.url).pathname;
+ var pathname = url.parse(req.url).pathname;
+
+ var finish = function (status, headers) {
+ that.finish(status, headers, req, res, promise, callback);
+ };
- that.serveFile(file, req, res, callback).addListener('success', function (result) {
+ process.nextTick(function () {
+ that.servePath(pathname, 200, {}, req, res, finish).on('success', function (result) {
promise.emit('success', result);
- }).addListener('error', function (result) {
- if (promise.listeners('error').length > 0) {
- promise.emit('error', result);
- }
+ }).on('error', function (err) {
+ promise.emit('error');
});
});
- return promise;
+ if (! callback) { return promise }
};
-this.Server.prototype.respond = function (pathname, files, stat, req, res, finish) {
+this.Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
var mtime = Date.parse(stat.mtime),
key = pathname || files[0],
headers = {};
// Copy default headers
- for (var k in this.options.headers) { headers[k] = this.options.headers[k] }
+ 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();
@@ -180,23 +200,25 @@ this.Server.prototype.respond = function (pathname, files, stat, req, res, finis
headers['Content-Type'] = mime.contentTypes[path.extname(files[0]).slice(1)] ||
'application/octet-stream';
- res.writeHead(200, headers);
+ for (var k in headers) { _headers[k] = headers[k] }
+
+ res.writeHead(status, _headers);
// If the file was cached and it's not older
// than what's on disk, serve the cached version.
if (this.cache && (key in exports.store) &&
exports.store[key].stat.mtime >= stat.mtime) {
res.end(exports.store[key].buffer);
- finish(200, {});
+ finish(status, _headers);
} else {
this.stream(pathname, files, new(buffer.Buffer)(stat.size), res, function (e, buffer) {
- if (e) { return finish(500, {}) }
+ if (e) { return finish(null, {}) }
exports.store[key] = {
stat: stat,
buffer: buffer,
timestamp: Date.now()
};
- finish(200, {});
+ finish(status, _headers);
});
}
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-static.git
More information about the Pkg-javascript-commits
mailing list