昨晚的雷把路由劈了 一天都没得网上 憋坏了 没奈何 只好上网吧来通宵玩
可是通宵玩游戏是绝对会睡的 所以就想到个东西 一直想写的一个小应用
其实就是个hash db只不过是基于http的
而且是基于url 呵呵
用lua编写的,用到了luasocket和copas库
所以最好用murgaLua去运行 因为库都内置了
murgaLua的地址 http://www.murga-projects.com/ 很好的东西 我再次强烈推荐
废话无多,代码如下:
-------------------------------------------------------------------
local server={}
local page={}
server["name"]="yunfan's webserver"
server["host"]="*"
server["port"]=8040
-------------------------myutils-----------------------------------
string.split=function(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
-------------------------myprocess--------------------------------
--this my process code lib
function callecho(skt,msg)
local response=msg
local length=string.len(response)
local res="HTTP/1.1 200 OK\nServer: "..server["name"].."\nContent-Length: "..length.."\nContent-Type: text/html\n\n"..response
copas.send(skt,res)
end
function callprocess(skt,query)
if string.match(query,"^/[^=]+$") then
print("calll showindex")
callshowindex(skt,query)
elseif string.match(query,"==") then
callecho(skt,"Sorry,you have put a strange query that we can not process! ==> "..query)
elseif query=="/" then
print("call showindex")
callshowindex(skt)
else
print("call createpage")
callcreatepage(skt,query)
end
end
function callshowindex(skt)
local tmp={}
for k in pairs(page) do
local value="<a href=\""..k.."\">"..k.."</a><br/>"
table.insert(tmp,value)
end
local msg=table.concat(tmp)
callecho(skt,msg)
end
function callshowpage(skt,query)
if page[query] then
callecho(skt,page[query])
else
local res="HTTP/1.1 404 Not Found\n\n"
copas.send(skt,res)
end
end
function callcreatepage(skt,query)
local argmt=string.split(query,"=")
if page[argmt[1]] then
local res="HTTP/1.1 301 Moved Permanently\nLocation: "..argmt[1].."\n\n"
copas.send(skt,res)
else
page[argmt[1]]=argmt[2]
local res="HTTP/1.1 302 Found\nLocation: "..argmt[1].."\n\n"
copas.send(skt,res)
end
end
-------------------------------------------------------------------------
function handle(skt)
local req=string.split(copas.receive(skt),"%s")
--local header=string.split(req[1],"\r\n")
--local request=string.split(header[1],"%s+?")
if (not(req[1]=="GET")) then
callecho(skt,"bad request header!!")
print("req1: "..req[1])
else
print(req[2])
callprocess(skt,req[2])
end
end
-------------------------------------------------------------------------
socket.trybind=socket.protect(function(host,port)
local s=socket.try(socket.bind(host,port))
return s
end)
local svr=socket.trybind(server["host"],server["port"])
if not svr then
print("Sorry,something may cause a error\nAnd so the server has not run successed!!\n")
os.exit()
end
copas.addserver(svr,handle)
copas.loop()
写得有点乱
其实一开始是弄得很好的 但是lua的局部变量访问全局变量似乎和别的不一致
我的分开的代码里的函数没办法访问全局配置的那个变量 所以压到一个文件里了
效果见:
http://pep525.vicp.net:8080/
多谢pep525给我提供了这么个linux主机来折腾 呵呵