1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
* luci
* Copyright (C) 2008 John Crispin <blogic@openwrt.org>
* Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <string.h>
#include <stdio.h>
#include <boa-plugin.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdbool.h>
#include <stdlib.h>
#define LUAMAIN "luci.lua"
static lua_State *L = NULL;
extern int luci_parse_header (lua_State *L);
static lua_State *luci_context_init(struct httpd_plugin *p)
{
char *path = NULL;
lua_State *Lnew;
int ret = 0;
Lnew = luaL_newstate();
if (!Lnew)
goto error;
luaL_openlibs(Lnew);
path = malloc(strlen(p->dir) + sizeof(LUAMAIN) + 2);
strcpy(path, p->dir);
strcat(path, "/" LUAMAIN);
ret = luaL_dofile(Lnew, path);
lua_getfield(Lnew, LUA_GLOBALSINDEX, "luci-plugin");
do {
if (!lua_istable(Lnew, -1)) {
ret = 1;
break;
}
lua_getfield(Lnew, -1, "init");
if (!lua_isfunction(Lnew, -1))
break;
lua_pushstring(Lnew, p->dir);
ret = lua_pcall(Lnew, 1, 0, 0);
} while (0);
free(path);
if (ret != 0)
goto error;
return Lnew;
error:
fprintf(stderr, "Error: ");
if (Lnew) {
const char *s = lua_tostring(Lnew, -1);
if (!s)
s = "unknown error";
fprintf(stderr, "%s\n", s);
lua_close(Lnew);
} else {
fprintf(stderr, "Out of memory!\n");
}
return NULL;
}
static int luci_init(struct httpd_plugin *p)
{
L = luci_context_init(p);
return (L != NULL);
}
static void pushvar(char *name, char *val)
{
if (!val)
return;
lua_pushstring(L, val);
lua_setfield(L, -2, name);
}
static int luci_pcall(lua_State *L, char *func, int narg)
{
int ret;
ret = lua_pcall(L, narg, narg, 0);
if (ret) {
const char *s = lua_tostring(L, -1);
if (s)
fprintf(stderr, "Error running %s: %s\n", func, s);
return ret;
}
if (!narg)
return ret;
ret = lua_isnumber(L, -1);
if (!ret)
goto done;
ret = lua_tonumber(L, -1);
done:
lua_pop(L, 1);
return ret;
}
static int luci_prepare_req(struct httpd_plugin *p, struct http_context *ctx)
{
int ret;
bool reload = false;
lua_getglobal(L, "luci-plugin");
lua_getfield(L, -1, "reload");
if (lua_isboolean(L, -1))
reload = lua_toboolean(L, -1);
lua_pop(L, 1);
if (reload) {
lua_close(L);
L = luci_context_init(p);
lua_getglobal(L, "luci-plugin");
}
lua_getfield(L, -1, "prepare_req");
ret = lua_isfunction(L, -1);
if (!ret)
goto done;
lua_pushstring(L, ctx->uri);
ret = luci_pcall(L, "prepare_req", 1);
done:
lua_pop(L, 1);
return ret;
}
static int luci_handle_req(struct httpd_plugin *p, struct http_context *ctx)
{
int ret;
lua_newtable(L); /* new table for the http context */
/* convert http_context data structure to lua table */
#define PUSH(x) pushvar(#x, ctx->x)
PUSH(cookie);
PUSH(request_method);
PUSH(server_addr);
PUSH(server_proto);
PUSH(query_string);
PUSH(remote_addr);
lua_pushinteger(L, ctx->remote_port);
lua_setfield(L, -2, "remote_port");
PUSH(content_type);
PUSH(content_length);
PUSH(http_accept);
#undef PUSH
if (!strncmp(ctx->uri, p->prefix, strlen(p->prefix)))
pushvar("uri", ctx->uri + strlen(p->prefix));
else
pushvar("uri", ctx->uri);
/* make sure the global 'luci' table is prepared */
lua_getglobal(L, "luci-plugin");
if (!lua_istable(L, -1))
return 0;
lua_getfield(L, -1, "init_req");
if (!lua_isfunction(L, -1)) {
/* ignore error */
lua_pop(L, 1);
} else {
lua_pushvalue(L, -3);
luci_pcall(L, "init_req", 1);
}
/* storage space for cgi variables */
lua_newtable(L);
lua_pushvalue(L, -1); /* copy for setfield */
lua_setfield(L, -3, "vars");
lua_pushvalue(L, -3); /* the http context table */
/*
* make luci_parse_header a closure
* argument 1: the luci.vars table
* argument 2: the http context table
*/
lua_pushcclosure(L, luci_parse_header, 2);
ret = luci_pcall(L, "parse_header", 0);
lua_getfield(L, -1, "handle_req");
ret = lua_isfunction(L, -1);
if (!ret)
goto done;
lua_pushvalue(L, -3);
ret = luci_pcall(L, "handle_req", 1);
/* pop the luci and http context tables */
done:
lua_pop(L, 2);
return ret;
}
static void luci_unload(struct httpd_plugin *p)
{
lua_close(L);
}
HTTPD_PLUGIN {
.prefix = "/luci/",
.init = luci_init,
.done = luci_unload,
.prepare_req = luci_prepare_req,
.handle_req = luci_handle_req,
};
|