From 15657b14270db5afbc2e2f175ed92f3e131a1337 Mon Sep 17 00:00:00 2001 From: zedshaw Date: Fri, 10 Feb 2006 04:56:51 +0000 Subject: [PATCH] Added simple mime type mapping for files. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@26 19e92222-5c0b-0410-8929-a290d50e31e9 --- lib/mongrel.rb | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/mongrel.rb b/lib/mongrel.rb index 377256c4..f864ade7 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -321,7 +321,7 @@ module Mongrel @req_queue = Queue.new @host = host @port = port - @num_procesors = num_processors + @num_processors = num_processors @num_processors.times {|i| Thread.new do while client = @req_queue.deq @@ -443,6 +443,20 @@ module Mongrel # that the final expanded path includes the root path. If it doesn't # than it simply gives a 404. class DirHandler < HttpHandler + MIME_TYPES = { + ".css" => "text/css", + ".gif" => "image/gif", + ".htm" => "text/html", + ".html" => "text/html", + ".jpeg" => "image/jpeg", + ".jpg" => "image/jpeg", + ".js" => "text/javascript", + ".png" => "image/png", + ".swf" => "application/x-shockwave-flash", + ".txt" => "text/plain" + } + + attr_reader :path # You give it the path to the directory root and an (optional) @@ -514,6 +528,15 @@ module Mongrel # opening and closing the file for each read. def send_file(req, response) response.start(200) do |head,out| + # set the mime type from our map based on the ending + dot_at = req.rindex(".") + if dot_at + ext = req[dot_at .. -1] + if MIME_TYPES[ext] + head['Content-Type'] = MIME_TYPES[ext] + end + end + open(req, "r") do |f| out.write(f.read) end @@ -541,10 +564,17 @@ module Mongrel response.reset response.start(403) do |head,out| out << "Error accessing file: #{details}" + out << details.backtrace.join("\n") end end end end end + + # There is a small number of default mime types for extensions, but + # this lets you add any others you'll need when serving content. + def add_mime_type(extension, type) + MIME_TYPES[extension] = type + end end