2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
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.
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.
20 #include <libubox/blobmsg.h>
30 #define UH_LUA_CB "handle_request"
32 static const struct uhttpd_ops *ops;
33 static struct config *_conf;
38 static int uh_lua_recv(lua_State *L)
40 static struct pollfd pfd = {
49 len = luaL_optnumber(L, 1, LUAL_BUFFERSIZE);
54 buf = luaL_prepbuffer(&B);
55 r = read(STDIN_FILENO, buf,
56 len < LUAL_BUFFERSIZE ? len : LUAL_BUFFERSIZE);
58 if (errno == EWOULDBLOCK || errno == EAGAIN) {
61 if (pfd.revents & POLLIN)
77 if (r != LUAL_BUFFERSIZE)
82 lua_pushnumber(L, data_len);
93 static int uh_lua_send(lua_State *L)
98 buf = luaL_checklstring(L, 1, &len);
100 len = write(STDOUT_FILENO, buf, len);
102 lua_pushnumber(L, len);
107 uh_lua_strconvert(lua_State *L, int (*convert)(char *, int, const char *, int))
110 static char out_buf[4096];
114 in_buf = luaL_checklstring(L, 1, &in_len);
115 out_len = convert(out_buf, sizeof(out_buf), in_buf, in_len);
121 error = "buffer overflow";
123 error = "malformed string";
125 luaL_error(L, "%s on URL conversion\n", error);
128 lua_pushlstring(L, out_buf, out_len);
132 static int uh_lua_urldecode(lua_State *L)
134 return uh_lua_strconvert(L, ops->urldecode);
137 static int uh_lua_urlencode(lua_State *L)
139 return uh_lua_strconvert(L, ops->urlencode);
142 static lua_State *uh_lua_state_init(void)
144 const char *msg = "(unknown error)";
152 /* build uhttpd api table */
155 lua_pushcfunction(L, uh_lua_send);
156 lua_setfield(L, -2, "send");
158 lua_pushcfunction(L, uh_lua_send);
159 lua_setfield(L, -2, "sendc");
161 lua_pushcfunction(L, uh_lua_recv);
162 lua_setfield(L, -2, "recv");
164 lua_pushcfunction(L, uh_lua_urldecode);
165 lua_setfield(L, -2, "urldecode");
167 lua_pushcfunction(L, uh_lua_urlencode);
168 lua_setfield(L, -2, "urlencode");
170 lua_pushstring(L, conf.docroot);
171 lua_setfield(L, -2, "docroot");
173 lua_setglobal(L, "uhttpd");
175 ret = luaL_loadfile(L, conf.lua_handler);
181 ret = lua_pcall(L, 0, 0, 0);
183 status = "initializing";
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");
196 if (!lua_isnil(L, -1))
197 msg = lua_tostring(L, -1);
199 fprintf(stderr, "Error %s Lua handler: %s\n", status, msg);
204 static void lua_main(struct client *cl, struct path_info *pi, char *url)
206 struct blob_attr *cur;
210 int path_len, prefix_len;
214 lua_getglobal(L, UH_LUA_CB);
216 /* new env table for this request */
219 prefix_len = strlen(conf.lua_prefix);
220 path_len = strlen(url);
221 str = strchr(url, '?');
225 path_len = str - url;
228 if (prefix_len > 0 && conf.lua_prefix[prefix_len - 1] == '/')
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");
237 for (var = ops->get_process_vars(cl, pi); var->name; var++) {
241 lua_pushstring(L, var->value);
242 lua_setfield(L, -2, var->name);
245 lua_pushnumber(L, 0.9 + (cl->request.version / 10.0));
246 lua_setfield(L, -2, "HTTP_VERSION");
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));
253 lua_setfield(L, -2, "headers");
255 switch(lua_pcall(L, 1, 0, 0)) {
258 error = luaL_checkstring(L, -1);
260 error = "(unknown error)";
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);
270 static void lua_handle_request(struct client *cl, char *url, struct path_info *pi)
272 static struct path_info _pi;
275 pi->name = conf.lua_prefix;
276 pi->phys = conf.lua_handler;
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));
284 static bool check_lua_url(const char *url)
286 return ops->path_match(conf.lua_prefix, url);
289 static struct dispatch_handler lua_dispatch = {
291 .check_url = check_lua_url,
292 .handle_request = lua_handle_request,
295 static int lua_plugin_init(const struct uhttpd_ops *o, struct config *c)
299 _L = uh_lua_state_init();
300 ops->dispatch_add(&lua_dispatch);
304 struct uhttpd_plugin uhttpd_plugin = {
305 .init = lua_plugin_init,