Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. mime = require("mime")
  2. function handle_request(env)
  3.     local debug = true
  4.     local function split(str,sep)
  5.         local t = {}
  6.         local ini = 1
  7.         local seplen = string.len(sep)
  8.         local len = string.len(str)
  9.         local iend = string.find(str,sep,ini,true)
  10.         if iend == nil then iend = len+1 end
  11.         repeat
  12.             t[#t+1] = string.sub(str,ini,iend-1)
  13.             ini = iend+seplen
  14.             iend = string.find(str,sep,ini,true)
  15.         until iend == nil
  16.         if ini <= len+1 then
  17.             t[#t+1] = string.sub(str,ini)
  18.         end
  19.         return t
  20.     end
  21.  
  22.     local function getboundary ()
  23.         local content_type, boundary = "", ""
  24.         if env["CONTENT_TYPE"] then
  25.             _,_,content_type, boundary = string.find (env["CONTENT_TYPE"], "(%S+)%s*boundary%=(.-)$")
  26.             cotent_type = content_type or ""
  27.             if boundary then boundary = "--"..boundary
  28.             else boundary = "" end
  29.         end
  30.         return content_type, boundary
  31.     end
  32.  
  33.     function showInfo()
  34.         uhttpd.send("HTTP/1.0 200 OK\r\n")
  35.         uhttpd.send("Content-Type: text-plain\r\n\r\n")
  36.    
  37.         uhttpd.send(content_type.."\r\n")
  38.         uhttpd.send(boundary.."\r\n")
  39.  
  40.         uhttpd.send("Request:\r\n")
  41.         for k, v in pairs(env) do
  42.             uhttpd.send(string.format("%35s = %s\r\n", k, tostring(v)))
  43.         end
  44.  
  45.         uhttpd.send("\r\n\r\nHeaders:\r\n")
  46.         for k, v in pairs(env.headers) do
  47.             uhttpd.send(string.format("%35s = %s\r\n", k, tostring(v)))
  48.         end
  49.         uhttpd.send("\n\nCGI Data:\r\n\r\n---\r\n")
  50.         for k, v in pairs(cgi) do
  51.             uhttpd.send(string.format("%35s = %s\r\n", k, tostring(v)))
  52.         end
  53.         uhttpd.send("\r\n---\r\n")
  54.         uhttpd.send("")
  55.     end
  56.  
  57.     function getData()
  58.         local params = {}
  59.         if env.REQUEST_METHOD == "POST" then
  60.             local rv, buf
  61.             local data = ""
  62.             repeat
  63.                 rv, buf = uhttpd.recv(4096)
  64.                 if buf then
  65.                     data = data..buf
  66.                 end
  67.             until rv <= 0
  68.             local bycontent = {}
  69.             if data ~= "" then
  70.                 for k,reg in ipairs(split(data,boundary.."\r\n")) do
  71.                     local content, name, value
  72.                     string.gsub(reg, '([^%c%s:]+):%C+"(%C+)"%c+(%C+)',
  73.                         function (k,v,z)
  74.                             content = string.lower(k)
  75.                             name = v
  76.                             value = z
  77.                         end)
  78.                     if content then
  79.                         if bycontent[content] == nil then bycontent[content] = {} end
  80.                         if name then
  81.                             bycontent[content][name] = value
  82.                             params[name] = value
  83.                         end
  84.                     end
  85.                 end
  86.             end
  87.             return params, bycontent
  88.         else
  89.             if env.QUERY_STRING then
  90.                 local str = uhttpd.urldecode(env.QUERY_STRING)
  91.                 for p in string.gmatch(str,"[^&]+") do
  92.                     for k, v in string.gfind(p,"([^=]+)=(%C*)") do
  93.                         params[k]=v
  94.                     end
  95.                 end
  96.             end
  97.             return params
  98.         end
  99.     end
  100.        
  101.     content_type, boundary = getboundary()
  102.     method = env.REQUEST_METHOD
  103.     cgi, cgibyType = getData()
  104.     __ENV = env
  105.     local  myenv = io.popen("env")
  106.     for line in myenv:lines() do
  107.         _, _, key, value = string.find(line, "([A-Z_0-9a-z]+)[%=]+(.*)")
  108.         __ENV[key] = value
  109.     end
  110.     __FORM = cgi
  111.     __HEADERS = env.headers
  112.     cgi.REMOTE_USER = "Unkonow"
  113.     local user, pass = "Unknow", "invalid"
  114.     if env.headers.Authorization then
  115.         _, _, user, pass = string.find(uhttpd.b64decode(string.sub(env.headers.Authorization,7)),"([^%:]+):(.+)")
  116.         userdb = "/etc/passwd"
  117.         huserdb = io.open(userdb,"r")
  118.         local myAuth = false;
  119.         for l in huserdb:lines() do
  120.             local _, _, username, passwd, UID, GID, full_name, directory, shell = string.find(l,"([^%:]+):([^%:]+):([^%:]+):([^%:]+):([^%:]+):([^%:]+):(.+)")
  121.             if username == user then
  122.                 __REALM = {}
  123.                 __REALM["USERNAME"] = username
  124.                 __REALM["PASSWD"] = passwd
  125.                 __REALM["UID"] = UID
  126.                 __REALM["GID"] = GID
  127.                 __REALM["FULL_NAME"] = full_name
  128.                 __REALM["DIRECTORY"] = directory
  129.                 __REALM["SHELL"] = shell
  130.                 -- Check password --
  131.                 env.REMOTE_USER = user
  132.                 myAuth = true
  133.                 break
  134.             end
  135.         end
  136.         if __REALM == nil then
  137.             env.headers.Authorization = nil
  138.         end
  139.     end
  140.     local script = env.SCRIPT_NAME..env.PATH_INFO
  141.     local rc, err = pcall(dofile,"/www"..script)
  142.     if not rc then
  143.         uhttpd.send("HTTP/1.0 200 OK\r\n")
  144.         uhttpd.send("Content-Type: text/html\r\n\r\n")
  145.         uhttpd.send("<pre>")
  146.         uhttpd.send("Error:\r\n\t"..err.."\r\n")
  147.         uhttpd.send("</pre>")
  148.     end
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement