forked from db4linq/lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_http.lua
More file actions
66 lines (58 loc) · 2 KB
/
sample_http.lua
File metadata and controls
66 lines (58 loc) · 2 KB
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
function split(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
function urldecode(payload)
result = {};
list=split(payload,"\r\n")
list=split(list[1]," ")
list=split(list[2],"\/")
table.insert(result, list[1]);
table.insert(result, list[2]);
table.insert(result, list[3]);
return result;
end
function index(conn)
conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
<!DOCTYPE HTML><html><head>\
<meta content="text/html;charset=utf-8"><title>ESP8266</title>\
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">\
<body bgcolor="#ffe4c4">\
<h2>Page Index</h2><hr>\
</body></html>')
end
function about(conn)
conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
<!DOCTYPE HTML><html><head>\
<meta content="text/html;charset=utf-8"><title>ESP8266</title>\
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">\
<body bgcolor="#ffe4c4">\
<h2>Page About</h2><hr>\
</body></html>')
end
function notfound(conn)
conn:send('HTTP/1.1 404 Not Found\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
<!DOCTYPE HTML>\
<html><head><meta content="text/html;charset=utf-8"><title>ESP8266</title></head>\
<body bgcolor="#ffe4c4"><h2>Page Not Found</h2>\
</body></html>')
end
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print("Http Request..\r\n")
print (payload)
list=urldecode(payload)
if ((list[2]=="") or (list[2]=="index.html")) then
index(conn)
elseif (list[2]=="about.html") then
about(conn)
else
notfound(conn)
end
conn:close()
end)
end)