extras/autoindex.rb in yahns-1.3.1 vs extras/autoindex.rb in yahns-1.4.0
- old
+ new
@@ -11,16 +11,17 @@
# all bikeshedding here :>
class Autoindex
FN = %{<a href="%s">%s</a>}
TFMT = "%Y-%m-%d %H:%M"
- def initialize(app, index = %w(index.html))
+ def initialize(app, index = %w(index.html), skip_gzip_static = true)
app.respond_to?(:root) or raise ArgumentError,
"wrapped app #{app.inspect} does not respond to :root"
@app = app
@root = app.root
@index = index
+ @skip_gz = skip_gzip_static
end
def redirect_slash(env)
req = Rack::Request.new(env)
location = "#{req.url}/"
@@ -65,14 +66,17 @@
end
# generate the index, show directories first
dirs = []
files = []
+ ngz_idx = {} if @skip_gz # used to avoid redundant stat()
dir.each do |base|
case base
when "."
next
+ when ".."
+ next if path_info == "/"
end
begin
st = File.stat("#{pfx}#{base}")
rescue
@@ -88,14 +92,28 @@
entry = sprintf(FN, url, name)
pad = 52 - name.size
entry << (" " * pad) if pad > 0
entry << st.mtime.strftime(TFMT)
entry << sprintf("% 8s", human_size(st))
+ entry = [name, entry]
- (st.directory? ? dirs : files) << [ name, entry ]
+ if st.directory?
+ dirs << entry
+ elsif ngz_idx
+ ngz_idx[name] = entry
+ else
+ files << entry
+ end
end
+ if ngz_idx
+ ngz_idx.each do |name, entry|
+ ngz_path = name.dup.gsub!(/\.gz\z/, '')
+ ngz_idx.include?(ngz_path) or files << entry
+ end
+ end
+
dirs.sort! { |(a,_),(b)| a <=> b }.map! { |(_,ent)| ent }
files.sort! { |(a,_),(b)| a <=> b }.map! { |(_,ent)| ent }
path_info_html = path_info_ue.split(%r{/}, -1).map! do |part|
Rack::Utils.escape_html(part)
@@ -115,10 +133,10 @@
r(500, e, env)
ensure
dir.close if dir
end
- def r(code, msg = nil, env = nil)
+ def r(code, exc = nil, env = nil)
if env && exc && logger = env["rack.logger"]
msg = exc.message
msg = msg.dump if /[[:cntrl:]]/ =~ msg # prevent code injection
logger.warn("#{env['REQUEST_METHOD']} #{env['PATH_INFO']} " \
"#{code} #{msg}")