page_keep_alive.lua 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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
  8. <a href="https://github.com/civetweb/civetweb/">CivetWeb web server</a>.
  9. </p>
  10. <p>It works by setting the Content-Length header field properly.
  11. </body></html>
  12. ]]
  13. else
  14. reply = "<html><body>Keep alive not possible!</body></html>"
  15. end
  16. -- First send the http headers
  17. mg.write("HTTP/1.1 200 OK\r\n")
  18. mg.write("Content-Type: text/html\r\n")
  19. mg.write("Date: " .. os.date("!%a, %d %b %Y %H:%M:%S") .. " GMT\r\n")
  20. mg.write("Cache-Control: no-cache\r\n")
  21. if canKeepAlive then
  22. mg.write("Content-Length: " .. tostring(string.len(reply)) .. "\r\n")
  23. mg.write("Connection: keep-alive\r\n")
  24. else
  25. mg.write("Connection: close\r\n")
  26. end
  27. mg.write("\r\n")
  28. -- Finally send the content
  29. mg.write(reply)