Skip to content

Instantly share code, notes, and snippets.

@soccermitchy
Created September 22, 2012 18:32
Show Gist options
  • Save soccermitchy/3767334 to your computer and use it in GitHub Desktop.
Save soccermitchy/3767334 to your computer and use it in GitHub Desktop.
Droidphal
--require "android" --Uncomment if in sl4a
--------------------------------
-- Android Ophal Web Server --
-- Created by Mitchell Monahan--
-- Running ophal version --
-- Ophal/0.1-alpha9 --
-- Is in GNU licence. --
--------------------------------
socket=require "socket"
config={}
errors={}
errors.generics={}
errors.NoPopen="Make sure the machine this is on has io.popen" --The error string to use if you do not have io.popen
errors.generics.server="\
<html>\
<head>\
<title> 500 Server Error </title>\
</head>\
<body>\
<h1> 500 Server Error </h1><br>\
Please contact soccermitchy@mini-irc.uni.me for support.<br>\
<hr><br>\
Droidphal web server running lua version ".._VERSION..".\
</body>\
</html>"
-- Configuration
-- Defaults are active if variable is nil
--[[ config.ip
Sets the IP address to bind to
Default: "*"
]]--
config.ip=nil
--[[ config.port
Sets the port to bind to
Default: 8080
]]--
config.port=nil
function getDate()
--return os.date("%d/%m/%Y %I:%M.%S %p").." "
return os.date().." "
end
function split(str, pat) --From http://lua-users.org/wiki/SplitJoin
local t = {}
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
print("[INFO] "..getDate().."--------------------------------")
print("[INFO] "..getDate().."-- Android Ophal Web Server --")
print("[INFO] "..getDate().."-- Created by Mitchell Monahan--")
print("[INFO] "..getDate().."-- Running ophal version --")
print("[INFO] "..getDate().."-- Ophal/0.1-alpha9 --")
print("[INFO] "..getDate().."-- Is under GNU license. --")
print("[INFO] "..getDate().."--------------------------------")
print("")
print("[INFO] "..getDate().."Luasocket loaded.")
print("[INFO] "..getDate().."Parsing configuration...")
config.ip=config.ip or "*"
config.port=config.port or 8080
print("[INFO] "..getDate().."Done.")
function parseData(client,config)
client.request=client.remote:receive()
if client.request then
client.data=split(client.request," ")
if client.data then
if client.data[1]=="GET" then
if not client.remote:getpeername() == "46.246.31.124" then
client.pathSplit=split(client.data[2],"/")
if io.popen then
require "includes.bootstrap"
client.send=ophal.bootstrap(function()
menu_execute_active_handler()
end)
print("[INFO] "..getDate().." Client requested "..client.data[2].." .")
if client.send==nil then
--client.send="You're not supposed to get this...it means an ophal module is not returning correctly. Check hook_exit [should be the function exit()]"
client.send="HELLO, MR. "..client.remote:getpeername()
end
client.remote:send("HTTP/1.0 200 OK\r\nDate: Sat, 09 Sep 2012 15:34:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#client.send.."\r\n\r\n"..client.send)
else
client.remote:send("HTTP/1.0 200 OK\r\nDate: Sat, 09 Sep 2012 15:32:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#errors.NoPopen.."\r\n\r\n"..errors.NoPopen)
end
end
end
else
client.remote:send("HTTP/1.0 500 OK\r\nServer: Droidphal\r\nDate: Sat, 09 Sep 2012 15:31:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#errors.generics.server"\r\n\r\n"..errors.generics.server)
end
else
client.remote:send("HTTP/1.0 500 OK\r\nDate: Sat, 09 Sep 2012 15:31:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#errors.generics.server.."\r\n\r\n"..errors.generics.server)
end
end
function startServer()
print("[INFO] "..getDate().."Binding to port "..config.port..". (IP to be used: "..config.ip..".)")
server=socket.bind(config.ip,config.port)
print("[INFO] "..getDate().."Bound to port "..config.port..".")
print("[INFO] "..getDate().."Preparing to accept clients...")
serverActive=true
while serverActive do
client={}
print("[INFO] "..getDate().."Accepting clients.")
client.remote=server:accept()
print("[INFO] "..getDate().."New client at IP "..client.remote:getpeername().." .")
print("[INFO] "..getDate().."Server is able to accept clients!")
parseData(client,config)
end
end
startServer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment