From b84259d374b769efbec75ad47f1ea882bd782d5a Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sat, 28 Jun 2008 23:18:10 +0000 Subject: [PATCH] * libs/http: added support for directory listings --- libs/http/luasrc/http/protocol.lua | 1 + libs/httpd/luasrc/httpd/handler/file.lua | 74 +++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/libs/http/luasrc/http/protocol.lua b/libs/http/luasrc/http/protocol.lua index 67b4258578..8fb5e6f2f1 100644 --- a/libs/http/luasrc/http/protocol.lua +++ b/libs/http/luasrc/http/protocol.lua @@ -779,6 +779,7 @@ end -- Status codes statusmsg = { [200] = "OK", + [301] = "Moved Permanently", [304] = "Not Modified", [400] = "Bad Request", [403] = "Forbidden", diff --git a/libs/httpd/luasrc/httpd/handler/file.lua b/libs/httpd/luasrc/httpd/handler/file.lua index 2ca3786ee8..7f8e64093c 100644 --- a/libs/httpd/luasrc/httpd/handler/file.lua +++ b/libs/httpd/luasrc/httpd/handler/file.lua @@ -1,7 +1,8 @@ --[[ -HTTP server implementation for LuCI - luci handler +HTTP server implementation for LuCI - file handler (c) 2008 Steven Barth +(c) 2008 Freifunk Leipzig / Jo-Philipp Wich Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -83,6 +84,77 @@ function Simple.handle_get(self, request, sourcein, sinkerr) else return Response( code, hdrs or { } ) end + + elseif stat.type == "directory" then + + local ruri = request.request_uri:gsub("/$","") + local root = self.docroot:gsub("/$","") + + -- check for index files + local index_candidates = { + "index.html", "index.htm", "default.html", "default.htm", + "index.txt", "default.txt" + } + + -- try to find an index file and redirect to it + for i, candidate in ipairs( index_candidates ) do + local istat = luci.fs.stat( + root .. "/" .. ruri .. "/" .. candidate + ) + + if istat ~= nil and istat.type == "regular" then + return Response( 301, { + ["Location"] = ruri .. "/" .. candidate + } ), ltn12.source.empty() + end + end + + + local html = string.format( + '\n' .. + '\n' .. + '\n' .. + '\n' .. + 'Index of %s\n' .. + '

Index of %s


    ', + file, file + ) + + for i, e in luci.util.vspairs( luci.fs.dir( file ) ) do + + if e ~= '.' then + local estat = luci.fs.stat( file .. "/" .. e ) + + if estat.type == "directory" then + html = html .. string.format( + '
  • %s/ ' .. + '(directory)
    ' .. + 'Changed: %s

  • ', + ruri, e, e, + self.date.to_http( estat.mtime ) + ) + else + html = html .. string.format( + '
  • %s ' .. + '(%s)
    ' .. + 'Size: %i Bytes | Changed: %s

  • ', + ruri, e, e, self.mime.to_mime( e ), + estat.size, self.date.to_http( estat.mtime ) + ) + end + end + end + + html = html .. '

' + + return Response( + 200, { + ["Date"] = self.date.to_http( os.time() ); + ["Content-Type"] = "text/html"; + } + ), ltn12.source.string(html) else return self:failure(403, "Unable to transmit " .. stat.type .. " " .. file) end -- 2.30.2