summaryrefslogtreecommitdiff
path: root/proxy_request.c
diff options
context:
space:
mode:
authordormando <dormando@rydia.net>2022-08-01 23:33:48 -0700
committerdormando <dormando@rydia.net>2022-08-03 11:53:33 -0700
commit4f27245d04e6bd0e7b32b2f930fe78bbae1080df (patch)
treea4d786d5fd74d28adff42939a3fbc530d9dcfab4 /proxy_request.c
parent71c7d8604713104fd9b6ad54c28dbdaa047251d8 (diff)
downloadmemcached-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.
Diffstat (limited to 'proxy_request.c')
-rw-r--r--proxy_request.c37
1 files changed, 37 insertions, 0 deletions
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