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
94 function Request._parse_input(self)
95 protocol.parse_message_body(
100 self.parsed_input = true
104 if not context.eoh then
109 if not context.closed then
110 context.closed = true
116 return context.request:content()
119 function formvalue(name, noparse)
120 return context.request:formvalue(name, noparse)
123 function formvaluetable(prefix)
124 return context.request:formvaluetable(prefix)
127 function getcookie(name)
128 return context.request:getcookie(name)
131 -- or the environment table itself.
132 function getenv(name)
133 return context.request:getenv(name)
136 function setfilehandler(callback)
137 return context.request:setfilehandler(callback)
140 function header(key, value)
141 if not context.headers then
144 context.headers[key:lower()] = value
145 coroutine.yield(2, key, value)
148 function prepare_content(mime)
149 if not context.headers or not context.headers["content-type"] then
150 if mime == "application/xhtml+xml" then
151 if not getenv("HTTP_ACCEPT") or
152 not getenv("HTTP_ACCEPT"):find("application/xhtml+xml", nil, true) then
153 mime = "text/html; charset=UTF-8"
155 header("Vary", "Accept")
157 header("Content-Type", mime)
162 return context.request.input
165 function status(code, message)
167 message = message or "OK"
168 context.status = code
169 coroutine.yield(1, code, message)
172 -- This function is as a valid LTN12 sink.
173 -- If the content chunk is nil this function will automatically invoke close.
174 function write(content, src_err)
182 elseif #content == 0 then
185 if not context.eoh then
186 if not context.status then
189 if not context.headers or not context.headers["content-type"] then
190 header("Content-Type", "text/html; charset=utf-8")
192 if not context.headers["cache-control"] then
193 header("Cache-Control", "no-cache")
194 header("Expires", "0")
201 coroutine.yield(4, content)
206 function splice(fd, size)
207 coroutine.yield(6, fd, size)
210 function redirect(url)
211 if url == "" then url = "/" end
213 header("Location", url)
217 function build_querystring(q)
220 for k, v in pairs(q) do
221 if #s > 1 then s[#s+1] = "&" end
223 s[#s+1] = urldecode(k)
225 s[#s+1] = urldecode(v)
228 return table.concat(s, "")
231 urldecode = protocol.urldecode
233 urlencode = protocol.urlencode
235 function write_json(x)
236 util.serialize_json(x, write)