3 var util = require('util'),
4 http = require('http'),
7 events = require('events');
9 var DEFAULT_PORT = 8000;
13 'GET': createServlet(StaticServlet),
14 'HEAD': createServlet(StaticServlet)
15 }).start(Number(argv[2]) || DEFAULT_PORT);
18 function escapeHtml(value) {
19 return value.toString().
22 replace('"', '"');
25 function createServlet(Class) {
26 var servlet = new Class();
27 return servlet.handleRequest.bind(servlet);
31 * An Http server implementation that uses a map of methods to decide
34 * @param {Object} Map of method => Handler function
36 function HttpServer(handlers) {
37 this.handlers = handlers;
38 this.server = http.createServer(this.handleRequest_.bind(this));
41 HttpServer.prototype.start = function(port) {
43 this.server.listen(port);
44 util.puts('Http Server running at http://localhost:' + port + '/');
47 HttpServer.prototype.parseUrl_ = function(urlString) {
48 var parsed = url.parse(urlString);
49 parsed.pathname = url.resolve('/', parsed.pathname);
50 return url.parse(url.format(parsed), true);
53 HttpServer.prototype.handleRequest_ = function(req, res) {
54 var logEntry = req.method + ' ' + req.url;
55 if (req.headers['user-agent']) {
56 logEntry += ' ' + req.headers['user-agent'];
59 req.url = this.parseUrl_(req.url);
60 var handler = this.handlers[req.method];
65 handler.call(this, req, res);
70 * Handles static content.
72 function StaticServlet() {}
74 StaticServlet.MimeMap = {
78 'xml': 'application/xml',
79 'json': 'application/json',
80 'js': 'application/javascript',
85 'svg': 'image/svg+xml'
88 StaticServlet.prototype.handleRequest = function(req, res) {
90 var path = ('./' + req.url.pathname).replace('//','/').replace(/%(..)/g, function(match, hex){
91 return String.fromCharCode(parseInt(hex, 16));
93 var parts = path.split('/');
94 if (parts[parts.length-1].charAt(0) === '.')
95 return self.sendForbidden_(req, res, path);
96 fs.stat(path, function(err, stat) {
98 return self.sendMissing_(req, res, path);
99 if (stat.isDirectory())
100 return self.sendDirectory_(req, res, path);
101 return self.sendFile_(req, res, path);
105 StaticServlet.prototype.sendError_ = function(req, res, error) {
107 'Content-Type': 'text/html'
109 res.write('<!doctype html>\n');
110 res.write('<title>Internal Server Error</title>\n');
111 res.write('<h1>Internal Server Error</h1>');
112 res.write('<pre>' + escapeHtml(util.inspect(error)) + '</pre>');
113 util.puts('500 Internal Server Error');
114 util.puts(util.inspect(error));
117 StaticServlet.prototype.sendMissing_ = function(req, res, path) {
118 path = path.substring(1);
120 'Content-Type': 'text/html'
122 res.write('<!doctype html>\n');
123 res.write('<title>404 Not Found</title>\n');
124 res.write('<h1>Not Found</h1>');
126 '<p>The requested URL ' +
128 ' was not found on this server.</p>'
131 util.puts('404 Not Found: ' + path);
134 StaticServlet.prototype.sendForbidden_ = function(req, res, path) {
135 path = path.substring(1);
137 'Content-Type': 'text/html'
139 res.write('<!doctype html>\n');
140 res.write('<title>403 Forbidden</title>\n');
141 res.write('<h1>Forbidden</h1>');
143 '<p>You do not have permission to access ' +
144 escapeHtml(path) + ' on this server.</p>'
147 util.puts('403 Forbidden: ' + path);
150 StaticServlet.prototype.sendRedirect_ = function(req, res, redirectUrl) {
152 'Content-Type': 'text/html',
153 'Location': redirectUrl
155 res.write('<!doctype html>\n');
156 res.write('<title>301 Moved Permanently</title>\n');
157 res.write('<h1>Moved Permanently</h1>');
159 '<p>The document has moved <a href="' +
164 util.puts('301 Moved Permanently: ' + redirectUrl);
167 StaticServlet.prototype.sendFile_ = function(req, res, path) {
169 var file = fs.createReadStream(path);
171 'Content-Type': StaticServlet.
172 MimeMap[path.split('.').pop()] || 'text/plain'
174 if (req.method === 'HEAD') {
177 file.on('data', res.write.bind(res));
178 file.on('close', function() {
181 file.on('error', function(error) {
182 self.sendError_(req, res, error);
187 StaticServlet.prototype.sendDirectory_ = function(req, res, path) {
189 if (path.match(/[^\/]$/)) {
190 req.url.pathname += '/';
191 var redirectUrl = url.format(url.parse(url.format(req.url)));
192 return self.sendRedirect_(req, res, redirectUrl);
194 fs.readdir(path, function(err, files) {
196 return self.sendError_(req, res, error);
199 return self.writeDirectoryIndex_(req, res, path, []);
201 var remaining = files.length;
202 files.forEach(function(fileName, index) {
203 fs.stat(path + '/' + fileName, function(err, stat) {
205 return self.sendError_(req, res, err);
206 if (stat.isDirectory()) {
207 files[index] = fileName + '/';
210 return self.writeDirectoryIndex_(req, res, path, files);
216 StaticServlet.prototype.writeDirectoryIndex_ = function(req, res, path, files) {
217 path = path.substring(1);
219 'Content-Type': 'text/html'
221 if (req.method === 'HEAD') {
225 res.write('<!doctype html>\n');
226 res.write('<title>' + escapeHtml(path) + '</title>\n');
227 res.write('<style>\n');
228 res.write(' ol { list-style-type: none; font-size: 1.2em; }\n');
229 res.write('</style>\n');
230 res.write('<h1>Directory: ' + escapeHtml(path) + '</h1>');
232 files.forEach(function(fileName) {
233 if (fileName.charAt(0) !== '.') {
234 res.write('<li><a href="' +
235 escapeHtml(fileName) + '">' +
236 escapeHtml(fileName) + '</a></li>');