* Last API changes before 0.4 API softfreeze

This commit is contained in:
Steven Barth 2008-05-07 20:23:42 +00:00
parent 54d92ebab5
commit 4264e6b780
17 changed files with 54 additions and 123 deletions

View file

@ -3,18 +3,19 @@ package.cpath = "/usr/lib/lua/?.so;" .. package.cpath
module("webuci", package.seeall)
function prepare_req(uri)
REQUEST_URI = uri
env = {}
env.REQUEST_URI = uri
require("ffluci.menu").get()
end
function init_req(context)
SERVER_PROTOCOL = context.server_proto
REMOTE_ADDR = context.remote_addr
REQUEST_METHOD = context.request_method
PATH_INFO = "/" .. context.uri
REMOTE_PORT = context.remote_port
SERVER_ADDR = context.server_addr
SCRIPT_NAME = REQUEST_URI:sub(1, #REQUEST_URI - #PATH_INFO)
env.SERVER_PROTOCOL = context.server_proto
env.REMOTE_ADDR = context.remote_addr
env.REQUEST_METHOD = context.request_method
env.PATH_INFO = "/" .. context.uri
env.REMOTE_PORT = context.remote_port
env.SERVER_ADDR = context.server_addr
env.SCRIPT_NAME = REQUEST_URI:sub(1, #REQUEST_URI - #PATH_INFO)
end
function handle_req(context)

View file

@ -104,7 +104,7 @@ function build_url(category, module, action)
module = module or "index"
action = action or "index"
local pattern = ffluci.http.get_script_name() .. "/%s/%s/%s"
local pattern = ffluci.http.env.SCRIPT_NAME .. "/%s/%s/%s"
return pattern:format(category, module, action)
end
@ -126,11 +126,11 @@ end
-- Sends a 404 error code and renders the "error404" template if available
function error404(message)
ffluci.http.set_status(404, "Not Found")
ffluci.http.status(404, "Not Found")
message = message or "Not Found"
if not pcall(ffluci.template.render, "error404") then
ffluci.http.set_content_type("text/plain")
ffluci.http.prepare_content("text/plain")
print(message)
end
return false
@ -138,10 +138,10 @@ end
-- Sends a 500 error code and renders the "error500" template if available
function error500(message)
ffluci.http.set_status(500, "Internal Server Error")
ffluci.http.status(500, "Internal Server Error")
if not pcall(ffluci.template.render, "error500", {message=message}) then
ffluci.http.set_content_type("text/plain")
ffluci.http.prepare_content("text/plain")
print(message)
end
return false
@ -150,7 +150,7 @@ end
-- Dispatches a request depending on the PATH_INFO variable
function httpdispatch()
local pathinfo = ffluci.http.get_path_info() or ""
local pathinfo = ffluci.http.env.PATH_INFO or ""
local parts = pathinfo:gmatch("/[%w-]+")
local sanitize = function(s, default)

View file

@ -53,26 +53,6 @@ function readfile(filename)
return data
end
-- Returns the content of file as array of lines
function readfilel(filename)
local fp, err = io.open(filename)
local line = ""
local data = {}
if fp == nil then
return nil, err
end
while true do
line = fp:read()
if (line == nil) then break end
table.insert(data, line)
end
fp:close()
return data
end
-- Writes given data to a file
function writefile(filename, data)
local fp, err = io.open(filename, "w")
@ -107,5 +87,8 @@ function dir(path)
return dir
end
-- Alias for lfs.mkdir
mkdir = posix.mkdir
-- Alias for posix.mkdir
mkdir = posix.mkdir
-- Alias for posix.rmdir
rmdir = posix.rmdir

View file

@ -33,4 +33,14 @@ if ENV and ENV.HASERLVER then
require("ffluci.sgi.haserl")
elseif webuci then
require("ffluci.sgi.webuci")
end
-- Asks the browser to redirect to "url"
function redirect(url, qs)
if qs then
url = url .. "?" .. qs
end
ffluci.http.status(302, "Found")
print("Location: " .. url .. "\n")
end

View file

@ -25,7 +25,6 @@ limitations under the License.
]]--
module("ffluci.i18n", package.seeall)
require("ffluci.config")
require("ffluci.sys")
table = {}
@ -50,7 +49,7 @@ end
-- Same as load but autocompletes the filename with .LANG from config.lang
function loadc(file)
return load(file .. "." .. ffluci.config.main.lang)
return load(file .. "." .. require("ffluci.config").main.lang)
end
-- Returns the i18n-value defined by "key" or if there is no such: "default"

View file

@ -36,7 +36,7 @@ modelpath = ffluci.sys.libpath() .. "/model/menu/"
scope = {
translate = function(...) return require("ffluci.i18n").translate(...) end,
loadtrans = function(...) return require("ffluci.i18n").loadc(...) end,
isfile = ffluci.fs.mtime
isfile = ffluci.fs.isfile
}
-- Local menu database

View file

@ -25,10 +25,9 @@ limitations under the License.
]]--
module("ffluci.sgi.haserl", package.seeall)
ENV = ENV or {}
FORM = FORM or {}
-- Environment Table
ffluci.http.env = ENV
-- HTTP interface
-- Returns a table of all COOKIE, GET and POST Parameters
function ffluci.http.formvalues()
@ -54,44 +53,13 @@ function ffluci.http.formvaluetable(prefix)
return ffluci.http.formvalue(prefix, {})
end
-- Returns the path info
function ffluci.http.get_path_info()
return ENV.PATH_INFO
end
-- Returns the User's IP
function ffluci.http.get_remote_addr()
return ENV.REMOTE_ADDR
end
-- Returns the request URI
function ffluci.http.get_request_uri()
return ENV.REQUEST_URI
end
-- Returns the script name
function ffluci.http.get_script_name()
return ENV.SCRIPT_NAME
end
-- Asks the browser to redirect to "url"
function ffluci.http.redirect(url, qs)
if qs then
url = url .. "?" .. qs
end
ffluci.http.set_status(302, "Found")
print("Location: " .. url .. "\n")
end
-- Set Content-Type
function ffluci.http.set_content_type(type)
function ffluci.http.prepare_content(type)
print("Content-Type: "..type.."\n")
end
-- Sets HTTP-Status-Header
function ffluci.http.set_status(code, message)
function ffluci.http.status(code, message)
print("Status: " .. tostring(code) .. " " .. message)
end

View file

@ -25,9 +25,11 @@ limitations under the License.
]]--
module("ffluci.sgi.webuci", package.seeall)
local status_set = false
-- Environment Table
ffluci.http.env = webuci.env
-- HTTP interface
local status_set = false
-- Returns a table of all COOKIE, GET and POST Parameters
function ffluci.http.formvalues()
@ -53,50 +55,18 @@ function ffluci.http.formvaluetable(prefix)
return vals
end
-- Returns the path info
function ffluci.http.get_path_info()
return webuci.PATH_INFO
end
-- Returns the User's IP
function ffluci.http.get_remote_addr()
return webuci.REMOTE_ADDR
end
-- Returns the request URI
function ffluci.http.get_request_uri()
return webuci.REQUEST_URI
end
-- Returns the script name
function ffluci.http.get_script_name()
return webuci.SCRIPT_NAME
end
-- Asks the browser to redirect to "url"
function ffluci.http.redirect(url, qs)
if qs then
url = url .. "?" .. qs
end
ffluci.http.set_status(302, "Found")
print("Location: " .. url .. "\n")
end
-- Set Content-Type
function ffluci.http.set_content_type(type)
function ffluci.http.prepare_content(type)
if not status_set then
ffluci.http.set_status(200, "OK")
ffluci.http.status(200, "OK")
end
print("Content-Type: "..type.."\n")
end
-- Sets HTTP-Status-Header
function ffluci.http.set_status(code, message)
print(webuci.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
function ffluci.http.status(code, message)
print(webuci.env.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
status_set = true
end

View file

@ -52,7 +52,7 @@ compiler_enable_bytecode = false
viewns = {
translate = function(...) return require("ffluci.i18n").translate(...) end,
config = function(...) return require("ffluci.model.uci").get(...) or "" end,
controller = ffluci.http.get_script_name(),
controller = ffluci.http.env.SCRIPT_NAME,
media = ffluci.config.main.mediaurlbase,
write = io.write,
include = function(name) Template(name):render(getfenv(2)) end,

View file

@ -170,8 +170,8 @@ function split(str, pat, max, regex)
end
-- Removes whitespace from beginning and end of a string
function trim(string)
local s = string:gsub("^%s*(.-)%s*$", "%1")
function trim(str)
local s = str:gsub("^%s*(.-)%s*$", "%1")
return s
end

View file

@ -1,5 +1,5 @@
<%+header%>
<form method="post" action="<%=ffluci.http.get_request_uri()%>">
<form method="post" action="<%=ffluci.http.env.REQUEST_URI%>">
<div>
<script type="text/javascript" src="<%=media%>/cbi.js"></script>
<input type="hidden" name="cbi.submit" value="1" />

View file

@ -1,5 +1,5 @@
<%+header%>
<h1>404 Not Found</h1>
<p>Sorry, the object you requested was not found.</p>
<tt>Unable to dispatch: <%=ffluci.http.get_path_info()%></tt>
<tt>Unable to dispatch: <%=ffluci.http.env.PATH_INFO%></tt>
<%+footer%>

View file

@ -5,7 +5,7 @@ local req = require("ffluci.dispatcher").request
local menu = require("ffluci.menu").get()[req.category]
menu = menu or {}
require("ffluci.i18n").loadc("default")
require("ffluci.http").set_content_type("text/html")
require("ffluci.http").prepare_content("text/html")
%><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View file

@ -1,7 +1,7 @@
module("ffluci.controller.splash.splash", package.seeall)
function action_activate()
local mac = ffluci.sys.net.ip4mac(ffluci.http.remote_addr())
local mac = ffluci.sys.net.ip4mac(ffluci.http.env.REMOTE_ADDR)
if mac and ffluci.http.formvalue("accept") then
os.execute("luci-splash add "..mac.." >/dev/null 2>&1")
ffluci.http.redirect(ffluci.model.uci.get("freifunk", "community", "homepage"))

View file

@ -1,5 +1,5 @@
<%
ffluci.http.set_content_type("text/plain")
ffluci.http.prepare_content("text/plain")
for k, v in pairs(ffluci.sys.wifi.getiwconfig()) do
%>
<tr>

View file

@ -1,5 +1,5 @@
<%
ffluci.http.set_content_type("text/plain")
ffluci.http.prepare_content("text/plain")
for iface, cells in pairs(ffluci.sys.wifi.iwscan()) do
for i, cell in ipairs(cells) do
%>

View file

@ -3,7 +3,7 @@ module("ffluci.controller.rpc.luciinfo", package.seeall)
function action_index()
local uci = ffluci.model.uci.StateSession()
ffluci.http.set_content_type("text/plain")
ffluci.http.prepare_content("text/plain")
-- General
print("luciinfo.api=1")