Serves files from a directory
Allow custom handling of requests for files with suffix by
class handler
# File webrick/httpservlet/filehandler.rb, line 138
def self.add_handler(suffix, handler)
HandlerTable[suffix] = handler
end
Creates a FileHandler servlet on
server that serves files starting at directory
root
If options is a Hash the following keys are allowed:
Array of languages allowed for accept-language
Allows preprocessing of directory requests
If true, show an index for directories
Allows preprocessing of file requests
Allows preprocessing of requests
Maps file suffixes to file handlers. DefaultFileHandler is used by default but any servlet can be used.
Do not show files matching this array of globs
Directory inside ~user to serve content from for /~user requests. Only works if mounted on /
If options is true or false then :FancyIndexing
is enabled or disabled respectively.
# File webrick/httpservlet/filehandler.rb, line 170
def initialize(server, root, options={}, default=Config::FileHandler)
@config = server.config
@logger = @config[:Logger]
@root = File.expand_path(root)
if options == true || options == false
options = { :FancyIndexing => options }
end
@options = default.dup.update(options)
end
# File webrick/httpservlet/filehandler.rb, line 201
def do_GET(req, res)
unless exec_handler(req, res)
set_dir_list(req, res)
end
end
# File webrick/httpservlet/filehandler.rb, line 213
def do_OPTIONS(req, res)
unless exec_handler(req, res)
super(req, res)
end
end
# File webrick/httpservlet/filehandler.rb, line 207
def do_POST(req, res)
unless exec_handler(req, res)
raise HTTPStatus::NotFound, "`#{req.path}' not found."
end
end
# File webrick/httpservlet/filehandler.rb, line 180
def service(req, res)
# if this class is mounted on "/" and /~username is requested.
# we're going to override path informations before invoking service.
if defined?(Etc) && @options[:UserDir] && req.script_name.empty?
if %r^(/~([^/]+))| =~ req.path_info
script_name, user = $1, $2
path_info = $'
begin
passwd = Etc::getpwnam(user)
@root = File::join(passwd.dir, @options[:UserDir])
req.script_name = script_name
req.path_info = path_info
rescue
@logger.debug "#{self.class}#do_GET: getpwnam(#{user}) failed"
end
end
end
prevent_directory_traversal(req, res)
super(req, res)
end
Commenting is here to help enhance the documentation. For example, sample code, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.