From 961bfcf77f6bf114e721a418220c2d6a3ef1379c Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Sun, 15 Jun 2008 12:34:16 +0000 Subject: [PATCH] * Generalized HTTP-API --- .../controller/luci_statistics/luci_statistics.lua | 4 +- libs/sgi-haserl/luasrc/sgi/haserl.lua | 6 +- libs/sgi-webuci/luasrc/sgi/webuci.lua | 6 +- libs/sgi-wsapi/luasrc/sgi/wsapi.lua | 9 +- libs/web/luasrc/http.lua | 97 +++++++++++++++------- 5 files changed, 81 insertions(+), 41 deletions(-) diff --git a/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua b/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua index 36c6d9b57..aeee63630 100644 --- a/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua +++ b/applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua @@ -80,7 +80,7 @@ function index() page.setuser = "nobody" page.setgroup = "nogroup" - local vars = luci.http.formvalues() + local vars = luci.http.getvalue() local span = vars.timespan or nil for i, plugin in luci.util.vspairs( tree:plugins() ) do @@ -148,7 +148,7 @@ function statistics_render() require("luci.template") require("luci.model.uci") - local vars = luci.http.formvalues() + local vars = luci.http.getvalue() local req = luci.dispatcher.context.request local path = luci.dispatcher.context.dispatched.path local uci = luci.model.uci diff --git a/libs/sgi-haserl/luasrc/sgi/haserl.lua b/libs/sgi-haserl/luasrc/sgi/haserl.lua index 315b4da73..88b7f0075 100644 --- a/libs/sgi-haserl/luasrc/sgi/haserl.lua +++ b/libs/sgi-haserl/luasrc/sgi/haserl.lua @@ -29,9 +29,9 @@ require("luci.util") require("luci.dispatcher") function run() - local r = luci.http.Request() - r.env = ENV - r.request = normalize_table(FORM) + local r = luci.http.Request(ENV, nil, io.stderr) + r.get = normalize_table(FORM) + r.post = r.get local x = coroutine.create(luci.dispatcher.httpdispatch) while coroutine.status(x) ~= "dead" do diff --git a/libs/sgi-webuci/luasrc/sgi/webuci.lua b/libs/sgi-webuci/luasrc/sgi/webuci.lua index abe279d05..8801cf447 100644 --- a/libs/sgi-webuci/luasrc/sgi/webuci.lua +++ b/libs/sgi-webuci/luasrc/sgi/webuci.lua @@ -29,9 +29,9 @@ require("luci.util") require("luci.dispatcher") function run(env, vars) - local r = luci.http.Request() - r.env = env - r.request = vars + local r = luci.http.Request(env, nil, io.stderr) + r.get = vars + r.post = r.get local x = coroutine.create(luci.dispatcher.httpdispatch) diff --git a/libs/sgi-wsapi/luasrc/sgi/wsapi.lua b/libs/sgi-wsapi/luasrc/sgi/wsapi.lua index e7082493c..f77ac5c5b 100644 --- a/libs/sgi-wsapi/luasrc/sgi/wsapi.lua +++ b/libs/sgi-wsapi/luasrc/sgi/wsapi.lua @@ -26,13 +26,12 @@ limitations under the License. module("luci.sgi.wsapi", package.seeall) require("luci.http") require("luci.dispatcher") -require("wsapi.request") +require("luci.http.protocol") function run(wsapi_env) - local r = luci.http.Request() - r.env = wsapi_env - r.request = wsapi.request.parse_post_data(wsapi_env, - wsapi.request.parse_qs(wsapi_env.QUERY_STRING)) + local r = luci.http.Request(wsapi_env, wsapi_env.input, wsapi_env.error) + r.postds = function() return wsapi.request.parse_post_data(wsapi_env) end + r.getds = function() return wsapi.request.parse_qs(wsapi_env.QUERY_STRING) end local res, id, data1, data2 = true, 0, nil, nil local headers = {} diff --git a/libs/web/luasrc/http.lua b/libs/web/luasrc/http.lua index 8ee864ac7..0319f104f 100644 --- a/libs/web/luasrc/http.lua +++ b/libs/web/luasrc/http.lua @@ -28,34 +28,59 @@ limitations under the License. ]]-- module("luci.http", package.seeall) +require("luci.http.protocol") require("luci.util") + context = luci.util.threadlocal() Request = luci.util.class() -function Request.__init__(self) - self.headers = {} - self.request = {} - self.uploads = {} - self.env = {} - self.data = "" -end +function Request.__init__(self, env, instream, errstream) + self.input = instream + self.error = errstream -function Request.formvalue(self, name, default) - return self.request[name] or default + -- Formdata tables + self.get = {} + self.post = {} + + -- File handler + self.filehandler = function() end + + -- Environment table + self.env = env + + setmetatable(self.get, {__index = + function(tbl, key) + tbl = luci.http.protocol.urldecode_params(self.env.QUERY_STRING) + setmetatable(tbl, nil) + return rawget(tbl, key) + end }) + + setmetatable(self.post, {__index = + function(tbl, key) + tbl = luci.http.protocol. + setmetatable(tbl, nil) + return rawget(tbl, key) + end }) end -function Request.formvalues(self) - return self.request +function Request.formvalue(self, name, default) + return tostring(self.post[name] or self.get[name] or default) end function Request.formvaluetable(self, prefix) local vals = {} prefix = prefix and prefix .. "." or "." - for k, v in pairs(self.request) do + for k, v in pairs(self.getvalue()) do if k:find(prefix, 1, true) == 1 then - vals[k:sub(#prefix + 1)] = v + vals[k:sub(#prefix + 1)] = tostring(v) + end + end + + for k, v in pairs(self.postvalue()) do + if k:find(prefix, 1, true) == 1 then + vals[k:sub(#prefix + 1)] = tostring(v) end end @@ -63,11 +88,21 @@ function Request.formvaluetable(self, prefix) end function Request.getenv(self, name) - return self.env[name] + return name and self.env[name] or self.env +end + +function Request.getvalue(self, name) + local void = self.get[nil] + return name and self.get[name] or self.get +end + +function Request.postvalue(self, name) + local void = self.post[nil] + return name and self.post[name] or self.post end -function Request.upload(self, name) - return self.uploads[name] +function Request.setfilehandler(self, callback) + self.filehandler = callback end @@ -87,18 +122,26 @@ function formvalue(...) return context.request:formvalue(...) end -function formvalues(...) - return context.request:formvalues(...) -end - function formvaluetable(...) return context.request:formvaluetable(...) end +function getvalue(...) + return context.request:getvalue(...) +end + +function postvalue(...) + return context.request:postvalue(...) +end + function getenv(...) return context.request:getenv(...) end +function setfilehandler(...) + return context.request:setfilehandler(...) +end + function header(key, value) if not context.status then status() @@ -157,22 +200,19 @@ function redirect(url) close() end -function upload(...) - return context.request:upload(...) -end - - - function build_querystring(table) local s="?" for k, v in pairs(table) do - s = s .. k .. "=" .. v .. "&" + s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&" end return s end +urldecode = luci.http.protocol.urldecode +urlencode = luci.http.protocol.urlencode +--[[ function urldecode(str) str = str:gsub("+", " ") str = str:gsub("%%(%x%x)", @@ -187,4 +227,5 @@ function urlencode(str) function (c) return string.format ("%%%02X", string.byte(c)) end) str = str:gsub(" ", "+") return str -end \ No newline at end of file +end +]]-- \ No newline at end of file -- 2.11.0