* Added initial version of RPC info API
[project/luci.git] / core / 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 ENV = ENV or {}
31 FORM = FORM or {}
32 module("ffluci.http", package.seeall)
33
34 require("ffluci.util")
35
36 -- Sets HTTP-Status-Header
37 function status(code, message)
38         print("Status: " .. tostring(code) .. " " .. message)
39 end
40
41
42 -- Asks the browser to redirect to "url"
43 function redirect(url, qs)
44         if qs then
45                 url = url .. "?" .. qs
46         end
47         
48         status(302, "Found")
49         print("Location: " .. url .. "\n")
50 end
51
52
53 -- Same as redirect but accepts category, module and action for internal use
54 function request_redirect(category, module, action, ...)
55         category = category or "public"
56         module   = module   or "index"
57         action   = action   or "index"
58         
59         local pattern = script_name() .. "/%s/%s/%s"
60         redirect(pattern:format(category, module, action), ...)
61 end
62
63
64 -- Returns the User's IP
65 function remote_addr()
66         return ENV.REMOTE_ADDR
67 end
68
69
70 -- Returns the script name
71 function script_name()
72         return ENV.SCRIPT_NAME
73 end
74
75
76 -- Gets form value from key
77 function formvalue(key, default)
78         local c = formvalues()
79         
80         for match in key:gmatch("[%w-_]+") do
81                 c = c[match]
82                 if c == nil then
83                         return default
84                 end
85         end
86         
87         return c
88 end
89
90
91 -- Returns a table of all COOKIE, GET and POST Parameters
92 function formvalues()
93         return FORM
94 end
95
96
97 -- Prints plaintext content-type header
98 function textheader()
99         print("Content-Type: text/plain\n")
100 end
101
102
103 -- Prints html content-type header
104 function htmlheader()
105         print("Content-Type: text/html\n")
106 end
107
108
109 -- Prints xml content-type header
110 function xmlheader()
111         print("Content-Type: text/xml\n")
112 end