| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 | 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")endfunction 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")endhandler = "filehandler.lua"sub_uri = mg.request_info.uri:sub(#handler+2)filename = "D:\\civetweb\\civetweb" .. sub_uriattr = lfs.attributes(filename)--[[if not attr then	send_not_found()	mg.write("\r\n")	mg.write("File " .. sub_uri .. " not available")	returnend]]if mg.request_info.request_method == "GET" then	-- send_file will handle 404 internally	mg.send_file(filename)	returnelseif mg.request_info.request_method == "HEAD" then	-- send_file can handle "GET" and "HEAD"	mg.send_file(filename)	returnelseif 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")	returnelseif 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")	returnelseif mg.request_info.request_method == "OPTIONS" then	send_ok()	mg.write("Allow: GET, HEAD, PUT, DELETE, OPTIONS\r\n")	mg.write("\r\n")	returnelse	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")	returnend
 |