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