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(struct lua_prefix
*lua
)
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
, 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 %s provides no "
190 UH_LUA_CB
"() callback.\n", lua
->handler
);
199 if (!lua_isnil(L
, -1))
200 msg
= lua_tostring(L
, -1);
202 fprintf(stderr
, "Error %s %s Lua handler: %s\n",
203 status
, lua
->handler
, msg
);
208 static void lua_main(struct client
*cl
, struct path_info
*pi
, char *url
)
210 struct blob_attr
*cur
;
214 int path_len
, prefix_len
;
218 lua_getglobal(L
, UH_LUA_CB
);
220 /* new env table for this request */
223 prefix_len
= strlen(pi
->name
);
224 path_len
= strlen(url
);
225 str
= strchr(url
, '?');
229 path_len
= str
- url
;
232 if (prefix_len
> 0 && pi
->name
[prefix_len
- 1] == '/')
235 if (path_len
> prefix_len
) {
236 lua_pushlstring(L
, url
+ prefix_len
,
237 path_len
- prefix_len
);
238 lua_setfield(L
, -2, "PATH_INFO");
241 for (var
= ops
->get_process_vars(cl
, pi
); var
->name
; var
++) {
245 lua_pushstring(L
, var
->value
);
246 lua_setfield(L
, -2, var
->name
);
249 lua_pushnumber(L
, 0.9 + (cl
->request
.version
/ 10.0));
250 lua_setfield(L
, -2, "HTTP_VERSION");
253 blob_for_each_attr(cur
, cl
->hdr
.head
, rem
) {
254 lua_pushstring(L
, blobmsg_data(cur
));
255 lua_setfield(L
, -2, blobmsg_name(cur
));
257 lua_setfield(L
, -2, "headers");
259 switch(lua_pcall(L
, 1, 0, 0)) {
262 error
= luaL_checkstring(L
, -1);
264 error
= "(unknown error)";
266 printf("Status: 500 Internal Server Error\r\n\r\n"
267 "Unable to launch the requested Lua program:\n"
268 " %s: %s\n", pi
->phys
, error
);
274 static void lua_handle_request(struct client
*cl
, char *url
, struct path_info
*pi
)
276 struct lua_prefix
*p
;
277 static struct path_info _pi
;
279 list_for_each_entry(p
, &conf
.lua_prefix
, list
) {
280 if (!ops
->path_match(p
->prefix
, url
))
284 pi
->name
= p
->prefix
;
285 pi
->phys
= p
->handler
;
289 if (!ops
->create_process(cl
, pi
, url
, lua_main
)) {
290 ops
->client_error(cl
, 500, "Internal Server Error",
291 "Failed to create CGI process: %s",
298 ops
->client_error(cl
, 500, "Internal Server Error",
299 "Failed to lookup matching handler");
302 static bool check_lua_url(const char *url
)
304 struct lua_prefix
*p
;
306 list_for_each_entry(p
, &conf
.lua_prefix
, list
)
307 if (ops
->path_match(p
->prefix
, url
))
313 static struct dispatch_handler lua_dispatch
= {
315 .check_url
= check_lua_url
,
316 .handle_request
= lua_handle_request
,
319 static int lua_plugin_init(const struct uhttpd_ops
*o
, struct config
*c
)
321 struct lua_prefix
*p
;
326 list_for_each_entry(p
, &conf
.lua_prefix
, list
)
327 uh_lua_state_init(p
);
329 ops
->dispatch_add(&lua_dispatch
);
333 struct uhttpd_plugin uhttpd_plugin
= {
334 .init
= lua_plugin_init
,