35bc1c9025575eeff5f878842ab76ab79717c243
[project/luci.git] / libs / sgi-haserl / luasrc / sgi / haserl.lua
1 --[[
2 LuCI - SGI-Module for Haserl
3
4 Description:
5 Server Gateway Interface for Haserl
6
7 FileId:
8 $Id: haserl.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.haserl", package.seeall)
27 require("luci.fs")
28
29 -- Environment Table
30 luci.http.env = ENV
31
32 -- Enforces user authentification
33 function luci.http.basic_auth(verify_callback, realm)
34         -- Dummy for Haserl
35         return true
36 end
37
38 -- Returns the main dispatcher URL
39 function luci.http.dispatcher()
40         return luci.http.env.SCRIPT_NAME or ""
41 end
42
43 -- Returns the upload dispatcher URL
44 function luci.http.dispatcher_upload()
45         return luci.http.dispatcher() .. "-upload"
46 end
47
48 -- Returns a table of all COOKIE, GET and POST Parameters
49 function luci.http.formvalues()
50         return FORM
51 end
52
53 -- Gets form value from key
54 function luci.http.formvalue(key, default)
55         local c = luci.http.formvalues()
56         
57         for match in key:gmatch("[%w-_]+") do
58                 c = c[match]
59                 if c == nil then
60                         return default
61                 end
62         end
63         
64         return c
65 end
66
67 -- Gets a table of values with a certain prefix
68 function luci.http.formvaluetable(prefix)
69         return luci.http.formvalue(prefix, {})
70 end
71
72 -- Sends a custom HTTP-Header
73 function luci.http.header(key, value)
74         print(key .. ": " .. value)
75 end
76
77 -- Set Content-Type
78 function luci.http.prepare_content(type)
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         local fpath = luci.http.formvalue(name)
93         local fname = luci.http.formvalue(name .. "_name")
94         
95         if fpath and fname and luci.fs.isfile(fpath) then
96                 return fpath
97         end
98 end
99
100 -- Sets HTTP-Status-Header
101 function luci.http.status(code, message)
102         print("Status: " .. tostring(code) .. " " .. message)
103 end