Class: Thin::DirHandler
Overview
Serve a directory from a URI.
Instance Method Summary collapse
-
#initialize(pwd) ⇒ DirHandler
constructor
A new instance of DirHandler.
- #process(request, response) ⇒ Object
- #serve_dir(base, path, response) ⇒ Object
- #serve_file(path, response) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Handler
Constructor Details
#initialize(pwd) ⇒ DirHandler
Returns a new instance of DirHandler.
21 22 23 |
# File 'lib/thin/handler.rb', line 21 def initialize(pwd) @pwd = pwd.dup end |
Instance Method Details
#process(request, response) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/thin/handler.rb', line 25 def process(request, response) path = File.join(@pwd, request.path) if File.directory?(path) serve_dir request.path, path, response return true elsif File.file?(path) serve_file path, response return true end false end |
#serve_dir(base, path, response) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/thin/handler.rb', line 37 def serve_dir(base, path, response) response.content_type = 'text/html' response.body << '<html><head><title>Dir listing</title></head>' response.body << "<body><h1>Listing #{base}</h1><ul>" Dir.entries(path).each do |entry| next if entry == '.' response.body << %Q{<li><a href="#{File.join(base, entry)}">#{entry}</a></li>} end response.body << '</ul></body></html>' end |
#serve_file(path, response) ⇒ Object
48 49 50 51 |
# File 'lib/thin/handler.rb', line 48 def serve_file(path, response) response.content_type = MIME_TYPES[File.extname(path)] || "application/octet-stream".freeze File.open(path, "rb") { |f| response.body << f.read } end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/thin/handler.rb', line 53 def to_s "dir #{@pwd}" end |