* ffluci.sgi.haserl: Several fixes
[project/luci.git] / core / src / ffluci / sgi / haserl.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.haserl", package.seeall)
27
28 ENV = ENV or {}
29 FORM = FORM or {}
30 require("ffluci.util")
31
32 -- HTTP interface
33
34 -- Returns a table of all COOKIE, GET and POST Parameters
35 function ffluci.http.formvalues(prefix)
36 return FORM
37 end
38
39 -- Gets form value from key
40 function ffluci.http.formvalue(key, default)
41 local c = ffluci.http.formvalues()
42
43 for match in key:gmatch("[%w-_]+") do
44 c = c[match]
45 if c == nil then
46 return default
47 end
48 end
49
50 return c
51 end
52
53 -- Gets a table of values with a certain prefix
54 function ffluci.http.formvaluetable(prefix)
55 return ffluci.http.formvalue(prefix, {})
56 end
57
58
59 -- Returns the User's IP
60 function ffluci.http.get_remote_addr()
61 return ENV.REMOTE_ADDR
62 end
63
64 -- Returns the script name
65 function ffluci.http.get_script_name()
66 return ENV.SCRIPT_NAME
67 end
68
69
70 -- Asks the browser to redirect to "url"
71 function ffluci.http.redirect(url, qs)
72 if qs then
73 url = url .. "?" .. qs
74 end
75
76 ffluci.http.set_status(302, "Found")
77 print("Location: " .. url .. "\n")
78 end
79
80
81 -- Set Content-Type
82 function ffluci.http.set_content_type(type)
83 print("Content-Type: "..type.."\n")
84 end
85
86 -- Sets HTTP-Status-Header
87 function ffluci.http.set_status(code, message)
88 print("Status: " .. tostring(code) .. " " .. message)
89 end