rework the ubus plugin to support JSON-RPC 2.0
[project/uhttpd.git] / lua.c
1 /*
2  * uhttpd - Tiny single-threaded httpd
3  *
4  *   Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20
21 #include <libubox/blobmsg.h>
22 #include <lua.h>
23 #include <lauxlib.h>
24 #include <lualib.h>
25 #include <stdio.h>
26 #include <poll.h>
27
28 #include "uhttpd.h"
29 #include "plugin.h"
30
31 #define UH_LUA_CB       "handle_request"
32
33 static const struct uhttpd_ops *ops;
34 static struct config *_conf;
35 #define conf (*_conf)
36
37 static lua_State *_L;
38
39 static int uh_lua_recv(lua_State *L)
40 {
41         static struct pollfd pfd = {
42                 .fd = STDIN_FILENO,
43                 .events = POLLIN,
44         };
45         luaL_Buffer B;
46         int data_len = 0;
47         int len;
48         int r;
49
50         len = luaL_checknumber(L, 1);
51         luaL_buffinit(L, &B);
52         while(len > 0) {
53                 char *buf;
54
55                 buf = luaL_prepbuffer(&B);
56                 r = read(STDIN_FILENO, buf, 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                 if (r != LUAL_BUFFERSIZE)
77                         break;
78         }
79
80         luaL_pushresult(&B);
81         lua_pushnumber(L, data_len);
82         if (data_len > 0) {
83                 lua_pushvalue(L, -2);
84                 lua_remove(L, -3);
85                 return 2;
86         } else {
87                 lua_remove(L, -2);
88                 return 1;
89         }
90 }
91
92 static int uh_lua_send(lua_State *L)
93 {
94         const char *buf;
95         size_t len;
96
97         buf = luaL_checklstring(L, 1, &len);
98         if (len > 0)
99                 len = write(STDOUT_FILENO, buf, len);
100
101         lua_pushnumber(L, len);
102         return 1;
103 }
104
105 static int
106 uh_lua_strconvert(lua_State *L, int (*convert)(char *, int, const char *, int))
107 {
108         const char *in_buf;
109         static char out_buf[4096];
110         size_t in_len;
111         int out_len;
112
113         in_buf = luaL_checklstring(L, 1, &in_len);
114         out_len = convert(out_buf, sizeof(out_buf), in_buf, in_len);
115
116         if (out_len < 0) {
117                 const char *error;
118
119                 if (out_len == -1)
120                         error = "buffer overflow";
121                 else
122                         error = "malformed string";
123
124                 luaL_error(L, "%s on URL conversion\n", error);
125         }
126
127         lua_pushlstring(L, out_buf, out_len);
128         return 1;
129 }
130
131 static int uh_lua_urldecode(lua_State *L)
132 {
133         return uh_lua_strconvert(L, ops->urldecode);
134 }
135
136 static int uh_lua_urlencode(lua_State *L)
137 {
138         return uh_lua_strconvert(L, ops->urlencode);
139 }
140
141 static lua_State *uh_lua_state_init(void)
142 {
143         const char *msg = "(unknown error)";
144         const char *status;
145         lua_State *L;
146         int ret;
147
148         L = lua_open();
149         luaL_openlibs(L);
150
151         /* build uhttpd api table */
152         lua_newtable(L);
153
154         lua_pushcfunction(L, uh_lua_send);
155         lua_setfield(L, -2, "send");
156
157         lua_pushcfunction(L, uh_lua_send);
158         lua_setfield(L, -2, "sendc");
159
160         lua_pushcfunction(L, uh_lua_recv);
161         lua_setfield(L, -2, "recv");
162
163         lua_pushcfunction(L, uh_lua_urldecode);
164         lua_setfield(L, -2, "urldecode");
165
166         lua_pushcfunction(L, uh_lua_urlencode);
167         lua_setfield(L, -2, "urlencode");
168
169         lua_pushstring(L, conf.docroot);
170         lua_setfield(L, -2, "docroot");
171
172         lua_setglobal(L, "uhttpd");
173
174         ret = luaL_loadfile(L, conf.lua_handler);
175         if (ret) {
176                 status = "loading";
177                 goto error;
178         }
179
180         ret = lua_pcall(L, 0, 0, 0);
181         if (ret) {
182                 status = "initializing";
183                 goto error;
184         }
185
186         lua_getglobal(L, UH_LUA_CB);
187         if (!lua_isfunction(L, -1)) {
188                 fprintf(stderr, "Error: Lua handler provides no " UH_LUA_CB "() callback.\n");
189                 exit(1);
190         }
191
192         return L;
193
194 error:
195         if (!lua_isnil(L, -1))
196                 msg = lua_tostring(L, -1);
197
198         fprintf(stderr, "Error %s Lua handler: %s\n", status, msg);
199         exit(1);
200         return NULL;
201 }
202
203 static void lua_main(struct client *cl, struct path_info *pi, char *url)
204 {
205         struct blob_attr *cur;
206         const char *error;
207         struct env_var *var;
208         lua_State *L = _L;
209         int path_len, prefix_len;
210         char *str;
211         int rem;
212
213         lua_getglobal(L, UH_LUA_CB);
214
215         /* new env table for this request */
216         lua_newtable(L);
217
218         prefix_len = strlen(conf.lua_prefix);
219         path_len = strlen(url);
220         str = strchr(url, '?');
221         if (str) {
222                 pi->query = str;
223                 path_len = str - url;
224         }
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");
229         }
230
231         for (var = ops->get_process_vars(cl, pi); var->name; var++) {
232                 if (!var->value)
233                         continue;
234
235                 lua_pushstring(L, var->value);
236                 lua_setfield(L, -2, var->name);
237         }
238
239         lua_pushnumber(L, 0.9 + (cl->request.version / 10.0));
240         lua_setfield(L, -2, "HTTP_VERSION");
241
242         lua_newtable(L);
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));
246         }
247         lua_setfield(L, -2, "headers");
248
249         switch(lua_pcall(L, 1, 0, 0)) {
250         case LUA_ERRMEM:
251         case LUA_ERRRUN:
252                 error = luaL_checkstring(L, -1);
253                 if (!error)
254                         error = "(unknown error)";
255
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));
259         }
260
261         exit(0);
262 }
263
264 static void lua_handle_request(struct client *cl, char *url, struct path_info *pi)
265 {
266         static struct path_info _pi;
267
268         pi = &_pi;
269         pi->name = conf.lua_prefix;
270         pi->phys = conf.lua_handler;
271
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));
275         }
276 }
277
278 static bool check_lua_url(const char *url)
279 {
280         return ops->path_match(conf.lua_prefix, url);
281 }
282
283 static struct dispatch_handler lua_dispatch = {
284         .check_url = check_lua_url,
285         .handle_request = lua_handle_request,
286 };
287
288 static int lua_plugin_init(const struct uhttpd_ops *o, struct config *c)
289 {
290         ops = o;
291         _conf = c;
292         _L = uh_lua_state_init();
293         ops->dispatch_add(&lua_dispatch);
294         return 0;
295 }
296
297 const struct uhttpd_plugin uhttpd_plugin = {
298         .init = lua_plugin_init,
299 };