page2.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. l = mg.request_info.http_headers["Content-Length"]
  47. if l then
  48. mg.write("<p>Content-Length = "..l..":<br>\n<pre>\n")
  49. mg.write(mg.read())
  50. mg.write("\n</pre>\n</p>\n")
  51. end
  52. mg.write("<p>\n");
  53. if lfs then
  54. mg.write("Files in " .. lfs.currentdir())
  55. mg.write("\n<ul>\n")
  56. for f in lfs.dir(".") do
  57. local mime = mg.get_mime_type(f)
  58. mg.write("<li>" .. f .. " (" .. mime .. ")</li>\n")
  59. local at = lfs.attributes(f);
  60. recurse(at)
  61. end
  62. mg.write("</ul>\n")
  63. end
  64. mg.write([[
  65. </p>
  66. </body></html>
  67. ]])