2 LuCI - HTTP-Interaction
5 HTTP-Header manipulator and form variable preprocessor
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
27 module("luci.http", package.seeall)
29 require("luci.http.protocol")
32 context = luci.util.threadlocal()
35 Request = luci.util.class()
36 function Request.__init__(self, env, sourcein, sinkerr)
42 self.filehandler = function() end
48 params = luci.http.protocol.urldecode_params(env.QUERY_STRING or ""),
51 self.parsed_input = false
54 function Request.formvalue(self, name, default)
55 if not self.parsed_input then
60 return self.message.params[name] and tostring(self.message.params[name]) or default
62 return self.message.params
66 function Request.formvaluetable(self, prefix)
68 prefix = prefix and prefix .. "." or "."
70 if not self.parsed_input then
74 local void = self.message.params[nil]
75 for k, v in pairs(self.message.params) do
76 if k:find(prefix, 1, true) == 1 then
77 vals[k:sub(#prefix + 1)] = tostring(v)
84 function Request.getcookie(self, name)
85 local c = string.gsub(";" .. (self:getenv("HTTP_COOKIE") or "") .. ";", "%s*;%s*", ";")
86 local p = ";" .. name .. "=(.-);"
87 local i, j, value = cookies:find(p)
88 return value and urldecode(value)
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
103 function Request._parse_input(self)
104 luci.http.protocol.parse_message_body(
109 self.parsed_input = true
114 if not context.eoh then
119 if not context.closed then
120 context.closed = true
125 function formvalue(...)
126 return context.request:formvalue(...)
129 function formvaluetable(...)
130 return context.request:formvaluetable(...)
133 function getvalue(...)
134 return context.request:getvalue(...)
137 function postvalue(...)
138 return context.request:postvalue(...)
142 return context.request:getenv(...)
145 function setfilehandler(...)
146 return context.request:setfilehandler(...)
149 function header(key, value)
150 if not context.status then
153 if not context.headers then
156 context.headers[key:lower()] = value
157 coroutine.yield(2, key, value)
160 function prepare_content(mime)
161 header("Content-Type", mime)
164 function status(code, message)
166 message = message or "OK"
167 context.status = code
168 coroutine.yield(1, code, message)
171 function write(content)
172 if not content or #content == 0 then
175 if not context.eoh then
176 if not context.status then
179 if not context.headers or not context.headers["content-type"] then
180 header("Content-Type", "text/html; charset=utf-8")
186 coroutine.yield(4, content)
189 function redirect(url)
190 header("Status", "302 Found")
191 header("Location", url)
195 function build_querystring(table)
198 for k, v in pairs(table) do
199 s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&"
205 urldecode = luci.http.protocol.urldecode
206 urlencode = luci.http.protocol.urlencode