summaryrefslogtreecommitdiffstats
path: root/src/civetweb/test/filehandler.lua
blob: 57bf4112873566a393ed83ab83db9f309cf51234 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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