0dd4d4c7418b040c6ef9e1da971cd85fe7f8da28
[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
93 uh_lua_strconvert(lua_State *L, int (*convert)(char *, int, const char *, int))
94 {
95         const char *in_buf;
96         static char out_buf[4096];
97         size_t in_len;
98         int out_len;
99
100         in_buf = luaL_checklstring(L, 1, &in_len);
101         out_len = convert(out_buf, sizeof(out_buf), in_buf, in_len);
102
103         if (out_len < 0) {
104                 const char *error;
105
106                 if (out_len == -1)
107                         error = "buffer overflow";
108                 else
109                         error = "malformed string";
110
111                 luaL_error(L, "%s on URL conversion\n", error);
112         }
113
114         lua_pushlstring(L, out_buf, out_len);
115         return 1;
116 }
117
118 static int uh_lua_urldecode(lua_State *L)
119 {
120         return uh_lua_strconvert(L, ops->urldecode);
121 }
122
123 static int uh_lua_urlencode(lua_State *L)
124 {
125         return uh_lua_strconvert(L, ops->urlencode);
126 }
127
128 static lua_State *uh_lua_state_init(void)
129 {
130         const char *msg = "(unknown error)";
131         const char *status;
132         lua_State *L;
133         int ret;
134
135         L = lua_open();
136         luaL_openlibs(L);
137
138         /* build uhttpd api table */
139         lua_newtable(L);
140
141         /* 
142          * use print as send and sendc implementation,
143          * chunked transfer is handled in the main server
144          */
145         lua_getglobal(L, "print");
146         lua_pushvalue(L, -1);
147         lua_setfield(L, -3, "send");
148         lua_setfield(L, -2, "sendc");
149
150         lua_pushcfunction(L, uh_lua_recv);
151         lua_setfield(L, -2, "recv");
152
153         lua_pushcfunction(L, uh_lua_urldecode);
154         lua_setfield(L, -2, "urldecode");
155
156         lua_pushcfunction(L, uh_lua_urlencode);
157         lua_setfield(L, -2, "urlencode");
158
159         lua_pushstring(L, conf.docroot);
160         lua_setfield(L, -2, "docroot");
161
162         lua_setglobal(L, "uhttpd");
163
164         ret = luaL_loadfile(L, conf.lua_handler);
165         if (ret) {
166                 status = "loading";
167                 goto error;
168         }
169
170         ret = lua_pcall(L, 0, 0, 0);
171         if (ret) {
172                 status = "initializing";
173                 goto error;
174         }
175
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");
179                 exit(1);
180         }
181
182         return L;
183
184 error:
185         if (!lua_isnil(L, -1))
186                 msg = lua_tostring(L, -1);
187
188         fprintf(stderr, "Error %s Lua handler: %s\n", status, msg);
189         exit(1);
190         return NULL;
191 }
192
193 static void lua_main(struct client *cl, struct path_info *pi, const char *url)
194 {
195         struct blob_attr *cur;
196         const char *error;
197         struct env_var *var;
198         lua_State *L = _L;
199         int path_len, prefix_len;
200         char *str;
201         int rem;
202
203         lua_getglobal(L, UH_LUA_CB);
204
205         /* new env table for this request */
206         lua_newtable(L);
207
208         prefix_len = strlen(conf.lua_prefix);
209         path_len = strlen(url);
210         str = strchr(url, '?');
211         if (str) {
212                 pi->query = str;
213                 path_len = str - url;
214         }
215         if (path_len > prefix_len) {
216                 lua_pushlstring(L, url + prefix_len,
217                                 path_len - prefix_len);
218                 lua_setfield(L, -2, "PATH_INFO");
219         }
220
221         for (var = ops->get_process_vars(cl, pi); var->name; var++) {
222                 if (!var->value)
223                         continue;
224
225                 lua_pushstring(L, var->value);
226                 lua_setfield(L, -2, var->name);
227         }
228
229         lua_pushnumber(L, 0.9 + (cl->request.version / 10.0));
230         lua_setfield(L, -2, "HTTP_VERSION");
231
232         lua_newtable(L);
233         blob_for_each_attr(cur, cl->hdr.head, rem) {
234                 lua_pushstring(L, blobmsg_data(cur));
235                 lua_setfield(L, -2, blobmsg_name(cur));
236         }
237         lua_setfield(L, -2, "headers");
238
239         switch(lua_pcall(L, 1, 0, 0)) {
240         case LUA_ERRMEM:
241         case LUA_ERRRUN:
242                 error = luaL_checkstring(L, -1);
243                 if (!error)
244                         error = "(unknown error)";
245
246                 printf("Status: 500 Internal Server Error\r\n\r\n"
247                "Unable to launch the requested Lua program:\n"
248                "  %s: %s\n", pi->phys, strerror(errno));
249         }
250
251         exit(0);
252 }
253
254 static void lua_handle_request(struct client *cl, const char *url, struct path_info *pi)
255 {
256         static struct path_info _pi;
257
258         pi = &_pi;
259         pi->name = conf.lua_prefix;
260         pi->phys = conf.lua_handler;
261
262         if (!ops->create_process(cl, pi, url, lua_main)) {
263                 ops->client_error(cl, 500, "Internal Server Error",
264                                   "Failed to create CGI process: %s", strerror(errno));
265         }
266 }
267
268 static bool check_lua_url(const char *url)
269 {
270         return ops->path_match(conf.lua_prefix, url);
271 }
272
273 static struct dispatch_handler lua_dispatch = {
274         .check_url = check_lua_url,
275         .handle_request = lua_handle_request,
276 };
277
278 static int lua_plugin_init(const struct uhttpd_ops *o, struct config *c)
279 {
280         ops = o;
281         _conf = c;
282         _L = uh_lua_state_init();
283         ops->dispatch_add(&lua_dispatch);
284         return 0;
285 }
286
287 const struct uhttpd_plugin uhttpd_plugin = {
288         .init = lua_plugin_init,
289 };