7aadf8b33d5e3b4f1b875bfa58035492b82dc664
[project/luci.git] / src / ffluci / http.lua
1 --[[
2 FFLuCI - HTTP-Interaction
3
4 Description:
5 HTTP-Header manipulator and form variable preprocessor
6
7 FileId:
8 $Id$
9
10 ToDo:
11 - Cookie handling
12
13 License:
14 Copyright 2008 Steven Barth <steven@midlink.org>
15
16 Licensed under the Apache License, Version 2.0 (the "License");
17 you may not use this file except in compliance with the License.
18 You may obtain a copy of the License at
19
20 http://www.apache.org/licenses/LICENSE-2.0
21
22 Unless required by applicable law or agreed to in writing, software
23 distributed under the License is distributed on an "AS IS" BASIS,
24 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 See the License for the specific language governing permissions and
26 limitations under the License.
27
28 ]]--
29
30 module("ffluci.http", package.seeall)
31
32 require("ffluci.util")
33
34 -- Sets HTTP-Status-Header
35 function status(code, message)
36 print("Status: " .. tostring(code) .. " " .. message)
37 end
38
39
40 -- Asks the browser to redirect to "url"
41 function redirect(url)
42 status(302, "Found")
43 print("Location: " .. url .. "\n")
44 end
45
46
47 -- Same as redirect but accepts category, module and action for internal use
48 function request_redirect(category, module, action)
49 category = category or "public"
50 module = module or "index"
51 action = action or "index"
52
53 local pattern = os.getenv("SCRIPT_NAME") .. "/%s/%s/%s"
54 redirect(pattern:format(category, module, action))
55 end
56
57 -- Form validation function:
58 -- Gets a form variable "key".
59 -- If it does not exist: return "default"
60 -- If cast_number is true and "key" is not a number: return "default"
61 -- If valid is a table and "key" is not in it: return "default"
62 -- If valid is a function and returns nil: return "default"
63 -- Else return the value of "key"
64 --
65 -- Examples:
66 -- Get a form variable "foo" and return "bar" if it is not set
67 -- = formvalue("foo", "bar")
68 --
69 -- Get "foo" and make sure it is either "bar" or "baz"
70 -- = formvalue("foo", nil, nil, {"bar", "baz"})
71 --
72 -- Get "foo", make sure its a number and below 10 else return 5
73 -- = formvalue("foo", 5, true, function(a) return a < 10 and a or nil end)
74 function formvalue(key, default, cast_number, valid, table)
75 table = table or formvalues()
76
77 if table[key] == nil then
78 return default
79 else
80 local value = table[key]
81
82 value = cast_number and tonumber(value) or not cast_number and nil
83
84 if type(valid) == "function" then
85 value = valid(value)
86 elseif type(valid) == "table" then
87 if not ffluci.util.contains(valid, value) then
88 value = nil
89 end
90 end
91
92 return value or default
93 end
94 end
95
96
97 -- Returns a table of all COOKIE, GET and POST Parameters
98 function formvalues()
99 return FORM
100 end
101
102
103 -- Prints plaintext content-type header
104 function textheader()
105 print("Content-Type: text/plain\n")
106 end
107
108
109 -- Prints html content-type header
110 function htmlheader()
111 print("Content-Type: text/html\n")
112 end
113
114
115 -- Prints xml content-type header
116 function xmlheader()
117 print("Content-Type: text/xml\n")
118 end