mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Added simple mime type mapping for files.
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@26 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
parent
edaaab5b63
commit
15657b1427
1 changed files with 31 additions and 1 deletions
|
@ -321,7 +321,7 @@ module Mongrel
|
||||||
@req_queue = Queue.new
|
@req_queue = Queue.new
|
||||||
@host = host
|
@host = host
|
||||||
@port = port
|
@port = port
|
||||||
@num_procesors = num_processors
|
@num_processors = num_processors
|
||||||
|
|
||||||
@num_processors.times {|i| Thread.new do
|
@num_processors.times {|i| Thread.new do
|
||||||
while client = @req_queue.deq
|
while client = @req_queue.deq
|
||||||
|
@ -443,6 +443,20 @@ module Mongrel
|
||||||
# that the final expanded path includes the root path. If it doesn't
|
# that the final expanded path includes the root path. If it doesn't
|
||||||
# than it simply gives a 404.
|
# than it simply gives a 404.
|
||||||
class DirHandler < HttpHandler
|
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
|
attr_reader :path
|
||||||
|
|
||||||
# You give it the path to the directory root and an (optional)
|
# 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.
|
# opening and closing the file for each read.
|
||||||
def send_file(req, response)
|
def send_file(req, response)
|
||||||
response.start(200) do |head,out|
|
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|
|
open(req, "r") do |f|
|
||||||
out.write(f.read)
|
out.write(f.read)
|
||||||
end
|
end
|
||||||
|
@ -541,10 +564,17 @@ module Mongrel
|
||||||
response.reset
|
response.reset
|
||||||
response.start(403) do |head,out|
|
response.start(403) do |head,out|
|
||||||
out << "Error accessing file: #{details}"
|
out << "Error accessing file: #{details}"
|
||||||
|
out << details.backtrace.join("\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue