From b41bc98c54502cea070a8ca11b5d267e02f30701 Mon Sep 17 00:00:00 2001 From: Elliott Sprehn Date: Thu, 21 Oct 2010 11:26:34 +0800 Subject: Better nodeserver that implements an HTTP server more completely --- lib/nodeserver/server.js | 184 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 167 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/nodeserver/server.js b/lib/nodeserver/server.js index 13eb2826..f05185fa 100644 --- a/lib/nodeserver/server.js +++ b/lib/nodeserver/server.js @@ -1,22 +1,172 @@ var sys = require('sys'), http = require('http'), - fs = require('fs'); - -http.createServer(function (req, res) { - res.writeHead(200, {}); - sys.p('GET ' + req.url); - var file = fs.createReadStream('.' + req.url); - file.addListener('data', bind(res, res.write)); - file.addListener('error', function( error ){ - sys.p(error); + fs = require('fs'), + url = require('url'), + events = require('events'); + +function main() { + new HttpServer({ + 'GET': (function() { + var servlet = new StaticServlet(); + return servlet.handleRequest.bind(servlet) + })() + }).start(8000); +} + +function escapeHtml(value) { + return value.toString(). + replace('<', '<'). + replace('>', '>'). + replace('"', '"'); +} + +/** + * An Http server implementation that uses a map of methods to decide + * action routing. + * + * @param {Object} Map of method => Handler function + */ +function HttpServer(handlers) { + this.handlers = handlers; + this.server = http.createServer(this.handleRequest_.bind(this)); +} + +HttpServer.prototype.start = function(port) { + this.port = port; + this.server.listen(port); + sys.puts('Http Server running at http://127.0.0.1:' + port + '/'); +}; + +HttpServer.prototype.parseUrl_ = function(urlString) { + var parsed = url.parse(urlString); + parsed.pathname = url.resolve('/', parsed.pathname); + return url.parse(url.format(parsed), true); +}; + +HttpServer.prototype.handleRequest_ = function(req, res) { + sys.puts(req.method + ' ' + req.url); + req.url = this.parseUrl_(req.url); + var handler = this.handlers[req.method]; + if (!handler) { + res.writeHead(501, 'Not Implemented'); res.end(); - }); - file.addListener('close', bind(res, res.end)); -}).listen(8000); -sys.puts('Server running at http://127.0.0.1:8000/'); + } else { + handler.call(this, req, res); + } +}; + + +/** + * Handles static content. + */ +function StaticServlet() {} -function bind(_this, _fn) { - return function(){ - return _fn.apply(_this, arguments); - }; +StaticServlet.MimeMap = { + 'txt': 'text/plain', + 'html': 'text/html', + 'css': 'text/css', + 'xml': 'application/xml', + 'json': 'application/json', + 'js': 'application/javascript', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'gif': 'image/gif', + 'png': 'image/png' +}; + +StaticServlet.prototype.handleRequest = function(req, res) { + var self = this; + var path = ('./' + req.url.pathname).replace('//','/'); + var parts = path.split('/'); + if (parts[parts.length-1].charAt(0) === '.') + return self.sendForbidden_(res, path); + fs.stat(path, function(err, stat) { + if (err) + return self.sendMissing_(res, path); + if (stat.isDirectory()) + return self.sendDirectory_(res, path); + return self.sendFile_(res, path); + }); } + +StaticServlet.prototype.sendError_ = function(res, error) { + res.writeHead(500, 'Internal Server Error', { + 'Content-Type': 'text/html' + }); + res.write('\n'); + res.write('
' + escapeHtml(sys.inspect(error)) + ''); + sys.puts('500 Internal Server Error'); + sys.puts(sys.inspect(error)); +}; + +StaticServlet.prototype.sendMissing_ = function(res, path) { + res.writeHead(404, 'Not Found', { + 'Content-Type': 'text/html' + }); + res.write('\n'); + res.write('