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)
93 return self.message.env[name]
95 return self.message.env
99 function Request.setfilehandler(self, callback)
100 self.filehandler = callback
105 if not context.eoh then
110 if not context.closed then
111 context.closed = true
116 function formvalue(...)
117 return context.request:formvalue(...)
120 function formvaluetable(...)
121 return context.request:formvaluetable(...)
124 function getvalue(...)
125 return context.request:getvalue(...)
128 function postvalue(...)
129 return context.request:postvalue(...)
133 return context.request:getenv(...)
136 function setfilehandler(...)
137 return context.request:setfilehandler(...)
140 function header(key, value)
141 if not context.status then
144 if not context.headers then
147 context.headers[key:lower()] = value
148 coroutine.yield(2, key, value)
151 function prepare_content(mime)
152 header("Content-Type", mime)
155 function status(code, message)
157 message = message or "OK"
158 context.status = code
159 coroutine.yield(1, code, message)
162 function write(content)
163 if not content or #content == 0 then
166 if not context.eoh then
167 if not context.status then
170 if not context.headers or not context.headers["content-type"] then
171 header("Content-Type", "text/html; charset=utf-8")
177 coroutine.yield(4, content)
181 function basic_auth(realm, errorpage)
182 header("Status", "401 Unauthorized")
183 header("WWW-Authenticate", string.format('Basic realm="%s"', realm or ""))
192 function redirect(url)
193 header("Status", "302 Found")
194 header("Location", url)
198 function build_querystring(table)
201 for k, v in pairs(table) do
202 s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&"
208 urldecode = luci.http.protocol.urldecode
209 urlencode = luci.http.protocol.urlencode