| 483 | } |
| 484 | |
| 485 | static void handle_reply_jsonrpc(struct stream_con *con) |
| 486 | { |
| 487 | /* got a reply on the connection */ |
| 488 | str buf; |
| 489 | cJSON *reply; |
| 490 | int bytes_read; |
| 491 | const char *end; |
| 492 | char buffer[STREAM_BUFFER_SIZE + 1]; |
| 493 | |
| 494 | do { |
| 495 | bytes_read = read(con->fd, buffer, STREAM_BUFFER_SIZE); |
| 496 | } while (bytes_read == -1 && errno == EINTR); |
| 497 | if (bytes_read < 0) { |
| 498 | LM_ERR("error while reading reply from %s:%hu\n", STREAM_ADDR(con)); |
| 499 | goto error; |
| 500 | } else if (bytes_read == 0) { |
| 501 | LM_INFO("connection to %s:%hu closed!\n", STREAM_ADDR(con)); |
| 502 | goto error; |
| 503 | } |
| 504 | |
| 505 | /* if not in reliable mode, we are not interested in the reply */ |
| 506 | if (stream_reliable_mode == 0) |
| 507 | return; |
| 508 | |
| 509 | /* got a reply - parse it and match a command */ |
| 510 | /* TODO: proper parse a reply */ |
| 511 | LM_INFO("Received reply %.*s\n", bytes_read, buffer); |
| 512 | |
| 513 | /* if there was something else in the buffer, merge with what we had */ |
| 514 | if (con->pending_buffer.len) { |
| 515 | /* XXX: this wasn't tested */ |
| 516 | con->pending_buffer.s = pkg_realloc(con->pending_buffer.s, |
| 517 | con->pending_buffer.len + bytes_read + 1); |
| 518 | if (!con->pending_buffer.s) { |
| 519 | LM_ERR("No more pkg memory to keep replies!\n"); |
| 520 | goto error; |
| 521 | } |
| 522 | memcpy(con->pending_buffer.s + con->pending_buffer.len, buffer, bytes_read); |
| 523 | con->pending_buffer.len += bytes_read; |
| 524 | con->pending_buffer.s[con->pending_buffer.len] = 0; |
| 525 | buf = con->pending_buffer; |
| 526 | } else { |
| 527 | buf.s = buffer; |
| 528 | buf.len = bytes_read; |
| 529 | } |
| 530 | |
| 531 | do { |
| 532 | reply = cJSON_ParseWithOpts(buf.s, &end, 0); |
| 533 | if (!reply && buf.s == end) { |
| 534 | LM_ERR("cannot parse reply [%.*s]\n", buf.len, buf.s); |
| 535 | goto error; |
| 536 | } |
| 537 | |
| 538 | if (reply) { |
| 539 | if (handle_cmd_reply(con, reply) < 0) { |
| 540 | cJSON_Delete(reply); |
| 541 | goto error; |
| 542 | } |
no test coverage detected