-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceivehead.lua
47 lines (47 loc) · 992 Bytes
/
receivehead.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
function receivehead(cl)
cl.headers={}
cl.method=nil
cl.url=nil
cl.path=nil
cl.postraw=nil
cl.postlen=nil
cl.post=nil
cl.get=nil
cl.getraw=nil
cl.onReceive=function(line)
if not cl.url then
local line=cl.receive()
if line then
local method,url=line:match("^(%S+) (.-) HTTP/.%..$")
if url then
cl.url=url
cl.getraw=url:match("%?(.*)") or ""
cl.get=parsepost(cl.getraw)
cl.path=fs.resolve(url:match("^[^%?]*"):match("^/?(.-)/?$"))
cl.method=method:lower()
end
end
end
if cl.postlen then
local data=cl.receive(cl.postlen)
if data then
cl.postraw=data
cl.post=parsepost(data)
cl.onReceive=nil
return serve(cl)
end
else
local line=cl.receive()
while line do
if line=="" then
return serve(cl)
end
local header,val=line:match("(%S+): (.+)")
if header then
cl.headers[header]=val
end
line=cl.receive()
end
end
end
end