utils: add uh_htmlescape() helper
[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_optnumber(L, 1, LUAL_BUFFERSIZE);
50         luaL_buffinit(L, &B);
51         while(len > 0) {
52                 char *buf;
53
54                 buf = luaL_prepbuffer(&B);
55                 r = read(STDIN_FILENO, buf,
56                          len < LUAL_BUFFERSIZE ? len : LUAL_BUFFERSIZE);
57                 if (r < 0) {
58                         if (errno == EWOULDBLOCK || errno == EAGAIN) {
59                                 pfd.revents = 0;
60                                 poll(&pfd, 1, 1000);
61                                 if (pfd.revents & POLLIN)
62                                         continue;
63                         }
64                         if (errno == EINTR)
65                                 continue;
66
67                         if (!data_len)
68                                 data_len = -1;
69                         break;
70                 }
71                 if (!r)
72                         break;
73
74                 luaL_addsize(&B, r);
75                 data_len += r;
76                 len -= r;
77                 if (r != LUAL_BUFFERSIZE)
78                         break;
79         }
80
81         luaL_pushresult(&B);
82         lua_pushnumber(L, data_len);
83         if (data_len > 0) {
84                 lua_pushvalue(L, -2);
85                 lua_remove(L, -3);
86                 return 2;
87         } else {
88                 lua_remove(L, -2);
89                 return 1;
90         }
91 }
92
93 static int uh_lua_send(lua_State *L)
94 {
95         const char *buf;
96         size_t len;
97
98         buf = luaL_checklstring(L, 1, &len);
99         if (len > 0)
100                 len = write(STDOUT_FILENO, buf, len);
101
102         lua_pushnumber(L, len);
103         return 1;
104 }
105
106 static int
107 uh_lua_strconvert(lua_State *L, int (*convert)(char *, int, const char *, int))
108 {
109         const char *in_buf;
110         static char out_buf[4096];
111         size_t in_len;
112         int out_len;
113
114         in_buf = luaL_checklstring(L, 1, &in_len);
115         out_len = convert(out_buf, sizeof(out_buf), in_buf, in_len);
116
117         if (out_len < 0) {
118                 const char *error;
119
120                 if (out_len == -1)
121                         error = "buffer overflow";
122                 else
123                         error = "malformed string";
124
125                 luaL_error(L, "%s on URL conversion\n", error);
126         }
127
128         lua_pushlstring(L, out_buf, out_len);
129         return 1;
130 }
131
132 static int uh_lua_urldecode(lua_State *L)
133 {
134         return uh_lua_strconvert(L, ops->urldecode);
135 }
136
137 static int uh_lua_urlencode(lua_State *L)
138 {
139         return uh_lua_strconvert(L, ops->urlencode);
140 }
141
142 static lua_State *uh_lua_state_init(void)
143 {
144         const char *msg = "(unknown error)";
145         const char *status;
146         lua_State *L;
147         int ret;
148
149         L = luaL_newstate();
150         luaL_openlibs(L);
151
152         /* build uhttpd api table */
153         lua_newtable(L);
154
155         lua_pushcfunction(L, uh_lua_send);
156         lua_setfield(L, -2, "send");
157
158         lua_pushcfunction(L, uh_lua_send);
159         lua_setfield(L, -2, "sendc");
160
161         lua_pushcfunction(L, uh_lua_recv);
162         lua_setfield(L, -2, "recv");
163
164         lua_pushcfunction(L, uh_lua_urldecode);
165         lua_setfield(L, -2, "urldecode");
166
167         lua_pushcfunction(L, uh_lua_urlencode);
168         lua_setfield(L, -2, "urlencode");
169
170         lua_pushstring(L, conf.docroot);
171         lua_setfield(L, -2, "docroot");
172
173         lua_setglobal(L, "uhttpd");
174
175         ret = luaL_loadfile(L, conf.lua_handler);
176         if (ret) {
177                 status = "loading";
178                 goto error;
179         }
180
181         ret = lua_pcall(L, 0, 0, 0);
182         if (ret) {
183                 status = "initializing";
184                 goto error;
185         }
186
187         lua_getglobal(L, UH_LUA_CB);
188         if (!lua_isfunction(L, -1)) {
189                 fprintf(stderr, "Error: Lua handler provides no " UH_LUA_CB "() callback.\n");
190                 exit(1);
191         }
192
193         return L;
194
195 error:
196         if (!lua_isnil(L, -1))
197                 msg = lua_tostring(L, -1);
198
199         fprintf(stderr, "Error %s Lua handler: %s\n", status, msg);
200         exit(1);
201         return NULL;
202 }
203
204 static void lua_main(struct client *cl, struct path_info *pi, char *url)
205 {
206         struct blob_attr *cur;
207         const char *error;
208         struct env_var *var;
209         lua_State *L = _L;
210         int path_len, prefix_len;
211         char *str;
212         int rem;
213
214         lua_getglobal(L, UH_LUA_CB);
215
216         /* new env table for this request */
217         lua_newtable(L);
218
219         prefix_len = strlen(conf.lua_prefix);
220         path_len = strlen(url);
221         str = strchr(url, '?');
222         if (str) {
223                 if (*(str + 1))
224                         pi->query = str + 1;
225                 path_len = str - url;
226         }
227
228         if (prefix_len > 0 && conf.lua_prefix[prefix_len - 1] == '/')
229                 prefix_len--;
230
231         if (path_len > prefix_len) {
232                 lua_pushlstring(L, url + prefix_len,
233                                 path_len - prefix_len);
234                 lua_setfield(L, -2, "PATH_INFO");
235         }
236
237         for (var = ops->get_process_vars(cl, pi); var->name; var++) {
238                 if (!var->value)
239                         continue;
240
241                 lua_pushstring(L, var->value);
242                 lua_setfield(L, -2, var->name);
243         }
244
245         lua_pushnumber(L, 0.9 + (cl->request.version / 10.0));
246         lua_setfield(L, -2, "HTTP_VERSION");
247
248         lua_newtable(L);
249         blob_for_each_attr(cur, cl->hdr.head, rem) {
250                 lua_pushstring(L, blobmsg_data(cur));
251                 lua_setfield(L, -2, blobmsg_name(cur));
252         }
253         lua_setfield(L, -2, "headers");
254
255         switch(lua_pcall(L, 1, 0, 0)) {
256         case LUA_ERRMEM:
257         case LUA_ERRRUN:
258                 error = luaL_checkstring(L, -1);
259                 if (!error)
260                         error = "(unknown error)";
261
262                 printf("Status: 500 Internal Server Error\r\n\r\n"
263                "Unable to launch the requested Lua program:\n"
264                "  %s: %s\n", pi->phys, error);
265         }
266
267         exit(0);
268 }
269
270 static void lua_handle_request(struct client *cl, char *url, struct path_info *pi)
271 {
272         static struct path_info _pi;
273
274         pi = &_pi;
275         pi->name = conf.lua_prefix;
276         pi->phys = conf.lua_handler;
277
278         if (!ops->create_process(cl, pi, url, lua_main)) {
279                 ops->client_error(cl, 500, "Internal Server Error",
280                                   "Failed to create CGI process: %s", strerror(errno));
281         }
282 }
283
284 static bool check_lua_url(const char *url)
285 {
286         return ops->path_match(conf.lua_prefix, url);
287 }
288
289 static struct dispatch_handler lua_dispatch = {
290         .script = true,
291         .check_url = check_lua_url,
292         .handle_request = lua_handle_request,
293 };
294
295 static int lua_plugin_init(const struct uhttpd_ops *o, struct config *c)
296 {
297         ops = o;
298         _conf = c;
299         _L = uh_lua_state_init();
300         ops->dispatch_add(&lua_dispatch);
301         return 0;
302 }
303
304 struct uhttpd_plugin uhttpd_plugin = {
305         .init = lua_plugin_init,
306 };