c60964662e8b8f14761ea358b8ca4fe17408f964
[project/luci.git] / core / src / ffluci / sgi / webuci.lua
1 --[[
2 FFLuCI - SGI-Module for Haserl
3
4 Description:
5 Server Gateway Interface for Haserl
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26 module("ffluci.sgi.webuci", package.seeall)
27
28 -- HTTP interface
29
30 -- Returns a table of all COOKIE, GET and POST Parameters
31 function ffluci.http.formvalues()
32         return webuci.vars
33 end
34
35 -- Gets form value from key
36 function ffluci.http.formvalue(key, default)
37         return ffluci.http.formvalues()[key] or default
38 end
39
40 -- Gets a table of values with a certain prefix
41 function ffluci.http.formvaluetable(prefix)
42         local vals = {}
43         prefix = prefix and prefix .. "." or "."
44         
45         for k, v in pairs(ffluci.http.formvalues()) do
46                 if k:find(prefix, 1, true) == 1 then
47                         vals[k:sub(#prefix + 1)] = v
48                 end
49         end
50         
51         return vals
52 end
53
54 -- Returns the path info
55 function ffluci.http.get_path_info()
56         return webuci.PATH_INFO
57 end
58
59 -- Returns the User's IP
60 function ffluci.http.get_remote_addr()
61         return webuci.REMOTE_ADDR
62 end
63
64 -- Returns the request URI
65 function ffluci.http.get_request_uri()
66         return webuci.REQUEST_URI
67 end
68
69
70 -- Returns the script name
71 function ffluci.http.get_script_name()
72         return webuci.SCRIPT_NAME
73 end
74
75
76 -- Asks the browser to redirect to "url"
77 function ffluci.http.redirect(url, qs)
78         if qs then
79                 url = url .. "?" .. qs
80         end
81         
82         ffluci.http.set_status(302, "Found")
83         print("Location: " .. url .. "\n")
84 end
85
86
87 -- Set Content-Type
88 function ffluci.http.set_content_type(type)
89         print("Content-Type: "..type.."\n")
90 end
91
92 -- Sets HTTP-Status-Header
93 function ffluci.http.set_status(code, message)
94         print(webuci.REQUEST_METHOD .. " " .. tostring(code) .. " " .. message)
95 end