mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 07:29:23 +00:00
Add GetCookie to redbean Lua (#269)
This commit is contained in:
parent
559b024e1d
commit
1b93066883
2 changed files with 26 additions and 2 deletions
|
@ -604,6 +604,9 @@ FUNCTIONS
|
|||
Returns comment text associated with asset in the ZIP central
|
||||
directory.
|
||||
|
||||
GetCookie(name:str) → str
|
||||
Returns cookie value.
|
||||
|
||||
GetRemoteAddr() → ip:uint32,port:uint16
|
||||
Returns client ip4 address and port, e.g. 0x01020304,31337 would
|
||||
represent 1.2.3.4:31337. This is the same as GetClientAddr except
|
||||
|
|
|
@ -4169,6 +4169,25 @@ static int LuaSetHeader(lua_State *L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int LuaGetCookie(lua_State *L) {
|
||||
char *cookie = 0, *cookietmpl, *cookieval;
|
||||
OnlyCallDuringRequest("GetCookie");
|
||||
cookietmpl = gc(xasprintf(" %s=", luaL_checkstring(L, 1)));
|
||||
|
||||
if (HasHeader(kHttpCookie)) {
|
||||
appends(&cookie, " "); // prepend space to simplify cookie search
|
||||
appendd(&cookie, HeaderData(kHttpCookie), HeaderLength(kHttpCookie));
|
||||
}
|
||||
if (cookie && (cookieval = strstr(cookie, cookietmpl))) {
|
||||
cookieval += strlen(cookietmpl);
|
||||
lua_pushlstring(L, cookieval, strchrnul(cookieval, ';') - cookieval);
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
if (cookie) free(cookie);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int LuaSetCookie(lua_State *L) {
|
||||
const char *key, *val;
|
||||
size_t keylen, vallen;
|
||||
|
@ -4197,7 +4216,8 @@ static int LuaSetCookie(lua_State *L) {
|
|||
keylen > strlen(securepref) &&
|
||||
SlicesEqual(key, strlen(securepref), securepref, strlen(securepref));
|
||||
if ((ishostpref || issecurepref) && !usessl) {
|
||||
luaL_argerror(L, 1, "__Host- and __Secure- prefixes require SSL");
|
||||
luaL_argerror(L, 1,
|
||||
gc(xasprintf("%s and %s prefixes require SSL", hostpref, securepref)));
|
||||
unreachable;
|
||||
}
|
||||
|
||||
|
@ -4265,7 +4285,7 @@ static int LuaSetCookie(lua_State *L) {
|
|||
}
|
||||
DEBUGF("(srvr) Set-Cookie: %s", buf);
|
||||
|
||||
// empty the stack and push header key/value
|
||||
// empty the stack and push header name/value
|
||||
lua_settop(L, 0);
|
||||
lua_pushliteral(L, "Set-Cookie");
|
||||
lua_pushstring(L, buf);
|
||||
|
@ -5292,6 +5312,7 @@ static const luaL_Reg kLuaFuncs[] = {
|
|||
{"GetAssetSize", LuaGetAssetSize}, //
|
||||
{"GetClientAddr", LuaGetClientAddr}, //
|
||||
{"GetComment", LuaGetComment}, //
|
||||
{"GetCookie", LuaGetCookie}, //
|
||||
{"GetDate", LuaGetDate}, //
|
||||
{"GetEffectivePath", LuaGetEffectivePath}, //
|
||||
{"GetFragment", LuaGetFragment}, //
|
||||
|
|
Loading…
Add table
Reference in a new issue