[sr-dev] [kamailio/kamailio] kazoo: consuming of events fails if message has no body (#1837)

mihovilkolaric notifications at github.com
Tue Feb 5 22:11:39 CET 2019


### Description

Consuming messages without message body fails due to a JSON parsing error:
```
ERROR: kazoo [kz_json.c:287]: kz_json_parse(): Error parsing json: unexpected end of data
ERROR: kazoo [kz_json.c:288]: kz_json_parse():
ERROR: kazoo [kz_amqp.c:2960]: kz_amqp_send_worker_event(): error parsing json body 
```
This makes it unable to consume messages from RabbitMQ's event exchange plugin ( https://www.rabbitmq.com/event-exchange.html ), as the documentatin states: **"The message body is always blank."**

### Possible Solutions
As a possible fix I tried following change in kz_amqp.c:
```
void kz_amqp_send_worker_event(kz_amqp_server_ptr server_ptr, amqp_envelope_t* envelope, kz_amqp_bind_ptr bind)
{
    char buffer[100];
    kz_amqp_cmd_ptr cmd = NULL;
    kz_amqp_consumer_delivery_ptr ptr = NULL;
    json_obj_ptr json_obj = NULL;
    json_object* JObj = NULL;
    str* message_id = NULL;
    int idx = envelope->channel-1;
    int worker = 0;
    int _kz_server_id = server_ptr->id;
    int msg_size = envelope->message.body.len;
   
//begin of changes
    int routingkey_size = envelope->routing_key.len;
   
    if (msg_size == 0 && routingkey_size > 0 ) {
        char *routingkey_data = pkg_malloc(routingkey_size + 1);
        memset(routingkey_data, 0, routingkey_size + 1);
        memcpy(routingkey_data, (char*)envelope->routing_key.bytes, routingkey_size);
      
        LM_DBG("Message size: %d\n", msg_size);
        LM_DBG("Routing Key: %s\n", routingkey_data);

        char *pos = strchr(routingkey_data,'.');
        while (pos){
            *pos = '_';
            pos = strchr(pos,'.');
        }
       
        LM_DBG("New Routing Key: %s\n", routingkey_data);
        sprintf(buffer, "kazoo:%s", routingkey_data);

        if(kz_amqp_consumer_fire_event(buffer) != 0) {
            LM_ERR("kazoo:%s not found", routingkey_data);
        }

        pkg_free(routingkey_data);
        return;
    }

    char *json_data = pkg_malloc(msg_size + 1);
    if(!json_data) {
               LM_ERR("no more package memory available. needed %d\n", msg_size + 1);
               return;
    }

    char *json_data = pkg_malloc(msg_size + 1);
//end of changes

    if(!json_data) {
               LM_ERR("no more package memory available. needed %d\n", msg_size + 1);
               return;
    }
```
The change is rather simple: If there is no message-body supplied, the event-route _kazoo:<binding_key>_ is called (after replacing any '.' with '_' in the routing_key).

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/issues/1837
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.kamailio.org/pipermail/sr-dev/attachments/20190205/0aa1fdf4/attachment.html>


More information about the sr-dev mailing list