* Generalized HTTP-API
authorSteven Barth <steven@midlink.org>
Sun, 15 Jun 2008 12:34:16 +0000 (12:34 +0000)
committerSteven Barth <steven@midlink.org>
Sun, 15 Jun 2008 12:34:16 +0000 (12:34 +0000)
applications/luci-statistics/luasrc/controller/luci_statistics/luci_statistics.lua
libs/sgi-haserl/luasrc/sgi/haserl.lua
libs/sgi-webuci/luasrc/sgi/webuci.lua
libs/sgi-wsapi/luasrc/sgi/wsapi.lua
libs/web/luasrc/http.lua

index 36c6d9b..aeee636 100644 (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
index 315b4da..88b7f00 100644 (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
index abe279d..8801cf4 100644 (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)
        
index e708249..f77ac5c 100644 (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 = {}
index 8ee864a..0319f10 100644 (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 = ""
-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