module Mongrel # :nodoc: all class DirHandler # This isn't proper class-inheritance-type overriding, instead we need # to literally overwrite Mongrel's method and replace it with our own. # Very graceful. But it works. alias _can_serve can_serve # This method is called by Mongrel::RailsHandler to see if mongrel can # get away with just serving up a file instead of invoking Rails. It # returns nil if it can't, and returns the filename if it can. def can_serve(file) # First let Mongrel decide if it can serve the file, then check to see # if we're controlling permissions on it next. if (file = _can_serve(file)) && FilePermissions.pattern.match(file) return nil else return file end end end end class FilePermissions # Look up the MIME type of a file, returns it as a string. Raises a # RuntimeError if it can't find it. Returns a string. Example: # mime = FilePermissions.mime_type("image.jpg") # # mime = "image/jpeg" def self.mime_type(file) raise(RuntimeError, "Don't know the MIME type of #{file}.")\ if !(match = file.match(/(\.\w+)$/)) || !(mime = Mongrel::DirHandler::MIME_TYPES[match[1]]) # (had to use Mongrel's list because Rails's list is useless) return mime end end