1-- Set keep_alive. The return value specifies if this is possible at all. 2canKeepAlive = mg.keep_alive(true) 3 4if canKeepAlive then 5 -- Create the entire response in a string variable first. Content-Length will be set to the length of this string. 6 reply = [[ 7 <html><body> 8 <p>This is a Lua script supporting html keep-alive with the 9 <a href="https://github.com/civetweb/civetweb/">CivetWeb web server</a>. 10 </p> 11 <p>It works by setting the Content-Length header field properly. 12 </body></html> 13 ]] 14else 15 reply = "<html><body>Keep alive not possible!</body></html>" 16end 17 18-- First send the http headers 19mg.write("HTTP/1.1 200 OK\r\n") 20mg.write("Content-Type: text/html\r\n") 21mg.write("Date: " .. os.date("!%a, %d %b %Y %H:%M:%S") .. " GMT\r\n") 22mg.write("Cache-Control: no-cache\r\n") 23 24if canKeepAlive then 25 mg.write("Content-Length: " .. tostring(string.len(reply)) .. "\r\n") 26 mg.write("Connection: keep-alive\r\n") 27else 28 mg.write("Connection: close\r\n") 29end 30mg.write("\r\n") 31 32-- Finally send the content 33mg.write(reply) 34 35