page2.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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="http://code.google.com/p/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)
  17. mg.write("<ul>\n")
  18. for k,v in pairs(tab) do
  19. if type(v) == "table" then
  20. mg.write("<li>" .. tostring(k) .. ":</li>\n")
  21. recurse(v)
  22. else
  23. mg.write("<li>" .. tostring(k) .. " = " .. tostring(v) .. "</li>\n")
  24. end
  25. end
  26. mg.write("</ul>\n")
  27. end
  28. -- Print Lua version and available libraries
  29. mg.write("<li>" .. _VERSION .. " with the following standard libraries</li>\n")
  30. mg.write("<ul>\n")
  31. libs = {"string", "math", "table", "io", "os", "bit32", "package", "coroutine", "debug"};
  32. for _,n in ipairs(libs) do
  33. print_if_available(_G[n], n);
  34. end
  35. mg.write("</ul>\n")
  36. print_if_available(sqlite3, "sqlite3 binding")
  37. print_if_available(lfs, "lua file system")
  38. -- Print mg library
  39. libname = "mg"
  40. print_if_available(_G[libname], libname .. " library")
  41. recurse(_G[libname])
  42. -- Print connect function
  43. print_if_available(connect, "connect function")
  44. mg.write("</ul></p>\n");
  45. mg.write("<p> Today is " .. os.date("%A") .. "</p>\n");
  46. mg.write("<p>\n");
  47. if lfs then
  48. mg.write("Files in " .. lfs.currentdir())
  49. mg.write("\n<ul>\n")
  50. for f in lfs.dir(".") do
  51. local mime = mg.get_mime_type(f)
  52. mg.write("<li>" .. f .. " (" .. mime .. ")</li>\n")
  53. local at = lfs.attributes(f);
  54. recurse(at)
  55. end
  56. mg.write("</ul>\n")
  57. end
  58. mg.write([[
  59. </p>
  60. </body></html>
  61. ]])