-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
50 lines (33 loc) · 1.05 KB
/
main.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
local Socket = require("socket").bind("127.0.0.1", 5000)
if not Socket then
print("Error: Could not bind socket.\n")
os.exit(0)
end
local Ip, Port = Socket:getsockname()
print(string.format("Listening to %s:%s", Ip, Port))
while true do
local Client = Socket:accept()
Client:settimeout(60)
local Request, Error = Client:receive()
if not Error then
local Split = {}
for Line in string.gmatch(Request, "[^ ]+") do
table.insert(Split, Line)
end
local Method, Url = Split[1], Split[2]
print(string.format("[Request] Method: %s, Url: %s:%s%s", Method, Ip, Port, Url))
local Directory = "./"
for File in string.gmatch(Url, "([^/]+)") do
Directory = Directory .. "/" .. File
end
Directory = Directory .. "/index.html"
local File = io.open(Directory, 'r')
if File then
Client:send(string.format("HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n %s\n", File:read("*all")))
File:close()
else
Client:send("HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n <h1>404 Not Found</h1>\n")
end
end
Client:close()
end