-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmg_lua.c
More file actions
156 lines (134 loc) · 4.2 KB
/
mg_lua.c
File metadata and controls
156 lines (134 loc) · 4.2 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
#define REPLACE_SKIP
#include "mg_core.h"
#ifdef USE_LUA
#undef REPLACE_SKIP
#include <lua.h>
#include <lauxlib.h>
#ifdef _WIN32
static void *mmap(void *addr, int64_t len, int prot, int flags, int fd,
int offset) {
HANDLE fh = (HANDLE) _get_osfhandle(fd);
HANDLE mh = CreateFileMapping(fh, 0, PAGE_READONLY, 0, 0, 0);
void *p = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, (size_t) len);
CloseHandle(fh);
CloseHandle(mh);
return p;
}
#define munmap(x, y) UnmapViewOfFile(x)
#define MAP_FAILED NULL
#define MAP_PRIVATE 0
#define PROT_READ 0
#else
#include <sys/mman.h>
#endif
static void lsp(struct mg_connection *conn, const char *p, int64_t len,
lua_State *L) {
int i, j, pos = 0;
for (i = 0; i < len; i++) {
if (p[i] == '<' && p[i + 1] == '?') {
for (j = i + 1; j < len ; j++) {
if (p[j] == '?' && p[j + 1] == '>') {
mg_write(conn, p + pos, i - pos);
if (luaL_loadbuffer(L, p + (i + 2), j - (i + 2), "") == LUA_OK) {
lua_pcall(L, 0, LUA_MULTRET, 0);
}
pos = j + 2;
i = pos - 1;
break;
}
}
}
}
if (i > pos) {
mg_write(conn, p + pos, i - pos);
}
}
static int lsp_mg_print(lua_State *L) {
int i, num_args;
const char *str;
size_t size;
struct mg_connection *conn = lua_touserdata(L, lua_upvalueindex(1));
num_args = lua_gettop(L);
for (i = 1; i <= num_args; i++) {
if (lua_isstring(L, i)) {
str = lua_tolstring(L, i, &size);
mg_write(conn, str, size);
}
}
return 0;
}
static int lsp_mg_read(lua_State *L) {
struct mg_connection *conn = lua_touserdata(L, lua_upvalueindex(1));
char buf[1024];
int len = mg_read(conn, buf, sizeof(buf));
lua_settop(L, 0);
lua_pushlstring(L, buf, len);
return 1;
}
static void reg_string(struct lua_State *L, const char *name, const char *val) {
lua_pushstring(L, name);
lua_pushstring(L, val);
lua_rawset(L, -3);
}
static void reg_int(struct lua_State *L, const char *name, int val) {
lua_pushstring(L, name);
lua_pushinteger(L, val);
lua_rawset(L, -3);
}
static void prepare_lua_environment(struct mg_connection *conn, lua_State *L) {
const struct mg_request_info *ri = mg_get_request_info(conn);
extern void luaL_openlibs(lua_State *);
int i;
luaL_openlibs(L);
// Register "print" function which calls mg_write()
lua_pushlightuserdata(L, conn);
lua_pushcclosure(L, lsp_mg_print, 1);
lua_setglobal(L, "print");
// Register mg_read()
lua_pushlightuserdata(L, conn);
lua_pushcclosure(L, lsp_mg_read, 1);
lua_setglobal(L, "read");
// Export request_info
lua_newtable(L);
reg_string(L, "request_method", ri->request_method);
reg_string(L, "uri", ri->uri);
reg_string(L, "http_version", ri->http_version);
reg_string(L, "query_string", ri->query_string);
reg_int(L, "remote_ip", ri->remote_ip);
reg_int(L, "remote_port", ri->remote_port);
reg_int(L, "num_headers", ri->num_headers);
lua_pushstring(L, "http_headers");
lua_newtable(L);
for (i = 0; i < ri->num_headers; i++) {
reg_string(L, ri->http_headers[i].name, ri->http_headers[i].value);
}
lua_rawset(L, -3);
lua_setglobal(L, "request_info");
}
REPLACE_STATIC void handle_lsp_request(struct mg_connection *conn, const char *path,
struct file *filep) {
void *p = NULL;
lua_State *L = NULL;
if (!mg_fopen(conn, path, "r", filep)) {
send_http_error(conn, 404, "Not Found", "%s", "File not found");
} else if (filep->membuf == NULL &&
(p = mmap(NULL, filep->size, PROT_READ, MAP_PRIVATE,
fileno(filep->fp), 0)) == MAP_FAILED) {
send_http_error(conn, 500, http_500_error, "%s", "x");
} else if ((L = luaL_newstate()) == NULL) {
send_http_error(conn, 500, http_500_error, "%s", "y");
} else {
mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\nConnection: close\r\n\r\n");
prepare_lua_environment(conn, L);
conn->request_info.ev_data = L;
call_user(conn, MG_INIT_LUA);
lsp(conn, filep->membuf == NULL ? p : filep->membuf, filep->size, L);
}
if (L) lua_close(L);
if (p) munmap(p, filep->size);
mg_fclose(filep);
}
#define REPLACE_SKIP
#endif // USE_LUA
#undef REPLACE_SKIP