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