Skip to content

Commit

Permalink
proxy: match, token = req:match_res(res)
Browse files Browse the repository at this point in the history
If req has 'k' and/or 'O' flags, match against the result object.
  • Loading branch information
dormando committed Jun 10, 2024
1 parent 90f1d91 commit 070f68a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ int mcplib_request_flag_set(lua_State *L);
int mcplib_request_flag_replace(lua_State *L);
int mcplib_request_flag_del(lua_State *L);
int mcplib_request_gc(lua_State *L);
int mcplib_request_match_res(lua_State *L);
void mcp_request_cleanup(LIBEVENT_THREAD *t, mcp_request_t *rq);

// response interface
Expand Down
1 change: 1 addition & 0 deletions proxy_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,7 @@ int proxy_register_libs(void *ctx, LIBEVENT_THREAD *t, void *state) {
{"flag_set", mcplib_request_flag_set},
{"flag_replace", mcplib_request_flag_replace},
{"flag_del", mcplib_request_flag_del},
{"match_res", mcplib_request_match_res},
{"__tostring", NULL},
{"__gc", mcplib_request_gc},
{NULL, NULL}
Expand Down
69 changes: 69 additions & 0 deletions proxy_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,75 @@ int mcplib_request_flag_del(lua_State *L) {
return 1;
}

// local match, token = req:match_res(res)
// checks if req has `k` or `O`. If so, checks response for `K` or `O`
// returns true, nil if matches
// returns false, res token if not match.
//
// FIXME: if k or O don't exist, fail the match or pass the match?
int mcplib_request_match_res(lua_State *L) {
mcp_request_t *rq = luaL_checkudata(L, 1, "mcp.request");
mcp_resp_t *rs = luaL_checkudata(L, 2, "mcp.response");

const char *opaque_token = NULL;
size_t opaque_len = 0;

// requests all have keys. check for an opaque.
mcp_request_find_flag_token(rq, 'O', &opaque_token, &opaque_len);

// scan the response line for tokens, since we don't have a reciprocal API
// yet. When we do this code will be replaced with a function call like
// the above.
const char *p = rs->resp.rline;
// TODO: Think this is an off-by-one in mcmc.
const char *e = p + rs->resp.rlen - 1;
while (p != e) {
if (*p == ' ') {
p++;
} else if (*p == 'k' || *p == 'O') {
const char *rq_token = NULL;
int rq_len = 0;
if (*p == 'k') {
rq_token = MCP_PARSER_KEY(rq->pr);
rq_len = rq->pr.klen;
} else if (*p == 'O') {
rq_token = opaque_token;
rq_len = opaque_len;
}
if (rq_token == NULL) {
lua_pushboolean(L, 0);
lua_pushnil(L);
return 2;
}

p++; // skip K and start comparing token
const char *rs_token = p;

// find end of token
while (p != e && !isspace(*p)) {
p++;
}

int rs_len = p - rs_token;
if (rq_len != rs_len || memcmp(rq_token, rs_token, rs_len) != 0) {
// FAIL, keys aren't the same length or don't match.
lua_pushboolean(L, 0);
lua_pushlstring(L, rs_token, rs_len);
return 2;
}
} else {
// skip token
while (p != e && *p != ' ') {
p++;
}
}
}

lua_pushboolean(L, 1);
lua_pushnil(L);
return 2;
}

void mcp_request_cleanup(LIBEVENT_THREAD *t, mcp_request_t *rq) {
// During nread c->item is the malloc'ed buffer. not yet put into
// rq->buf - this gets freed because we've also set c->item_malloced if
Expand Down

0 comments on commit 070f68a

Please sign in to comment.