page2.lua 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. -- Get all server options
  53. mg.write("<li>server options</li>\n")
  54. recurse(mg.get_option())
  55. mg.write("</ul></p>\n");
  56. mg.write("<p> Today is " .. os.date("%A") .. "</p>\n");
  57. l = mg.request_info.content_length
  58. if l then
  59. mg.write("<p>Content-Length = "..l..":<br>\n<pre>\n")
  60. mg.write(mg.read())
  61. mg.write("\n</pre>\n</p>\n")
  62. end
  63. mg.write("<p>\n");
  64. if lfs then
  65. mg.write("Files in " .. lfs.currentdir())
  66. mg.write("\n<ul>\n")
  67. for f in lfs.dir(".") do
  68. local mime = mg.get_mime_type(f)
  69. mg.write("<li>" .. f .. " (" .. mime .. ")</li>\n")
  70. local at = lfs.attributes(f);
  71. recurse(at)
  72. end
  73. mg.write("</ul>\n")
  74. end
  75. mg.write([[
  76. </p>
  77. </body></html>
  78. ]])