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:
Paul Kulchenko 2021-11-12 15:00:41 -08:00 committed by GitHub
parent e5d1536256
commit ca611efc43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 12 deletions

View file

@ -597,6 +597,10 @@ FUNCTIONS
Turns integer like 0x01020304 into a string like 1.2.3.4. See also
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
Returns UNIX-style octal mode for ZIP asset (or local file if the
-D flag is used)
@ -605,9 +609,9 @@ FUNCTIONS
Returns byte size of uncompressed contents of ZIP asset (or local
file if the -D flag is used)
GetComment(path:str) → str
Returns comment text associated with asset in the ZIP central
directory.
GetBody() → str
Returns the request message body if present or an empty string.
Also available as GetPayload (deprecated).
GetCookie(name:str) → str
Returns cookie value.
@ -714,13 +718,13 @@ FUNCTIONS
GetScheme() → str
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
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
Returns the effective Request-URL as an ASCII string, where
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
(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
Returns true if IP address is part of the localhost network
(127.0.0.0/8).

View file

@ -3143,7 +3143,7 @@ static int LuaRespond(lua_State *L, char *R(unsigned, const char *)) {
luaheaderp = R(code, GetHttpReason(code));
} else {
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);
free(p);
} else {
@ -3158,6 +3158,15 @@ static int LuaSetStatus(lua_State *L) {
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) {
return LuaRespond(L, ServeError);
}
@ -3971,6 +3980,12 @@ static int LuaIsLoopbackIp(lua_State *L) {
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) {
lua_pushstring(L, GetIpCategoryName(CategorizeIp(luaL_checkinteger(L, 1))));
return 1;
@ -4088,7 +4103,7 @@ static int LuaParseHttpDateTime(lua_State *L) {
return 1;
}
static int LuaGetPayload(lua_State *L) {
static int LuaGetBody(lua_State *L) {
lua_pushlstring(L, inbuf.p + hdrsize, payloadlength);
return 1;
}
@ -5083,7 +5098,7 @@ static int LuaIsDaemon(lua_State *L) {
return 1;
}
static int LuaGetComment(lua_State *L) {
static int LuaGetAssetComment(lua_State *L) {
struct Asset *a;
const char *path;
size_t pathlen, m;
@ -5316,10 +5331,13 @@ static const luaL_Reg kLuaFuncs[] = {
{"Fetch", LuaFetch}, //
{"FormatHttpDateTime", LuaFormatHttpDateTime}, //
{"FormatIp", LuaFormatIp}, //
{"GetAssetComment", LuaGetAssetComment}, //
{"GetComment", LuaGetAssetComment}, //
{"GetAssetMode", LuaGetAssetMode}, //
{"GetAssetSize", LuaGetAssetSize}, //
{"GetBody", LuaGetBody}, //
{"GetPayload", LuaGetBody}, //
{"GetClientAddr", LuaGetClientAddr}, //
{"GetComment", LuaGetComment}, //
{"GetCookie", LuaGetCookie}, //
{"GetDate", LuaGetDate}, //
{"GetEffectivePath", LuaGetEffectivePath}, //
@ -5338,13 +5356,13 @@ static const luaL_Reg kLuaFuncs[] = {
{"GetParams", LuaGetParams}, //
{"GetPass", LuaGetPass}, //
{"GetPath", LuaGetPath}, //
{"GetPayload", LuaGetPayload}, //
{"GetPort", LuaGetPort}, //
{"GetRandomBytes", LuaGetRandomBytes}, //
{"GetRedbeanVersion", LuaGetRedbeanVersion}, //
{"GetRemoteAddr", LuaGetRemoteAddr}, //
{"GetScheme", LuaGetScheme}, //
{"GetServerAddr", LuaGetServerAddr}, //
{"GetStatus", LuaGetStatus}, //
{"GetTime", LuaGetTime}, //
{"GetUrl", LuaGetUrl}, //
{"GetUser", LuaGetUser}, //
@ -5359,6 +5377,7 @@ static const luaL_Reg kLuaFuncs[] = {
{"IsCompressed", LuaIsCompressed}, //
{"IsDaemon", LuaIsDaemon}, //
{"IsHiddenPath", LuaIsHiddenPath}, //
{"IsLoopbackClient", LuaIsLoopbackClient}, //
{"IsLoopbackIp", LuaIsLoopbackIp}, //
{"IsPrivateIp", LuaIsPrivateIp}, //
{"IsPublicIp", LuaIsPublicIp}, //