20
20
#include <ts/remap.h>
27
LuaPluginRelease(lua_State * lua)
29
lua_getglobal(lua, "release");
30
if (lua_isnil(lua, -1)) {
31
// No "release" callback.
35
if (lua_pcall(lua, 0, 0, 0) != 0) {
36
LuaLogDebug("release failed: %s", lua_tostring(lua, -1));
27
static pthread_mutex_t PluginInstanceLock = PTHREAD_MUTEX_INITIALIZER;
44
29
static TSRemapStatus
45
30
LuaPluginRemap(lua_State * lua, TSHttpTxn txn, TSRemapRequestInfo * rri)
72
TSRemapDeleteInstance(void * ih)
74
lua_State * lua = (lua_State *)ih;
77
LuaPluginRelease(lua);
83
57
TSRemapInit(TSRemapInterface * api_info, char * errbuf, int errbuf_size)
85
59
LuaLogDebug("loading lua plugin");
61
// Allocate a TSHttpTxn argument index for handling per-transaction hooks.
62
TSReleaseAssert(TSHttpArgIndexReserve("lua", "lua", &LuaHttpArgIndex) == TS_SUCCESS);
90
68
TSRemapNewInstance(int argc, char * argv[], void ** ih, char * errbuf, int errsz)
92
LuaPluginState * remap;
95
// Copy the plugin arguments so that we can use them to allocate a per-thread Lua state. It would be cleaner
96
// to clone a Lua state, but there's no built-in way to do that, and to implement that ourselves would require
97
// locking the template state (we need to manipulate the stack to copy values out).
98
remap = tsnew<LuaPluginState>();
99
remap->init((unsigned)argc, (const char **)argv);
101
// Test whether we can successfully load the Lua program.
102
lua = LuaPluginNewState(remap);
70
instanceid_t instanceid;
72
pthread_mutex_lock(&PluginInstanceLock);
74
// Register a new Lua plugin instance, skipping the first two arguments (which are the remap URLs).
75
instanceid = LuaPluginRegister((unsigned)argc - 2, (const char **)argv + 2);
76
*ih = (void *)(intptr_t)instanceid;
78
pthread_mutex_unlock(&PluginInstanceLock);
109
79
return TS_SUCCESS;
83
TSRemapDeleteInstance(void * ih)
85
instanceid_t instanceid = (intptr_t)ih;
87
pthread_mutex_lock(&PluginInstanceLock);
88
LuaPluginUnregister(instanceid);
89
pthread_mutex_unlock(&PluginInstanceLock);
113
93
TSRemapDoRemap(void * ih, TSHttpTxn txn, TSRemapRequestInfo * rri)
115
LuaThreadInstance * lthread;
117
// Find or clone the per-thread Lua state.
118
lthread = LuaGetThreadInstance();
120
LuaPluginState * lps;
122
lps = (LuaPluginState *)ih;
123
lthread = tsnew<LuaThreadInstance>();
125
LuaLogDebug("allocating new Lua state on thread 0x%llx", (unsigned long long)pthread_self());
126
lthread->lua = LuaPluginNewState(lps);
127
LuaSetThreadInstance(lthread);
130
return LuaPluginRemap(lthread->lua, txn, rri);
95
ScopedLuaState lstate((intptr_t)ih);
97
TSReleaseAssert(lstate);
98
return LuaPluginRemap(lstate->lua, txn, rri);