5e177195e17673915f41706da1bfe1c0fd653fdb
[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 -- Gets form value from key
58 function formvalue(key, default)
59 local c = formvalues()
60
61 for match in key:gmatch("%w+") do
62 c = c[match]
63 if c == nil then
64 return default
65 end
66 end
67
68 return c
69 end
70
71
72 -- Returns a table of all COOKIE, GET and POST Parameters
73 function formvalues()
74 return FORM
75 end
76
77
78 -- Prints plaintext content-type header
79 function textheader()
80 print("Content-Type: text/plain\n")
81 end
82
83
84 -- Prints html content-type header
85 function htmlheader()
86 print("Content-Type: text/html\n")
87 end
88
89
90 -- Prints xml content-type header
91 function xmlheader()
92 print("Content-Type: text/xml\n")
93 end