From 4264e6b7808066686223d0294bd01a84659f9f38 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Wed, 7 May 2008 20:23:42 +0000 Subject: [PATCH] * Last API changes before 0.4 API softfreeze --- core/contrib/webuci_bootstrap.lua | 17 +++---- core/src/ffluci/dispatcher.lua | 12 ++--- core/src/ffluci/fs.lua | 27 ++--------- core/src/ffluci/http.lua | 10 ++++ core/src/ffluci/i18n.lua | 3 +- core/src/ffluci/menu.lua | 2 +- core/src/ffluci/sgi/haserl.lua | 40 ++-------------- core/src/ffluci/sgi/webuci.lua | 46 ++++--------------- core/src/ffluci/template.lua | 2 +- core/src/ffluci/util.lua | 4 +- core/src/ffluci/view/cbi/header.htm | 2 +- core/src/ffluci/view/error404.htm | 2 +- core/src/ffluci/view/header.htm | 2 +- .../src/controller/splash/splash.lua | 2 +- .../src/view/sudo_status/iwconfig.htm | 2 +- .../src/view/sudo_status/iwscan.htm | 2 +- .../rpc-core/src/controller/rpc/luciinfo.lua | 2 +- 17 files changed, 54 insertions(+), 123 deletions(-) diff --git a/core/contrib/webuci_bootstrap.lua b/core/contrib/webuci_bootstrap.lua index c8aa18cca..9a2fafb0e 100644 --- a/core/contrib/webuci_bootstrap.lua +++ b/core/contrib/webuci_bootstrap.lua @@ -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) diff --git a/core/src/ffluci/dispatcher.lua b/core/src/ffluci/dispatcher.lua index 813e35d59..c60e5dcd1 100644 --- a/core/src/ffluci/dispatcher.lua +++ b/core/src/ffluci/dispatcher.lua @@ -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) diff --git a/core/src/ffluci/fs.lua b/core/src/ffluci/fs.lua index 3eb562b80..ffa4cb84f 100644 --- a/core/src/ffluci/fs.lua +++ b/core/src/ffluci/fs.lua @@ -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 \ No newline at end of file +-- Alias for posix.mkdir +mkdir = posix.mkdir + +-- Alias for posix.rmdir +rmdir = posix.rmdir \ No newline at end of file diff --git a/core/src/ffluci/http.lua b/core/src/ffluci/http.lua index f4ba57094..eab12e8f8 100644 --- a/core/src/ffluci/http.lua +++ b/core/src/ffluci/http.lua @@ -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 \ No newline at end of file diff --git a/core/src/ffluci/i18n.lua b/core/src/ffluci/i18n.lua index 489308cc9..88381dde7 100644 --- a/core/src/ffluci/i18n.lua +++ b/core/src/ffluci/i18n.lua @@ -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" diff --git a/core/src/ffluci/menu.lua b/core/src/ffluci/menu.lua index 5cbb725a4..5724b2cb9 100644 --- a/core/src/ffluci/menu.lua +++ b/core/src/ffluci/menu.lua @@ -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 diff --git a/core/src/ffluci/sgi/haserl.lua b/core/src/ffluci/sgi/haserl.lua index e58189d1c..0db558d99 100644 --- a/core/src/ffluci/sgi/haserl.lua +++ b/core/src/ffluci/sgi/haserl.lua @@ -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 diff --git a/core/src/ffluci/sgi/webuci.lua b/core/src/ffluci/sgi/webuci.lua index 222304b41..75fffa553 100644 --- a/core/src/ffluci/sgi/webuci.lua +++ b/core/src/ffluci/sgi/webuci.lua @@ -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 diff --git a/core/src/ffluci/template.lua b/core/src/ffluci/template.lua index a972f2bc4..7ac47cbbe 100644 --- a/core/src/ffluci/template.lua +++ b/core/src/ffluci/template.lua @@ -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, diff --git a/core/src/ffluci/util.lua b/core/src/ffluci/util.lua index b76278dda..9e3c7f25e 100644 --- a/core/src/ffluci/util.lua +++ b/core/src/ffluci/util.lua @@ -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 diff --git a/core/src/ffluci/view/cbi/header.htm b/core/src/ffluci/view/cbi/header.htm index 97542f031..3b615d729 100644 --- a/core/src/ffluci/view/cbi/header.htm +++ b/core/src/ffluci/view/cbi/header.htm @@ -1,5 +1,5 @@ <%+header%> -
+
diff --git a/core/src/ffluci/view/error404.htm b/core/src/ffluci/view/error404.htm index 1a9d74a9f..51ea176d6 100644 --- a/core/src/ffluci/view/error404.htm +++ b/core/src/ffluci/view/error404.htm @@ -1,5 +1,5 @@ <%+header%>

404 Not Found

Sorry, the object you requested was not found.

-Unable to dispatch: <%=ffluci.http.get_path_info()%> +Unable to dispatch: <%=ffluci.http.env.PATH_INFO%> <%+footer%> \ No newline at end of file diff --git a/core/src/ffluci/view/header.htm b/core/src/ffluci/view/header.htm index 7ed735de5..bc65e3e89 100644 --- a/core/src/ffluci/view/header.htm +++ b/core/src/ffluci/view/header.htm @@ -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") %> diff --git a/module/admin-core/src/controller/splash/splash.lua b/module/admin-core/src/controller/splash/splash.lua index 7e3d67ee9..62088be52 100644 --- a/module/admin-core/src/controller/splash/splash.lua +++ b/module/admin-core/src/controller/splash/splash.lua @@ -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")) diff --git a/module/public-core/src/view/sudo_status/iwconfig.htm b/module/public-core/src/view/sudo_status/iwconfig.htm index 92371ef60..6f5dede70 100644 --- a/module/public-core/src/view/sudo_status/iwconfig.htm +++ b/module/public-core/src/view/sudo_status/iwconfig.htm @@ -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 %> diff --git a/module/public-core/src/view/sudo_status/iwscan.htm b/module/public-core/src/view/sudo_status/iwscan.htm index f4268574b..6f321ae9e 100644 --- a/module/public-core/src/view/sudo_status/iwscan.htm +++ b/module/public-core/src/view/sudo_status/iwscan.htm @@ -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 %> diff --git a/module/rpc-core/src/controller/rpc/luciinfo.lua b/module/rpc-core/src/controller/rpc/luciinfo.lua index d4e5cb306..611423fbb 100644 --- a/module/rpc-core/src/controller/rpc/luciinfo.lua +++ b/module/rpc-core/src/controller/rpc/luciinfo.lua @@ -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") -- 2.25.1