498bca9214d58d0b3bcb3061f5b68f64f9ad44f3
[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 local status_set = false
29
30 -- Initialize the environment
31 function initenv(env)
32         luci.http.env = env
33 end
34
35 -- Returns the main dispatcher URL
36 function luci.http.dispatcher()
37         return luci.http.env.SCRIPT_NAME or ""
38 end
39
40 -- Returns the upload dispatcher URL
41 function luci.http.dispatcher_upload()
42         -- To be implemented
43 end
44
45 -- Returns a table of all COOKIE, GET and POST Parameters
46 function luci.http.formvalues()
47         return webuci.vars
48 end
49
50 -- Gets form value from key
51 function luci.http.formvalue(key, default)
52         return luci.http.formvalues()[key] or default
53 end
54
55 -- Gets a table of values with a certain prefix
56 function luci.http.formvaluetable(prefix)
57         local vals = {}
58         prefix = prefix and prefix .. "." or "."
59         
60         for k, v in pairs(luci.http.formvalues()) do
61                 if k:find(prefix, 1, true) == 1 then
62                         vals[k:sub(#prefix + 1)] = v
63                 end
64         end
65         
66         return vals
67 end
68
69 -- Sends a custom HTTP-Header
70 function luci.http.header(key, value)
71         print(key .. ": " .. value)
72 end
73
74 -- Set Content-Type
75 function luci.http.prepare_content(type)
76         if not status_set then
77                 luci.http.status(200, "OK")
78         end
79         
80         print("Content-Type: "..type.."\n")
81 end
82
83 -- Asks the browser to redirect to "url"
84 function luci.http.redirect(url)
85         luci.http.status(302, "Found")
86         luci.http.header("Location", url)
87         print()
88 end
89
90 -- Returns the path of an uploaded file
91 -- WARNING! File uploads can be easily spoofed! Do additional sanity checks!
92 function luci.http.upload(name)
93         -- To be implemented
94 end
95
96 -- Sets HTTP-Status-Header
97 function luci.http.status(code, message)
98         print(luci.http.env.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
99         status_set = true
100 end