-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
101 lines (94 loc) · 3.17 KB
/
init.lua
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
94
95
96
97
98
99
100
101
if srv~=nil then
srv:close()
end
wifi.setmode(wifi.STATION)
wifi.sta.config("metalab","")
wifi.sta.setip({ip="10.20.30.22",netmask="255.255.0.0",gateway="10.20.30.1"});
srv=net.createServer(net.TCP)
local httpRequest={};
httpRequest["/"]="dramabarsite.html";
httpRequest["/home"]="dramabarsite.html";
httpRequest["/dramabarsite.html"]="dramabarsite.html";
httpRequest["/credits"]="credits.html";
httpRequest["/defaults"]="dramabarsite.html";
local getContentType={};
getContentType["/"]="dramabarsite.html";
getContentType["/home"]="dramabarsite.html";
getContentType["/dramabarsite.html"]="dramabarsite.html";
getContentType["/credits"]="credits.html";
getContentType["/defaults"]="dramabarsite.hmtl";
local pos = 0;
local chunksize = 1024;
function filesize (filename)
local buf = file.list();
for name,size in pairs(buf) do
if (name == filename) then
return size;
end
end
end
srv:listen(80,function(conn)
conn:on("receive", function(conn,request)
--print("Request:\n" ..request.. "\nRequest end\n");
if (string.match(request, 'POST')) then
s, e = string.find(request, "cH");
data = string.sub(request, s);
uart.write(0, data, "\0");
elseif (string.match(request, "GET /favicon.ico")) then
conn:send("HTTP/1.1 404 Not found\r\n\r\n");
conn:close();
collectgarbage();
end
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
header = 'HTTP/1.1 200 OK\r\n';
if getContentType[path] then
requestFile = httpRequest[path];
pos = 0;
conn:send("HTTP/1.1 200 OK\r\nContent-Type: " ..getContentType[path].."\r\n\r\n");
else
--print("File not found (invalid path?): " ..path);
conn:send("HTTP/1.1 404 Not found\r\n\r\n");
conn:close();
collectgarbage();
end
if (filesize(requestFile) > chunksize) then
conn:send(header);
else
file.open(requestFile, "r");
conn:send(header..file.read())
file.close();
end
end)
conn:on("sent", function(conn)
if requestFile then
if file.open(requestFile, r) then
--print("File: " ..requestFile);
file.seek("set", pos*chunksize);
partialData = file.read();
file.close();
if (partialData ~= 0) then
pos = pos + 1;
conn:send(partialData);
if (#partialData == chunksize) then
return;
else
pos = 0;
end
end
else
--print("Error with opening file " ..requestFile);
end
conn:close();
collectgarbage();
end
end)
end)