This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathplugin-lua.c
193 lines (144 loc) · 3.51 KB
/
plugin-lua.c
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _LUA_PLUGIN
/*******************************************************************************
*
* Lua plugin
*
*/
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>
#include "core.h"
#include "utils.h"
#include "main.h"
#include "plugin.h"
#define xlog_lua(t, ...) xlog(t, "["_LUA_VERSION_"] " __VA_ARGS__)
/**
*
*/
int proxenet_lua_load_file(plugin_t* plugin)
{
char* pathname;
lua_State* lua_interpreter;
if(plugin->state != INACTIVE){
#ifdef DEBUG
if(cfg->verbose > 2)
xlog_lua(LOG_DEBUG, "Plugin '%s' is already loaded. Skipping...\n", plugin->name);
#endif
return 0;
}
pathname = plugin->fullpath;
lua_interpreter = (lua_State*) plugin->interpreter->vm;
if (luaL_dofile(lua_interpreter, pathname)) {
xlog_lua(LOG_ERROR, "Failed to load '%s'\n", pathname);
return -1;
}
lua_getglobal(lua_interpreter, CFG_ONLOAD_PLUGIN_FUNCTION);
lua_call(lua_interpreter, 0, 0);
return 0;
}
/**
*
*/
int proxenet_lua_initialize_vm(plugin_t* plugin)
{
interpreter_t* interpreter;
lua_State* lua_interpreter;
interpreter = plugin->interpreter;
if (interpreter->ready)
return 0;
lua_interpreter = luaL_newstate();
luaL_openlibs(lua_interpreter);
plugin->interpreter->vm = lua_interpreter;
plugin->interpreter->ready = true;
return 0;
}
/**
*
*/
int proxenet_lua_destroy_plugin(plugin_t* plugin)
{
interpreter_t* interpreter;
lua_State* lua_interpreter;
interpreter = plugin->interpreter;
lua_interpreter = (lua_State*) interpreter->vm;
proxenet_plugin_set_state(plugin, INACTIVE);
lua_getglobal(lua_interpreter, CFG_ONLEAVE_PLUGIN_FUNCTION);
lua_call(lua_interpreter, 0, 0);
plugin->pre_function = NULL;
plugin->post_function = NULL;
return 0;
}
/**
*
*/
int proxenet_lua_destroy_vm(interpreter_t* interpreter)
{
lua_State* lua_interpreter;
lua_interpreter = (lua_State*)interpreter->vm;
lua_close(lua_interpreter);
interpreter->ready = false;
interpreter->vm = NULL;
return 0;
}
/**
*
*/
static char* proxenet_lua_execute_function(interpreter_t* interpreter, request_t *request)
{
const char *lRet;
char *buf;
lua_State* lua_interpreter;
size_t len;
char *uri;
uri = request->http_infos.uri;
if (!uri)
return NULL;
lua_interpreter = (lua_State*) interpreter->vm;
if (request->type == REQUEST)
lua_getglobal(lua_interpreter, CFG_REQUEST_PLUGIN_FUNCTION);
else
lua_getglobal(lua_interpreter, CFG_RESPONSE_PLUGIN_FUNCTION);
lua_pushnumber(lua_interpreter, request->id);
lua_pushlstring(lua_interpreter, request->data, request->size);
lua_pushlstring(lua_interpreter, uri, strlen(uri));
lua_call(lua_interpreter, 3, 1);
lRet = lua_tolstring(lua_interpreter, -1, &len);
if (!lRet)
return NULL;
buf = proxenet_xstrdup(lRet, len);
if (!buf)
return NULL;
request->size = len;
return buf;
}
/**
*
*/
static void proxenet_lua_lock_vm(interpreter_t *interpreter)
{
pthread_mutex_lock(&interpreter->mutex);
}
/**
*
*/
static void proxenet_lua_unlock_vm(interpreter_t *interpreter)
{
pthread_mutex_unlock(&interpreter->mutex);
}
/**
*
*/
char* proxenet_lua_plugin(plugin_t* plugin, request_t *request)
{
char* buf = NULL;
interpreter_t *interpreter = plugin->interpreter;
proxenet_lua_lock_vm(interpreter);
buf = proxenet_lua_execute_function(interpreter, request);
proxenet_lua_unlock_vm(interpreter);
return buf;
}
#endif /* _LUA_PLUGIN */