Server Gateway Interface for Webuci
FileId:
-$Id: webuci.lua 2027 2008-05-07 21:16:35Z Cyrus $
+$Id$
License:
Copyright 2008 Steven Barth <steven@midlink.org>
]]--
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 ""
+local ltn12 = require("luci.ltn12")
+require("luci.http")
+require("luci.util")
+require("luci.dispatcher")
+
+function run(env, vars)
+ local r = luci.http.Request(
+ env,
+ ltn12.source.empty(),
+ ltn12.sink.file(io.stderr)
+ )
- 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 "."
+ r.message.params = vars
- for k, v in pairs(luci.http.formvalues()) do
- if k:find(prefix, 1, true) == 1 then
- vals[k:sub(#prefix + 1)] = v
- end
- end
+ local x = coroutine.create(luci.dispatcher.httpdispatch)
+ local status = ""
+ local headers = {}
+ local active = true
- 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")
+ 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 active then
+ if id == 1 then
+ status = env.SERVER_PROTOCOL .. " " .. tostring(data1) .. " " .. data2 .. "\r\n"
+ elseif id == 2 then
+ headers[data1] = data2
+ elseif id == 3 then
+ io.write(status)
+ for k, v in pairs(headers) do
+ io.write(k .. ": " .. v .. "\r\n")
+ end
+ io.write("\r\n")
+ elseif id == 4 then
+ io.write(data1)
+ elseif id == 5 then
+ active = false
+ end
+ end
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