]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/test/page_keep_alive_chunked.lua
buildsys: switch source download to quincy
[ceph.git] / ceph / src / civetweb / test / page_keep_alive_chunked.lua
1 -- Set keep_alive. The return value specifies if this is possible at all.
2 canKeepAlive = mg.keep_alive(true)
3 now = os.date("!%a, %d %b %Y %H:%M:%S")
4
5 -- First send the http headers
6 mg.write("HTTP/1.1 200 OK\r\n")
7 mg.write("Content-Type: text/html\r\n")
8 mg.write("Date: " .. now .. " GMT\r\n")
9 mg.write("Cache-Control: no-cache\r\n")
10 mg.write("Last-Modified: " .. now .. " GMT\r\n")
11 if not canKeepAlive then
12 mg.write("Connection: close\r\n")
13 mg.write("\r\n")
14 mg.write("<html><body>Keep alive not possible!</body></html>")
15 return
16 end
17 if mg.request_info.http_version ~= "1.1" then
18 -- wget will use HTTP/1.0 and Connection: keep-alive, so chunked transfer is not possible
19 mg.write("Connection: close\r\n")
20 mg.write("\r\n")
21 mg.write("<html><body>Chunked transfer is only possible for HTTP/1.1 requests!</body></html>")
22 mg.keep_alive(false)
23 return
24 end
25
26 -- use chunked encoding (http://www.jmarshall.com/easy/http/#http1.1c2)
27 mg.write("Cache-Control: max-age=0, must-revalidate\r\n")
28 --mg.write("Cache-Control: no-cache\r\n")
29 --mg.write("Cache-Control: no-store\r\n")
30 mg.write("Connection: keep-alive\r\n")
31 mg.write("Transfer-Encoding: chunked\r\n")
32 mg.write("\r\n")
33
34 -- function to send a chunk
35 function send(str)
36 local len = string.len(str)
37 mg.write(string.format("%x\r\n", len))
38 mg.write(str.."\r\n")
39 end
40
41 -- send the chunks
42 send("<html>")
43 send("<head><title>Civetweb Lua script chunked transfer test page</title></head>")
44 send("<body>\n")
45
46 fileCnt = 0
47 if lfs then
48 send("Files in " .. lfs.currentdir())
49 send('\n<table border="1">\n')
50 send('<tr><th>name</th><th>type</th><th>size</th></tr>\n')
51 for f in lfs.dir(".") do
52 local at = lfs.attributes(f);
53 if at then
54 send('<tr><td>' .. f .. '</td><td>' .. at.mode .. '</td><td>' .. at.size .. '</td></tr>\n')
55 end
56 fileCnt = fileCnt + 1
57 end
58 send("</table>\n")
59 end
60
61 send(fileCnt .. " entries (" .. now .. " GMT)\n")
62 send("</body>")
63 send("</html>")
64
65 -- end
66 send("")