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 luci.http.protocol.parse_message_body(
62 setmetatable(tbl, nil)
63 return rawget(tbl, key)
68 function Request.formvalue(self, name, default)
70 return self.message.params[name] and tostring(self.message.params[name]) or default
72 return self.message.params
76 function Request.formvaluetable(self, prefix)
78 prefix = prefix and prefix .. "." or "."
80 local void = self.message.params[nil]
81 for k, v in pairs(self.message.params) do
82 if k:find(prefix, 1, true) == 1 then
83 vals[k:sub(#prefix + 1)] = tostring(v)
90 function Request.getenv(self, name)
91 return name and self.message.env[name] or self.message.env
94 function Request.setfilehandler(self, callback)
95 self.filehandler = callback
100 if not context.eoh then
105 if not context.closed then
106 context.closed = true
111 function formvalue(...)
112 return context.request:formvalue(...)
115 function formvaluetable(...)
116 return context.request:formvaluetable(...)
119 function getvalue(...)
120 return context.request:getvalue(...)
123 function postvalue(...)
124 return context.request:postvalue(...)
128 return context.request:getenv(...)
131 function setfilehandler(...)
132 return context.request:setfilehandler(...)
135 function header(key, value)
136 if not context.status then
139 if not context.headers then
142 context.headers[key:lower()] = value
143 coroutine.yield(2, key, value)
146 function prepare_content(mime)
147 header("Content-Type", mime)
150 function status(code, message)
152 message = message or "OK"
153 context.status = code
154 coroutine.yield(1, code, message)
157 function write(content)
158 if not content or #content == 0 then
161 if not context.eoh then
162 if not context.status then
165 if not context.headers or not context.headers["content-type"] then
166 header("Content-Type", "text/html; charset=utf-8")
172 coroutine.yield(4, content)
176 function basic_auth(realm, errorpage)
177 header("Status", "401 Unauthorized")
178 header("WWW-Authenticate", string.format('Basic realm="%s"', realm or ""))
187 function redirect(url)
188 header("Status", "302 Found")
189 header("Location", url)
193 function build_querystring(table)
196 for k, v in pairs(table) do
197 s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&"
203 urldecode = luci.http.protocol.urldecode
204 urlencode = luci.http.protocol.urlencode