luci-lib-base: forward luci.http.context.request.message to ucode
[project/luci.git] / libs / luci-lib-base / luasrc / http.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2018 Jo-Philipp Wich <jo@mein.io>
3 -- Licensed to the public under the Apache License 2.0.
4
5 local util = require "luci.util"
6 local coroutine = require "coroutine"
7 local table = require "table"
8 local lhttp = require "lucihttp"
9
10 local L, table, ipairs, pairs, type, error = _G.L, table, ipairs, pairs, type, error
11
12 module "luci.http"
13
14 HTTP_MAX_CONTENT = 1024*100 -- 100 kB maximum content size
15
16 function close()
17 L.http:close()
18 end
19
20 function content()
21 return L.http:content()
22 end
23
24 function formvalue(name, noparse)
25 return L.http:formvalue(name, noparse)
26 end
27
28 function formvaluetable(prefix)
29 return L.http:formvaluetable(prefix)
30 end
31
32 function getcookie(name)
33 return L.http:getcookie(name)
34 end
35
36 -- or the environment table itself.
37 function getenv(name)
38 return L.http:getenv(name)
39 end
40
41 function setfilehandler(callback)
42 return L.http:setfilehandler(callback)
43 end
44
45 function header(key, value)
46 L.http:header(key, value)
47 end
48
49 function prepare_content(mime)
50 L.http:prepare_content(mime)
51 end
52
53 function source()
54 return L.http.input
55 end
56
57 function status(code, message)
58 L.http:status(code, message)
59 end
60
61 -- This function is as a valid LTN12 sink.
62 -- If the content chunk is nil this function will automatically invoke close.
63 function write(content, src_err)
64 if src_err then
65 error(src_err)
66 end
67
68 return L.print(content)
69 end
70
71 function splice(fd, size)
72 coroutine.yield(6, fd, size)
73 end
74
75 function redirect(url)
76 L.http:redirect(url)
77 end
78
79 function build_querystring(q)
80 local s, n, k, v = {}, 1, nil, nil
81
82 for k, v in pairs(q) do
83 s[n+0] = (n == 1) and "?" or "&"
84 s[n+1] = util.urlencode(k)
85 s[n+2] = "="
86 s[n+3] = util.urlencode(v)
87 n = n + 4
88 end
89
90 return table.concat(s, "")
91 end
92
93 urldecode = util.urldecode
94
95 urlencode = util.urlencode
96
97 function write_json(x)
98 L.printf('%J', x)
99 end
100
101 -- separated by "&". Tables are encoded as parameters with multiple values by
102 -- repeating the parameter name with each value.
103 function urlencode_params(tbl)
104 local k, v
105 local n, enc = 1, {}
106 for k, v in pairs(tbl) do
107 if type(v) == "table" then
108 local i, v2
109 for i, v2 in ipairs(v) do
110 if enc[1] then
111 enc[n] = "&"
112 n = n + 1
113 end
114
115 enc[n+0] = lhttp.urlencode(k)
116 enc[n+1] = "="
117 enc[n+2] = lhttp.urlencode(v2)
118 n = n + 3
119 end
120 else
121 if enc[1] then
122 enc[n] = "&"
123 n = n + 1
124 end
125
126 enc[n+0] = lhttp.urlencode(k)
127 enc[n+1] = "="
128 enc[n+2] = lhttp.urlencode(v)
129 n = n + 3
130 end
131 end
132
133 return table.concat(enc, "")
134 end
135
136 context = {
137 request = {
138 formvalue = function(self, ...) return formvalue(...) end;
139 formvaluetable = function(self, ...) return formvaluetable(...) end;
140 content = function(self, ...) return content(...) end;
141 getcookie = function(self, ...) return getcookie(...) end;
142 setfilehandler = function(self, ...) return setfilehandler(...) end;
143 message = L.http.message
144 }
145 }