Globally reduce copyright headers
[project/luci.git] / modules / luci-base / luasrc / http.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
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"
10
11 local ipairs, pairs, next, type, tostring, error =
12         ipairs, pairs, next, type, tostring, error
13
14 --- LuCI Web Framework high-level HTTP functions.
15 module "luci.http"
16
17 context = util.threadlocal()
18
19 Request = util.class()
20 function Request.__init__(self, env, sourcein, sinkerr)
21         self.input = sourcein
22         self.error = sinkerr
23
24
25         -- File handler nil by default to let .content() work
26         self.filehandler = nil
27
28         -- HTTP-Message table
29         self.message = {
30                 env = env,
31                 headers = {},
32                 params = protocol.urldecode_params(env.QUERY_STRING or ""),
33         }
34
35         self.parsed_input = false
36 end
37
38 function Request.formvalue(self, name, noparse)
39         if not noparse and not self.parsed_input then
40                 self:_parse_input()
41         end
42
43         if name then
44                 return self.message.params[name]
45         else
46                 return self.message.params
47         end
48 end
49
50 function Request.formvaluetable(self, prefix)
51         local vals = {}
52         prefix = prefix and prefix .. "." or "."
53
54         if not self.parsed_input then
55                 self:_parse_input()
56         end
57
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)
62                 end
63         end
64
65         return vals
66 end
67
68 function Request.content(self)
69         if not self.parsed_input then
70                 self:_parse_input()
71         end
72
73         return self.message.content, self.message.content_length
74 end
75
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)
81 end
82
83 function Request.getenv(self, name)
84         if name then
85                 return self.message.env[name]
86         else
87                 return self.message.env
88         end
89 end
90
91 function Request.setfilehandler(self, callback)
92         self.filehandler = callback
93 end
94
95 function Request._parse_input(self)
96         protocol.parse_message_body(
97                  self.input,
98                  self.message,
99                  self.filehandler
100         )
101         self.parsed_input = true
102 end
103
104 --- Close the HTTP-Connection.
105 function close()
106         if not context.eoh then
107                 context.eoh = true
108                 coroutine.yield(3)
109         end
110
111         if not context.closed then
112                 context.closed = true
113                 coroutine.yield(5)
114         end
115 end
116
117 --- Return the request content if the request was of unknown type.
118 -- @return      HTTP request body
119 -- @return      HTTP request body length
120 function content()
121         return context.request:content()
122 end
123
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)
130 end
131
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)
137 end
138
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)
144 end
145
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)
152 end
153
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)
158 end
159
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
165                 context.headers = {}
166         end
167         context.headers[key:lower()] = value
168         coroutine.yield(2, key, value)
169 end
170
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"
179                         end
180                         header("Vary", "Accept")
181                 end
182                 header("Content-Type", mime)
183         end
184 end
185
186 --- Get the RAW HTTP input source
187 -- @return      HTTP LTN12 source
188 function source()
189         return context.request.input
190 end
191
192 --- Set the HTTP status code and status message.
193 -- @param code          Status code
194 -- @param message       Status message
195 function status(code, message)
196         code = code or 200
197         message = message or "OK"
198         context.status = code
199         coroutine.yield(1, code, message)
200 end
201
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)
207 -- @see close
208 function write(content, src_err)
209         if not content then
210                 if src_err then
211                         error(src_err)
212                 else
213                         close()
214                 end
215                 return true
216         elseif #content == 0 then
217                 return true
218         else
219                 if not context.eoh then
220                         if not context.status then
221                                 status()
222                         end
223                         if not context.headers or not context.headers["content-type"] then
224                                 header("Content-Type", "text/html; charset=utf-8")
225                         end
226                         if not context.headers["cache-control"] then
227                                 header("Cache-Control", "no-cache")
228                                 header("Expires", "0")
229                         end
230
231
232                         context.eoh = true
233                         coroutine.yield(3)
234                 end
235                 coroutine.yield(4, content)
236                 return true
237         end
238 end
239
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)
245 end
246
247 --- Redirects the client to a new URL and closes the connection.
248 -- @param url   Target URL
249 function redirect(url)
250         status(302, "Found")
251         header("Location", url)
252         close()
253 end
254
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)
259         local s = { "?" }
260
261         for k, v in pairs(q) do
262                 if #s > 1 then s[#s+1] = "&" end
263
264                 s[#s+1] = urldecode(k)
265                 s[#s+1] = "="
266                 s[#s+1] = urldecode(v)
267         end
268
269         return table.concat(s, "")
270 end
271
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
276 -- @see urlencode
277 urldecode = protocol.urldecode
278
279 --- Return the URL-encoded equivalent of a string.
280 -- @param str           Source string
281 -- @return                      URL-encoded string
282 -- @see urldecode
283 urlencode = protocol.urlencode
284
285 --- Send the given data as JSON encoded string.
286 -- @param data          Data to send
287 function write_json(x)
288         if x == nil then
289                 write("null")
290         elseif type(x) == "table" then
291                 local k, v
292                 if type(next(x)) == "number" then
293                         write("[ ")
294                         for k, v in ipairs(x) do
295                                 write_json(v)
296                                 if next(x, k) then
297                                         write(", ")
298                                 end
299                         end
300                         write(" ]")
301                 else
302                         write("{ ")
303                         for k, v in pairs(x) do
304                         write("%q: " % k)
305                                 write_json(v)
306                                 if next(x, k) then
307                                         write(", ")
308                                 end
309                         end
310                         write(" }")
311                 end
312         elseif type(x) == "number" or type(x) == "boolean" then
313                 if (x ~= x) then
314                         -- NaN is the only value that doesn't equal to itself.
315                         write("Number.NaN")
316                 else
317                         write(tostring(x))
318                 end
319         else
320                 write('"%s"' % tostring(x):gsub('["%z\1-\31]', function(c)
321                         return '\\u%04x' % c:byte(1)
322                 end))
323         end
324 end