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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
#include "uhttpd.h"
#include "uhttpd-utils.h"
#include "uhttpd-lua.h"
static int uh_lua_recv(lua_State *L)
{
size_t length;
char buffer[UH_LIMIT_MSGHEAD];
ssize_t rlen = 0;
fd_set reader;
struct timeval timeout;
struct client *cl;
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
cl = (struct client *) lua_topointer(L, 1);
length = luaL_checknumber(L, 2);
if( (cl != NULL) && (length > 0) && (length <= sizeof(buffer)) )
{
FD_ZERO(&reader);
FD_SET(cl->socket, &reader);
/* fail after 0.1s */
timeout.tv_sec = 0;
timeout.tv_usec = 100000;
/* first return stuff from peek buffer */
if( cl->peeklen > 0 )
{
/* receive data */
rlen = uh_tcp_recv(cl, buffer, min(cl->peeklen, length));
lua_pushnumber(L, rlen);
lua_pushlstring(L, buffer, rlen);
return 2;
}
/* check whether fd is readable */
else if( select(cl->socket + 1, &reader, NULL, NULL, &timeout) > 0 )
{
/* receive data */
rlen = uh_tcp_recv(cl, buffer, length);
lua_pushnumber(L, rlen);
if( rlen > 0 )
{
lua_pushlstring(L, buffer, rlen);
return 2;
}
return 1;
}
/* no, timeout and actually no data */
lua_pushnumber(L, -2);
return 1;
}
/* parameter error */
lua_pushnumber(L, -3);
return 1;
}
static int uh_lua_send(lua_State *L)
{
size_t length;
const char *buffer;
ssize_t slen = 0;
struct client *cl;
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
cl = (struct client *) lua_topointer(L, 1);
buffer = luaL_checklstring(L, 2, &length);
if( (cl != NULL) && (length > 0) )
{
slen = uh_tcp_send(cl, buffer, length);
lua_pushnumber(L, slen);
return 1;
}
lua_pushnumber(L, -1);
return 1;
}
static int uh_lua_urldecode(lua_State *L)
{
size_t inlen, outlen;
const char *inbuf;
char outbuf[UH_LIMIT_MSGHEAD];
inbuf = luaL_checklstring(L, 1, &inlen);
outlen = uh_urldecode(outbuf, sizeof(outbuf), inbuf, inlen);
lua_pushlstring(L, outbuf, outlen);
return 1;
}
lua_State * uh_lua_init(const char *handler)
{
lua_State *L = lua_open();
const luaL_reg *lib;
const char *err_str = NULL;
/* Declare the Lua libraries we wish to use. */
/* Note: If you are opening and running a file containing Lua code */
/* using 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries */
/* used in that file here also. */
static const luaL_reg lualibs[] =
{
{ "base", luaopen_base },
{ "string", luaopen_string },
{ NULL, NULL }
};
/* preload libraries */
for (lib = lualibs; lib->func != NULL; lib++)
{
lib->func(L);
lua_settop(L, 0);
}
/* register global send and receive functions */
lua_pushcfunction(L, uh_lua_recv);
lua_setfield(L, LUA_GLOBALSINDEX, "recv");
lua_pushcfunction(L, uh_lua_send);
lua_setfield(L, LUA_GLOBALSINDEX, "send");
lua_pushcfunction(L, uh_lua_urldecode);
lua_setfield(L, LUA_GLOBALSINDEX, "urldecode");
/* load Lua handler */
switch( luaL_loadfile(L, handler) )
{
case LUA_ERRSYNTAX:
fprintf(stderr,
"Lua handler contains syntax errors, unable to continue\n");
exit(1);
case LUA_ERRMEM:
fprintf(stderr,
"Lua handler ran out of memory, unable to continue\n");
exit(1);
case LUA_ERRFILE:
fprintf(stderr,
"Lua cannot open the handler script, unable to continue\n");
exit(1);
default:
/* compile Lua handler */
switch( lua_pcall(L, 0, 0, 0) )
{
case LUA_ERRRUN:
err_str = luaL_checkstring(L, -1);
fprintf(stderr,
"Lua handler had runtime error, unable to continue\n"
"Error: %s\n", err_str
);
exit(1);
case LUA_ERRMEM:
err_str = luaL_checkstring(L, -1);
fprintf(stderr,
"Lua handler ran out of memory, unable to continue\n"
"Error: %s\n", err_str
);
exit(1);
default:
/* test handler function */
lua_getglobal(L, UH_LUA_CALLBACK);
if( ! lua_isfunction(L, -1) )
{
fprintf(stderr,
"Lua handler provides no " UH_LUA_CALLBACK "(), unable to continue\n");
exit(1);
}
lua_pop(L, 1);
break;
}
break;
}
return L;
}
void uh_lua_request(struct client *cl, struct http_request *req, lua_State *L)
{
int i;
char *query_string;
const char *prefix = cl->server->conf->lua_prefix;
const char *err_str = NULL;
/* put handler callback on stack */
lua_getglobal(L, UH_LUA_CALLBACK);
/* build env table */
lua_newtable(L);
/* client object */
lua_pushlightuserdata(L, (void *)cl);
lua_setfield(L, -2, "client");
/* request method */
switch(req->method)
{
case UH_HTTP_MSG_GET:
lua_pushstring(L, "get");
break;
case UH_HTTP_MSG_HEAD:
lua_pushstring(L, "head");
break;
case UH_HTTP_MSG_POST:
lua_pushstring(L, "post");
break;
}
lua_setfield(L, -2, "request_method");
/* request url */
lua_pushstring(L, req->url);
lua_setfield(L, -2, "request_url");
/* query string, path info */
if( (query_string = strchr(req->url, '?')) != NULL )
{
lua_pushstring(L, query_string + 1);
lua_setfield(L, -2, "query_string");
if( (int)(query_string - req->url) > strlen(prefix) )
{
lua_pushlstring(L,
&req->url[strlen(prefix)],
(int)(query_string - req->url) - strlen(prefix)
);
lua_setfield(L, -2, "path_info");
}
}
else if( strlen(req->url) > strlen(prefix) )
{
lua_pushstring(L, &req->url[strlen(prefix)]);
lua_setfield(L, -2, "path_info");
}
/* http protcol version */
lua_pushnumber(L, floor(req->version * 10) / 10);
lua_setfield(L, -2, "http_version");
/* address information */
lua_pushstring(L, sa_straddr(&cl->peeraddr));
lua_setfield(L, -2, "remote_addr");
lua_pushinteger(L, sa_port(&cl->peeraddr));
lua_setfield(L, -2, "remote_port");
lua_pushstring(L, sa_straddr(&cl->servaddr));
lua_setfield(L, -2, "server_addr");
lua_pushinteger(L, sa_port(&cl->servaddr));
lua_setfield(L, -2, "server_port");
/* headers */
lua_newtable(L);
foreach_header(i, req->headers)
{
lua_pushstring(L, req->headers[i+1]);
lua_setfield(L, -2, req->headers[i]);
}
lua_setfield(L, -2, "headers");
/* call */
switch( lua_pcall(L, 1, 0, 0) )
{
case LUA_ERRRUN:
err_str = luaL_checkstring(L, -1);
uh_http_sendhf(cl, 500, "Lua runtime error",
"Lua raised an error:\n%s\n", err_str);
break;
case LUA_ERRMEM:
err_str = luaL_checkstring(L, -1);
uh_http_sendhf(cl, 500, "Lua out of memory",
"Lua raised an error:\n%s\n", err_str);
break;
default:
break;
}
}
|