File: page_keep_alive_chunked.lua

package info (click to toggle)
civetweb 1.16%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,576 kB
  • sloc: ansic: 32,463; cpp: 1,374; sh: 480; javascript: 204; makefile: 119; php: 11; perl: 6; python: 3
file content (66 lines) | stat: -rw-r--r-- 2,018 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Set keep_alive. The return value specifies if this is possible at all.
canKeepAlive = mg.keep_alive(true)
now = os.date("!%a, %d %b %Y %H:%M:%S")

-- First send the http headers
mg.write("HTTP/1.1 200 OK\r\n")
mg.write("Content-Type: text/html\r\n")
mg.write("Date: " .. now .. " GMT\r\n")
mg.write("Cache-Control: no-cache\r\n")
mg.write("Last-Modified: " .. now .. " GMT\r\n")
if not canKeepAlive then
    mg.write("Connection: close\r\n")
    mg.write("\r\n")
    mg.write("<html><body>Keep alive not possible!</body></html>")
    return
end
if mg.request_info.http_version ~= "1.1" then
    -- wget will use HTTP/1.0 and Connection: keep-alive, so chunked transfer is not possible
    mg.write("Connection: close\r\n")
    mg.write("\r\n")
    mg.write("<html><body>Chunked transfer is only possible for HTTP/1.1 requests!</body></html>")
    mg.keep_alive(false)
    return
end

-- use chunked encoding (http://www.jmarshall.com/easy/http/#http1.1c2)
mg.write("Cache-Control: max-age=0, must-revalidate\r\n")
--mg.write("Cache-Control: no-cache\r\n")
--mg.write("Cache-Control: no-store\r\n")
mg.write("Connection: keep-alive\r\n")
mg.write("Transfer-Encoding: chunked\r\n")
mg.write("\r\n")

-- function to send a chunk
function send(str)
    local len = string.len(str)
    mg.write(string.format("%x\r\n", len))
    mg.write(str.."\r\n")
end

-- send the chunks
send("<html>")
send("<head><title>Civetweb Lua script chunked transfer test page</title></head>")
send("<body>\n")

fileCnt = 0
if lfs then
    send("Files in " .. lfs.currentdir())
    send('\n<table border="1">\n')
    send('<tr><th>name</th><th>type</th><th>size</th></tr>\n')
    for f in lfs.dir(".") do
        local at = lfs.attributes(f);
        if at then
          send('<tr><td>' .. f .. '</td><td>' .. at.mode .. '</td><td>' .. at.size .. '</td></tr>\n')
        end
        fileCnt = fileCnt + 1
    end
    send("</table>\n")
end

send(fileCnt .. " entries (" .. now .. " GMT)\n")
send("</body>")
send("</html>")

-- end
send("")