commit 4f6198094cf4134179d1f9c9fa8f79759a27c87e
[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 -- Returns the main dispatcher URL
33 function luci.http.dispatcher()
34 return luci.http.env.SCRIPT_NAME or ""
35 end
36
37 -- Returns the upload dispatcher URL
38 function luci.http.dispatcher_upload()
39 return luci.http.dispatcher() .. "-upload"
40 end
41
42 -- Returns a table of all COOKIE, GET and POST Parameters
43 function luci.http.formvalues()
44 return FORM
45 end
46
47 -- Gets form value from key
48 function luci.http.formvalue(key, default)
49 local c = luci.http.formvalues()
50
51 for match in key:gmatch("[%w-_]+") do
52 c = c[match]
53 if c == nil then
54 return default
55 end
56 end
57
58 return c
59 end
60
61 -- Gets a table of values with a certain prefix
62 function luci.http.formvaluetable(prefix)
63 return luci.http.formvalue(prefix, {})
64 end
65
66 -- Sends a custom HTTP-Header
67 function luci.http.header(key, value)
68 print(key .. ": " .. value)
69 end
70
71 -- Set Content-Type
72 function luci.http.prepare_content(type)
73 print("Content-Type: "..type.."\n")
74 end
75
76 -- Asks the browser to redirect to "url"
77 function luci.http.redirect(url)
78 luci.http.status(302, "Found")
79 luci.http.header("Location", url)
80 print()
81 end
82
83 -- Returns the path of an uploaded file
84 -- WARNING! File uploads can be easily spoofed! Do additional sanity checks!
85 function luci.http.upload(name)
86 local fpath = luci.http.formvalue(name)
87 local fname = luci.http.formvalue(name .. "_name")
88
89 if fpath and fname and luci.fs.isfile(fpath) then
90 return fpath
91 end
92 end
93
94 -- Sets HTTP-Status-Header
95 function luci.http.status(code, message)
96 print("Status: " .. tostring(code) .. " " .. message)
97 end