* Rewrote Luci to be coroutine-safe allowing the use of non-forking webservers
[project/luci.git] / libs / sgi-webuci / luasrc / sgi / webuci.lua
index a40888e..abe279d 100644 (file)
@@ -24,93 +24,36 @@ limitations under the License.
 
 ]]--
 module("luci.sgi.webuci", package.seeall)
-
-local status_set = false
-
--- Initialize the environment
-function initenv(env, vars)
-       luci.http.env = env
-       luci.http.vars = vars
-end
-
--- Enforces user authentification
-function luci.http.basic_auth(verify_callback, realm)
-       local user = luci.http.env.auth_user
-       local pass = luci.http.env.auth_password
-       realm = realm or ""
+require("luci.http")
+require("luci.util")
+require("luci.dispatcher")
+
+function run(env, vars)
+       local r = luci.http.Request()
+       r.env = env
+       r.request = vars
        
-       if not user or not verify_callback(user, pass) then
-               luci.http.status("401", "Unauthorized")
-               luci.http.header("WWW-Authenticate", string.format('Basic realm="%s"', realm))
-               return false    
-       else
-               return true
-       end
-end
-
--- Returns the main dispatcher URL
-function luci.http.dispatcher()
-       return luci.http.env.SCRIPT_NAME or ""
-end
-
--- Returns the upload dispatcher URL
-function luci.http.dispatcher_upload()
-       -- To be implemented
-end
-
--- Returns a table of all COOKIE, GET and POST Parameters
-function luci.http.formvalues()
-       return luci.http.vars
-end
-
--- Gets form value from key
-function luci.http.formvalue(key, default)
-       return luci.http.formvalues()[key] or default
-end
-
--- Gets a table of values with a certain prefix
-function luci.http.formvaluetable(prefix)
-       local vals = {}
-       prefix = prefix and prefix .. "." or "."
+       local x = coroutine.create(luci.dispatcher.httpdispatch)
        
-       for k, v in pairs(luci.http.formvalues()) do
-               if k:find(prefix, 1, true) == 1 then
-                       vals[k:sub(#prefix + 1)] = v
+       while coroutine.status(x) ~= "dead" do
+               local res, id, data1, data2 = coroutine.resume(x, r)
+
+               if not res then
+                       print(env.SERVER_PROTOCOL .. " 500 Internal Server Error")
+                       print("Content-Type: text/plain\n")
+                       print(id)
+                       break;
                end
+               
+               if id == 1 then
+                       io.write(env.SERVER_PROTOCOL .. " " .. tostring(data1) .. " " .. data2 .. "\n")
+               elseif id == 2 then
+                       io.write(data1 .. ": " .. data2 .. "\n")
+               elseif id == 3 then
+                       io.write("\n")
+               elseif id == 4 then
+                       io.write(data1)
+               end
+               
        end
-       
-       return vals
-end
-
--- Sends a custom HTTP-Header
-function luci.http.header(key, value)
-       print(key .. ": " .. value)
-end
-
--- Set Content-Type
-function luci.http.prepare_content(type)
-       if not status_set then
-               luci.http.status(200, "OK")
-       end
-       
-       print("Content-Type: "..type.."\n")
-end
-
--- Asks the browser to redirect to "url"
-function luci.http.redirect(url)
-       luci.http.status(302, "Found")
-       luci.http.header("Location", url)
-       print()
-end
-
--- Returns the path of an uploaded file
--- WARNING! File uploads can be easily spoofed! Do additional sanity checks!
-function luci.http.upload(name)
-       -- To be implemented
-end
-
--- Sets HTTP-Status-Header
-function luci.http.status(code, message)
-       print(luci.http.env.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
-       status_set = true
 end