3 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
18 #include <boa-plugin.h>
24 #define LUAMAIN "luci.lua"
26 static lua_State *L = NULL;
28 extern int luci_parse_header (lua_State *L);
30 static int luci_init(struct httpd_plugin *p)
41 path = malloc(strlen(p->dir) + sizeof(LUAMAIN) + 2);
43 strcat(path, "/" LUAMAIN);
45 ret = luaL_dofile(L, path);
47 lua_getfield(L, LUA_GLOBALSINDEX, "luci-plugin");
49 if (!lua_istable(L, -1)) {
54 lua_getfield(L, -1, "init");
55 if (!lua_isfunction(L, -1))
58 lua_pushstring(L, p->dir);
59 ret = lua_pcall(L, 1, 0, 0);
69 fprintf(stderr, "Error: ");
71 const char *s = lua_tostring(L, -1);
74 fprintf(stderr, "%s\n", s);
77 fprintf(stderr, "Out of memory!\n");
82 static void pushvar(char *name, char *val)
87 lua_pushstring(L, val);
88 lua_setfield(L, -2, name);
91 static int luci_pcall(lua_State *L, char *func, int narg)
95 ret = lua_pcall(L, narg, narg, 0);
97 const char *s = lua_tostring(L, -1);
99 fprintf(stderr, "Error running %s: %s\n", func, s);
105 ret = lua_isnumber(L, -1);
109 ret = lua_tonumber(L, -1);
116 static int luci_prepare_req(struct httpd_plugin *p, struct http_context *ctx)
120 lua_getglobal(L, "luci-plugin");
121 lua_getfield(L, -1, "prepare_req");
123 ret = lua_isfunction(L, -1);
127 lua_pushstring(L, ctx->uri);
129 ret = luci_pcall(L, "prepare_req", 1);
136 static int luci_handle_req(struct httpd_plugin *p, struct http_context *ctx)
140 lua_newtable(L); /* new table for the http context */
142 /* convert http_context data structure to lua table */
143 #define PUSH(x) pushvar(#x, ctx->x)
144 PUSH(request_method);
149 lua_pushinteger(L, ctx->remote_port);
150 lua_setfield(L, -2, "remote_port");
152 PUSH(content_length);
156 if (!strncmp(ctx->uri, p->prefix, strlen(p->prefix)))
157 pushvar("uri", ctx->uri + strlen(p->prefix));
159 pushvar("uri", ctx->uri);
162 /* make sure the global 'luci' table is prepared */
163 lua_getglobal(L, "luci-plugin");
164 if (!lua_istable(L, -1))
167 lua_getfield(L, -1, "init_req");
168 if (!lua_isfunction(L, -1)) {
172 lua_pushvalue(L, -3);
173 luci_pcall(L, "init_req", 1);
176 /* storage space for cgi variables */
178 lua_pushvalue(L, -1); /* copy for setfield */
179 lua_setfield(L, -3, "vars");
181 lua_pushvalue(L, -3); /* the http context table */
184 * make luci_parse_header a closure
185 * argument 1: the luci.vars table
186 * argument 2: the http context table
188 lua_pushcclosure(L, luci_parse_header, 2);
189 ret = luci_pcall(L, "parse_header", 0);
191 lua_getfield(L, -1, "handle_req");
192 ret = lua_isfunction(L, -1);
196 lua_pushvalue(L, -3);
197 ret = luci_pcall(L, "handle_req", 1);
199 /* pop the luci and http context tables */
205 static void luci_unload(struct httpd_plugin *p)
214 .prepare_req = luci_prepare_req,
215 .handle_req = luci_handle_req,