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)
28 local ltn12 = require("luci.ltn12")
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)
55 if not self.parsed_input then
60 return self.message.params[name]
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 = c: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 getcookie(...)
134 return context.request:getcookie(...)
137 function getvalue(...)
138 return context.request:getvalue(...)
141 function postvalue(...)
142 return context.request:postvalue(...)
146 return context.request:getenv(...)
149 function setfilehandler(...)
150 return context.request:setfilehandler(...)
153 function header(key, value)
154 if not context.headers then
157 context.headers[key:lower()] = value
158 coroutine.yield(2, key, value)
161 function prepare_content(mime)
162 header("Content-Type", mime)
165 function status(code, message)
167 message = message or "OK"
168 context.status = code
169 coroutine.yield(1, code, message)
172 function write(content)
173 if not content or #content == 0 then
176 if not context.eoh then
177 if not context.status then
180 if not context.headers or not context.headers["content-type"] then
181 header("Content-Type", "text/html; charset=utf-8")
187 coroutine.yield(4, content)
190 function redirect(url)
192 header("Location", url)
196 function build_querystring(table)
199 for k, v in pairs(table) do
200 s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&"
206 urldecode = luci.http.protocol.urldecode
207 urlencode = luci.http.protocol.urlencode