file_list.lua 907 B

123456789101112131415161718192021
  1. return function (connection, args)
  2. connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
  3. connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>')
  4. connection:send('<body>')
  5. connection:send('<h1>Server File Listing</h1>')
  6. connection:send("<ul>\n")
  7. for name, size in pairs(file.list()) do
  8. local isHttpFile = string.match(name, "(http/)") ~= nil
  9. if isHttpFile then
  10. local url = string.match(name, ".*/(.*)")
  11. connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
  12. -- this list could be very long, so we'll yield in order to avoid overflowing the send buffer.
  13. coroutine.yield()
  14. end
  15. end
  16. connection:send("</ul>\n")
  17. connection:send('</body></html>')
  18. end