1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local ltn12 = require "luci.ltn12"
5 local protocol = require "luci.http.protocol"
6 local util = require "luci.util"
7 local string = require "string"
8 local coroutine = require "coroutine"
9 local table = require "table"
11 local ipairs, pairs, next, type, tostring, error =
12 ipairs, pairs, next, type, tostring, error
16 context = util.threadlocal()
18 Request = util.class()
19 function Request.__init__(self, env, sourcein, sinkerr)
24 -- File handler nil by default to let .content() work
25 self.filehandler = nil
31 params = protocol.urldecode_params(env.QUERY_STRING or ""),
34 self.parsed_input = false
37 function Request.formvalue(self, name, noparse)
38 if not noparse and not self.parsed_input then
43 return self.message.params[name]
45 return self.message.params
49 function Request.formvaluetable(self, prefix)
51 prefix = prefix and prefix .. "." or "."
53 if not self.parsed_input then
57 local void = self.message.params[nil]
58 for k, v in pairs(self.message.params) do
59 if k:find(prefix, 1, true) == 1 then
60 vals[k:sub(#prefix + 1)] = tostring(v)
67 function Request.content(self)
68 if not self.parsed_input then
72 return self.message.content, self.message.content_length
75 function Request.getcookie(self, name)
76 local c = string.gsub(";" .. (self:getenv("HTTP_COOKIE") or "") .. ";", "%s*;%s*", ";")
77 local p = ";" .. name .. "=(.-);"
78 local i, j, value = c:find(p)
79 return value and urldecode(value)
82 function Request.getenv(self, name)
84 return self.message.env[name]
86 return self.message.env
90 function Request.setfilehandler(self, callback)
91 self.filehandler = callback
93 -- If input has already been parsed then any files are either in temporary files
94 -- or are in self.message.params[key]
95 if self.parsed_input then
96 for param, value in pairs(self.message.params) do
98 -- We're only interested in files
99 if (not value["file"]) then break end
100 -- If we were able to write to temporary file
101 if (value["fd"]) then
105 filedata = fd:read(1024)
106 if (filedata:len() < 1024) then
109 callback({ name=value["name"], file=value["file"] }, filedata, eof)
113 -- We had to read into memory
115 -- There should only be one numbered value in table - the data
116 for k, v in ipairs(value) do
117 callback({ name=value["name"], file=value["file"] }, v, true)
125 function Request._parse_input(self)
126 protocol.parse_message_body(
131 self.parsed_input = true
135 if not context.eoh then
140 if not context.closed then
141 context.closed = true
147 return context.request:content()
150 function formvalue(name, noparse)
151 return context.request:formvalue(name, noparse)
154 function formvaluetable(prefix)
155 return context.request:formvaluetable(prefix)
158 function getcookie(name)
159 return context.request:getcookie(name)
162 -- or the environment table itself.
163 function getenv(name)
164 return context.request:getenv(name)
167 function setfilehandler(callback)
168 return context.request:setfilehandler(callback)
171 function header(key, value)
172 if not context.headers then
175 context.headers[key:lower()] = value
176 coroutine.yield(2, key, value)
179 function prepare_content(mime)
180 if not context.headers or not context.headers["content-type"] then
181 if mime == "application/xhtml+xml" then
182 if not getenv("HTTP_ACCEPT") or
183 not getenv("HTTP_ACCEPT"):find("application/xhtml+xml", nil, true) then
184 mime = "text/html; charset=UTF-8"
186 header("Vary", "Accept")
188 header("Content-Type", mime)
193 return context.request.input
196 function status(code, message)
198 message = message or "OK"
199 context.status = code
200 coroutine.yield(1, code, message)
203 -- This function is as a valid LTN12 sink.
204 -- If the content chunk is nil this function will automatically invoke close.
205 function write(content, src_err)
213 elseif #content == 0 then
216 if not context.eoh then
217 if not context.status then
220 if not context.headers or not context.headers["content-type"] then
221 header("Content-Type", "text/html; charset=utf-8")
223 if not context.headers["cache-control"] then
224 header("Cache-Control", "no-cache")
225 header("Expires", "0")
232 coroutine.yield(4, content)
237 function splice(fd, size)
238 coroutine.yield(6, fd, size)
241 function redirect(url)
242 if url == "" then url = "/" end
244 header("Location", url)
248 function build_querystring(q)
251 for k, v in pairs(q) do
252 if #s > 1 then s[#s+1] = "&" end
254 s[#s+1] = urldecode(k)
256 s[#s+1] = urldecode(v)
259 return table.concat(s, "")
262 urldecode = protocol.urldecode
264 urlencode = protocol.urlencode
266 function write_json(x)
267 util.serialize_json(x, write)