Class: QuickServe::Handlers::Directory

Inherits:
Mongrel::DirHandler
  • Object
show all
Defined in:
lib/quick_serve/handlers/directory.rb

Constant Summary collapse

STYLESHEET =
"html, body {\n  font-family: \"Lucida Grande\", Verdana, sans-serif;\n  font-size: 90%;\n  font-weight: normal;\n  line-height: auto;\n}\nhtml {\n  background-color: #F0F0F0;\n}\n#body {\n  -moz-border-radius-bottomleft:10px;\n  -moz-border-radius-bottomright:10px;\n  -moz-border-radius-topleft:10px;\n  -moz-border-radius-topright:10px;\n  background-color: #fff;\n  border:1px solid #E1E1E1;\n  color:-moz-fieldtext;\n  width: 70%;\n  margin:4em auto;\n  padding:3em;\n}\nh1 {\n  font-size: 130%;\n  border-bottom: 1px solid #999;\n  padding: 3px;\n}\na {\n  color: #666;\n  text-decoration: none\n}\na:hover { color: #000 }\nh3 { \n  font-size: 115%;\n  margin-bottom: 10px; \n}\n"

Instance Method Summary collapse

Instance Method Details

#send_dir_listing(base, dir, response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/quick_serve/handlers/directory.rb', line 43

def send_dir_listing(base, dir, response)
  # take off any trailing / so the links come out right
  base = Mongrel::HttpRequest.unescape(base)
  base.chop! if base[-1] == "/"[-1]

  if @listing_allowed
    response.start(200) do |head,out|
      head[Mongrel::Const::CONTENT_TYPE] = "text/html"
      
      out << "<html><head><title>Directory Listing for #{dir}</title><style type=\"text/css\">#{STYLESHEET}</style></head><body><div id=\"body\"><h1>Directory Listing for #{dir}</h1>"
      entries = Dir.entries(dir)
      entries = entries - ['..']
      out << "<h3><a href=\"#{base}/..\">Up to higher level directory</a></h3>"
      entries.each do |child|
        next if child == "."
        out << "<a href=\"#{base}/#{ Mongrel::HttpRequest.escape(child)}\">"
        out << child
        out << "</a><br/>"
      end
      out << "</div></body></html>"
    end
  else
    response.start(403) do |head,out|
      out.write("Directory listings not allowed")
    end
  end
end