diff options
author | zedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9> | 2006-04-08 18:00:35 +0000 |
---|---|---|
committer | zedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9> | 2006-04-08 18:00:35 +0000 |
commit | d95ed2690eaad1624a3797ee263ffc5b99819873 (patch) | |
tree | 8670db1e6dec4585f66638b07da7f94a1ef3bcee /lib/mongrel | |
parent | 35940eb82f13da239ef65e89b11e803498097c8c (diff) | |
download | unicorn-d95ed2690eaad1624a3797ee263ffc5b99819873.tar.gz |
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@151 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'lib/mongrel')
-rw-r--r-- | lib/mongrel/debug.rb | 2 | ||||
-rw-r--r-- | lib/mongrel/handlers.rb | 40 |
2 files changed, 27 insertions, 15 deletions
diff --git a/lib/mongrel/debug.rb b/lib/mongrel/debug.rb index 840182f..3e3cdbd 100644 --- a/lib/mongrel/debug.rb +++ b/lib/mongrel/debug.rb @@ -129,7 +129,7 @@ module RequestLog def process(request,response) p = request.params - STDERR.puts "#{p['REMOTE_ADDR']} - [#{Mongrel::HttpServer.httpdate(Time.now)}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"" + STDERR.puts "#{p['REMOTE_ADDR']} - [#{Time.now.httpdate}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"" end end diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb index 6bc2a70..b060329 100644 --- a/lib/mongrel/handlers.rb +++ b/lib/mongrel/handlers.rb @@ -70,7 +70,15 @@ module Mongrel # converting all paths to an absolute expanded path, and then making sure # that the final expanded path includes the root path. If it doesn't # than it simply gives a 404. + # + # The default content type is "text/plain; charset=ISO-8859-1" but you + # can change it anything you want using the DirHandler.default_content_type + # attribute. class DirHandler < HttpHandler + attr_reader :default_content_type + attr_writer :default_content_type + attr_reader :path + MIME_TYPES = { ".css" => "text/css", ".gif" => "image/gif", @@ -86,13 +94,12 @@ module Mongrel ONLY_HEAD_GET="Only HEAD and GET allowed.".freeze - attr_reader :path - # You give it the path to the directory root and an (optional) def initialize(path, listing_allowed=true, index_html="index.html") @path = File.expand_path(path) @listing_allowed=listing_allowed @index_html = index_html + @default_content_type = "text/plain; charset=ISO-8859-1".freeze end # Checks if the given path can be served and returns the full path (or nil if not). @@ -162,22 +169,26 @@ module Mongrel # first we setup the headers and status then we do a very fast send on the socket directly response.status = 200 + stat = File.stat(req) + header = response.header + + # Set the last modified times as well and etag for all files + header[Const::LAST_MODIFIED] = stat.mtime.httpdate + # Calculated the same as apache, not sure how well the works on win32 + header[Const::ETAG] = Const::ETAG_FORMAT % [stat.mtime.to_i, stat.size, stat.ino] # 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] - stat = File.stat(req) - response.header[Const::CONTENT_TYPE] = MIME_TYPES[ext] || "text" - # TODO: Confirm this works for rfc 1123 - response.header[Const::LAST_MODIFIED] = HttpServer.httpdate(stat.mtime) - # TODO that this is a valid way to calculate an etag - response.header[Const::ETAG] = Const::ETAG_FORMAT % [stat.mtime.to_i, stat.size, stat.ino] - end + if dot_at and MIME_TYPES.has_key? req[dot_at .. -1] + header[Const::CONTENT_TYPE] = MIME_TYPES[ext] || @default_content_type + # TODO: Confirm this works for rfc 1123 + else + header[Const::CONTENT_TYPE] = @default_content_type end - response.send_status(File.size(req)) + + # send a status with out content length + response.send_status(stat.size) response.send_header if not header_only @@ -187,8 +198,9 @@ module Mongrel else File.open(req, "rb") { |f| response.socket.write(f.read) } end - rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL + rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF # ignore these since it means the client closed off early + STDERR.puts "Client closed socket requesting file #{req}: #$!" end else response.send_body # should send nothing |