proc: do not declare empty process variables
[project/uhttpd.git] / lua.c
1 /*
2 * uhttpd - Tiny single-threaded httpd
3 *
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <libubox/blobmsg.h>
21 #include <lua.h>
22 #include <lauxlib.h>
23 #include <lualib.h>
24 #include <stdio.h>
25 #include <poll.h>
26
27 #include "uhttpd.h"
28 #include "plugin.h"
29
30 #define UH_LUA_CB "handle_request"
31
32 static const struct uhttpd_ops *ops;
33 static struct config *_conf;
34 #define conf (*_conf)
35
36 static lua_State *_L;
37
38 static int uh_lua_recv(lua_State *L)
39 {
40 static struct pollfd pfd = {
41 .fd = STDIN_FILENO,
42 .events = POLLIN,
43 };
44 luaL_Buffer B;
45 int data_len = 0;
46 int len;
47 int r;
48
49 len = luaL_checknumber(L, 1);
50 luaL_buffinit(L, &B);
51 while(len > 0) {
52 char *buf;
53
54 buf = luaL_prepbuffer(&B);
55 r = read(STDIN_FILENO, buf, LUAL_BUFFERSIZE);
56 if (r < 0) {
57 if (errno == EWOULDBLOCK || errno == EAGAIN) {
58 pfd.revents = 0;
59 poll(&pfd, 1, 1000);
60 if (pfd.revents & POLLIN)
61 continue;
62 }
63 if (errno == EINTR)
64 continue;
65
66 if (!data_len)
67 data_len = -1;
68 break;
69 }
70 if (!r)
71 break;
72
73 luaL_addsize(&B, r);
74 data_len += r;
75 if (r != LUAL_BUFFERSIZE)
76 break;
77 }
78
79 luaL_pushresult(&B);
80 lua_pushnumber(L, data_len);
81 if (data_len > 0) {
82 lua_pushvalue(L, -2);
83 lua_remove(L, -3);
84 return 2;
85 } else {
86 lua_remove(L, -2);
87 return 1;
88 }
89 }
90
91 static int uh_lua_send(lua_State *L)
92 {
93 const char *buf;
94 size_t len;
95
96 buf = luaL_checklstring(L, 1, &len);
97 if (len > 0)
98 len = write(STDOUT_FILENO, buf, len);
99
100 lua_pushnumber(L, len);
101 return 1;
102 }
103
104 static int
105 uh_lua_strconvert(lua_State *L, int (*convert)(char *, int, const char *, int))
106 {
107 const char *in_buf;
108 static char out_buf[4096];
109 size_t in_len;
110 int out_len;
111
112 in_buf = luaL_checklstring(L, 1, &in_len);
113 out_len = convert(out_buf, sizeof(out_buf), in_buf, in_len);
114
115 if (out_len < 0) {
116 const char *error;
117
118 if (out_len == -1)
119 error = "buffer overflow";
120 else
121 error = "malformed string";
122
123 luaL_error(L, "%s on URL conversion\n", error);
124 }
125
126 lua_pushlstring(L, out_buf, out_len);
127 return 1;
128 }
129
130 static int uh_lua_urldecode(lua_State *L)
131 {
132 return uh_lua_strconvert(L, ops->urldecode);
133 }
134
135 static int uh_lua_urlencode(lua_State *L)
136 {
137 return uh_lua_strconvert(L, ops->urlencode);
138 }
139
140 static lua_State *uh_lua_state_init(void)
141 {
142 const char *msg = "(unknown error)";
143 const char *status;
144 lua_State *L;
145 int ret;
146
147 L = luaL_newstate();
148 luaL_openlibs(L);
149
150 /* build uhttpd api table */
151 lua_newtable(L);
152
153 lua_pushcfunction(L, uh_lua_send);
154 lua_setfield(L, -2, "send");
155
156 lua_pushcfunction(L, uh_lua_send);
157 lua_setfield(L, -2, "sendc");
158
159 lua_pushcfunction(L, uh_lua_recv);
160 lua_setfield(L, -2, "recv");
161
162 lua_pushcfunction(L, uh_lua_urldecode);
163 lua_setfield(L, -2, "urldecode");
164
165 lua_pushcfunction(L, uh_lua_urlencode);
166 lua_setfield(L, -2, "urlencode");
167
168 lua_pushstring(L, conf.docroot);
169 lua_setfield(L, -2, "docroot");
170
171 lua_setglobal(L, "uhttpd");
172
173 ret = luaL_loadfile(L, conf.lua_handler);
174 if (ret) {
175 status = "loading";
176 goto error;
177 }
178
179 ret = lua_pcall(L, 0, 0, 0);
180 if (ret) {
181 status = "initializing";
182 goto error;
183 }
184
185 lua_getglobal(L, UH_LUA_CB);
186 if (!lua_isfunction(L, -1)) {
187 fprintf(stderr, "Error: Lua handler provides no " UH_LUA_CB "() callback.\n");
188 exit(1);
189 }
190
191 return L;
192
193 error:
194 if (!lua_isnil(L, -1))
195 msg = lua_tostring(L, -1);
196
197 fprintf(stderr, "Error %s Lua handler: %s\n", status, msg);
198 exit(1);
199 return NULL;
200 }
201
202 static void lua_main(struct client *cl, struct path_info *pi, char *url)
203 {
204 struct blob_attr *cur;
205 const char *error;
206 struct env_var *var;
207 lua_State *L = _L;
208 int path_len, prefix_len;
209 char *str;
210 int rem;
211
212 lua_getglobal(L, UH_LUA_CB);
213
214 /* new env table for this request */
215 lua_newtable(L);
216
217 prefix_len = strlen(conf.lua_prefix);
218 path_len = strlen(url);
219 str = strchr(url, '?');
220 if (str) {
221 if (*(str + 1))
222 pi->query = str + 1;
223 path_len = str - url;
224 }
225
226 if (prefix_len > 0 && conf.lua_prefix[prefix_len - 1] == '/')
227 prefix_len--;
228
229 if (path_len > prefix_len) {
230 lua_pushlstring(L, url + prefix_len,
231 path_len - prefix_len);
232 lua_setfield(L, -2, "PATH_INFO");
233 }
234
235 for (var = ops->get_process_vars(cl, pi); var->name; var++) {
236 if (!var->value)
237 continue;
238
239 lua_pushstring(L, var->value);
240 lua_setfield(L, -2, var->name);
241 }
242
243 lua_pushnumber(L, 0.9 + (cl->request.version / 10.0));
244 lua_setfield(L, -2, "HTTP_VERSION");
245
246 lua_newtable(L);
247 blob_for_each_attr(cur, cl->hdr.head, rem) {
248 lua_pushstring(L, blobmsg_data(cur));
249 lua_setfield(L, -2, blobmsg_name(cur));
250 }
251 lua_setfield(L, -2, "headers");
252
253 switch(lua_pcall(L, 1, 0, 0)) {
254 case LUA_ERRMEM:
255 case LUA_ERRRUN:
256 error = luaL_checkstring(L, -1);
257 if (!error)
258 error = "(unknown error)";
259
260 printf("Status: 500 Internal Server Error\r\n\r\n"
261 "Unable to launch the requested Lua program:\n"
262 " %s: %s\n", pi->phys, error);
263 }
264
265 exit(0);
266 }
267
268 static void lua_handle_request(struct client *cl, char *url, struct path_info *pi)
269 {
270 static struct path_info _pi;
271
272 pi = &_pi;
273 pi->name = conf.lua_prefix;
274 pi->phys = conf.lua_handler;
275
276 if (!ops->create_process(cl, pi, url, lua_main)) {
277 ops->client_error(cl, 500, "Internal Server Error",
278 "Failed to create CGI process: %s", strerror(errno));
279 }
280 }
281
282 static bool check_lua_url(const char *url)
283 {
284 return ops->path_match(conf.lua_prefix, url);
285 }
286
287 static struct dispatch_handler lua_dispatch = {
288 .script = true,
289 .check_url = check_lua_url,
290 .handle_request = lua_handle_request,
291 };
292
293 static int lua_plugin_init(const struct uhttpd_ops *o, struct config *c)
294 {
295 ops = o;
296 _conf = c;
297 _L = uh_lua_state_init();
298 ops->dispatch_add(&lua_dispatch);
299 return 0;
300 }
301
302 struct uhttpd_plugin uhttpd_plugin = {
303 .init = lua_plugin_init,
304 };