diff options
author | dormando <dormando@rydia.net> | 2022-08-01 23:33:48 -0700 |
---|---|---|
committer | dormando <dormando@rydia.net> | 2022-08-03 11:53:33 -0700 |
commit | 4f27245d04e6bd0e7b32b2f930fe78bbae1080df (patch) | |
tree | a4d786d5fd74d28adff42939a3fbc530d9dcfab4 | |
parent | 71c7d8604713104fd9b6ad54c28dbdaa047251d8 (diff) | |
download | memcached-4f27245d04e6bd0e7b32b2f930fe78bbae1080df.tar.gz |
proxy: add req:flag_token("F")
function accepts a flag, returns (bool, token|nil).
bool indicates if the flag exists, and if the flag has a token it is
returned instead of nil as the second value.
-rw-r--r-- | proxy.h | 1 | ||||
-rw-r--r-- | proxy_lua.c | 1 | ||||
-rw-r--r-- | proxy_request.c | 37 | ||||
-rw-r--r-- | t/startfile.lua | 4 |
4 files changed, 43 insertions, 0 deletions
@@ -458,6 +458,7 @@ int mcplib_request_rtrimkey(lua_State *L); int mcplib_request_token(lua_State *L); int mcplib_request_ntokens(lua_State *L); int mcplib_request_has_flag(lua_State *L); +int mcplib_request_flag_token(lua_State *L); int mcplib_request_gc(lua_State *L); int mcplib_open_dist_jump_hash(lua_State *L); diff --git a/proxy_lua.c b/proxy_lua.c index d611a45..021ae74 100644 --- a/proxy_lua.c +++ b/proxy_lua.c @@ -815,6 +815,7 @@ int proxy_register_libs(LIBEVENT_THREAD *t, void *ctx) { {"token", mcplib_request_token}, {"ntokens", mcplib_request_ntokens}, {"has_flag", mcplib_request_has_flag}, + {"flag_token", mcplib_request_flag_token}, {"__tostring", NULL}, {"__gc", mcplib_request_gc}, {NULL, NULL} diff --git a/proxy_request.c b/proxy_request.c index 31e6035..3face1f 100644 --- a/proxy_request.c +++ b/proxy_request.c @@ -716,6 +716,43 @@ int mcplib_request_has_flag(lua_State *L) { return 1; } +int mcplib_request_flag_token(lua_State *L) { + mcp_request_t *rq = luaL_checkudata(L, 1, "mcp.request"); + size_t len = 0; + const char *flagstr = luaL_checklstring(L, 2, &len); + if (len != 1) { + proxy_lua_error(L, "has_flag(): meta flag must be a single character"); + return 0; + } + if (flagstr[0] < 65 || flagstr[0] > 122) { + proxy_lua_error(L, "has_flag(): invalid flag, must be A-Z,a-z"); + return 0; + } + uint64_t flagbit = (uint64_t)1 << (flagstr[0] - 65); + + if (rq->pr.t.meta.flags & flagbit) { + // The flag definitely exists, but sadly we need to scan for the + // actual flag to see if it has a token. + lua_pushboolean(L, 1); + for (int x = rq->pr.keytoken+1; x < rq->pr.ntokens; x++) { + const char *s = rq->pr.request + rq->pr.tokens[x]; + if (s[0] == flagstr[0]) { + size_t vlen = _process_token_len(&rq->pr, x); + if (vlen > 1) { + // strip the flag off the token and return. + lua_pushlstring(L, s+1, vlen-1); + return 2; + } + break; + } + } + } else { + lua_pushboolean(L, 0); + } + + return 1; +} + int mcplib_request_gc(lua_State *L) { mcp_request_t *rq = luaL_checkudata(L, -1, "mcp.request"); // During nread c->item is the malloc'ed buffer. not yet put into diff --git a/t/startfile.lua b/t/startfile.lua index e5dd606..e60c05c 100644 --- a/t/startfile.lua +++ b/t/startfile.lua @@ -166,6 +166,10 @@ function meta_get_factory(zones, local_zone) if r:has_flag("l") == true then print("client asking for last access time") end + local texists, token = r:flag_token("O") + if token ~= nil then + print("meta opaque flag token: " .. token) + end local res = near_zone(r) return res |