2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <libubox/blobmsg.h>
31 #define UH_LUA_CB "handle_request"
33 static const struct uhttpd_ops *ops;
34 static struct config *_conf;
39 static int uh_lua_recv(lua_State *L)
41 static struct pollfd pfd = {
50 len = luaL_checknumber(L, 1);
55 buf = luaL_prepbuffer(&B);
56 r = read(STDIN_FILENO, buf, LUAL_BUFFERSIZE);
58 if (errno == EWOULDBLOCK || errno == EAGAIN) {
61 if (pfd.revents & POLLIN)
76 if (r != LUAL_BUFFERSIZE)
81 lua_pushnumber(L, data_len);
93 uh_lua_strconvert(lua_State *L, int (*convert)(char *, int, const char *, int))
96 static char out_buf[4096];
100 in_buf = luaL_checklstring(L, 1, &in_len);
101 out_len = convert(out_buf, sizeof(out_buf), in_buf, in_len);
107 error = "buffer overflow";
109 error = "malformed string";
111 luaL_error(L, "%s on URL conversion\n", error);
114 lua_pushlstring(L, out_buf, out_len);
118 static int uh_lua_urldecode(lua_State *L)
120 return uh_lua_strconvert(L, ops->urldecode);
123 static int uh_lua_urlencode(lua_State *L)
125 return uh_lua_strconvert(L, ops->urlencode);
128 static lua_State *uh_lua_state_init(void)
130 const char *msg = "(unknown error)";
138 /* build uhttpd api table */
142 * use print as send and sendc implementation,
143 * chunked transfer is handled in the main server
145 lua_getglobal(L, "print");
146 lua_pushvalue(L, -1);
147 lua_setfield(L, -3, "send");
148 lua_setfield(L, -2, "sendc");
150 lua_pushcfunction(L, uh_lua_recv);
151 lua_setfield(L, -2, "recv");
153 lua_pushcfunction(L, uh_lua_urldecode);
154 lua_setfield(L, -2, "urldecode");
156 lua_pushcfunction(L, uh_lua_urlencode);
157 lua_setfield(L, -2, "urlencode");
159 lua_pushstring(L, conf.docroot);
160 lua_setfield(L, -2, "docroot");
162 lua_setglobal(L, "uhttpd");
164 ret = luaL_loadfile(L, conf.lua_handler);
170 ret = lua_pcall(L, 0, 0, 0);
172 status = "initializing";
176 lua_getglobal(L, UH_LUA_CB);
177 if (!lua_isfunction(L, -1)) {
178 fprintf(stderr, "Error: Lua handler provides no " UH_LUA_CB "() callback.\n");
185 if (!lua_isnil(L, -1))
186 msg = lua_tostring(L, -1);
188 fprintf(stderr, "Error %s Lua handler: %s\n", status, msg);
193 static void lua_main(struct client *cl, struct path_info *pi, const char *url)
198 int path_len, prefix_len;
201 lua_getglobal(L, UH_LUA_CB);
203 /* new env table for this request */
206 prefix_len = strlen(conf.lua_prefix);
207 path_len = strlen(url);
208 str = strchr(url, '?');
211 path_len = str - url;
213 if (path_len > prefix_len) {
214 lua_pushlstring(L, url + prefix_len,
215 path_len - prefix_len);
216 lua_setfield(L, -2, "PATH_INFO");
219 for (var = ops->get_process_vars(cl, pi); var->name; var++) {
223 lua_pushstring(L, var->value);
224 lua_setfield(L, -2, var->name);
227 lua_pushnumber(L, 0.9 + (cl->request.version / 10.0));
228 lua_setfield(L, -2, "HTTP_VERSION");
230 switch(lua_pcall(L, 1, 0, 0)) {
233 error = luaL_checkstring(L, -1);
235 error = "(unknown error)";
237 printf("Status: 500 Internal Server Error\r\n\r\n"
238 "Unable to launch the requested Lua program:\n"
239 " %s: %s\n", pi->phys, strerror(errno));
245 static void lua_handle_request(struct client *cl, const char *url, struct path_info *pi)
247 static struct path_info _pi;
250 pi->name = conf.lua_prefix;
251 pi->phys = conf.lua_handler;
253 if (!ops->create_process(cl, pi, url, lua_main)) {
254 ops->client_error(cl, 500, "Internal Server Error",
255 "Failed to create CGI process: %s", strerror(errno));
259 static bool check_lua_url(const char *url)
261 return ops->path_match(conf.lua_prefix, url);
264 static struct dispatch_handler lua_dispatch = {
265 .check_url = check_lua_url,
266 .handle_request = lua_handle_request,
269 static int lua_plugin_init(const struct uhttpd_ops *o, struct config *c)
273 _L = uh_lua_state_init();
274 ops->dispatch_add(&lua_dispatch);
278 const struct uhttpd_plugin uhttpd_plugin = {
279 .init = lua_plugin_init,