Renamed FFLuCI to LuCI, ffluci to luci and Freifunk Lua Configuration Interface to...
[project/luci.git] / applications / sgi-webuci / src / sgi / webuci.lua
1 --[[
2 LuCI - SGI-Module for Haserl
3
4 Description:
5 Server Gateway Interface for Haserl
6
7 FileId:
8 $Id: webuci.lua 2027 2008-05-07 21:16:35Z Cyrus $
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("luci.sgi.webuci", package.seeall)
27
28 -- Environment Table
29 luci.http.env = webuci.env
30
31
32 local status_set = false
33
34 -- Returns the main dispatcher URL
35 function luci.http.dispatcher()
36         return luci.http.env.SCRIPT_NAME or ""
37 end
38
39 -- Returns the upload dispatcher URL
40 function luci.http.dispatcher_upload()
41         -- To be implemented
42 end
43
44 -- Returns a table of all COOKIE, GET and POST Parameters
45 function luci.http.formvalues()
46         return webuci.vars
47 end
48
49 -- Gets form value from key
50 function luci.http.formvalue(key, default)
51         return luci.http.formvalues()[key] or default
52 end
53
54 -- Gets a table of values with a certain prefix
55 function luci.http.formvaluetable(prefix)
56         local vals = {}
57         prefix = prefix and prefix .. "." or "."
58         
59         for k, v in pairs(luci.http.formvalues()) do
60                 if k:find(prefix, 1, true) == 1 then
61                         vals[k:sub(#prefix + 1)] = v
62                 end
63         end
64         
65         return vals
66 end
67
68 -- Sends a custom HTTP-Header
69 function luci.http.header(key, value)
70         print(key .. ": " .. value)
71 end
72
73 -- Set Content-Type
74 function luci.http.prepare_content(type)
75         if not status_set then
76                 luci.http.status(200, "OK")
77         end
78         
79         print("Content-Type: "..type.."\n")
80 end
81
82 -- Asks the browser to redirect to "url"
83 function luci.http.redirect(url)
84         luci.http.status(302, "Found")
85         luci.http.header("Location", url)
86         print()
87 end
88
89 -- Returns the path of an uploaded file
90 -- WARNING! File uploads can be easily spoofed! Do additional sanity checks!
91 function luci.http.upload(name)
92         -- To be implemented
93 end
94
95 -- Sets HTTP-Status-Header
96 function luci.http.status(code, message)
97         print(webuci.env.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
98         status_set = true
99 end