-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaWebserverApi.c
More file actions
288 lines (266 loc) · 8.81 KB
/
LuaWebserverApi.c
File metadata and controls
288 lines (266 loc) · 8.81 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
#include "goLuaWebserver.h"
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdbool.h>
typedef struct
{
} LuaAliveStruct;
static int myResourceGC(lua_State* L)
{
LuaAliveStruct* resource = (LuaAliveStruct*)lua_touserdata(L, 1);
StopLuaStateFunctions(L);
return 0;
}
static void setUserdataMetatable(lua_State* L)
{
luaL_newmetatable(L, "MyResourceMeta");
lua_pushstring(L, "__gc");
lua_pushcfunction(L, myResourceGC);
lua_settable(L, -3);
}
static const char* USERDATA_KEY = "IsLuaStateStillAlive?";
static int createMyResource(lua_State* L)
{
LuaAliveStruct* resource = (LuaAliveStruct*)lua_newuserdata(L, sizeof(LuaAliveStruct));
setUserdataMetatable(L); // Call this to set the metatable for the userdata
lua_setmetatable(L, -2); // Set the metatable for the userdata on the stack
lua_pushlightuserdata(L, (void*)&USERDATA_KEY);
lua_pushvalue(L, -2); // Copy userdata to the top
lua_settable(L, LUA_REGISTRYINDEX); // Stores the copy in the registry
lua_settop(L, 0);
return 0;
}
/* Error handling function */
void errorArgumentType(lua_State* L, const char* functionName, int pos, const char* publicName, const char* typeName, int isOptional)
{
if (!isOptional) {
luaL_error(L, "Argument %d (%s) must be a %s in function %s", pos, publicName, typeName, functionName);
}
}
/* Generalized argument verification function */
void getVerifiedArgument(lua_State* L, int pos, const char* functionName, const char* publicName, int type, int isOptional)
{
switch (type) {
case LUA_TSTRING:
if (!lua_isstring(L, pos)) {
errorArgumentType(L, functionName, pos, publicName, "string", isOptional);
}
break;
case LUA_TNUMBER:
if (!lua_isnumber(L, pos)) {
errorArgumentType(L, functionName, pos, publicName, "integer", isOptional);
}
break;
case LUA_TTABLE:
if (!lua_istable(L, pos)) {
errorArgumentType(L, functionName, pos, publicName, "table", isOptional);
}
break;
case LUA_TFUNCTION:
if (!lua_isfunction(L, pos)) {
errorArgumentType(L, functionName, pos, publicName, "function", isOptional);
}
break;
default:
luaL_error(L, "Unsupported type %d for verification in function %s", type, functionName);
}
}
static int startWebserver(lua_State* L)
{
Message message;
getVerifiedArgument(L, 1, __func__, "address", LUA_TSTRING, 0);
const char* address = lua_tostring(L, 1);
message = StartServer(address, NULL, NULL);
if (!message.success) {
lua_pushinteger(L, -1);
lua_pushstring(L, message.msg);
free(message.msg);
return 2;
}
lua_pushinteger(L, message.id);
return 1;
}
static int startSecureWebserver(lua_State* L)
{
getVerifiedArgument(L, 1, __func__, "address", LUA_TSTRING, 0);
getVerifiedArgument(L, 2, __func__, "certFile", LUA_TSTRING, 0);
getVerifiedArgument(L, 3, __func__, "keyFile", LUA_TSTRING, 0);
const char* address = lua_tostring(L, 1);
const char* certFile = lua_tostring(L, 2);
const char* keyFile = lua_tostring(L, 3);
Message message = StartServer(address, certFile, keyFile);
if (!message.success) {
lua_pushinteger(L, -1);
lua_pushstring(L, message.msg);
free(message.msg);
return 2;
}
lua_pushinteger(L, message.id);
return 1;
}
static int serve(lua_State* L)
{
Message message;
getVerifiedArgument(L, 1, __func__, "server id", LUA_TNUMBER, 0);
getVerifiedArgument(L, 2, __func__, "path", LUA_TSTRING, 0);
getVerifiedArgument(L, 3, __func__, "hook function", LUA_TFUNCTION, 0);
int serverId = luaL_checkinteger(L, 1);
const char* path = lua_tostring(L, 2);
lua_pushvalue(L, 3);
int luaRef = luaL_ref(L, LUA_REGISTRYINDEX);
message = Serve(L, serverId, path, luaRef);
if (!message.success) {
lua_pushboolean(L, false);
lua_pushstring(L, message.msg);
free(message.msg);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int serveWebSocket(lua_State* L)
{
Message message;
getVerifiedArgument(L, 1, __func__, "server id", LUA_TNUMBER, 0);
getVerifiedArgument(L, 2, __func__, "path", LUA_TSTRING, 0);
getVerifiedArgument(L, 3, __func__, "hook function", LUA_TFUNCTION, 0);
int serverId = luaL_checkinteger(L, 1);
const char* path = lua_tostring(L, 2);
lua_pushvalue(L, 3);
int luaRef = luaL_ref(L, LUA_REGISTRYINDEX);
message = ServeWebSocket(L, serverId, path, luaRef);
if (!message.success) {
lua_pushboolean(L, false);
lua_pushstring(L, message.msg);
free(message.msg);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int writeWebSocket(lua_State* L)
{
Message message;
getVerifiedArgument(L, 1, __func__, "server id", LUA_TNUMBER, 0);
getVerifiedArgument(L, 2, __func__, "client", LUA_TSTRING, 0);
getVerifiedArgument(L, 3, __func__, "message", LUA_TSTRING, 0);
int serverId = luaL_checkinteger(L, 1);
const char* client = lua_tostring(L, 2);
const char* sendMessage = lua_tostring(L, 3);
message = WriteToWebSocketClient(serverId, client, sendMessage);
if (!message.success) {
lua_pushboolean(L, false);
lua_pushstring(L, message.msg);
free(message.msg);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int broadcastWebSocket(lua_State* L)
{
Message message;
getVerifiedArgument(L, 1, __func__, "server id", LUA_TNUMBER, 0);
getVerifiedArgument(L, 2, __func__, "path", LUA_TSTRING, 0);
getVerifiedArgument(L, 3, __func__, "message", LUA_TSTRING, 0);
int serverId = luaL_checkinteger(L, 1);
const char* path = lua_tostring(L, 2);
const char* sendMessage = lua_tostring(L, 3);
message = BroadcastToWebSocket(serverId, path, sendMessage);
if (!message.success) {
lua_pushboolean(L, false);
lua_pushstring(L, message.msg);
free(message.msg);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int getWebSocketClients(lua_State* L)
{
ClientInfo infos;
getVerifiedArgument(L, 1, __func__, "server id", LUA_TNUMBER, 0);
int serverId = luaL_checkinteger(L, 1);
infos = GetWebSocketClients(serverId);
if (!infos.errHandling.success) {
lua_pushboolean(L, false);
lua_pushstring(L, infos.errHandling.msg);
free(infos.errHandling.msg);
return 2;
}
lua_pushboolean(L, true);
lua_newtable(L);
for (int i = 0; i < infos.clientCount; i++) {
lua_pushstring(L, infos.paths[i]);
lua_setfield(L, -2, infos.clientIds[i]);
free(infos.clientIds[i]);
free(infos.paths[i]);
}
free(infos.clientIds);
free(infos.paths);
return 2;
}
static int serveFiles(lua_State* L)
{
Message message;
getVerifiedArgument(L, 1, __func__, "server id", LUA_TNUMBER, 0);
getVerifiedArgument(L, 2, __func__, "path", LUA_TSTRING, 0);
getVerifiedArgument(L, 3, __func__, "directory", LUA_TSTRING, 0);
int serverId = luaL_checkinteger(L, 1);
const char* path = lua_tostring(L, 2);
const char* dir = lua_tostring(L, 3);
message = ServeFiles(serverId, path, dir);
if (!message.success) {
lua_pushboolean(L, false);
lua_pushstring(L, message.msg);
free(message.msg);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int stopWebserver(lua_State* L)
{
getVerifiedArgument(L, 1, __func__, "server id", LUA_TNUMBER, 0);
int serverId = luaL_checkinteger(L, 1);
Message message = StopServer(serverId);
if (!message.success) {
lua_pushboolean(L, false);
lua_pushstring(L, message.msg);
free(message.msg);
return 2;
}
return 0;
}
#ifndef LUAWEBSERVER_LIB
#ifdef _WIN32
#define LUAWEBSERVER_LIB __declspec(dllexport)
#else
#define LUAWEBSERVER_LIB __attribute__((visibility("default")))
#endif
#endif
LUAWEBSERVER_LIB int luaopen_goLuaWebserver(lua_State* L)
{
createMyResource(L);
lua_newtable(L);
lua_pushcfunction(L, startWebserver);
lua_setfield(L, -2, "startWebserver");
lua_pushcfunction(L, startSecureWebserver);
lua_setfield(L, -2, "startSecureWebserver");
lua_pushcfunction(L, stopWebserver);
lua_setfield(L, -2, "stopWebserver");
lua_pushcfunction(L, serve);
lua_setfield(L, -2, "serve");
lua_pushcfunction(L, serveWebSocket);
lua_setfield(L, -2, "serveWebSocket");
lua_pushcfunction(L, writeWebSocket);
lua_setfield(L, -2, "writeWebSocket");
lua_pushcfunction(L, serveFiles);
lua_setfield(L, -2, "serveFiles");
lua_pushcfunction(L, broadcastWebSocket);
lua_setfield(L, -2, "broadcastWebSocket");
lua_pushcfunction(L, getWebSocketClients);
lua_setfield(L, -2, "getWebSocketClients");
return 1;
}