From d91295cf823de957ede44f180ec72f52c1279058 Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 28 Jan 2016 09:31:46 +1100 Subject: [PATCH] Decode bkeys for clients that don't support them, otherwise forward on entire message --- src/connector.c | 50 ++++++++++++++++++++++++++++++++---------------- src/stratifier.c | 2 +- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/connector.c b/src/connector.c index 8c177498..422e2b68 100644 --- a/src/connector.c +++ b/src/connector.c @@ -911,27 +911,25 @@ static void send_client(cdata_t *cdata, const int64_t id, char *buf, int slen, u { ckpool_t *ckp = cdata->ckp; sender_send_t *sender_send; + uint32_t blen = len - slen; client_instance_t *client; char *bkey = NULL; json_t *val; + if (unlikely(blen > 0)) + bkey = strstr(buf + slen - 4 - 1, "bkey"); if (unlikely(ckp->node && !id)) { - uint32_t blen = slen - len; - - if (blen > 0) { - bkey = strstr(buf + slen - 4 - 1, "bkey"); - if (likely(bkey)) { - val = json_loads(buf, JSON_DISABLE_EOF_CHECK, NULL); - if (unlikely(!val)) { - LOGWARNING("No json in bkey appended message %s", buf); - free(buf); - return; - } - json_append_bkeys(val, bkey, blen); + if (bkey) { + val = json_loads(buf, JSON_DISABLE_EOF_CHECK, NULL); + if (unlikely(!val)) { + LOGWARNING("No json in bkey appended message %s", buf); free(buf); - buf = json_dumps(val, JSON_EOL | JSON_COMPACT); - json_decref(val); + return; } + json_append_bkeys(val, bkey, blen); + free(buf); + buf = json_dumps(val, JSON_COMPACT); + json_decref(val); } LOGDEBUG("Message for node: %s", buf); send_proc(ckp->stratifier, buf); @@ -972,12 +970,17 @@ static void send_client(cdata_t *cdata, const int64_t id, char *buf, int slen, u if (ckp->node) { char *msg; - val = json_loads(buf, 0, NULL); - if (!val) // Can happen if client sent invalid json message + val = json_loads(buf, JSON_DISABLE_EOF_CHECK, NULL); + if (!val) { + // Can happen if client sent invalid json message + len = slen; goto out; + } json_object_set_new_nocheck(val, "client_id", json_integer(client->id)); json_object_set_new_nocheck(val, "address", json_string(client->address_name)); json_object_set_new_nocheck(val, "server", json_integer(client->server)); + if (bkey) + json_append_bkeys(val, bkey, blen); msg = json_dumps(val, JSON_COMPACT); json_decref(val); send_proc(ckp->stratifier, msg); @@ -986,6 +989,21 @@ static void send_client(cdata_t *cdata, const int64_t id, char *buf, int slen, u if (ckp->redirector && !client->redirected) test_redirector_shares(ckp, client, buf); } + + /* Append bkeys as regular json for clients that can't decode them */ + if (unlikely(bkey && !client->bkey)) { + val = json_loads(buf, JSON_DISABLE_EOF_CHECK, NULL); + if (unlikely(!val)) { + LOGERR("Failed to decode json in bkey encoded message %s", buf); + bkey = '\0'; + len = strlen(buf); + goto out; + } + free(buf); + json_append_bkeys(val, bkey, blen); + buf = json_dumps(val, JSON_COMPACT); + json_decref(val); + } out: sender_send = ckzalloc(sizeof(sender_send_t)); sender_send->client = client; diff --git a/src/stratifier.c b/src/stratifier.c index c7c9648a..ee916644 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -6190,7 +6190,7 @@ static void srecv_process(ckpool_t *ckp, char *buf) json_t *val; int server; - val = json_loads(buf, 0, NULL); + val = json_loads(buf, JSON_DISABLE_EOF_CHECK, NULL); if (unlikely(!val)) { LOGWARNING("Received unrecognised non-json message: %s", buf); goto out;