-
Notifications
You must be signed in to change notification settings - Fork 11
/
httphost.lua
98 lines (74 loc) · 2.48 KB
/
httphost.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
-- at the bottom: sockServ = HTTPHost(4665)
require( "bromsock" );
if (sockServ) then
sockServ:Close()
end
local HTTPContentTypes = {
["html"] = "text/html; charset=UTF-8",
["txt"] = "text/plain"
}
function HTTPHost(port)
servsock = BromSock();
if (not servsock:Listen(port)) then
print("[BS:S] Failed to listen!")
else
print("[BS:S] Server listening...")
end
servsock:SetCallbackAccept(function(serversock, clientsock)
print("[BS:S] Accepted:", serversock, clientsock)
clientsock:SetCallbackReceive(function(sock, packet)
print("[BS:S] Received:", sock, packet)
local headerdata = packet:ReadStringAll()
local rawheaders = string.Explode("\r\n", headerdata)
local headers = {}
local requestlinedata = nil
for _, header in pairs(rawheaders) do
if (not requestlinedata) then
requestlinedata = string.Explode(" ", header)
end
local splited = string.Explode(":", header)
headers[splited[1]] = #splited > 1 and splited[2] or ""
end
local method = string.lower(requestlinedata[1])
local path = string.Right(requestlinedata[2], #requestlinedata[2] - 1)
local httpver = string.lower(requestlinedata[3])
local filetype = "text/plain"
if (path == "") then
path = "index.html"
end
local extentiondata = string.Explode(".", path)
local extention = string.lower(extentiondata[#extentiondata])
local contentype = HTTPContentTypes[extention]
print("handleing request: ", httpver, method, path, filetype, extention)
local statuscode = "200 OK"
local filedata = nil
if (not file.Exists(path, "DATA")) then
filedata = "<h1>file not found</h1>"
contentype = HTTPContentTypes["html"]
statuscode = "404 FILE NOT FOUND"
else
filedata = file.Read(path, "DATA")
end
local pPacket = BromPacket()
pPacket:WriteLine("HTTP/1.1 " .. statuscode)
pPacket:WriteLine("Content-Type: " .. (contentype or "text/plain"))
pPacket:WriteLine("Server:bromsock")
pPacket:WriteLine("Content-length: " .. #filedata)
pPacket:WriteLine("")
pPacket:WriteStringRaw(filedata)
sock:Send(pPacket, true)
end)
clientsock:SetCallbackDisconnect(function(sock)
print("[BS:S] Disconnected:", sock)
end)
clientsock:SetCallbackSend(function(sock, a, b, c)
print("[BS:S] Send packet:", sock, a, b, c)
end)
clientsock:SetTimeout(1000)
clientsock:ReceiveUntil("\r\n\r")
serversock:Accept()
end)
servsock:Accept()
return servsock
end
sockServ = HTTPHost(4665)