1mg.write("HTTP/1.0 200 OK\r\n") 2mg.write("Content-Type: text/html\r\n") 3mg.write("\r\n") 4mg.write([[<html><body> 5<p>This is Lua script example 2, served by the 6<a href="https://github.com/civetweb/civetweb">CivetWeb web server</a>, 7version ]] .. mg.version .. [[. 8</p><p> 9The following features are available: 10<ul> 11]]) 12 13function print_if_available(tab, name) 14 if tab then 15 mg.write("<li>" .. name .. " available</li>\n") 16 else 17 mg.write("<li>" .. name .. " not available</li>\n") 18 end 19end 20 21function recurse(tab, excl) 22 excl = excl or {} 23 mg.write("<ul>\n") 24 for k,v in pairs(tab) do 25 if type(v) == "table" then 26 mg.write("<li>" .. tostring(k) .. ":</li>\n") 27 if excl[v] then 28 -- cyclic 29 else 30 excl[v] = true 31 recurse(v, excl) 32 excl[v] = false 33 end 34 else 35 mg.write("<li>" .. tostring(k) .. " = " .. tostring(v) .. "</li>\n") 36 end 37 end 38 mg.write("</ul>\n") 39end 40 41-- Print Lua version and available libraries 42mg.write("<li>" .. _VERSION .. " with the following standard libraries</li>\n") 43mg.write("<ul>\n") 44libs = {"string", "math", "table", "io", "os", "bit32", "utf8", "package", "coroutine", "debug"}; 45for _,n in ipairs(libs) do 46 print_if_available(_G[n], n); 47end 48mg.write("</ul>\n") 49print_if_available(sqlite3, "sqlite3 binding") 50print_if_available(lfs, "lua file system") 51 52--recurse(_G) 53 54-- Print mg library 55libname = "mg" 56print_if_available(_G[libname], libname .. " library") 57recurse(_G[libname]) 58 59-- Print connect function 60print_if_available(connect, "connect function") 61 62-- Get all server options 63mg.write("<li>server options</li>\n") 64recurse(mg.get_option()) 65 66-- Print some data from random generators 67if mg.uuid then 68 mg.write("<li>random data</li>\n") 69 local t = {GUID = mg.uuid(), random = mg.random()} 70 recurse(t) 71end 72 73-- Print loaded packages known to Lua 74if package and (type(package.loaded)=="table") then 75 if #package.loaded > 0 then 76 mg.write("<li>loaded packages</li>\n") 77 recurse(package.loaded) 78 else 79 mg.write("<li>loaded packages: none</li>\n") 80 end 81end 82 83-- Print preloaded packages known to Lua 84if xml then 85 mg.write("<li>xml</li>\n") 86 recurse(xml) 87end 88 89-- Current date/time 90mg.write("</ul></p>\n"); 91mg.write("<p> Today is " .. os.date("%A") .. "</p>\n"); 92 93-- Request content 94mg.write("\n<hr/>\n") 95l = mg.request_info.content_length 96if l then 97 mg.write("<p>Content-Length = "..l..":<br>\n<pre>\n") 98 mg.write(mg.read()) 99 mg.write("\n</pre>\n</p>\n") 100else 101 mg.write("<p>not request content available</p>\n") 102end 103 104-- Directory listing 105mg.write("\n<hr/>\n") 106mg.write("<p>\n"); 107if not lfs then 108 mg.write("lfs not available\n") 109else 110 mg.write("Files in " .. lfs.currentdir()) 111 mg.write("\n<ul>\n") 112 local cnt = 0 113 for f in lfs.dir(".") do 114 cnt = cnt + 1 115 if (cnt < 6) then 116 local mime = mg.get_mime_type(f) 117 mg.write("<li>" .. f .. " (" .. mime .. ")</li>\n") 118 local at = lfs.attributes(f); 119 recurse(at) 120 end 121 end 122 mg.write("</ul>\n") 123 mg.write(string.format("<ul>%u files total</ul>\n", cnt)) 124end 125 126mg.write([[ 127</p> 128</body></html> 129]]) 130 131