shavrin777
New member
.
Последнее редактирование:
-- Close old Server
if httpd then
httpd:close()
httpd = nil
collectgarbage()
end
httpd=net.createServer(net.TCP)
-- decode URI
local function decodeURI(s)
if(s) then
s = string.gsub(s, '%%(%x%x)',
function (hex) return string.char(tonumber(hex,16)) end )
end
return s
end
local function receive_http(sck, data)
-- sendfile class
sendfile = {}
sendfile.__index = sendfile
function sendfile.new(sck, fname)
local self = setmetatable({}, sendfile)
self.sck = sck
self.fd = file.open(fname, "r")
if self.fd then
function send(localSocket)
local response = self.fd:read(128)
if response then
localSocket:send(response)
else
if self.fd then
self.fd:close()
end
localSocket:close()
end
response = nil
collectgarbage()
end
self.sck:on("sent", send)
send(self.sck)
else
localSocket:close()
end
localSocket = nil
fname = nil
collectgarbage()
collectgarbage()
return self
end
-----
local host_name = string.match(data,"Host: ([0-9,\.]*)\r",1)
local url_file = string.match(data,"[^/]*\/([^ ?]*)[ ?]",1)
local uri = decodeURI(string.match(data,"[^?]*\?([^ ]*)[ ]",1))
-- parse GET parameters
local GET={}
if uri then
for key, value in string.gmatch(uri, "([^=&]*)=([^&]*)") do
GET[key]=value
print(key, value)
key = nil
value = nil
cillectgarbage()
end
end
-- parse POST parameters
local post = string.match(data,"\n([^\n]*)$",1):gsub("+", " ")
local POST={}
if post then
for key, value in string.gmatch(post, "([^=&]*)=([^&]*)") do
POST[key]=decodeURI(value)
print(key, POST[key])
key = nil
value = nil
cillectgarbage()
end
end
print("HTTP request:", data)
request_OK = false
-- if file not specified then send index.html
if url_file == '' then
sendfile.new(sck, 'index.html')
self = nil
sendfile = nil
fext = nil
sck = nil
collectgarbage()
request_OK = true
else
local fext=url_file:match("^.+(%..+)$")
if fext == '.html' or
fext == '.txt' or
fext == '.js' or
fext == '.json' or
fext == '.css' or
fext == '.png' or
fext == '.gif' or
fext == '.ico' then
if file.exists(url_file) then
sendfile.new(sck, url_file)
self = nil
sendfile = nil
fext = nil
url_file = nil
sck = nil
collectgarbage()
request_OK = true
end
end
-- execute LUA file
-- IT IS HAZARDOUS
if fext == '.lua' or fext == '.lc' then
if file.exists(url_file) then
local response=dofile(url_file)
sck:on("sent", function() sck:close() end)
sck:send(response)
response = nil
self = nil
sendfile = nil
fext = nil
sck = nil
collectgarbage()
request_OK = true
end
end
fext = nil
end
if request_OK == false then
sck:on("sent", function() sck:close() end)
sck:send('<html><h1>Error 404</h1></html>')
sck = nil
end
response = nil
data = nil
POST = nil
request_OK = nil
post = nil
self = nil
fext = nil
value = nil
GET = nil
key = nil
host_name = nil
url_file = nil
uri = nil
sck = nil
collectgarbage()
collectgarbage()
collectgarbage()
collectgarbage()
end
if httpd then
httpd:listen(80, function(conn)
conn:on("receive", receive_http)
conn = nil
collectgarbage()
end)
end
Попробуйте изменить структуру программывот сервер что смог устранил , но память течёт