Class | Mongrel::DirHandler
In: | lib/mongrel/handlers.rb
Parent: | HttpHandler

Serves the contents of a directory. You give it the path to the root where the files are located, and it tries to find the files based on the PATH_INFO inside the directory. If the requested path is a directory then it returns a simple directory listing.

It does a simple protection against going outside it‘s root path by 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.

If you pass nil as the root path, it will not check any locations or expand any paths. This lets you serve files from multiple drives on win32. It should probably not be used in a public-facing way without additional checks.

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.

### Methods

add_mime_type can_serve new process send_dir_listing send_file

### Constants

MIME_TYPES_FILE | = | "mime_types.yml"
MIME_TYPES | = | YAML.load_file(File.join(File.dirname(__FILE__), MIME_TYPES_FILE))
ONLY_HEAD_GET | = | "Only HEAD and GET allowed.".freeze

### Attributes

default_content_type | [RW] |
path | [R] |

### Public Class methods

add_mime_type(extension, type)

There is a small number of default mime types for extensions, but this lets you add any others you‘ll need when serving content.

new(path, listing_allowed=true, index_html="index.html")

You give it the path to the directory root and and optional listing_allowed and index_html

### Public Instance methods

can_serve(path_info)

Checks if the given path can be served and returns the full path (or nil if not).

process(request, response)

Process the request to either serve a file or a directory listing if allowed (based on the listing_allowed parameter to the constructor).

send_dir_listing(base, dir, response)

Returns a simplistic directory listing if they‘re enabled, otherwise a 403. Base is the base URI from the REQUEST_URI, dir is the directory to serve on the file system (comes from can_serve()), and response is the HttpResponse object to send the results on.

send_file(req_path, request, response, header_only=false)

Sends the contents of a file back to the user. Not terribly efficient since it‘s opening and closing the file for each read.

[Validate]