* Generalized HTTP-API

This commit is contained in:
Steven Barth 2008-06-15 12:34:16 +00:00
parent 8b0524cd91
commit 961bfcf77f
5 changed files with 81 additions and 41 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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 = {}

View file

@ -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 = ""
function Request.__init__(self, env, instream, errstream)
self.input = instream
self.error = errstream
-- 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.formvalue(self, name, default)
return self.request[name] or default
end
function Request.formvalues(self)
return self.request
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.upload(self, name)
return self.uploads[name]
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.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
end
]]--