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
14 --- LuCI Web Framework high-level HTTP functions.
17 context = util.threadlocal()
19 Request = util.class()
20 function Request.__init__(self, env, sourcein, sinkerr)
25 -- File handler nil by default to let .content() work
26 self.filehandler = nil
32 params = protocol.urldecode_params(env.QUERY_STRING or ""),
35 self.parsed_input = false
38 function Request.formvalue(self, name, noparse)
39 if not noparse and not self.parsed_input then
44 return self.message.params[name]
46 return self.message.params
50 function Request.formvaluetable(self, prefix)
52 prefix = prefix and prefix .. "." or "."
54 if not self.parsed_input then
58 local void = self.message.params[nil]
59 for k, v in pairs(self.message.params) do
60 if k:find(prefix, 1, true) == 1 then
61 vals[k:sub(#prefix + 1)] = tostring(v)
68 function Request.content(self)
69 if not self.parsed_input then
73 return self.message.content, self.message.content_length
76 function Request.getcookie(self, name)
77 local c = string.gsub(";" .. (self:getenv("HTTP_COOKIE") or "") .. ";", "%s*;%s*", ";")
78 local p = ";" .. name .. "=(.-);"
79 local i, j, value = c:find(p)
80 return value and urldecode(value)
83 function Request.getenv(self, name)
85 return self.message.env[name]
87 return self.message.env
91 function Request.setfilehandler(self, callback)
92 self.filehandler = callback
95 function Request._parse_input(self)
96 protocol.parse_message_body(
101 self.parsed_input = true
104 --- Close the HTTP-Connection.
106 if not context.eoh then
111 if not context.closed then
112 context.closed = true
117 --- Return the request content if the request was of unknown type.
118 -- @return HTTP request body
119 -- @return HTTP request body length
121 return context.request:content()
124 --- Get a certain HTTP input value or a table of all input values.
125 -- @param name Name of the GET or POST variable to fetch
126 -- @param noparse Don't parse POST data before getting the value
127 -- @return HTTP input value or table of all input value
128 function formvalue(name, noparse)
129 return context.request:formvalue(name, noparse)
132 --- Get a table of all HTTP input values with a certain prefix.
133 -- @param prefix Prefix
134 -- @return Table of all HTTP input values with given prefix
135 function formvaluetable(prefix)
136 return context.request:formvaluetable(prefix)
139 --- Get the value of a certain HTTP-Cookie.
140 -- @param name Cookie Name
141 -- @return String containing cookie data
142 function getcookie(name)
143 return context.request:getcookie(name)
146 --- Get the value of a certain HTTP environment variable
147 -- or the environment table itself.
148 -- @param name Environment variable
149 -- @return HTTP environment value or environment table
150 function getenv(name)
151 return context.request:getenv(name)
154 --- Set a handler function for incoming user file uploads.
155 -- @param callback Handler function
156 function setfilehandler(callback)
157 return context.request:setfilehandler(callback)
160 --- Send a HTTP-Header.
161 -- @param key Header key
162 -- @param value Header value
163 function header(key, value)
164 if not context.headers then
167 context.headers[key:lower()] = value
168 coroutine.yield(2, key, value)
171 --- Set the mime type of following content data.
172 -- @param mime Mimetype of following content
173 function prepare_content(mime)
174 if not context.headers or not context.headers["content-type"] then
175 if mime == "application/xhtml+xml" then
176 if not getenv("HTTP_ACCEPT") or
177 not getenv("HTTP_ACCEPT"):find("application/xhtml+xml", nil, true) then
178 mime = "text/html; charset=UTF-8"
180 header("Vary", "Accept")
182 header("Content-Type", mime)
186 --- Get the RAW HTTP input source
187 -- @return HTTP LTN12 source
189 return context.request.input
192 --- Set the HTTP status code and status message.
193 -- @param code Status code
194 -- @param message Status message
195 function status(code, message)
197 message = message or "OK"
198 context.status = code
199 coroutine.yield(1, code, message)
202 --- Send a chunk of content data to the client.
203 -- This function is as a valid LTN12 sink.
204 -- If the content chunk is nil this function will automatically invoke close.
205 -- @param content Content chunk
206 -- @param src_err Error object from source (optional)
208 function write(content, src_err)
216 elseif #content == 0 then
219 if not context.eoh then
220 if not context.status then
223 if not context.headers or not context.headers["content-type"] then
224 header("Content-Type", "text/html; charset=utf-8")
226 if not context.headers["cache-control"] then
227 header("Cache-Control", "no-cache")
228 header("Expires", "0")
235 coroutine.yield(4, content)
240 --- Splice data from a filedescriptor to the client.
241 -- @param fp File descriptor
242 -- @param size Bytes to splice (optional)
243 function splice(fd, size)
244 coroutine.yield(6, fd, size)
247 --- Redirects the client to a new URL and closes the connection.
248 -- @param url Target URL
249 function redirect(url)
251 header("Location", url)
255 --- Create a querystring out of a table of key - value pairs.
256 -- @param table Query string source table
257 -- @return Encoded HTTP query string
258 function build_querystring(q)
261 for k, v in pairs(q) do
262 if #s > 1 then s[#s+1] = "&" end
264 s[#s+1] = urldecode(k)
266 s[#s+1] = urldecode(v)
269 return table.concat(s, "")
272 --- Return the URL-decoded equivalent of a string.
273 -- @param str URL-encoded string
274 -- @param no_plus Don't decode + to " "
275 -- @return URL-decoded string
277 urldecode = protocol.urldecode
279 --- Return the URL-encoded equivalent of a string.
280 -- @param str Source string
281 -- @return URL-encoded string
283 urlencode = protocol.urlencode
285 --- Send the given data as JSON encoded string.
286 -- @param data Data to send
287 function write_json(x)
288 util.serialize_json(x, write)