mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-03 16:30:29 +00:00
Implemented changes related to redbean Lua Handler
+ Removed SetHandler altogether, as we don't need it. + Using OnHttpRequest instead of Handler inside Lua for the global function to check for + OnHttpRequest can now be a callable table, as Lua may expect. + OnHttpRequest can now fail and fallback, like every other check. + CallLuaHandler renamed to TryLuaHandler to match the new API. (e.g. TryIndex)
This commit is contained in:
parent
a20a1f6853
commit
4c384e1a37
1 changed files with 22 additions and 5 deletions
|
@ -2548,7 +2548,19 @@ static int LuaReleaseRegex(lua_State *L) {
|
|||
}
|
||||
|
||||
static bool HasLuaHandler() {
|
||||
bool r = lua_getglobal(L, "Handler") == LUA_TFUNCTION;
|
||||
bool r = lua_getglobal(L, "OnHttpRequest") == LUA_TFUNCTION || LUA_TTABLE;
|
||||
|
||||
// Did we get a callable table?
|
||||
if(lua_type(L, -1) == LUA_TTABLE) {
|
||||
if(luaL_getmetafield(L, 1, "__call") == LUA_TNIL) {
|
||||
r = false;
|
||||
} else {
|
||||
// Pop the metatable.
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Pop OnHttpRequest or Nil
|
||||
lua_pop(L, 1);
|
||||
|
||||
return r;
|
||||
|
@ -2695,12 +2707,14 @@ static void LuaReload(void) {
|
|||
#endif
|
||||
}
|
||||
|
||||
const char *CallLuaHandler(const char *path, size_t pathlen) {
|
||||
lua_getglobal(L, "Handler");
|
||||
const char *TryLuaHandler(const char *path, size_t pathlen) {
|
||||
if(lua_getglobal(L, "OnHttpRequest") == LUA_TNIL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *p;
|
||||
luaheaderp = NULL;
|
||||
sauce = FreeLater(strndup(request.path.p + 1, request.path.n - 1));
|
||||
sauce = FreeLater(strndup(url.path.p + 1, url.path.n - 1));
|
||||
if(lua_pcall(L, 0, 0, 0) == LUA_OK) {
|
||||
if (!(p = luaheaderp)) {
|
||||
p = SetStatus(200, "OK");
|
||||
|
@ -3222,7 +3236,10 @@ static char *HandleMessage(void) {
|
|||
return ServeError(400, "Bad Request");
|
||||
}
|
||||
if (url.host.n && (p = TryHost(url.host.p, url.host.n))) return p;
|
||||
if (url.path.n == 1 && url.path.p[0] == '/') {
|
||||
if(HasLuaHandler() && (p = TryLuaHandler(url.path.p, url.path.n))) {
|
||||
return p;
|
||||
}
|
||||
else if (url.path.n == 1 && url.path.p[0] == '/') {
|
||||
if ((p = TryIndex("/", 1))) return p;
|
||||
return ServeListing();
|
||||
} else if ((p = TryPath(url.path.p, url.path.n))) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue