page_keep_alive.lua 1.0 KB

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