page2.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. mg.write("HTTP/1.0 200 OK\r\n")
  2. mg.write("Content-Type: text/html\r\n")
  3. mg.write("\r\n")
  4. mg.write([[<html><body>
  5. <p>This is another example of a Lua server page, served by
  6. <a href="https://github.com/civetweb/civetweb">CivetWeb web server</a>.
  7. </p><p>
  8. The following features are available:
  9. <ul>
  10. ]])
  11. function print_if_available(tab, name)
  12. if tab then
  13. mg.write("<li>" .. name .. "</li>\n")
  14. end
  15. end
  16. function recurse(tab, excl)
  17. excl = excl or {}
  18. mg.write("<ul>\n")
  19. for k,v in pairs(tab) do
  20. if type(v) == "table" then
  21. mg.write("<li>" .. tostring(k) .. ":</li>\n")
  22. if excl[v] then
  23. -- cyclic
  24. else
  25. excl[v] = true
  26. recurse(v, excl)
  27. excl[v] = false
  28. end
  29. else
  30. mg.write("<li>" .. tostring(k) .. " = " .. tostring(v) .. "</li>\n")
  31. end
  32. end
  33. mg.write("</ul>\n")
  34. end
  35. -- Print Lua version and available libraries
  36. mg.write("<li>" .. _VERSION .. " with the following standard libraries</li>\n")
  37. mg.write("<ul>\n")
  38. libs = {"string", "math", "table", "io", "os", "bit32", "utf8", "package", "coroutine", "debug"};
  39. for _,n in ipairs(libs) do
  40. print_if_available(_G[n], n);
  41. end
  42. mg.write("</ul>\n")
  43. print_if_available(sqlite3, "sqlite3 binding")
  44. print_if_available(lfs, "lua file system")
  45. --recurse(_G)
  46. -- Print mg library
  47. libname = "mg"
  48. print_if_available(_G[libname], libname .. " library")
  49. recurse(_G[libname])
  50. -- Print connect function
  51. print_if_available(connect, "connect function")
  52. mg.write("</ul></p>\n");
  53. mg.write("<p> Today is " .. os.date("%A") .. "</p>\n");
  54. l = mg.request_info.content_length
  55. if l then
  56. mg.write("<p>Content-Length = "..l..":<br>\n<pre>\n")
  57. mg.write(mg.read())
  58. mg.write("\n</pre>\n</p>\n")
  59. end
  60. mg.write("<p>\n");
  61. if lfs then
  62. mg.write("Files in " .. lfs.currentdir())
  63. mg.write("\n<ul>\n")
  64. for f in lfs.dir(".") do
  65. local mime = mg.get_mime_type(f)
  66. mg.write("<li>" .. f .. " (" .. mime .. ")</li>\n")
  67. local at = lfs.attributes(f);
  68. recurse(at)
  69. end
  70. mg.write("</ul>\n")
  71. end
  72. mg.write([[
  73. </p>
  74. </body></html>
  75. ]])