summaryrefslogtreecommitdiffstats
path: root/src/civetweb/test/filehandler.lua
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/civetweb/test/filehandler.lua
parentInitial commit. (diff)
downloadceph-6d07fdb6bb33b1af39833b850bb6cf8af79fe293.tar.xz
ceph-6d07fdb6bb33b1af39833b850bb6cf8af79fe293.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
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 000000000..57bf41128
--- /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