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_checknumber(L, 1);
54 buf = luaL_prepbuffer(&B);
55 r = read(STDIN_FILENO, buf, LUAL_BUFFERSIZE);
57 if (errno == EWOULDBLOCK || errno == EAGAIN) {
60 if (pfd.revents & POLLIN)
75 if (r != LUAL_BUFFERSIZE)
80 lua_pushnumber(L, data_len);
91 static int uh_lua_send(lua_State *L)
96 buf = luaL_checklstring(L, 1, &len);
98 len = write(STDOUT_FILENO, buf, len);
100 lua_pushnumber(L, len);
105 uh_lua_strconvert(lua_State *L, int (*convert)(char *, int, const char *, int))
108 static char out_buf[4096];
112 in_buf = luaL_checklstring(L, 1, &in_len);
113 out_len = convert(out_buf, sizeof(out_buf), in_buf, in_len);
119 error = "buffer overflow";
121 error = "malformed string";
123 luaL_error(L, "%s on URL conversion\n", error);
126 lua_pushlstring(L, out_buf, out_len);
130 static int uh_lua_urldecode(lua_State *L)
132 return uh_lua_strconvert(L, ops->urldecode);
135 static int uh_lua_urlencode(lua_State *L)
137 return uh_lua_strconvert(L, ops->urlencode);
140 static lua_State *uh_lua_state_init(void)
142 const char *msg = "(unknown error)";
150 /* build uhttpd api table */
153 lua_pushcfunction(L, uh_lua_send);
154 lua_setfield(L, -2, "send");
156 lua_pushcfunction(L, uh_lua_send);
157 lua_setfield(L, -2, "sendc");
159 lua_pushcfunction(L, uh_lua_recv);
160 lua_setfield(L, -2, "recv");
162 lua_pushcfunction(L, uh_lua_urldecode);
163 lua_setfield(L, -2, "urldecode");
165 lua_pushcfunction(L, uh_lua_urlencode);
166 lua_setfield(L, -2, "urlencode");
168 lua_pushstring(L, conf.docroot);
169 lua_setfield(L, -2, "docroot");
171 lua_setglobal(L, "uhttpd");
173 ret = luaL_loadfile(L, conf.lua_handler);
179 ret = lua_pcall(L, 0, 0, 0);
181 status = "initializing";
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");
194 if (!lua_isnil(L, -1))
195 msg = lua_tostring(L, -1);
197 fprintf(stderr, "Error %s Lua handler: %s\n", status, msg);
202 static void lua_main(struct client *cl, struct path_info *pi, char *url)
204 struct blob_attr *cur;
208 int path_len, prefix_len;
212 lua_getglobal(L, UH_LUA_CB);
214 /* new env table for this request */
217 prefix_len = strlen(conf.lua_prefix);
218 path_len = strlen(url);
219 str = strchr(url, '?');
223 path_len = str - url;
225 if (path_len > prefix_len) {
226 lua_pushlstring(L, url + prefix_len,
227 path_len - prefix_len);
228 lua_setfield(L, -2, "PATH_INFO");
231 for (var = ops->get_process_vars(cl, pi); var->name; var++) {
235 lua_pushstring(L, var->value);
236 lua_setfield(L, -2, var->name);
239 lua_pushnumber(L, 0.9 + (cl->request.version / 10.0));
240 lua_setfield(L, -2, "HTTP_VERSION");
243 blob_for_each_attr(cur, cl->hdr.head, rem) {
244 lua_pushstring(L, blobmsg_data(cur));
245 lua_setfield(L, -2, blobmsg_name(cur));
247 lua_setfield(L, -2, "headers");
249 switch(lua_pcall(L, 1, 0, 0)) {
252 error = luaL_checkstring(L, -1);
254 error = "(unknown error)";
256 printf("Status: 500 Internal Server Error\r\n\r\n"
257 "Unable to launch the requested Lua program:\n"
258 " %s: %s\n", pi->phys, strerror(errno));
264 static void lua_handle_request(struct client *cl, char *url, struct path_info *pi)
266 static struct path_info _pi;
269 pi->name = conf.lua_prefix;
270 pi->phys = conf.lua_handler;
272 if (!ops->create_process(cl, pi, url, lua_main)) {
273 ops->client_error(cl, 500, "Internal Server Error",
274 "Failed to create CGI process: %s", strerror(errno));
278 static bool check_lua_url(const char *url)
280 return ops->path_match(conf.lua_prefix, url);
283 static struct dispatch_handler lua_dispatch = {
285 .check_url = check_lua_url,
286 .handle_request = lua_handle_request,
289 static int lua_plugin_init(const struct uhttpd_ops *o, struct config *c)
293 _L = uh_lua_state_init();
294 ops->dispatch_add(&lua_dispatch);
298 const struct uhttpd_plugin uhttpd_plugin = {
299 .init = lua_plugin_init,