2008-05-05 20:15:51 +00:00
|
|
|
--[[
|
2008-06-08 08:14:31 +00:00
|
|
|
LuCI - SGI-Module for Webuci
|
2008-05-05 20:15:51 +00:00
|
|
|
|
|
|
|
Description:
|
2008-06-08 08:14:31 +00:00
|
|
|
Server Gateway Interface for Webuci
|
2008-05-05 20:15:51 +00:00
|
|
|
|
|
|
|
FileId:
|
2008-05-08 15:37:41 +00:00
|
|
|
$Id: webuci.lua 2027 2008-05-07 21:16:35Z Cyrus $
|
2008-05-05 20:15:51 +00:00
|
|
|
|
|
|
|
License:
|
|
|
|
Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
]]--
|
2008-05-25 17:00:30 +00:00
|
|
|
module("luci.sgi.webuci", package.seeall)
|
2008-05-05 20:15:51 +00:00
|
|
|
|
2008-05-07 20:23:42 +00:00
|
|
|
local status_set = false
|
2008-05-05 20:15:51 +00:00
|
|
|
|
2008-05-26 09:45:12 +00:00
|
|
|
-- Initialize the environment
|
2008-05-28 15:28:13 +00:00
|
|
|
function initenv(env, vars)
|
2008-05-26 09:45:12 +00:00
|
|
|
luci.http.env = env
|
2008-05-28 15:28:13 +00:00
|
|
|
luci.http.vars = vars
|
2008-05-26 09:45:12 +00:00
|
|
|
end
|
|
|
|
|
2008-06-02 17:49:27 +00:00
|
|
|
-- 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 ""
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-05-17 10:00:44 +00:00
|
|
|
-- Returns the main dispatcher URL
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.dispatcher()
|
|
|
|
return luci.http.env.SCRIPT_NAME or ""
|
2008-05-17 10:00:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns the upload dispatcher URL
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.dispatcher_upload()
|
2008-05-17 10:00:44 +00:00
|
|
|
-- To be implemented
|
|
|
|
end
|
|
|
|
|
2008-05-05 20:15:51 +00:00
|
|
|
-- Returns a table of all COOKIE, GET and POST Parameters
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.formvalues()
|
2008-05-28 15:28:13 +00:00
|
|
|
return luci.http.vars
|
2008-05-05 20:15:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Gets form value from key
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.formvalue(key, default)
|
|
|
|
return luci.http.formvalues()[key] or default
|
2008-05-05 20:15:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Gets a table of values with a certain prefix
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.formvaluetable(prefix)
|
2008-05-05 20:15:51 +00:00
|
|
|
local vals = {}
|
|
|
|
prefix = prefix and prefix .. "." or "."
|
|
|
|
|
2008-05-25 17:00:30 +00:00
|
|
|
for k, v in pairs(luci.http.formvalues()) do
|
2008-05-05 20:15:51 +00:00
|
|
|
if k:find(prefix, 1, true) == 1 then
|
|
|
|
vals[k:sub(#prefix + 1)] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return vals
|
|
|
|
end
|
|
|
|
|
2008-05-07 21:08:16 +00:00
|
|
|
-- Sends a custom HTTP-Header
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.header(key, value)
|
2008-05-07 21:08:16 +00:00
|
|
|
print(key .. ": " .. value)
|
|
|
|
end
|
2008-05-05 20:15:51 +00:00
|
|
|
|
|
|
|
-- Set Content-Type
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.prepare_content(type)
|
2008-05-06 22:43:04 +00:00
|
|
|
if not status_set then
|
2008-05-25 17:00:30 +00:00
|
|
|
luci.http.status(200, "OK")
|
2008-05-06 22:43:04 +00:00
|
|
|
end
|
|
|
|
|
2008-05-05 20:15:51 +00:00
|
|
|
print("Content-Type: "..type.."\n")
|
|
|
|
end
|
|
|
|
|
2008-05-07 21:16:35 +00:00
|
|
|
-- Asks the browser to redirect to "url"
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.redirect(url)
|
|
|
|
luci.http.status(302, "Found")
|
|
|
|
luci.http.header("Location", url)
|
2008-05-07 21:16:35 +00:00
|
|
|
print()
|
|
|
|
end
|
|
|
|
|
2008-05-13 09:21:10 +00:00
|
|
|
-- Returns the path of an uploaded file
|
|
|
|
-- WARNING! File uploads can be easily spoofed! Do additional sanity checks!
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.upload(name)
|
2008-05-13 09:21:10 +00:00
|
|
|
-- To be implemented
|
|
|
|
end
|
|
|
|
|
2008-05-05 20:15:51 +00:00
|
|
|
-- Sets HTTP-Status-Header
|
2008-05-25 17:00:30 +00:00
|
|
|
function luci.http.status(code, message)
|
2008-05-26 09:45:12 +00:00
|
|
|
print(luci.http.env.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
|
2008-05-06 22:43:04 +00:00
|
|
|
status_set = true
|
2008-05-05 20:15:51 +00:00
|
|
|
end
|