-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathluasrv.c
More file actions
424 lines (352 loc) · 10.8 KB
/
luasrv.c
File metadata and controls
424 lines (352 loc) · 10.8 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* luasrv - a HTTPd for lua
* (c) 2009-19 Alacner zhang <alacner@gmail.com>
* This content is released under the MIT License.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
#include <stdbool.h>
#include <pthread.h>
#include <err.h>
#include <event.h>
#include <evhttp.h>
#define VERSION "1.0.0"
#define SESSION_CLEANUP_ALARM 3
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
#include "compat-5.1.h"
#endif
#include "libs/lfs.h"
#include "libs/luaiconv.h"
#include "libs/md5.h"
#include "libs/luamysql.h"
#if LUA_VERSION_NUM < 501
#define luaL_register(a, b, c) luaL_openlib((a), (b), (c), 0)
#endif
#define safe_emalloc(nmemb, size, offset) malloc((nmemb) * (size) + (offset))
//Global Setting
lua_State *L; /* lua state handle */
char *luasrv_settings_rootpath = NULL;
struct evhttp *httpd;
struct evbuffer *buf;
struct evkeyvalq *input_headers;
struct evkeyvalq *output_headers;
pthread_t ntid[2];
static void show_help(void)
{
char *b = "--------------------------------------------------------------------------------------------------\n"
"HTTP lua Service - luasrv v" VERSION "\n\n"
"Author: Alacner Zhang (http://alacner.com), E-mail: alacner@gmail.com\n"
"\n"
"-l <ip_addr> interface to listen on, default is 0.0.0.0\n"
"-p <num> TCP port number to listen on (default: 1206)\n"
"-x <path> root directory (example: /var/www/html)\n"
"-t <second> timeout for an http request (default: 1)\n"
"-d run as a daemon\n"
"-h print this help and exit\n\n"
"Use command \"killall luasrv\", \"pkill luasrv\" and \"kill PID of luasrv\" to stop luasrv.\n"
"Please note that don't use the command \"pkill -9 luasrv\" and \"kill -9 PID of luasrv\"!\n"
"\n"
"Please visit \"http://alacner.com/pro/luasrv\" for more help information.\n\n"
"--------------------------------------------------------------------------------------------------\n"
"\n";
fprintf(stderr, b, strlen(b));
}
#define NUL '\0'
#define MICRO_IN_SEC 1000000.00
#define SEC_IN_MIN 60
static int luaM_microtime (lua_State *L) {
struct timeval tp = {0};
int get_as_float = luaL_optnumber(L, 1, 0);
if (gettimeofday(&tp, NULL)) {
lua_pushboolean(L, 0);
}
if (get_as_float) {
lua_pushnumber(L, (double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
} else {
lua_pushfstring(L, "%f %d", tp.tv_usec / MICRO_IN_SEC, tp.tv_sec);
}
return 1;
}
static int luaM_md5 (lua_State *L) {
struct MD5Context md5c;
unsigned char ss[16];
char *s = (char *)luaL_optstring(L, 1, NULL);
int raw_output = luaL_optnumber(L, 2, 0);
int i;
MD5Init( &md5c );
MD5Update( &md5c, s, strlen(s) );
MD5Final( ss, &md5c );
if (raw_output) {
lua_pushstring(L, (char *)ss);
}
else
{
char md5_32[32][2];
for(i=0; i<16; i++ ) {
sprintf(md5_32[i], "%02x", ss[i]);
lua_pushstring(L, md5_32[i]);
}
lua_concat(L, 16);
}
return 1;
}
static int luaM_print (lua_State *L) {
const char *str = luaL_optstring(L, 1, NULL);
evbuffer_add_printf(buf, "%s", str);
return 0;
}
static int luaM_get_header (lua_State *L) {
const char *header = luaL_optstring(L, 1, NULL);
const char *header_data = evhttp_find_header (input_headers, header);
lua_pushstring(L, header_data);
return 1;
}
static int luaM_set_header (lua_State *L) {
const char *header = luaL_optstring(L, 1, NULL);
const char *value = luaL_optstring(L, 2, NULL);
evhttp_add_header(output_headers, header, value);
return 0;
}
/* 处理模块 */
void luasrv_handler(struct evhttp_request *req, void *arg)
{
buf = evbuffer_new();
input_headers = req->input_headers;
output_headers = req->output_headers;
// GET
char *decode_uri = strdup((char*) evhttp_request_uri(req));
lua_pushstring(L, decode_uri);
lua_setglobal(L, "GET_DATA");
if (req->type == EVHTTP_REQ_POST) { // POST
int post_data_len;
post_data_len = EVBUFFER_LENGTH(req->input_buffer);
if (post_data_len > 0) {
char *post_data;
// copy post data, The string can contain embedded zeros.
post_data = (char *)malloc(post_data_len + 1);
memset(post_data, '\0', post_data_len + 1);
memcpy (post_data, EVBUFFER_DATA(req->input_buffer), post_data_len);
lua_pushlstring(L, post_data, post_data_len + 1);
lua_setglobal(L, "POST_DATA");
free(post_data);
}
}
evhttp_add_header(req->output_headers, "Server", "luasrv " VERSION);
evhttp_add_header(req->output_headers, "Keep-Alive", "120");
//evhttp_add_header(req->output_headers, "Connection", "Keep-Alive");
char *script_name = strtok(decode_uri, "?");
char *query_string = strtok(NULL, "?");
lua_pushfstring(L, "%s%s", luasrv_settings_rootpath, script_name);
const char *script_filename = lua_tolstring(L, -1, NULL);
lua_pop(L, 1);
struct stat stat_buf;
if (lstat(script_filename, &stat_buf) < 0) {
fprintf(stderr, "lstat error for %s\r\n", script_filename);
evhttp_send_reply(req, HTTP_NOTFOUND, "NOT FOUND", buf);
/* 内存释放 */
free(decode_uri);
evbuffer_free(buf);
return;
}
if ( !S_ISREG(stat_buf.st_mode)) {//S_ISDIR
fprintf(stderr, "not found!\n");
evhttp_send_reply(req, HTTP_NOTFOUND, "NOT FOUND", buf);
/* 内存释放 */
free(decode_uri);
evbuffer_free(buf);
return;
}
lua_newtable(L);
lua_pushstring(L, "SCRIPT_NAME");
lua_pushstring(L, script_name);
lua_rawset(L, -3);
lua_pushstring(L, "REQUEST_URI");
lua_pushstring(L, req->uri);
lua_rawset(L, -3);
lua_pushstring(L, "REQUEST_TIME");
lua_pushnumber(L, (int)time((time_t*)NULL));
lua_rawset(L, -3);
lua_pushstring(L, "DOCUMENT_ROOT");
lua_pushstring(L, luasrv_settings_rootpath);
lua_rawset(L, -3);
lua_pushstring(L, "SCRIPT_FILENAME");
lua_pushstring(L, script_filename);
lua_rawset(L, -3);
lua_pushstring(L, "REQUEST_METHOD");
lua_pushnumber(L, req->type);
lua_rawset(L, -3);
lua_pushstring(L, "QUERY_STRING");
lua_pushstring(L, query_string);
lua_rawset(L, -3);
lua_pushstring(L, "HTTP_X_FORWARDED_FOR");
lua_pushstring(L, req->remote_host);
lua_rawset(L, -3);
lua_setglobal(L, "SERVER");
//evbuffer_add_printf(buf, "<br/>%s<br/>", script_filename);
if (luaL_loadfile(L, "./script/loader.lua") || lua_pcall(L, 0, 0, 0)) {
fprintf (stderr, "cannot run loader.lua: %s", lua_tostring(L, -1));
return;
}
/* 输出内容给客户端 */
evhttp_send_reply(req, HTTP_OK, "OK", buf);
/* 内存释放 */
free(decode_uri);
evbuffer_free(buf);
}
/* 信号处理 */
static void kill_signal(const int sig) {
lua_close(L);
exit(0);
}
static void upload_clearup() {
while(1) {
lua_getglobal(L, "upload_cleanup");
if (lua_pcall(L, 0, 1, 0)) {
fprintf (stderr, "cannot run upload_clearup: %s\n", lua_tostring(L, -1));
} else {
//int cnt = (int) lua_tointeger(L, -1);
//lua_pop(L, 1);
//fprintf (stderr, "upload total clearup: %d\n", cnt);
}
sleep(1);
}
}
static void session_clearup() {
while(1) {
lua_getglobal(L, "session_cleanup");
if (lua_pcall(L, 0, 1, 0)) {
fprintf (stderr, "cannot run session_clearup: %s\n", lua_tostring(L, -1));
} else {
//int cnt = (int) lua_tointeger(L, -1);
//lua_pop(L, 1);
//fprintf (stderr, "session total clearup: %d\n", cnt);
}
sleep(1);
}
}
int main(int argc, char **argv) {
int c;
/* 默认参数设置 */
char *luasrv_settings_listen = "0.0.0.0";
int luasrv_settings_port = 1206;
bool luasrv_settings_daemon = false;
int luasrv_settings_timeout = 1; /* 单位:秒 */
L = luaL_newstate();
luaL_openlibs(L);
struct luaL_reg driver[] = {
{ "print", luaM_print },
{ "microtime", luaM_microtime },
{ "md5", luaM_md5 },
{ "get_header", luaM_get_header },
{ "set_header", luaM_set_header },
{ NULL, NULL },
};
luaL_register (L, "cgi", driver);
luaopen_lfs (L);
luaopen_iconv (L);
luaopen_mysql (L);
if (luaL_loadfile(L, "./script/init.lua") || lua_pcall(L, 0, 0, 0)) { /* load the compile template functions */
fprintf (stderr, "cannot run init.lua: %s", lua_tostring(L, -1));
kill_signal(1);
return 0;
}
/* process arguments */
while ((c = getopt(argc, argv, "l:p:x:t:dh")) != -1) {
switch (c) {
case 'l':
luasrv_settings_listen = strdup(optarg);
break;
case 'p':
luasrv_settings_port = atoi(optarg);
break;
case 'x':
luasrv_settings_rootpath = strdup(optarg); /* luasrv根路径 */
if (access(luasrv_settings_rootpath, R_OK) != 0) { /* 如果目录不可写 */
fprintf(stderr, "luasrv root directory not readable\n");
}
break;
case 't':
luasrv_settings_timeout = atoi(optarg);
break;
case 'd':
luasrv_settings_daemon = true;
break;
case 'h':
default:
show_help();
return 1;
}
}
/* 判断是否加了必填参数 -x */
if (luasrv_settings_rootpath == NULL) {
show_help();
fprintf(stderr, "Attention: Please use the indispensable argument: -x <path>\n\n");
kill_signal(1);
}
/* 如果加了-d参数,以守护进程运行 */
if (luasrv_settings_daemon == true){
pid_t pid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
}
/* 忽略Broken Pipe信号 */
signal (SIGPIPE, SIG_IGN);
/* 处理kill信号 */
signal (SIGINT, kill_signal);
signal (SIGKILL, kill_signal);
signal (SIGQUIT, kill_signal);
signal (SIGTERM, kill_signal);
signal (SIGHUP, kill_signal);
/* more thread */
if (pthread_create(&ntid[0], NULL, (void *(*)(void *))session_clearup, NULL) != 0) {
fprintf(stderr, "can't create session_clearup thread.\n");
}
if (pthread_create(&ntid[1], NULL, (void *(*)(void *))upload_clearup, NULL) != 0) {
fprintf(stderr, "can't create upload_clearup thread.\n");
}
/* 请求处理部分 */
event_init();
httpd = evhttp_start(luasrv_settings_listen, luasrv_settings_port);
evhttp_set_timeout(httpd, luasrv_settings_timeout);
/* Set a callback for requests to "/specific". */
/* evhttp_set_cb(httpd, "/select", select_handler, NULL); */
/* Set a callback for all other requests. */
evhttp_set_gencb(httpd, luasrv_handler, NULL);
event_dispatch();
/* Not reached in this code as it is now. */
evhttp_free(httpd);
return 0;
}