2 LuCI - HTTP-Interaction
5 HTTP-Header manipulator and form variable preprocessor
14 Copyright 2008 Steven Barth <steven@midlink.org>
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
20 http://www.apache.org/licenses/LICENSE-2.0
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.
30 module("luci.http", package.seeall)
31 require("luci.http.protocol")
34 context = luci.util.threadlocal()
37 Request = luci.util.class()
38 function Request.__init__(self, env, instream, errstream)
40 self.error = errstream
42 -- Provide readline function
43 self.inputreader = self.input.readline
44 or self.input.read and function() return self.input:read() end
45 or self.input.receive and function() return self.input:receive() end
46 or function() return nil end
49 self.filehandler = function() end
55 params = luci.http.protocol.urldecode_params("?"..(env.QUERY_STRING or "")),
58 setmetatable(self.message.params, {__index =
60 luci.http.protocol.parse_message_body(
66 setmetatable(tbl, nil)
67 return rawget(tbl, key)
72 function Request.formvalue(self, name, default)
74 return self.message.params[name] and tostring(self.message.params[name]) or default
76 return self.message.params
80 function Request.formvaluetable(self, prefix)
82 prefix = prefix and prefix .. "." or "."
84 local void = self.message.params[nil]
85 for k, v in pairs(self.message.params) do
86 if k:find(prefix, 1, true) == 1 then
87 vals[k:sub(#prefix + 1)] = tostring(v)
94 function Request.getenv(self, name)
95 return name and self.message.env[name] or self.message.env
98 function Request.setfilehandler(self, callback)
99 self.filehandler = callback
104 if not context.eoh then
109 if not context.closed then
110 context.closed = true
115 function formvalue(...)
116 return context.request:formvalue(...)
119 function formvaluetable(...)
120 return context.request:formvaluetable(...)
123 function getvalue(...)
124 return context.request:getvalue(...)
127 function postvalue(...)
128 return context.request:postvalue(...)
132 return context.request:getenv(...)
135 function setfilehandler(...)
136 return context.request:setfilehandler(...)
139 function header(key, value)
140 if not context.status then
143 if not context.headers then
146 context.headers[key:lower()] = value
147 coroutine.yield(2, key, value)
150 function prepare_content(mime)
151 header("Content-Type", mime)
154 function status(code, message)
156 message = message or "OK"
157 context.status = code
158 coroutine.yield(1, code, message)
161 function write(content)
162 if not content or #content == 0 then
165 if not context.eoh then
166 if not context.status then
169 if not context.headers or not context.headers["content-type"] then
170 header("Content-Type", "text/html; charset=utf-8")
176 coroutine.yield(4, content)
180 function basic_auth(realm, errorpage)
181 header("Status", "401 Unauthorized")
182 header("WWW-Authenticate", string.format('Basic realm="%s"', realm or ""))
191 function redirect(url)
192 header("Status", "302 Found")
193 header("Location", url)
197 function build_querystring(table)
200 for k, v in pairs(table) do
201 s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&"
207 urldecode = luci.http.protocol.urldecode
208 urlencode = luci.http.protocol.urlencode