mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 07:29:23 +00:00
Redbean getstatus and more (#308)
* Add GetBody() Lua API to redbean. This improves consistency with RFC 7230 terminology and should be favored over the old GetPayload function. * Add GetStatus() API to redbean. This is useful to get status after it's changed/set by Redbean, for example if 505 or 508 is set when ServeRedirect is called. * Introduce GetAssetComment() API to redbean. This function should be favored over the old name GetComment(). * Introduce IsLoopbackClient() API to redbean * Limit redbean reason to 128 chars when set instead of reporting an error
This commit is contained in:
parent
e5d1536256
commit
ca611efc43
2 changed files with 39 additions and 12 deletions
|
@ -597,6 +597,10 @@ FUNCTIONS
|
||||||
Turns integer like 0x01020304 into a string like 1.2.3.4. See also
|
Turns integer like 0x01020304 into a string like 1.2.3.4. See also
|
||||||
ParseIp for the inverse operation.
|
ParseIp for the inverse operation.
|
||||||
|
|
||||||
|
GetAssetComment(path:str) → str
|
||||||
|
Returns comment text associated with asset in the ZIP central
|
||||||
|
directory. Also available as GetComment (deprecated).
|
||||||
|
|
||||||
GetAssetMode(path:str) → int
|
GetAssetMode(path:str) → int
|
||||||
Returns UNIX-style octal mode for ZIP asset (or local file if the
|
Returns UNIX-style octal mode for ZIP asset (or local file if the
|
||||||
-D flag is used)
|
-D flag is used)
|
||||||
|
@ -605,9 +609,9 @@ FUNCTIONS
|
||||||
Returns byte size of uncompressed contents of ZIP asset (or local
|
Returns byte size of uncompressed contents of ZIP asset (or local
|
||||||
file if the -D flag is used)
|
file if the -D flag is used)
|
||||||
|
|
||||||
GetComment(path:str) → str
|
GetBody() → str
|
||||||
Returns comment text associated with asset in the ZIP central
|
Returns the request message body if present or an empty string.
|
||||||
directory.
|
Also available as GetPayload (deprecated).
|
||||||
|
|
||||||
GetCookie(name:str) → str
|
GetCookie(name:str) → str
|
||||||
Returns cookie value.
|
Returns cookie value.
|
||||||
|
@ -714,13 +718,13 @@ FUNCTIONS
|
||||||
GetScheme() → str
|
GetScheme() → str
|
||||||
Returns scheme from Request-URL, if any.
|
Returns scheme from Request-URL, if any.
|
||||||
|
|
||||||
|
GetStatus() → int
|
||||||
|
Returns current status (as set by an earlier SetStatus call) or
|
||||||
|
`nil` if the status hasn't been set yet.
|
||||||
|
|
||||||
GetTime() → seconds:number
|
GetTime() → seconds:number
|
||||||
Returns current time as a UNIX timestamp with 0.0001s precision.
|
Returns current time as a UNIX timestamp with 0.0001s precision.
|
||||||
|
|
||||||
GetPayload() → str
|
|
||||||
Returns the request message payload, or empty string if there
|
|
||||||
isn't one.
|
|
||||||
|
|
||||||
GetUrl() → str
|
GetUrl() → str
|
||||||
Returns the effective Request-URL as an ASCII string, where
|
Returns the effective Request-URL as an ASCII string, where
|
||||||
illegal characters or UTF-8 is guaranteed to be percent encoded,
|
illegal characters or UTF-8 is guaranteed to be percent encoded,
|
||||||
|
@ -763,6 +767,10 @@ FUNCTIONS
|
||||||
Returns true if IP address is part of a private network
|
Returns true if IP address is part of a private network
|
||||||
(10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
|
(10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
|
||||||
|
|
||||||
|
IsLoopbackClient() → bool
|
||||||
|
Returns true if the client IP address (returned by GetRemoteAddr)
|
||||||
|
is part of the localhost network (127.0.0.0/8).
|
||||||
|
|
||||||
IsLoopbackIp(uint32) → bool
|
IsLoopbackIp(uint32) → bool
|
||||||
Returns true if IP address is part of the localhost network
|
Returns true if IP address is part of the localhost network
|
||||||
(127.0.0.0/8).
|
(127.0.0.0/8).
|
||||||
|
|
|
@ -3143,7 +3143,7 @@ static int LuaRespond(lua_State *L, char *R(unsigned, const char *)) {
|
||||||
luaheaderp = R(code, GetHttpReason(code));
|
luaheaderp = R(code, GetHttpReason(code));
|
||||||
} else {
|
} else {
|
||||||
reason = lua_tolstring(L, 2, &reasonlen);
|
reason = lua_tolstring(L, 2, &reasonlen);
|
||||||
if (reasonlen < 128 && (p = EncodeHttpHeaderValue(reason, reasonlen, 0))) {
|
if ((p = EncodeHttpHeaderValue(reason, MIN(reasonlen, 128), 0))) {
|
||||||
luaheaderp = R(code, p);
|
luaheaderp = R(code, p);
|
||||||
free(p);
|
free(p);
|
||||||
} else {
|
} else {
|
||||||
|
@ -3158,6 +3158,15 @@ static int LuaSetStatus(lua_State *L) {
|
||||||
return LuaRespond(L, SetStatus);
|
return LuaRespond(L, SetStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int LuaGetStatus(lua_State *L) {
|
||||||
|
OnlyCallDuringRequest(L, "GetStatus");
|
||||||
|
if (!luaheaderp)
|
||||||
|
lua_pushnil(L);
|
||||||
|
else
|
||||||
|
lua_pushinteger(L, statuscode);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int LuaServeError(lua_State *L) {
|
static int LuaServeError(lua_State *L) {
|
||||||
return LuaRespond(L, ServeError);
|
return LuaRespond(L, ServeError);
|
||||||
}
|
}
|
||||||
|
@ -3971,6 +3980,12 @@ static int LuaIsLoopbackIp(lua_State *L) {
|
||||||
return LuaIsIp(L, IsLoopbackIp);
|
return LuaIsIp(L, IsLoopbackIp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int LuaIsLoopbackClient(lua_State *L) {
|
||||||
|
OnlyCallDuringRequest(L, "IsLoopbackClient");
|
||||||
|
lua_pushboolean(L, IsLoopbackClient());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int LuaCategorizeIp(lua_State *L) {
|
static int LuaCategorizeIp(lua_State *L) {
|
||||||
lua_pushstring(L, GetIpCategoryName(CategorizeIp(luaL_checkinteger(L, 1))));
|
lua_pushstring(L, GetIpCategoryName(CategorizeIp(luaL_checkinteger(L, 1))));
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -4088,7 +4103,7 @@ static int LuaParseHttpDateTime(lua_State *L) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LuaGetPayload(lua_State *L) {
|
static int LuaGetBody(lua_State *L) {
|
||||||
lua_pushlstring(L, inbuf.p + hdrsize, payloadlength);
|
lua_pushlstring(L, inbuf.p + hdrsize, payloadlength);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -5083,7 +5098,7 @@ static int LuaIsDaemon(lua_State *L) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LuaGetComment(lua_State *L) {
|
static int LuaGetAssetComment(lua_State *L) {
|
||||||
struct Asset *a;
|
struct Asset *a;
|
||||||
const char *path;
|
const char *path;
|
||||||
size_t pathlen, m;
|
size_t pathlen, m;
|
||||||
|
@ -5316,10 +5331,13 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"Fetch", LuaFetch}, //
|
{"Fetch", LuaFetch}, //
|
||||||
{"FormatHttpDateTime", LuaFormatHttpDateTime}, //
|
{"FormatHttpDateTime", LuaFormatHttpDateTime}, //
|
||||||
{"FormatIp", LuaFormatIp}, //
|
{"FormatIp", LuaFormatIp}, //
|
||||||
|
{"GetAssetComment", LuaGetAssetComment}, //
|
||||||
|
{"GetComment", LuaGetAssetComment}, //
|
||||||
{"GetAssetMode", LuaGetAssetMode}, //
|
{"GetAssetMode", LuaGetAssetMode}, //
|
||||||
{"GetAssetSize", LuaGetAssetSize}, //
|
{"GetAssetSize", LuaGetAssetSize}, //
|
||||||
|
{"GetBody", LuaGetBody}, //
|
||||||
|
{"GetPayload", LuaGetBody}, //
|
||||||
{"GetClientAddr", LuaGetClientAddr}, //
|
{"GetClientAddr", LuaGetClientAddr}, //
|
||||||
{"GetComment", LuaGetComment}, //
|
|
||||||
{"GetCookie", LuaGetCookie}, //
|
{"GetCookie", LuaGetCookie}, //
|
||||||
{"GetDate", LuaGetDate}, //
|
{"GetDate", LuaGetDate}, //
|
||||||
{"GetEffectivePath", LuaGetEffectivePath}, //
|
{"GetEffectivePath", LuaGetEffectivePath}, //
|
||||||
|
@ -5338,13 +5356,13 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"GetParams", LuaGetParams}, //
|
{"GetParams", LuaGetParams}, //
|
||||||
{"GetPass", LuaGetPass}, //
|
{"GetPass", LuaGetPass}, //
|
||||||
{"GetPath", LuaGetPath}, //
|
{"GetPath", LuaGetPath}, //
|
||||||
{"GetPayload", LuaGetPayload}, //
|
|
||||||
{"GetPort", LuaGetPort}, //
|
{"GetPort", LuaGetPort}, //
|
||||||
{"GetRandomBytes", LuaGetRandomBytes}, //
|
{"GetRandomBytes", LuaGetRandomBytes}, //
|
||||||
{"GetRedbeanVersion", LuaGetRedbeanVersion}, //
|
{"GetRedbeanVersion", LuaGetRedbeanVersion}, //
|
||||||
{"GetRemoteAddr", LuaGetRemoteAddr}, //
|
{"GetRemoteAddr", LuaGetRemoteAddr}, //
|
||||||
{"GetScheme", LuaGetScheme}, //
|
{"GetScheme", LuaGetScheme}, //
|
||||||
{"GetServerAddr", LuaGetServerAddr}, //
|
{"GetServerAddr", LuaGetServerAddr}, //
|
||||||
|
{"GetStatus", LuaGetStatus}, //
|
||||||
{"GetTime", LuaGetTime}, //
|
{"GetTime", LuaGetTime}, //
|
||||||
{"GetUrl", LuaGetUrl}, //
|
{"GetUrl", LuaGetUrl}, //
|
||||||
{"GetUser", LuaGetUser}, //
|
{"GetUser", LuaGetUser}, //
|
||||||
|
@ -5359,6 +5377,7 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"IsCompressed", LuaIsCompressed}, //
|
{"IsCompressed", LuaIsCompressed}, //
|
||||||
{"IsDaemon", LuaIsDaemon}, //
|
{"IsDaemon", LuaIsDaemon}, //
|
||||||
{"IsHiddenPath", LuaIsHiddenPath}, //
|
{"IsHiddenPath", LuaIsHiddenPath}, //
|
||||||
|
{"IsLoopbackClient", LuaIsLoopbackClient}, //
|
||||||
{"IsLoopbackIp", LuaIsLoopbackIp}, //
|
{"IsLoopbackIp", LuaIsLoopbackIp}, //
|
||||||
{"IsPrivateIp", LuaIsPrivateIp}, //
|
{"IsPrivateIp", LuaIsPrivateIp}, //
|
||||||
{"IsPublicIp", LuaIsPublicIp}, //
|
{"IsPublicIp", LuaIsPublicIp}, //
|
||||||
|
|
Loading…
Add table
Reference in a new issue