1-- This is a Lua script that handles sub-resources, e.g. resource_script_demo.lua/path/file.ext
2
3scriptUri = "resource_script_demo.lua"
4envVar = "resource_script_demo_storage"
5
6resourcedir = os.getenv(envVar) or "R:\\RESOURCEDIR"
7method = mg.request_info.request_method:upper()
8
9if resourcedir then
10  attr = lfs.attributes(resourcedir)
11end
12
13if (not mg.request_info.uri:find(scriptUri)) or (not resourcedir) or (not attr) or (attr.mode~="directory") then
14    mg.write("HTTP/1.0 500 OK\r\n")
15    mg.write("Connection: close\r\n")
16    mg.write("Content-Type: text/html; charset=utf-8\r\n")
17    mg.write("\r\n")
18    mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
19    mg.write("<body>\r\nServer error.<br>\r\n")
20    mg.write("The server admin must make sure this script is available as URI " .. scriptUri .. "<br>\r\n")
21    mg.write("The server admin must set the environment variable " .. envVar .. " to a directory.<br>\r\n")
22    mg.write("</body>\r\n</html>\r\n")
23    return
24end
25subresource = mg.request_info.uri:match(scriptUri .. "/(.*)")
26
27if not subresource then
28    if method=="GET" then
29        mg.write("HTTP/1.0 200 OK\r\n")
30        mg.write("Connection: close\r\n")
31        mg.write("Content-Type: text/html; charset=utf-8\r\n")
32        mg.write("\r\n")
33        mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
34        mg.write("<body>No resource specified.<br>resourcedir is " .. resourcedir .. "</body></html>\r\n")
35    else
36        mg.write("HTTP/1.0 405 Method Not Allowed\r\n")
37        mg.write("Connection: close\r\n")
38        mg.write("Content-Type: text/html; charset=utf-8\r\n")
39        mg.write("\r\n")
40        mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
41        mg.write("<body>Method not allowed.</body></html>\r\n")
42    end
43    return
44end
45
46
47if method=="GET" then
48    file = resourcedir .. "/" .. subresource
49    if lfs.attributes(file) then
50        mg.send_file(file)
51    else
52        mime = mg.get_mime_type(file)
53        mg.write("HTTP/1.0 404 Not Found\r\n")
54        mg.write("Connection: close\r\n")
55        mg.write("Content-Type: text/html; charset=utf-8\r\n")
56        mg.write("\r\n")
57        mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
58        mg.write("<body>Resource of type \"" .. mime .. "\" not found.</body></html>\r\n")
59    end
60    return
61end
62
63if method=="PUT" then
64    file = resourcedir .. "/" .. subresource
65    mime = mg.get_mime_type(file)
66    if lfs.attributes(file) then
67        mg.write("HTTP/1.0 405 Method Not Allowed\r\n")
68        mg.write("Connection: close\r\n")
69        mg.write("Content-Type: text/html; charset=utf-8\r\n")
70        mg.write("\r\n")
71        mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
72        mg.write("<body>Resource of type \"" .. mime .. "\" already exists.</body></html>\r\n")
73    else
74        local f = io.open(file, "w")
75
76        local data = {}
77        repeat
78            local l = mg.read();
79            data[#data+1] = l;
80        until ((l == "") or (l == nil));
81
82        f:write(table.concat(data, ""))
83        f:close()
84        mg.write("HTTP/1.0 200 OK\r\n")
85        mg.write("Connection: close\r\n")
86        mg.write("Content-Type: text/html; charset=utf-8\r\n")
87        mg.write("\r\n")
88        mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
89        mg.write("<body>Resource of type \"" .. mime .. "\" created.</body></html>\r\n")
90    end
91    return
92end
93
94if method=="DELETE" then
95    file = resourcedir .. "/" .. subresource
96    mime = mg.get_mime_type(file)
97    if lfs.attributes(file) then
98        os.remove(file)
99        mg.write("HTTP/1.0 200 OK\r\n")
100        mg.write("Connection: close\r\n")
101        mg.write("Content-Type: text/html; charset=utf-8\r\n")
102        mg.write("\r\n")
103        mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
104        mg.write("<body>Resource of type \"" .. mime .. "\" deleted.</body></html>\r\n")
105    else
106        mime = mg.get_mime_type(file)
107        mg.write("HTTP/1.0 404 Not Found\r\n")
108        mg.write("Connection: close\r\n")
109        mg.write("Content-Type: text/html; charset=utf-8\r\n")
110        mg.write("\r\n")
111        mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
112        mg.write("<body>Resource of type \"" .. mime .. "\" not found.</body></html>\r\n")
113    end
114    return
115end
116
117-- Any other method
118mg.write("HTTP/1.0 405 Method Not Allowed\r\n")
119mg.write("Connection: close\r\n")
120mg.write("Content-Type: text/html; charset=utf-8\r\n")
121mg.write("\r\n")
122mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
123mg.write("<body>Method not allowed.</body></html>\r\n")
124
125