mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-04-20 22:04:45 +00:00
Add ServeRedirect function to redbean Lua (#246)
This commit is contained in:
parent
3d0347e26e
commit
4486ad5c9e
2 changed files with 37 additions and 2 deletions
|
@ -1005,13 +1005,18 @@ FUNCTIONS
|
||||||
causes what would normally happen outside a dynamic handler to
|
causes what would normally happen outside a dynamic handler to
|
||||||
happen. The asset can be sourced from either the zip or local
|
happen. The asset can be sourced from either the zip or local
|
||||||
filesystem if -D is used. This function is mutually exclusive with
|
filesystem if -D is used. This function is mutually exclusive with
|
||||||
SetStatus and ServeError.
|
SetStatus and other Serve* functions.
|
||||||
|
|
||||||
ServeError(code:int[,reason:str])
|
ServeError(code:int[,reason:str])
|
||||||
Instructs redbean to serve a boilerplate error page. This takes
|
Instructs redbean to serve a boilerplate error page. This takes
|
||||||
care of logging the error, setting the reason phrase, and adding a
|
care of logging the error, setting the reason phrase, and adding a
|
||||||
payload. This function is mutually exclusive with SetStatus and
|
payload. This function is mutually exclusive with SetStatus and
|
||||||
ServeAsset.
|
other Serve* functions.
|
||||||
|
|
||||||
|
ServeRedirect(code:int,location:str)
|
||||||
|
Instructs redbean to return the specified redirect code along with
|
||||||
|
the Location header set. This function is mutually exclusive with
|
||||||
|
SetStatus and other Serve* functions.
|
||||||
|
|
||||||
SetLogLevel(level:int)
|
SetLogLevel(level:int)
|
||||||
Sets logger verbosity. Reasonable values for level are kLogDebug >
|
Sets logger verbosity. Reasonable values for level are kLogDebug >
|
||||||
|
|
|
@ -3059,6 +3059,35 @@ static int LuaServeIndex(lua_State *L) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int LuaServeRedirect(lua_State *L) {
|
||||||
|
size_t loclen;
|
||||||
|
const char *location, *eval;
|
||||||
|
int code;
|
||||||
|
OnlyCallDuringRequest("ServeRedirect");
|
||||||
|
|
||||||
|
code = luaL_checkinteger(L, 1);
|
||||||
|
if (!(300 <= code && code <= 399)) {
|
||||||
|
luaL_argerror(L, 1, "bad status code");
|
||||||
|
unreachable;
|
||||||
|
}
|
||||||
|
location = luaL_checklstring(L, 2, &loclen);
|
||||||
|
if (msg.version < 10) {
|
||||||
|
(void)ServeError(505, "HTTP Version Not Supported");
|
||||||
|
lua_pushboolean(L, false);
|
||||||
|
} else {
|
||||||
|
if (!(eval = EncodeHttpHeaderValue(location, loclen, 0))) {
|
||||||
|
luaL_argerror(L, 2, "invalid location");
|
||||||
|
unreachable;
|
||||||
|
}
|
||||||
|
LOGF("REDIRECT %d to %s", code, location);
|
||||||
|
luaheaderp = AppendHeader(
|
||||||
|
SetStatus(code, GetHttpReason(code)), "Location", eval);
|
||||||
|
free(eval);
|
||||||
|
lua_pushboolean(L, true);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int LuaRoutePath(lua_State *L) {
|
static int LuaRoutePath(lua_State *L) {
|
||||||
size_t pathlen;
|
size_t pathlen;
|
||||||
const char *path;
|
const char *path;
|
||||||
|
@ -5230,6 +5259,7 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"ServeError", LuaServeError}, //
|
{"ServeError", LuaServeError}, //
|
||||||
{"ServeIndex", LuaServeIndex}, //
|
{"ServeIndex", LuaServeIndex}, //
|
||||||
{"ServeListing", LuaServeListing}, //
|
{"ServeListing", LuaServeListing}, //
|
||||||
|
{"ServeRedirect", LuaServeRedirect}, //
|
||||||
{"ServeStatusz", LuaServeStatusz}, //
|
{"ServeStatusz", LuaServeStatusz}, //
|
||||||
{"SetHeader", LuaSetHeader}, //
|
{"SetHeader", LuaSetHeader}, //
|
||||||
{"SetLogLevel", LuaSetLogLevel}, //
|
{"SetLogLevel", LuaSetLogLevel}, //
|
||||||
|
|
Loading…
Add table
Reference in a new issue