summaryrefslogtreecommitdiffstats
path: root/src/civetweb/test/filehandler.lua
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/civetweb/test/filehandler.lua
parentInitial commit. (diff)
downloadceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz
ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/civetweb/test/filehandler.lua')
-rw-r--r--src/civetweb/test/filehandler.lua93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/civetweb/test/filehandler.lua b/src/civetweb/test/filehandler.lua
new file mode 100644
index 00000000..57bf4112
--- /dev/null
+++ b/src/civetweb/test/filehandler.lua
@@ -0,0 +1,93 @@
+function send_ok()
+ mg.write("HTTP/1.0 200 OK\r\n")
+ mg.write("Connection: close\r\n")
+ mg.write("Date: " .. os.date("%a, %d %b %Y %H:%M:%S GMT") .. "\r\n")
+end
+
+
+function send_not_found()
+ mg.write("HTTP/1.0 404 Not Found\r\n")
+ mg.write("Connection: close\r\n")
+ mg.write("Date: " .. os.date("%a, %d %b %Y %H:%M:%S GMT") .. "\r\n")
+end
+
+
+handler = "filehandler.lua"
+sub_uri = mg.request_info.uri:sub(#handler+2)
+filename = "D:\\civetweb\\civetweb" .. sub_uri
+attr = lfs.attributes(filename)
+
+--[[
+if not attr then
+ send_not_found()
+ mg.write("\r\n")
+ mg.write("File " .. sub_uri .. " not available")
+ return
+end
+]]
+
+if mg.request_info.request_method == "GET" then
+ -- send_file will handle 404 internally
+ mg.send_file(filename)
+ return
+
+elseif mg.request_info.request_method == "HEAD" then
+ -- send_file can handle "GET" and "HEAD"
+ mg.send_file(filename)
+ return
+
+elseif mg.request_info.request_method == "PUT" then
+ local f = io.open(filename, "w")
+ if (not f) then
+ mg.write("HTTP/1.0 500 Internal Server Error\r\n")
+ mg.write("Connection: close\r\n")
+ mg.write("Date: " .. os.date("%a, %d %b %Y %H:%M:%S GMT") .. "\r\n")
+ mg.write("\r\n")
+ return
+ end
+
+ mg.write("HTTP/1.0 201 Created\r\n")
+ mg.write("Connection: close\r\n")
+ mg.write("Date: " .. os.date("%a, %d %b %Y %H:%M:%S GMT") .. "\r\n")
+ mg.write("\r\n")
+ repeat
+ local buf = mg.read();
+ if (buf) then
+ f:write(buf)
+ end
+ until (not buf);
+ f:close()
+
+ mg.write("HTTP/1.0 201 Created\r\n")
+ mg.write("Connection: close\r\n")
+ mg.write("Date: " .. os.date("%a, %d %b %Y %H:%M:%S GMT") .. "\r\n")
+ mg.write("\r\n")
+ return
+
+elseif mg.request_info.request_method == "DELETE" then
+ if not attr then
+ send_not_found()
+ mg.write("\r\n")
+ mg.write("File " .. sub_uri .. " not available")
+ return
+ end
+ os.remove(filename)
+ mg.write("HTTP/1.0 204 No Content\r\n")
+ mg.write("Connection: close\r\n")
+ mg.write("Date: " .. os.date("%a, %d %b %Y %H:%M:%S GMT") .. "\r\n")
+ mg.write("\r\n")
+ return
+
+elseif mg.request_info.request_method == "OPTIONS" then
+ send_ok()
+ mg.write("Allow: GET, HEAD, PUT, DELETE, OPTIONS\r\n")
+ mg.write("\r\n")
+ return
+
+else
+ mg.write("HTTP/1.0 405 Method Not Allowed\r\n")
+ mg.write("Connection: close\r\n")
+ mg.write("Date: " .. os.date("%a, %d %b %Y %H:%M:%S GMT") .. "\r\n")
+ mg.write("\r\n")
+ return
+end