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)
32 require("luci.http.protocol")
35 context = luci.util.threadlocal()
38 Request = luci.util.class()
39 function Request.__init__(self, env, sourcein, sinkerr)
45 self.filehandler = function() end
51 params = luci.http.protocol.urldecode_params(env.QUERY_STRING or ""),
54 setmetatable(self.message.params, {__index =
56 setmetatable(tbl, nil)
58 luci.http.protocol.parse_message_body(
64 return rawget(tbl, key)
69 function Request.formvalue(self, name, default)
71 return self.message.params[name] and tostring(self.message.params[name]) or default
73 return self.message.params
77 function Request.formvaluetable(self, prefix)
79 prefix = prefix and prefix .. "." or "."
81 local void = self.message.params[nil]
82 for k, v in pairs(self.message.params) do
83 if k:find(prefix, 1, true) == 1 then
84 vals[k:sub(#prefix + 1)] = tostring(v)
91 function Request.getenv(self, name)
92 return name and self.message.env[name] or self.message.env
95 function Request.setfilehandler(self, callback)
96 self.filehandler = callback
101 if not context.eoh then
106 if not context.closed then
107 context.closed = true
112 function formvalue(...)
113 return context.request:formvalue(...)
116 function formvaluetable(...)
117 return context.request:formvaluetable(...)
120 function getvalue(...)
121 return context.request:getvalue(...)
124 function postvalue(...)
125 return context.request:postvalue(...)
129 return context.request:getenv(...)
132 function setfilehandler(...)
133 return context.request:setfilehandler(...)
136 function header(key, value)
137 if not context.status then
140 if not context.headers then
143 context.headers[key:lower()] = value
144 coroutine.yield(2, key, value)
147 function prepare_content(mime)
148 header("Content-Type", mime)
151 function status(code, message)
153 message = message or "OK"
154 context.status = code
155 coroutine.yield(1, code, message)
158 function write(content)
159 if not content or #content == 0 then
162 if not context.eoh then
163 if not context.status then
166 if not context.headers or not context.headers["content-type"] then
167 header("Content-Type", "text/html; charset=utf-8")
173 coroutine.yield(4, content)
177 function basic_auth(realm, errorpage)
178 header("Status", "401 Unauthorized")
179 header("WWW-Authenticate", string.format('Basic realm="%s"', realm or ""))
188 function redirect(url)
189 header("Status", "302 Found")
190 header("Location", url)
194 function build_querystring(table)
197 for k, v in pairs(table) do
198 s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&"
204 urldecode = luci.http.protocol.urldecode
205 urlencode = luci.http.protocol.urlencode