Skip to content

Commit

Permalink
proxy: fix inspector with mcp.internal() responses
Browse files Browse the repository at this point in the history
mcp.internal's result lines are in r->cresp->wbuf referenced into an
iov, not r->buf. This fix checks for where the res line is before
attempting to reference it.
  • Loading branch information
dormando committed Aug 19, 2024
1 parent b2fb23d commit 41db678
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
16 changes: 9 additions & 7 deletions proxy_inspector.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ struct mcp_inspector {

// PRIVATE INTERFACE

#define res_buf(r) (r->cresp ? r->cresp->iov[0].iov_base : r->buf)

// COMMON ARG HANDLERS

// multiple step types only take 'flag' as an argument.
Expand Down Expand Up @@ -311,7 +313,7 @@ static int mcp_inspector_hasflag_r(lua_State *L, struct mcp_inspector *ins, stru
// result object may not be tokenized. this will do so if not
// already. any future hits agains the same object will use the
// cached tokenizer struct.
mcmc_tokenize_res(res->buf, res->resp.reslen, &res->tok);
mcmc_tokenize_res(res_buf(res), res->resp.reslen, &res->tok);
if (mcmc_token_has_flag_bit(&res->tok, c->bit) == MCMC_OK) {
lua_pushboolean(L, 1);
} else {
Expand Down Expand Up @@ -341,11 +343,11 @@ static int mcp_inspector_flagtoken_r(lua_State *L, struct mcp_inspector *ins, st
} else {
mcp_resp_t *res = arg;
if (res->resp.type == MCMC_RESP_META) {
mcmc_tokenize_res(res->buf, res->resp.reslen, &res->tok);
mcmc_tokenize_res(res_buf(res), res->resp.reslen, &res->tok);
if (mcmc_token_has_flag_bit(&res->tok, c->bit) == MCMC_OK) {
lua_pushboolean(L, 1); // flag exists
int tlen = 0;
const char *tok = mcmc_token_get_flag(res->buf, &res->tok, c->f, &tlen);
const char *tok = mcmc_token_get_flag(res_buf(res), &res->tok, c->f, &tlen);
lua_pushlstring(L, tok, tlen); // flag's token
return 2;
}
Expand Down Expand Up @@ -377,11 +379,11 @@ static int mcp_inspector_flagint_r(lua_State *L, struct mcp_inspector *ins, stru
} else {
mcp_resp_t *res = arg;
if (res->resp.type == MCMC_RESP_META) {
mcmc_tokenize_res(res->buf, res->resp.reslen, &res->tok);
mcmc_tokenize_res(res_buf(res), res->resp.reslen, &res->tok);
if (mcmc_token_has_flag_bit(&res->tok, c->bit) == MCMC_OK) {
lua_pushboolean(L, 1); // flag exists
int64_t tok = 0;
if (mcmc_token_get_flag_64(res->buf, &res->tok, c->f, &tok) == MCMC_OK) {
if (mcmc_token_get_flag_64(res_buf(res), &res->tok, c->f, &tok) == MCMC_OK) {
lua_pushinteger(L, tok);
} else {
lua_pushnil(L); // token couldn't be converted
Expand Down Expand Up @@ -451,11 +453,11 @@ static int mcp_inspector_flagis_r(lua_State *L, struct mcp_inspector *ins, struc
} else {
mcp_resp_t *res = arg;
if (res->resp.type == MCMC_RESP_META) {
mcmc_tokenize_res(res->buf, res->resp.reslen, &res->tok);
mcmc_tokenize_res(res_buf(res), res->resp.reslen, &res->tok);
if (mcmc_token_has_flag_bit(&res->tok, c->bit) == MCMC_OK) {
lua_pushboolean(L, 1); // flag exists
int tlen = 0;
const char *tok = mcmc_token_get_flag(res->buf, &res->tok, c->f, &tlen);
const char *tok = mcmc_token_get_flag(res_buf(res), &res->tok, c->f, &tlen);
if (tlen == c->len && strncmp(tok, str, c->len) == 0) {
lua_pushboolean(L, 1);
} else {
Expand Down
15 changes: 15 additions & 0 deletions t/proxyins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,25 @@ function mcp_config_routes(p)
end
})

local mgintres = mcp.funcgen_new()
-- no handle: using mcp.internal()
mgintres:ready({
n = "intres", f = function(rctx)
return function(r)
--local key = r:key()
local res = mcp.internal(r)
local has_O, O, has_t, t = mgresflaga_ins(res)
return string.format("SERVER_ERROR O[%q]: %s t[%q]: %q\r\n",
has_O, O, has_t, t)
end
end
})

local mgr = mcp.router_new({ map = {
sepkey = mgsepkey,
reshasf = mgreshasf,
reqkey = mgreqkey,
intres = mgintres,
}})
mcp.attach(mcp.CMD_MG, mgr)
end
19 changes: 17 additions & 2 deletions t/proxyins.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,23 @@ print $w "watch proxyevents\r\n";
is(<$w>, "OK\r\n");

{
test_mgreq();
test_mgres();
test_mgintres();
#test_mgreq();
#test_mgres();
}

sub test_mgintres {
'note testing mcp.internal()';
$t->c_send("ms intres/tokenint 5 F5\r\n");
$t->c_send("hello\r\n");
$t->c_recv("HD\r\n");
$t->clear();

subtest 'flagtoken and flagint' => sub {
$t->c_send("mg intres/tokenint f t s Omoo\r\n");
$t->c_recv("SERVER_ERROR O[true]: moo t[true]: -1\r\n");
$t->clear();
};
}

sub test_mgreq {
Expand Down

0 comments on commit 41db678

Please sign in to comment.