Kafka.xs

filter: Statementstmt 74.8% 2204 / 2944 Branchbran 47.7% 1023 / 2142 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.4% 3227 / 5086 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/Kafka.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #include "EVAPI.h"
7 #include "ngx-queue.h"
8  
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netinet/tcp.h>
12 #include <netdb.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <arpa/inet.h>
18 #include <sys/time.h>
19  
20 #ifdef HAVE_OPENSSL
21 #include <openssl/ssl.h>
22 #include <openssl/err.h>
23 #include <openssl/x509v3.h>
24 #include <openssl/hmac.h>
25 #include <openssl/sha.h>
26 #include <openssl/rand.h>
27 #include <openssl/bio.h>
28 #include <openssl/buffer.h>
29 #include <openssl/evp.h>
30 #endif
31  
32 #ifdef HAVE_LZ4
33 #include <lz4.h>
34 #include <lz4frame.h> /* Kafka's wire format is LZ4 FRAME (KIP-57) */
35 #endif
36  
37 #ifdef HAVE_ZLIB
38 #include <zlib.h>
39 #endif
40  
41 #ifdef HAVE_ZSTD
42 #include <zstd.h>
43 #endif
44  
45 #ifdef HAVE_SNAPPY
46 #include <snappy-c.h>
47 #endif
48  
49 /* ================================================================
50 * Constants
51 * ================================================================ */
52  
53 #define KF_MAGIC_ALIVE 0xCAFEBEEF
54 #define KF_MAGIC_FREED 0xDEADCAFE
55  
56 #define KF_BUF_INIT 16384
57  
58 /* True during Perl global destruction — calling user callbacks then is unsafe
59 * because closed-over SVs may already have been freed. */
60 #ifdef PERL_PHASE_DESTRUCT
61 # define KF_IN_GLOBAL_DESTRUCT() (PL_phase == PERL_PHASE_DESTRUCT)
62 #else
63 # define KF_IN_GLOBAL_DESTRUCT() (PL_dirty)
64 #endif
65  
66 /* Connection states */
67 #define CONN_DISCONNECTED 0
68 #define CONN_CONNECTING 1
69 #define CONN_TLS_HANDSHAKE 2
70 #define CONN_SASL_HANDSHAKE 3
71 #define CONN_SASL_AUTH 4
72 #define CONN_API_VERSIONS 5
73 #define CONN_READY 6
74  
75 /* Kafka API keys */
76 #define API_PRODUCE 0
77 #define API_FETCH 1
78 #define API_LIST_OFFSETS 2
79 #define API_METADATA 3
80 #define API_OFFSET_COMMIT 8
81 #define API_OFFSET_FETCH 9
82 #define API_FIND_COORDINATOR 10
83 #define API_JOIN_GROUP 11
84 #define API_HEARTBEAT 12
85 #define API_LEAVE_GROUP 13
86 #define API_SYNC_GROUP 14
87 #define API_SASL_HANDSHAKE 17
88 #define API_API_VERSIONS 18
89 #define API_CREATE_TOPICS 19
90 #define API_DELETE_TOPICS 20
91 #define API_INIT_PRODUCER_ID 22
92 #define API_ADD_PARTITIONS_TXN 24
93 #define API_END_TXN 26
94 #define API_TXN_OFFSET_COMMIT 28
95 #define API_SASL_AUTHENTICATE 36
96  
97 #define API_VERSIONS_MAX_KEY 64
98  
99 /* Compression types — Kafka wire codes. */
100 #define COMPRESS_NONE 0
101 #define COMPRESS_GZIP 1
102 #define COMPRESS_SNAPPY 2
103 #define COMPRESS_LZ4 3
104 #define COMPRESS_ZSTD 4
105  
106 #define CLEAR_HANDLER(field) \
107 do { if (NULL != (field)) { SvREFCNT_dec(field); (field) = NULL; } } while(0)
108  
109 /* DESTROY nulls the IV in the blessed ref, so every copy of the ref
110 * yields a NULL self; guard every XSUB entry against stale use. */
111 #define KF_REQUIRE_CONN(s) \
112 do { if (!(s)) croak("EV::Kafka::Conn: method called on destroyed connection"); } while (0)
113  
114 /* call_sv a stored handler SV, pinned across the call so the callback may
115 * clear its own handler (e.g. $obj->on_error(undef)) without freeing the CV
116 * mid-call. Args must already be pushed. */
117 #define PINNED_CALL_SV(handler, flags) \
118 STMT_START { SV *_pin = (handler); SvREFCNT_inc_simple_void_NN(_pin); \
119 call_sv(_pin, (flags)); SvREFCNT_dec(_pin); } STMT_END
120  
121 /* ================================================================
122 * Type declarations
123 * ================================================================ */
124  
125 typedef struct kf_buf_s kf_buf_t;
126 typedef struct ev_kafka_conn_s ev_kafka_conn_t;
127 typedef struct ev_kafka_conn_cb_s ev_kafka_conn_cb_t;
128  
129 typedef ev_kafka_conn_t* EV__Kafka__Conn;
130  
131 /* ================================================================
132 * Dynamic buffer
133 * ================================================================ */
134  
135 struct kf_buf_s {
136 char *data;
137 size_t len;
138 size_t cap;
139 };
140  
141 710 static void kf_buf_init(kf_buf_t *b) {
142 710 Newx(b->data, 256, char);
143 710 b->len = 0;
144 710 b->cap = 256;
145 710 }
146  
147 4149 static void kf_buf_grow(kf_buf_t *b, size_t need) {
148 4149 ▶ if (b->cap >= need) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->cap >= need) return; 4130 19 4149
149 19 size_t newcap = b->cap * 2;
150 19 ▶ if (newcap < need) newcap = need;
Branch: 1 branch
BranchTrueFalseTotal
if (newcap < need) newcap = need; 6 13 19
151 19 Renew(b->data, newcap, char);
152 19 b->cap = newcap;
153 }
154  
155 710 static void kf_buf_free(kf_buf_t *b) {
156 710 ▶ if (b->data) { Safefree(b->data); b->data = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->data) { Safefree(b->data); b->data = NULL; } 710 0 710
157 710 b->len = 0;
158 710 b->cap = 0;
159 710 }
160  
161 956 static void kf_buf_append(kf_buf_t *b, const char *data, size_t len) {
162 956 kf_buf_grow(b, b->len + len);
163 956 Copy(data, b->data + b->len, len, char);
164 956 b->len += len;
165 956 }
166  
167 233 static void kf_buf_append_i8(kf_buf_t *b, int8_t val) {
168 233 kf_buf_grow(b, b->len + 1);
169 233 b->data[b->len++] = val;
170 233 }
171  
172 880 static void kf_buf_append_i16(kf_buf_t *b, int16_t val) {
173 880 kf_buf_grow(b, b->len + 2);
174 880 uint16_t v = htons((uint16_t)val);
175 880 memcpy(b->data + b->len, &v, 2);
176 880 b->len += 2;
177 880 }
178  
179 851 static void kf_buf_append_i32(kf_buf_t *b, int32_t val) {
180 851 kf_buf_grow(b, b->len + 4);
181 851 uint32_t v = htonl((uint32_t)val);
182 851 memcpy(b->data + b->len, &v, 4);
183 851 b->len += 4;
184 851 }
185  
186 203 static void kf_buf_append_i64(kf_buf_t *b, int64_t val) {
187 203 kf_buf_grow(b, b->len + 8);
188 203 uint32_t hi = htonl((uint32_t)((uint64_t)val >> 32));
189 203 uint32_t lo = htonl((uint32_t)((uint64_t)val & 0xFFFFFFFF));
190 203 memcpy(b->data + b->len, &hi, 4);
191 203 memcpy(b->data + b->len + 4, &lo, 4);
192 203 b->len += 8;
193 203 }
194  
195 /* Kafka STRING: INT16 length + bytes. NULL → -1 length */
196 140 static void kf_buf_append_string(kf_buf_t *b, const char *s, int16_t len) {
197 140 kf_buf_append_i16(b, len);
198 140 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 133 7 140
199 140 }
200  
201 228 static void kf_buf_append_nullable_string(kf_buf_t *b, const char *s, int16_t len) {
202 228 ▶ if (!s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s) { 23 205 228
203 23 kf_buf_append_i16(b, -1);
204 } else {
205 205 kf_buf_append_i16(b, len);
206 205 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 201 4 205
207 }
208 228 }
209  
210 /* Kafka BYTES: INT32 length + bytes */
211 12 static void kf_buf_append_bytes(kf_buf_t *b, const char *s, int32_t len) {
212 12 kf_buf_append_i32(b, len);
213 12 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 12 0 12
214 12 }
215  
216 8 static void kf_buf_append_nullable_bytes(kf_buf_t *b, const char *s, int32_t len) {
217 8 ▶ if (!s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s) { 8 0 8
218 8 kf_buf_append_i32(b, -1);
219 } else {
220 0 kf_buf_append_i32(b, len);
221 0 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 0 0 0
222 }
223 8 }
224  
225 /* Unsigned varint (ZigZag for signed is not used in Kafka framing) */
226 1026 static void kf_buf_append_uvarint(kf_buf_t *b, uint64_t val) {
227 1026 kf_buf_grow(b, b->len + 10);
228 1047 ▶ while (val >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
while (val >= 0x80) { 21 1026 1047
229 21 b->data[b->len++] = (char)((val & 0x7F) | 0x80);
230 21 val >>= 7;
231 }
232 1026 b->data[b->len++] = (char)val;
233 1026 }
234  
235 /* Signed varint (ZigZag encoding) */
236 1026 static void kf_buf_append_varint(kf_buf_t *b, int64_t val) {
237 1026 uint64_t uval = ((uint64_t)val << 1) ^ (uint64_t)(val >> 63);
238 1026 kf_buf_append_uvarint(b, uval);
239 1026 }
240  
241 /* Compact string: uvarint(len+1) + bytes. NULL → 0 */
242 0 static void kf_buf_append_compact_string(kf_buf_t *b, const char *s, int32_t len) {
243 0 ▶ if (!s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s) { 0 0 0
244 0 kf_buf_append_uvarint(b, 0);
245 } else {
246 0 kf_buf_append_uvarint(b, (uint64_t)len + 1);
247 0 ▶ if (len > 0) kf_buf_append(b, s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) kf_buf_append(b, s, len); 0 0 0
248 }
249 0 }
250  
251 /* Tagged fields: just 0 = no tagged fields */
252 0 static void kf_buf_append_tagged_fields(kf_buf_t *b) {
253 0 kf_buf_append_uvarint(b, 0);
254 0 }
255  
256 /* ================================================================
257 * Wire read helpers (big-endian)
258 * ================================================================ */
259  
260 1946 static int16_t kf_read_i16(const char *buf) {
261 uint16_t v;
262 1946 memcpy(&v, buf, 2);
263 1946 return (int16_t)ntohs(v);
264 }
265  
266 53131 static int32_t kf_read_i32(const char *buf) {
267 uint32_t v;
268 53131 memcpy(&v, buf, 4);
269 53131 return (int32_t)ntohl(v);
270 }
271  
272 102 static int64_t kf_read_i64(const char *buf) {
273 uint32_t hi, lo;
274 102 memcpy(&hi, buf, 4);
275 102 memcpy(&lo, buf + 4, 4);
276 102 return ((int64_t)ntohl(hi) << 32) | (uint32_t)ntohl(lo);
277 }
278  
279 /* Read unsigned varint, returns bytes consumed or -1 on error */
280 104892 static int kf_read_uvarint(const char *buf, const char *end, uint64_t *out) {
281 104892 uint64_t val = 0;
282 104892 int shift = 0;
283 104892 const char *p = buf;
284 104921 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 52921 52000 104921
285 52921 uint8_t b = (uint8_t)*p++;
286 52921 val |= ((uint64_t)(b & 0x7F)) << shift;
287 52921 ▶ if (!(b & 0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(b & 0x80)) { 52892 29 52921
288 52892 *out = val;
289 52892 return (int)(p - buf);
290 }
291 29 shift += 7;
292 29 ▶ if (shift >= 64) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (shift >= 64) return -1; 0 29 29
293 }
294 52000 return -1; /* incomplete */
295 }
296  
297 /* Read signed varint (ZigZag) */
298 888 static int kf_read_varint(const char *buf, const char *end, int64_t *out) {
299 uint64_t uval;
300 888 int n = kf_read_uvarint(buf, end, &uval);
301 888 ▶ if (n < 0) return n;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return n; 0 888 888
302 888 *out = (int64_t)((uval >> 1) ^ -(int64_t)(uval & 1));
303 888 return n;
304 }
305  
306 /* Read Kafka STRING: i16 len + bytes. Returns pointer into buf, sets *len. Returns bytes consumed. */
307 215 static int kf_read_string(const char *buf, const char *end, const char **out, int16_t *slen) {
308 215 ▶ if (end - buf < 2) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end - buf < 2) return -1; 0 215 215
309 215 int16_t len = kf_read_i16(buf);
310 215 ▶ if (len < 0) { /* nullable null */
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) { /* nullable null */ 50 165 215
311 50 *out = NULL;
312 50 *slen = 0;
313 50 return 2;
314 }
315 165 ▶ if (end - buf < 2 + len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end - buf < 2 + len) return -1; 0 165 165
316 165 *out = buf + 2;
317 165 *slen = len;
318 165 return 2 + len;
319 }
320  
321 /* Read compact string: uvarint(len+1) + bytes */
322 52002 static int kf_read_compact_string(const char *buf, const char *end, const char **out, int32_t *slen) {
323 uint64_t raw;
324 52002 int n = kf_read_uvarint(buf, end, &raw);
325 52002 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 52000 2 52002
326 2 ▶ if (raw == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (raw == 0) { 0 2 2
327 0 *out = NULL;
328 0 *slen = 0;
329 0 return n;
330 }
331 /* reject lengths that would wrap negative in the int32_t cast below */
332 2 ▶ if (raw - 1 > (uint64_t)INT32_MAX) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (raw - 1 > (uint64_t)INT32_MAX) return -1; 2 0 2
333 0 int32_t len = (int32_t)(raw - 1);
334 0 ▶ if (end - buf - n < len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end - buf - n < len) return -1; 0 0 0
335 0 *out = buf + n;
336 0 *slen = len;
337 0 return n + len;
338 }
339  
340 /* Skip tagged fields */
341 0 static int kf_skip_tagged_fields(const char *buf, const char *end) {
342 uint64_t count;
343 0 int n = kf_read_uvarint(buf, end, &count);
344 0 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 0 0 0
345 0 const char *p = buf + n;
346 uint64_t i;
347 0 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 0 0 0
348 uint64_t tag;
349 0 int tn = kf_read_uvarint(p, end, &tag);
350 0 ▶ if (tn < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (tn < 0) return -1; 0 0 0
351 0 p += tn;
352 uint64_t dlen;
353 0 int dn = kf_read_uvarint(p, end, &dlen);
354 0 ▶ if (dn < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dn < 0) return -1; 0 0 0
355 0 p += dn;
356 0 ▶ if ((uint64_t)(end - p) < dlen) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((uint64_t)(end - p) < dlen) return -1; 0 0 0
357 0 p += dlen;
358 }
359 0 return (int)(p - buf);
360 }
361  
362 /* ================================================================
363 * CRC32C (software implementation)
364 * ================================================================ */
365  
366 static uint32_t crc32c_table[256];
367 static int crc32c_table_inited = 0;
368  
369 34 static void crc32c_init_table(void) {
370 uint32_t i, j;
371 8738 ▶ for (i = 0; i < 256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) { 8704 34 8738
372 8704 uint32_t crc = i;
373 78336 ▶ for (j = 0; j < 8; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 8; j++) { 69632 8704 78336
374 69632 ▶ if (crc & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (crc & 1) 34816 34816 69632
375 34816 crc = (crc >> 1) ^ 0x82F63B78;
376 else
377 34816 crc >>= 1;
378 }
379 8704 crc32c_table[i] = crc;
380 }
381 34 crc32c_table_inited = 1;
382 34 }
383  
384 66 static uint32_t crc32c(const char *buf, size_t len) {
385 66 uint32_t crc = 0xFFFFFFFF;
386 size_t i;
387 66 ▶ if (!crc32c_table_inited) crc32c_init_table();
Branch: 1 branch
BranchTrueFalseTotal
if (!crc32c_table_inited) crc32c_init_table(); 0 66 66
388 206628 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 206562 66 206628
389 206562 crc = crc32c_table[(crc ^ (uint8_t)buf[i]) & 0xFF] ^ (crc >> 8);
390 66 return crc ^ 0xFFFFFFFF;
391 }
392  
393 /* ================================================================
394 * Connection callback struct
395 * ================================================================ */
396  
397 struct ev_kafka_conn_cb_s {
398 SV *cb;
399 ngx_queue_t queue;
400 int32_t correlation_id;
401 int16_t api_key;
402 int16_t api_version;
403 int internal; /* 1 = internal handshake cb, don't invoke Perl */
404 };
405  
406 /* ================================================================
407 * Broker connection struct (Layer 1)
408 * ================================================================ */
409  
410 struct ev_kafka_conn_s {
411 unsigned int magic;
412 struct ev_loop *loop;
413 SV *loop_sv; /* held so a custom EV::Loop cannot be ev_loop_destroy'd
414 * under live watchers; NULL for EV_DEFAULT */
415 int fd;
416 int state;
417  
418 /* EV watchers */
419 ev_io rio, wio;
420 ev_timer timer;
421 int reading, writing, timing;
422  
423 /* TLS */
424 #ifdef HAVE_OPENSSL
425 SSL_CTX *ssl_ctx;
426 SSL *ssl;
427 #endif
428 int tls_enabled;
429 char *tls_ca_file;
430 int tls_skip_verify;
431  
432 /* Connection params */
433 char *host;
434 int port;
435  
436 /* SASL */
437 char *sasl_mechanism;
438 char *sasl_username;
439 char *sasl_password;
440  
441 /* SCRAM state */
442 int scram_step;
443 char *scram_nonce;
444 char *scram_client_first;
445 size_t scram_client_first_len;
446 /* For server-final-message signature verification (RFC 5802). */
447 unsigned char scram_server_key[64];
448 int scram_server_key_len;
449 char *scram_auth_message;
450 size_t scram_auth_message_len;
451  
452 /* Buffers */
453 char *rbuf;
454 size_t rbuf_len, rbuf_cap;
455 char *wbuf;
456 size_t wbuf_len, wbuf_off, wbuf_cap;
457  
458 /* Request/response correlation */
459 ngx_queue_t cb_queue;
460 int32_t next_correlation_id;
461 int pending_count;
462  
463 /* Client identity */
464 char *client_id;
465 int client_id_len;
466  
467 /* API version negotiation */
468 int16_t api_versions[API_VERSIONS_MAX_KEY];
469 int api_versions_known;
470  
471 /* Event handlers */
472 SV *on_error;
473 SV *on_connect;
474 SV *on_disconnect;
475  
476 /* Reconnection */
477 int auto_reconnect;
478 int reconnect_delay_ms;
479 ev_timer reconnect_timer;
480 int reconnect_timing;
481 int intentional_disconnect;
482 /* Backoff state: attempts since the last successful connect;
483 * delay = reconnect_delay_ms * 2^attempts, capped and jittered. */
484 int reconnect_attempts;
485  
486 /* Connect/handshake timeout (seconds); the single self->timer covers
487 * TCP connect AND all handshake phases until CONN_READY. */
488 double connect_timeout;
489  
490 /* Safety: callback_depth is bumped by the outermost event-loop watcher
491 * frames; DESTROY defers Safefree while depth>0 (free_pending) so a
492 * callback that drops the last ref to the conn cannot free it out from
493 * under the still-running C frames. */
494 int callback_depth;
495 int free_pending;
496 };
497  
498 /* ================================================================
499 * Forward declarations
500 * ================================================================ */
501  
502 static void conn_io_cb(EV_P_ ev_io *w, int revents);
503 static void conn_timer_cb(EV_P_ ev_timer *w, int revents);
504 static void conn_reconnect_timer_cb(EV_P_ ev_timer *w, int revents);
505 static void conn_io_cb_inner(EV_P_ ev_io *w, int revents);
506 static void conn_timer_cb_inner(EV_P_ ev_timer *w, int revents);
507 static void conn_reconnect_timer_cb_inner(EV_P_ ev_timer *w, int revents);
508 static void conn_do_free(pTHX_ ev_kafka_conn_t *self);
509 static void conn_start_reading(ev_kafka_conn_t *self);
510 static void conn_stop_reading(ev_kafka_conn_t *self);
511 static void conn_start_writing(ev_kafka_conn_t *self);
512 static void conn_stop_writing(ev_kafka_conn_t *self);
513 static void conn_emit_error(pTHX_ ev_kafka_conn_t *self, const char *msg);
514 static void conn_cleanup(pTHX_ ev_kafka_conn_t *self);
515 static int conn_check_destroyed(ev_kafka_conn_t *self);
516 static void conn_cancel_pending(pTHX_ ev_kafka_conn_t *self, const char *err);
517 static void conn_on_connect_done(pTHX_ ev_kafka_conn_t *self);
518 static void conn_process_responses(pTHX_ ev_kafka_conn_t *self);
519 static void conn_schedule_reconnect(pTHX_ ev_kafka_conn_t *self);
520 static int32_t conn_send_request(pTHX_ ev_kafka_conn_t *self,
521 int16_t api_key, int16_t api_version, kf_buf_t *body, SV *cb,
522 int internal, int no_response);
523 static void conn_send_api_versions(pTHX_ ev_kafka_conn_t *self);
524 static void conn_start_connect(pTHX_ ev_kafka_conn_t *self,
525 const char *host, int port, double timeout);
526 static void conn_send_sasl_handshake(pTHX_ ev_kafka_conn_t *self);
527 static void conn_parse_sasl_handshake_response(pTHX_ ev_kafka_conn_t *self,
528 const char *data, size_t len);
529 static void conn_send_sasl_authenticate(pTHX_ ev_kafka_conn_t *self);
530 static void conn_parse_sasl_authenticate_response(pTHX_ ev_kafka_conn_t *self,
531 const char *data, size_t len);
532  
533 /* Response parsers */
534 static SV* conn_parse_metadata_response(pTHX_ ev_kafka_conn_t *self,
535 int16_t version, const char *data, size_t len);
536 static SV* conn_parse_produce_response(pTHX_ ev_kafka_conn_t *self,
537 int16_t version, const char *data, size_t len);
538 static SV* conn_parse_fetch_response(pTHX_ ev_kafka_conn_t *self,
539 int16_t version, const char *data, size_t len);
540 static SV* conn_parse_list_offsets_response(pTHX_ ev_kafka_conn_t *self,
541 int16_t version, const char *data, size_t len);
542 static SV* conn_parse_find_coordinator_response(pTHX_ ev_kafka_conn_t *self,
543 int16_t version, const char *data, size_t len);
544 static SV* conn_parse_join_group_response(pTHX_ ev_kafka_conn_t *self,
545 int16_t version, const char *data, size_t len);
546 static SV* conn_parse_sync_group_response(pTHX_ ev_kafka_conn_t *self,
547 int16_t version, const char *data, size_t len);
548 static SV* conn_parse_heartbeat_response(pTHX_ ev_kafka_conn_t *self,
549 int16_t version, const char *data, size_t len);
550 static SV* conn_parse_offset_commit_response(pTHX_ ev_kafka_conn_t *self,
551 int16_t version, const char *data, size_t len);
552 static SV* conn_parse_offset_fetch_response(pTHX_ ev_kafka_conn_t *self,
553 int16_t version, const char *data, size_t len);
554 static SV* conn_parse_leave_group_response(pTHX_ ev_kafka_conn_t *self,
555 int16_t version, const char *data, size_t len);
556 static SV* conn_parse_create_topics_response(pTHX_ ev_kafka_conn_t *self,
557 int16_t version, const char *data, size_t len);
558 static SV* conn_parse_delete_topics_response(pTHX_ ev_kafka_conn_t *self,
559 int16_t version, const char *data, size_t len);
560 static SV* conn_parse_init_producer_id_response(pTHX_ ev_kafka_conn_t *self,
561 int16_t version, const char *data, size_t len);
562 static SV* conn_parse_add_partitions_to_txn_response(pTHX_ ev_kafka_conn_t *self,
563 int16_t version, const char *data, size_t len);
564 static SV* conn_parse_end_txn_response(pTHX_ ev_kafka_conn_t *self,
565 int16_t version, const char *data, size_t len);
566 static SV* conn_parse_txn_offset_commit_response(pTHX_ ev_kafka_conn_t *self,
567 int16_t version, const char *data, size_t len);
568  
569 #ifdef HAVE_OPENSSL
570 static int is_ip_literal(const char *host);
571 #endif
572  
573 /* ================================================================
574 * I/O helpers (with optional TLS)
575 * ================================================================ */
576  
577 189 static ssize_t kf_io_read(ev_kafka_conn_t *self, void *buf, size_t len) {
578 #ifdef HAVE_OPENSSL
579 189 ▶ if (self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl) { 0 189 189
580 0 ▶ int ssl_len = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
Branch: 1 branch
BranchTrueFalseTotal
int ssl_len = (len > (size_t)INT_MAX) ? INT_MAX : (int)len; 0 0 0
581 0 ERR_clear_error();
582 0 int ret = SSL_read(self->ssl, buf, ssl_len);
583 0 ▶ if (ret <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret <= 0) { 0 0 0
584 0 int err = SSL_get_error(self->ssl, ret);
585 0 ▶ if (err == SSL_ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_READ) { 0 0 0
586 0 errno = EAGAIN;
587 0 return -1;
588 }
589 0 ▶ if (err == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_WRITE) { 0 0 0
590 0 conn_start_writing(self);
591 0 errno = EAGAIN;
592 0 return -1;
593 }
594 0 ▶ if (err == SSL_ERROR_ZERO_RETURN) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_ZERO_RETURN) return 0; 0 0 0
595 0 ERR_clear_error();
596 0 errno = EIO;
597 0 return -1;
598 }
599 0 return ret;
600 }
601 #endif
602 189 return read(self->fd, buf, len);
603 }
604  
605 183 static ssize_t kf_io_write(ev_kafka_conn_t *self, const void *buf, size_t len) {
606 #ifdef HAVE_OPENSSL
607 183 ▶ if (self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl) { 0 183 183
608 0 ▶ int ssl_len = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
Branch: 1 branch
BranchTrueFalseTotal
int ssl_len = (len > (size_t)INT_MAX) ? INT_MAX : (int)len; 0 0 0
609 0 ERR_clear_error();
610 0 int ret = SSL_write(self->ssl, buf, ssl_len);
611 0 ▶ if (ret <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret <= 0) { 0 0 0
612 0 int err = SSL_get_error(self->ssl, ret);
613 0 ▶ if (err == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_WRITE) { 0 0 0
614 0 errno = EAGAIN;
615 0 return -1;
616 }
617 0 ▶ if (err == SSL_ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_READ) { 0 0 0
618 0 conn_start_reading(self);
619 0 errno = EAGAIN;
620 0 return -1;
621 }
622 0 ERR_clear_error();
623 0 errno = EIO;
624 0 return -1;
625 }
626 0 return ret;
627 }
628 #endif
629 #ifdef MSG_NOSIGNAL
630 /* MSG_NOSIGNAL: a write to an RST socket must fail with EPIPE, not kill
631 * the process with SIGPIPE (BSD/macOS use SO_NOSIGPIPE instead). */
632 183 return send(self->fd, buf, len, MSG_NOSIGNAL);
633 #else
634 return write(self->fd, buf, len);
635 #endif
636 }
637  
638 /* ================================================================
639 * Buffer management
640 * ================================================================ */
641  
642 189 static void conn_ensure_rbuf(ev_kafka_conn_t *self, size_t need) {
643 189 ▶ if (self->rbuf_cap >= need) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_cap >= need) return; 189 0 189
644 0 size_t newcap = self->rbuf_cap * 2;
645 0 ▶ if (newcap < need) newcap = need;
Branch: 1 branch
BranchTrueFalseTotal
if (newcap < need) newcap = need; 0 0 0
646 0 Renew(self->rbuf, newcap, char);
647 0 self->rbuf_cap = newcap;
648 }
649  
650 198 static void conn_ensure_wbuf(ev_kafka_conn_t *self, size_t need) {
651 198 ▶ if (self->wbuf_cap >= need) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_cap >= need) return; 198 0 198
652 0 size_t newcap = self->wbuf_cap * 2;
653 0 ▶ if (newcap < need) newcap = need;
Branch: 1 branch
BranchTrueFalseTotal
if (newcap < need) newcap = need; 0 0 0
654 0 Renew(self->wbuf, newcap, char);
655 0 self->wbuf_cap = newcap;
656 }
657  
658 /* ================================================================
659 * Watcher control
660 * ================================================================ */
661  
662 60 static void conn_start_reading(ev_kafka_conn_t *self) {
663 60 ▶ if (!self->reading && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reading && self->fd >= 0) { 60 0 60
if (!self->reading && self->fd >= 0) { 60 0 60
664 60 ev_io_start(self->loop, &self->rio);
665 60 self->reading = 1;
666 }
667 60 }
668  
669 130 static void conn_stop_reading(ev_kafka_conn_t *self) {
670 130 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 60 70 130
671 60 ev_io_stop(self->loop, &self->rio);
672 60 self->reading = 0;
673 }
674 130 }
675  
676 268 static void conn_start_writing(ev_kafka_conn_t *self) {
677 268 ▶ if (!self->writing && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->writing && self->fd >= 0) { 257 11 268
if (!self->writing && self->fd >= 0) { 257 0 257
678 257 ev_io_start(self->loop, &self->wio);
679 257 self->writing = 1;
680 }
681 268 }
682  
683 382 static void conn_stop_writing(ev_kafka_conn_t *self) {
684 382 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 257 125 382
685 257 ev_io_stop(self->loop, &self->wio);
686 257 self->writing = 0;
687 }
688 382 }
689  
690 /* Arm (or re-arm) the single connect/handshake timer; it stays armed
691 * until CONN_READY so a silent peer fails the conn instead of wedging it. */
692 138 static void conn_arm_timer(ev_kafka_conn_t *self) {
693 138 ▶ if (self->connect_timeout <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timeout <= 0) return; 0 138 138
694 138 ▶ if (self->timing)
Branch: 1 branch
BranchTrueFalseTotal
if (self->timing) 68 70 138
695 68 ev_timer_stop(self->loop, &self->timer);
696 138 ev_timer_init(&self->timer, conn_timer_cb, self->connect_timeout, 0.0);
697 138 self->timer.data = (void *)self;
698 138 ev_timer_start(self->loop, &self->timer);
699 138 self->timing = 1;
700 }
701  
702 186 static void conn_disarm_timer(ev_kafka_conn_t *self) {
703 186 ▶ if (self->timing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->timing) { 69 117 186
704 69 ev_timer_stop(self->loop, &self->timer);
705 69 self->timing = 0;
706 }
707 186 }
708  
709 /* ================================================================
710 * Callback invocation helpers
711 * ================================================================ */
712  
713 17 static void conn_emit_error(pTHX_ ev_kafka_conn_t *self, const char *msg) {
714 17 ▶ if (!self->on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->on_error) { 1 16 17
715 /* Never croak in watcher context: a longjmp would skip the
716 * enclosing frame's --callback_depth, stranding the deferred
717 * conn_do_free forever. Warn instead. */
718 1 warn("EV::Kafka::Conn: %s", msg);
719 1 return;
720 }
721  
722 16 self->callback_depth++;
723 {
724 16 dSP;
725 16 ENTER;
726 16 SAVETMPS;
727 16 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 16 16
728 16 ▶ XPUSHs(sv_2mortal(newSVpv(msg, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(msg, 0))); 0 16 16
729 16 PUTBACK;
730 16 PINNED_CALL_SV(self->on_error, G_DISCARD | G_EVAL);
731 16 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 16 0 16
if (SvTRUE(ERRSV)) { 0 16 16
732 0 ▶ warn("EV::Kafka::Conn: on_error callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Kafka::Conn: on_error callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
733 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
734 }
735 16 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 16 0 16
736 16 LEAVE;
737 }
738 16 self->callback_depth--;
739 }
740  
741 56 static void conn_emit_connect(pTHX_ ev_kafka_conn_t *self) {
742 56 ▶ if (!self->on_connect) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->on_connect) return; 2 54 56
743  
744 54 self->callback_depth++;
745 {
746 54 dSP;
747 54 ENTER;
748 54 SAVETMPS;
749 54 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 54 54
750 54 PUTBACK;
751 54 PINNED_CALL_SV(self->on_connect, G_DISCARD | G_EVAL);
752 54 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 54 0 54
if (SvTRUE(ERRSV)) { 0 54 54
753 0 ▶ warn("EV::Kafka::Conn: on_connect callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Kafka::Conn: on_connect callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
754 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
755 }
756 54 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 54 54
757 54 LEAVE;
758 }
759 54 self->callback_depth--;
760 }
761  
762 61 static void conn_emit_disconnect(pTHX_ ev_kafka_conn_t *self) {
763 61 ▶ if (!self->on_disconnect) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->on_disconnect) return; 41 20 61
764  
765 20 self->callback_depth++;
766 {
767 20 dSP;
768 20 ENTER;
769 20 SAVETMPS;
770 20 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 20 20
771 20 PUTBACK;
772 20 PINNED_CALL_SV(self->on_disconnect, G_DISCARD | G_EVAL);
773 20 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 20 0 20
if (SvTRUE(ERRSV)) { 0 20 20
774 0 ▶ warn("EV::Kafka::Conn: on_disconnect callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Kafka::Conn: on_disconnect callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
775 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
776 }
777 20 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 20 20
778 20 LEAVE;
779 }
780 20 self->callback_depth--;
781 }
782  
783 /* Invoke a command callback: cb->(result, error) */
784 133 static void conn_invoke_cb(pTHX_ ev_kafka_conn_t *self, SV *cb, SV *result, SV *error) {
785 133 ▶ if (!cb) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cb) return; 0 133 133
786  
787 133 self->callback_depth++;
788 {
789 133 dSP;
790 133 ENTER;
791 133 SAVETMPS;
792 133 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 133 133
793 133 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 133 133
794 133 ▶ PUSHs(result ? result : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(result ? result : &PL_sv_undef); 115 18 133
795 133 ▶ PUSHs(error ? error : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(error ? error : &PL_sv_undef); 18 115 133
796 133 PUTBACK;
797 133 call_sv(cb, G_DISCARD | G_EVAL);
798 133 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 133 0 133
if (SvTRUE(ERRSV)) { 0 133 133
799 0 ▶ warn("EV::Kafka::Conn: callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Kafka::Conn: callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
800 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
801 }
802 133 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 133 133
803 133 LEAVE;
804 }
805 133 self->callback_depth--;
806 }
807  
808 317 static int conn_check_destroyed(ev_kafka_conn_t *self) {
809 /* free_pending: DESTROY deferred the Safefree to the outermost watcher
810 * frame; treat the conn as gone, but the memory is valid until then. */
811 317 ▶ return self->magic != KF_MAGIC_ALIVE || self->free_pending;
Branch: 2 branches
BranchTrueFalseTotal
return self->magic != KF_MAGIC_ALIVE || self->free_pending; 316 1 317
return self->magic != KF_MAGIC_ALIVE || self->free_pending; 0 316 316
812 }
813  
814 /* The actual teardown: stop the reconnect timer, drop handlers, free all
815 * buffers/strings and the struct. Called by DESTROY when not inside a
816 * callback, or by the outermost event-loop watcher frame once it unwinds to
817 * callback_depth 0 with free_pending set. conn_cleanup() must have run first. */
818 69 static void conn_do_free(pTHX_ ev_kafka_conn_t *self) {
819 /* Defensive: if a future early-return ever strands queue entries,
820 * free them here (callbacks are NOT invoked). */
821 69 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 0 69 69
822 0 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
823 0 ngx_queue_remove(q);
824 0 ev_kafka_conn_cb_t *cbt = ngx_queue_data(q, ev_kafka_conn_cb_t, queue);
825 0 ▶ if (cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
826 0 Safefree(cbt);
827 }
828  
829 69 ▶ if (self->reconnect_timing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timing) { 4 65 69
830 4 ev_timer_stop(self->loop, &self->reconnect_timer);
831 4 self->reconnect_timing = 0;
832 }
833  
834 69 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 27 42 69
835 69 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 16 53 69
836 69 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 11 58 69
837  
838 69 ▶ if (self->loop_sv) SvREFCNT_dec(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (self->loop_sv) SvREFCNT_dec(self->loop_sv); 1 68 69
839  
840 69 ▶ if (self->host) Safefree(self->host);
Branch: 1 branch
BranchTrueFalseTotal
if (self->host) Safefree(self->host); 60 9 69
841 69 ▶ if (self->client_id) Safefree(self->client_id);
Branch: 1 branch
BranchTrueFalseTotal
if (self->client_id) Safefree(self->client_id); 69 0 69
842 69 ▶ if (self->sasl_mechanism) Safefree(self->sasl_mechanism);
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_mechanism) Safefree(self->sasl_mechanism); 4 65 69
843 69 ▶ if (self->sasl_username) Safefree(self->sasl_username);
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_username) Safefree(self->sasl_username); 2 67 69
844 69 ▶ if (self->sasl_password) Safefree(self->sasl_password);
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_password) Safefree(self->sasl_password); 2 67 69
845 69 ▶ if (self->scram_nonce) Safefree(self->scram_nonce);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_nonce) Safefree(self->scram_nonce); 3 66 69
846 69 ▶ if (self->scram_client_first) Safefree(self->scram_client_first);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_client_first) Safefree(self->scram_client_first); 1 68 69
847 #ifdef HAVE_OPENSSL
848 69 OPENSSL_cleanse(self->scram_server_key, sizeof(self->scram_server_key));
849 69 ▶ if (self->scram_auth_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_auth_message) { 0 69 69
850 0 OPENSSL_cleanse(self->scram_auth_message, self->scram_auth_message_len);
851 0 Safefree(self->scram_auth_message);
852 }
853 #endif
854 69 ▶ if (self->tls_ca_file) Safefree(self->tls_ca_file);
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_ca_file) Safefree(self->tls_ca_file); 0 69 69
855 69 ▶ if (self->rbuf) Safefree(self->rbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf) Safefree(self->rbuf); 69 0 69
856 69 ▶ if (self->wbuf) Safefree(self->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf) Safefree(self->wbuf); 69 0 69
857  
858 69 Safefree(self);
859 69 }
860  
861 /* ================================================================
862 * Connection cleanup
863 * ================================================================ */
864  
865 130 static void conn_cleanup(pTHX_ ev_kafka_conn_t *self) {
866 130 conn_stop_reading(self);
867 130 conn_stop_writing(self);
868 130 conn_disarm_timer(self);
869 #ifdef HAVE_OPENSSL
870 130 ▶ if (self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl) { 0 130 130
871 0 SSL_free(self->ssl);
872 0 self->ssl = NULL;
873 }
874 130 ▶ if (self->ssl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl_ctx) { 0 130 130
875 0 SSL_CTX_free(self->ssl_ctx);
876 0 self->ssl_ctx = NULL;
877 }
878 #endif
879 130 ▶ if (self->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd >= 0) { 70 60 130
880 70 close(self->fd);
881 70 self->fd = -1;
882 }
883 /* Stale bytes must not leak onto the next connection: a reconnect
884 * must not resume behind leftover output or a partial old response. */
885 130 self->wbuf_len = 0;
886 130 self->wbuf_off = 0;
887 130 self->rbuf_len = 0;
888 130 self->state = CONN_DISCONNECTED;
889 130 }
890  
891 130 static void conn_cancel_pending(pTHX_ ev_kafka_conn_t *self, const char *err) {
892 130 int in_destruct = KF_IN_GLOBAL_DESTRUCT();
893 130 ▶ SV *err_sv = in_destruct ? NULL : newSVpv(err, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *err_sv = in_destruct ? NULL : newSVpv(err, 0); 125 5 130
894 ngx_queue_t *q;
895  
896 /* Drain the WHOLE queue even when a callback destroys the conn
897 * mid-loop. Safe: each cbt is unlinked BEFORE its callback runs, so
898 * a re-entrant drain cannot invoke it twice, and callback_depth > 0
899 * defers conn_do_free, keeping self valid for the rest of the drain. */
900 149 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 19 130 149
901 19 q = ngx_queue_head(&self->cb_queue);
902 19 ngx_queue_remove(q);
903 19 ev_kafka_conn_cb_t *cbt = ngx_queue_data(q, ev_kafka_conn_cb_t, queue);
904 19 self->pending_count--;
905 19 ▶ if (cbt->cb && !cbt->internal && !in_destruct)
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->cb && !cbt->internal && !in_destruct) 18 1 19
if (cbt->cb && !cbt->internal && !in_destruct) 18 0 18
if (cbt->cb && !cbt->internal && !in_destruct) 18 0 18
906 18 conn_invoke_cb(aTHX_ self, cbt->cb, NULL, sv_2mortal(newSVsv(err_sv)));
907 19 ▶ if (cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) SvREFCNT_dec(cbt->cb); 18 1 19
908 19 Safefree(cbt);
909 }
910 130 ▶ if (err_sv) SvREFCNT_dec(err_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (err_sv) SvREFCNT_dec(err_sv); 125 5 130
911 130 }
912  
913 64 static void conn_handle_disconnect(pTHX_ ev_kafka_conn_t *self, const char *reason) {
914 /* Idempotent: conn_cleanup() below leaves state == CONN_DISCONNECTED,
915 * so a re-entrant disconnect() stops at this guard. Callers already
916 * DISCONNECTED must schedule their own conn_schedule_reconnect. */
917 64 ▶ if (self->state == CONN_DISCONNECTED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == CONN_DISCONNECTED) return; 3 61 64
918  
919 61 conn_cleanup(aTHX_ self);
920  
921 /* Fail pending requests BEFORE emitting disconnect: requests enqueued
922 * from on_disconnect (e.g. a re-entrant connect()) belong to the NEW
923 * connection and must not be cancelled by the old one's drain. */
924 61 conn_cancel_pending(aTHX_ self, reason);
925 61 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 61 61
926  
927 61 conn_emit_disconnect(aTHX_ self);
928 61 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 61 61
929  
930 61 ▶ if (!self->intentional_disconnect && self->auto_reconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->auto_reconnect) { 22 39 61
if (!self->intentional_disconnect && self->auto_reconnect) { 14 8 22
931 14 conn_schedule_reconnect(aTHX_ self);
932 }
933 }
934  
935 /* ================================================================
936 * Reconnection
937 * ================================================================ */
938  
939 /* Outermost watcher frame: owns callback_depth so a callback that drops the
940 * last ref to the conn defers the free to here (after all C frames unwind). */
941 12 static void conn_reconnect_timer_cb(EV_P_ ev_timer *w, int revents) {
942 12 ev_kafka_conn_t *self = (ev_kafka_conn_t *)((char *)w - offsetof(ev_kafka_conn_t, reconnect_timer));
943 dTHX;
944 (void)loop;
945 12 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 12 12
946 12 self->callback_depth++;
947 {
948 /* Per-event tmps frame: see conn_io_cb. */
949 12 ENTER;
950 12 SAVETMPS;
951 12 conn_reconnect_timer_cb_inner(loop, w, revents);
952 12 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 12 12
953 12 LEAVE;
954 }
955 12 ▶ if (--self->callback_depth == 0 && self->free_pending)
Branch: 2 branches
BranchTrueFalseTotal
if (--self->callback_depth == 0 && self->free_pending) 12 0 12
if (--self->callback_depth == 0 && self->free_pending) 0 12 12
956 0 conn_do_free(aTHX_ self);
957 }
958  
959 12 static void conn_reconnect_timer_cb_inner(EV_P_ ev_timer *w, int revents) {
960 12 ev_kafka_conn_t *self = (ev_kafka_conn_t *)((char *)w - offsetof(ev_kafka_conn_t, reconnect_timer));
961 dTHX;
962 (void)loop;
963 (void)revents;
964  
965 12 self->reconnect_timing = 0;
966 12 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 12 12
967 12 ▶ if (self->state != CONN_DISCONNECTED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_DISCONNECTED) return; 0 12 12
968 12 ▶ if (!self->host) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->host) return; 0 12 12
969  
970 12 conn_start_connect(aTHX_ self, self->host, self->port, self->connect_timeout);
971 }
972  
973 16 static void conn_schedule_reconnect(pTHX_ ev_kafka_conn_t *self) {
974 16 ▶ if (self->reconnect_timing) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timing) return; 0 16 16
975 /* Capped exponential backoff with jitter: retry N>1 waits
976 * reconnect_delay_ms * 2^(N-1), capped at 30s, jittered +/-25%. */
977 16 int attempt = self->reconnect_attempts;
978 16 double delay = self->reconnect_delay_ms / 1000.0;
979 16 ▶ if (delay < 0.01) delay = 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0.01) delay = 1.0; 0 16 16
980 16 ▶ if (attempt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (attempt > 0) { 5 11 16
981 5 int shift = attempt > 5 ? 5 : attempt;
982 5 delay *= (double)(1 << shift);
983 5 ▶ if (delay > 30.0) delay = 30.0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay > 30.0) delay = 30.0; 0 5 5
984 5 delay *= 0.75 + 0.5 * ((double)rand() / (double)RAND_MAX);
985 }
986 16 self->reconnect_attempts = attempt + 1;
987 16 ev_timer_init(&self->reconnect_timer, conn_reconnect_timer_cb, delay, 0.0);
988 16 ev_timer_start(self->loop, &self->reconnect_timer);
989 16 self->reconnect_timing = 1;
990 }
991  
992 /* ================================================================
993 * Request framing
994 * ================================================================ */
995  
996 /* Build request header + body, append to wbuf, enqueue callback.
997 * For api_version >= threshold, uses compact header (v2).
998 * ApiVersions v3+ uses flexible (v1) request header.
999 * Returns correlation_id.
1000 */
1001 198 static int32_t conn_send_request(pTHX_ ev_kafka_conn_t *self,
1002 int16_t api_key, int16_t api_version, kf_buf_t *body, SV *cb,
1003 int internal, int no_response)
1004 {
1005 198 int32_t corr_id = self->next_correlation_id++;
1006 kf_buf_t hdr;
1007 198 kf_buf_init(&hdr);
1008  
1009 /* Request header v1 (non-flexible): api_key, api_version, correlation_id, client_id */
1010 /* Request header v2 (flexible): same + tagged_fields */
1011 /* ApiVersions v3+ uses header v2 (flexible) */
1012 198 ▶ int flexible = (api_key == API_API_VERSIONS && api_version >= 3);
Branch: 2 branches
BranchTrueFalseTotal
int flexible = (api_key == API_API_VERSIONS && api_version >= 3); 60 138 198
int flexible = (api_key == API_API_VERSIONS && api_version >= 3); 0 60 60
1013  
1014 198 kf_buf_append_i16(&hdr, api_key);
1015 198 kf_buf_append_i16(&hdr, api_version);
1016 198 kf_buf_append_i32(&hdr, corr_id);
1017  
1018 198 ▶ if (flexible) {
Branch: 1 branch
BranchTrueFalseTotal
if (flexible) { 0 198 198
1019 /* compact string for client_id */
1020 0 kf_buf_append_compact_string(&hdr, self->client_id, self->client_id_len);
1021 0 kf_buf_append_tagged_fields(&hdr);
1022 } else {
1023 198 kf_buf_append_nullable_string(&hdr, self->client_id, self->client_id_len);
1024 }
1025  
1026 /* Total size = header + body */
1027 198 size_t raw_size = hdr.len + body->len;
1028 198 ▶ if (raw_size > (size_t)INT32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (raw_size > (size_t)INT32_MAX) { 0 198 198
1029 0 kf_buf_free(&hdr);
1030 0 croak("request too large");
1031 }
1032 198 int32_t total_size = (int32_t)raw_size;
1033  
1034 /* Compact wbuf if sent prefix wastes significant space */
1035 198 ▶ if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) { 0 198 198
if (self->wbuf_off > 0 && self->wbuf_off > self->wbuf_len / 2) { 0 0 0
1036 0 self->wbuf_len -= self->wbuf_off;
1037 0 ▶ if (self->wbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_len > 0) 0 0 0
1038 0 memmove(self->wbuf, self->wbuf + self->wbuf_off, self->wbuf_len);
1039 0 self->wbuf_off = 0;
1040 }
1041  
1042 /* Append to wbuf: [size:i32][header][body] */
1043 198 conn_ensure_wbuf(self, self->wbuf_len + 4 + total_size);
1044 {
1045 198 uint32_t sz = htonl((uint32_t)total_size);
1046 198 memcpy(self->wbuf + self->wbuf_len, &sz, 4);
1047 198 self->wbuf_len += 4;
1048 }
1049 198 Copy(hdr.data, self->wbuf + self->wbuf_len, hdr.len, char);
1050 198 self->wbuf_len += hdr.len;
1051 198 Copy(body->data, self->wbuf + self->wbuf_len, body->len, char);
1052 198 self->wbuf_len += body->len;
1053  
1054 198 kf_buf_free(&hdr);
1055  
1056 /* Enqueue callback (skip for no-response requests like acks=0) */
1057 198 ▶ if (!no_response) {
Branch: 1 branch
BranchTrueFalseTotal
if (!no_response) { 197 1 198
1058 ev_kafka_conn_cb_t *cbt;
1059 197 Newxz(cbt, 1, ev_kafka_conn_cb_t);
1060 197 cbt->correlation_id = corr_id;
1061 197 cbt->api_key = api_key;
1062 197 cbt->api_version = api_version;
1063 197 cbt->internal = internal;
1064 197 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 132 65 197
1065 132 cbt->cb = cb;
1066 132 SvREFCNT_inc(cb);
1067 }
1068 197 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1069 197 self->pending_count++;
1070 }
1071  
1072 198 conn_start_writing(self);
1073  
1074 198 ▶ if (no_response && cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (no_response && cb) { 1 197 198
if (no_response && cb) { 1 0 1
1075 /* acks=0: the broker never answers, so fire the callback now with
1076 * an empty success result: it means "handed to the socket", not
1077 * "broker acknowledged". */
1078 1 conn_invoke_cb(aTHX_ self, cb,
1079 1 sv_2mortal(newRV_noinc((SV*)newHV())), NULL);
1080 }
1081  
1082 198 return corr_id;
1083 }
1084  
1085 /* ================================================================
1086 * ApiVersions (API 18)
1087 * ================================================================ */
1088  
1089 60 static void conn_send_api_versions(pTHX_ ev_kafka_conn_t *self) {
1090 kf_buf_t body;
1091 60 kf_buf_init(&body);
1092  
1093 /* ApiVersions v0: empty body, most compatible */
1094 60 self->state = CONN_API_VERSIONS;
1095 60 conn_send_request(aTHX_ self, API_API_VERSIONS, 0, &body, NULL, 1, 0);
1096 60 kf_buf_free(&body);
1097 60 }
1098  
1099 60 static void conn_parse_api_versions_response(pTHX_ ev_kafka_conn_t *self,
1100 const char *data, size_t len)
1101 {
1102 60 const char *p = data;
1103 60 const char *end = data + len;
1104 int i;
1105  
1106 /* Initialize all as unsupported */
1107 3900 ▶ for (i = 0; i < API_VERSIONS_MAX_KEY; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < API_VERSIONS_MAX_KEY; i++) 3840 60 3900
1108 3840 self->api_versions[i] = -1;
1109  
1110 60 ▶ if (end - p < 2) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto err; 0 60 60
1111 60 int16_t error_code = kf_read_i16(p); p += 2;
1112 60 ▶ if (error_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error_code != 0) { 0 60 60
1113 char errbuf[128];
1114 0 snprintf(errbuf, sizeof(errbuf), "ApiVersions error: %d", error_code);
1115 0 conn_emit_error(aTHX_ self, errbuf);
1116 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1117 0 conn_handle_disconnect(aTHX_ self, errbuf);
1118 0 return;
1119 }
1120  
1121 /* v0 response: array(api_key:i16, min_version:i16, max_version:i16) */
1122 60 ▶ if (end - p < 4) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto err; 0 60 60
1123 60 int32_t count = kf_read_i32(p); p += 4;
1124  
1125 796 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 736 60 796
1126 736 ▶ if (end - p < 6) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) goto err; 0 736 736
1127 736 int16_t key = kf_read_i16(p); p += 2;
1128 736 /* int16_t min_version = kf_read_i16(p); */ p += 2;
1129 736 int16_t max_version = kf_read_i16(p); p += 2;
1130  
1131 736 ▶ if (key >= 0 && key < API_VERSIONS_MAX_KEY)
Branch: 2 branches
BranchTrueFalseTotal
if (key >= 0 && key < API_VERSIONS_MAX_KEY) 736 0 736
if (key >= 0 && key < API_VERSIONS_MAX_KEY) 736 0 736
1132 736 self->api_versions[key] = max_version;
1133 }
1134  
1135 60 self->api_versions_known = 1;
1136  
1137 /* Handshake complete (or continue to SASL) */
1138 60 ▶ if (self->sasl_mechanism) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_mechanism) { 4 56 60
1139 4 self->state = CONN_SASL_HANDSHAKE;
1140 4 conn_arm_timer(self); /* re-arm for the SASL phase */
1141 4 conn_send_sasl_handshake(aTHX_ self);
1142 } else {
1143 56 self->state = CONN_READY;
1144 56 conn_disarm_timer(self); /* handshake done */
1145 56 conn_emit_connect(aTHX_ self);
1146 }
1147 60 return;
1148  
1149 0 err:
1150 0 conn_emit_error(aTHX_ self, "malformed ApiVersions response");
1151 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1152 0 conn_handle_disconnect(aTHX_ self, "malformed ApiVersions response");
1153 }
1154  
1155 /* ================================================================
1156 * SASL Handshake (API 17) + Authenticate (API 36)
1157 * ================================================================ */
1158  
1159 4 static void conn_send_sasl_handshake(pTHX_ ev_kafka_conn_t *self) {
1160 kf_buf_t body;
1161 4 kf_buf_init(&body);
1162  
1163 /* SaslHandshake v1: mechanism (STRING) */
1164 4 STRLEN mech_len = strlen(self->sasl_mechanism);
1165 4 kf_buf_append_string(&body, self->sasl_mechanism, (int16_t)mech_len);
1166  
1167 4 self->state = CONN_SASL_HANDSHAKE;
1168 4 conn_send_request(aTHX_ self, API_SASL_HANDSHAKE, 1, &body, NULL, 1, 0);
1169 4 kf_buf_free(&body);
1170 4 }
1171  
1172 4 static void conn_parse_sasl_handshake_response(pTHX_ ev_kafka_conn_t *self,
1173 const char *data, size_t len)
1174 {
1175 4 const char *p = data;
1176 4 const char *end = data + len;
1177  
1178 4 ▶ if (end - p < 2) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto err; 0 4 4
1179 4 int16_t error_code = kf_read_i16(p); p += 2;
1180  
1181 /* skip mechanisms array */
1182 4 ▶ if (end - p < 4) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto err; 0 4 4
1183 4 int32_t count = kf_read_i32(p); p += 4;
1184 int32_t i;
1185 8 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 4 4 8
1186 const char *s; int16_t slen;
1187 4 int n = kf_read_string(p, end, &s, &slen);
1188 4 ▶ if (n < 0) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto err; 0 4 4
1189 4 p += n;
1190 }
1191  
1192 4 ▶ if (error_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error_code != 0) { 0 4 4
1193 0 conn_emit_error(aTHX_ self, "SASL handshake failed: mechanism not supported");
1194 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1195 0 conn_handle_disconnect(aTHX_ self, "SASL handshake failed");
1196 0 return;
1197 }
1198  
1199 /* Proceed to authenticate */
1200 4 conn_arm_timer(self);
1201 4 conn_send_sasl_authenticate(aTHX_ self);
1202 4 return;
1203  
1204 0 err:
1205 0 conn_emit_error(aTHX_ self, "malformed SaslHandshake response");
1206 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1207 0 conn_handle_disconnect(aTHX_ self, "malformed SaslHandshake response");
1208 }
1209  
1210 /* SCRAM state for multi-step SASL */
1211 #define SCRAM_STEP_CLIENT_FIRST 0
1212 #define SCRAM_STEP_CLIENT_FINAL 1
1213 #define SCRAM_STEP_DONE 2
1214  
1215 4 static void conn_send_sasl_authenticate(pTHX_ ev_kafka_conn_t *self) {
1216 kf_buf_t body;
1217 4 kf_buf_init(&body);
1218  
1219 4 ▶ if (self->sasl_mechanism && strcmp(self->sasl_mechanism, "PLAIN") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->sasl_mechanism && strcmp(self->sasl_mechanism, "PLAIN") == 0) { 4 0 4
if (self->sasl_mechanism && strcmp(self->sasl_mechanism, "PLAIN") == 0) { 0 4 4
1220 /* PLAIN: \0username\0password */
1221 0 ▶ STRLEN ulen = self->sasl_username ? strlen(self->sasl_username) : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN ulen = self->sasl_username ? strlen(self->sasl_username) : 0; 0 0 0
1222 0 ▶ STRLEN plen = self->sasl_password ? strlen(self->sasl_password) : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN plen = self->sasl_password ? strlen(self->sasl_password) : 0; 0 0 0
1223 0 int32_t auth_len = 1 + (int32_t)ulen + 1 + (int32_t)plen;
1224  
1225 0 kf_buf_append_i32(&body, auth_len);
1226 0 kf_buf_append_i8(&body, 0);
1227 0 ▶ if (ulen > 0) kf_buf_append(&body, self->sasl_username, ulen);
Branch: 1 branch
BranchTrueFalseTotal
if (ulen > 0) kf_buf_append(&body, self->sasl_username, ulen); 0 0 0
1228 0 kf_buf_append_i8(&body, 0);
1229 0 ▶ if (plen > 0) kf_buf_append(&body, self->sasl_password, plen);
Branch: 1 branch
BranchTrueFalseTotal
if (plen > 0) kf_buf_append(&body, self->sasl_password, plen); 0 0 0
1230 }
1231 #ifdef HAVE_OPENSSL
1232 4 ▶ else if (self->sasl_mechanism &&
Branch: 1 branch
BranchTrueFalseTotal
else if (self->sasl_mechanism && 4 0 4
1233 4 ▶ (strcmp(self->sasl_mechanism, "SCRAM-SHA-256") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(strcmp(self->sasl_mechanism, "SCRAM-SHA-256") == 0 || 1 3 4
1234 2 ▶ strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0)) { 0 1 1
1235 /* SCRAM client-first-message: n,,n=<user>,r=<nonce> */
1236 char nonce[33];
1237 {
1238 unsigned char rnd[16];
1239 int i;
1240 3 RAND_bytes(rnd, 16);
1241 51 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 48 3 51
1242 48 snprintf(nonce + i*2, 3, "%02x", rnd[i]);
1243 3 nonce[32] = '\0';
1244 }
1245  
1246 /* save nonce for later steps */
1247 3 ▶ if (self->scram_nonce) Safefree(self->scram_nonce);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_nonce) Safefree(self->scram_nonce); 0 3 3
1248 3 self->scram_nonce = savepv(nonce);
1249 3 self->scram_step = SCRAM_STEP_CLIENT_FIRST;
1250  
1251 3 ▶ if (!self->sasl_username) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->sasl_username) { 2 1 3
1252 2 conn_emit_error(aTHX_ self, "SCRAM: username required");
1253 2 kf_buf_free(&body);
1254 4 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 2 2
1255 2 conn_handle_disconnect(aTHX_ self, "SCRAM: username required");
1256 2 return;
1257 }
1258 kf_buf_t msg;
1259 1 kf_buf_init(&msg);
1260 1 kf_buf_append(&msg, "n,,n=", 5);
1261 1 kf_buf_append(&msg, self->sasl_username, strlen(self->sasl_username));
1262 1 kf_buf_append(&msg, ",r=", 3);
1263 1 kf_buf_append(&msg, nonce, 32);
1264  
1265 /* save client-first-message-bare for later binding */
1266 1 ▶ if (self->scram_client_first) Safefree(self->scram_client_first);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_client_first) Safefree(self->scram_client_first); 0 1 1
1267 /* bare = after "n,," */
1268 1 self->scram_client_first = savepvn(msg.data + 3, msg.len - 3);
1269 1 self->scram_client_first_len = msg.len - 3;
1270  
1271 1 kf_buf_append_i32(&body, (int32_t)msg.len);
1272 1 kf_buf_append(&body, msg.data, msg.len);
1273 1 kf_buf_free(&msg);
1274 }
1275 #endif
1276 else {
1277 1 conn_emit_error(aTHX_ self, "unsupported SASL mechanism");
1278 1 kf_buf_free(&body);
1279 1 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 1 1
1280 1 conn_handle_disconnect(aTHX_ self, "unsupported SASL mechanism");
1281 1 return;
1282 }
1283  
1284 1 self->state = CONN_SASL_AUTH;
1285 {
1286 1 int16_t ver = self->api_versions[API_SASL_AUTHENTICATE];
1287 1 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 1 0 1
1288 1 ▶ if (ver > 2) ver = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 2) ver = 2; 0 1 1
1289 1 conn_send_request(aTHX_ self, API_SASL_AUTHENTICATE, ver, &body, NULL, 1, 0);
1290 }
1291 1 kf_buf_free(&body);
1292 }
1293  
1294 0 static void conn_parse_sasl_authenticate_response(pTHX_ ev_kafka_conn_t *self,
1295 const char *data, size_t len)
1296 {
1297 0 const char *p = data;
1298 0 const char *end = data + len;
1299  
1300 0 ▶ if (end - p < 2) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto err; 0 0 0
1301 0 int16_t error_code = kf_read_i16(p); p += 2;
1302  
1303 0 const char *errmsg_str = NULL;
1304 0 int16_t errmsg_len = 0;
1305 {
1306 0 int n = kf_read_string(p, end, &errmsg_str, &errmsg_len);
1307 0 ▶ if (n < 0) goto err;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto err; 0 0 0
1308 0 p += n;
1309 }
1310  
1311 /* auth_bytes */
1312 0 const char *auth_data = NULL;
1313 0 int32_t auth_data_len = 0;
1314 0 ▶ if (end - p >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 4) { 0 0 0
1315 0 auth_data_len = kf_read_i32(p); p += 4;
1316 0 ▶ if (auth_data_len > 0 && end - p >= auth_data_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (auth_data_len > 0 && end - p >= auth_data_len) { 0 0 0
if (auth_data_len > 0 && end - p >= auth_data_len) { 0 0 0
1317 0 auth_data = p;
1318 0 p += auth_data_len;
1319 }
1320 }
1321  
1322 0 ▶ if (error_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error_code != 0) { 0 0 0
1323 char errbuf[512];
1324 0 ▶ if (errmsg_str && errmsg_len > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (errmsg_str && errmsg_len > 0) 0 0 0
if (errmsg_str && errmsg_len > 0) 0 0 0
1325 0 snprintf(errbuf, sizeof(errbuf), "SASL auth failed: %.*s", (int)errmsg_len, errmsg_str);
1326 else
1327 0 snprintf(errbuf, sizeof(errbuf), "SASL auth failed: error %d", error_code);
1328 0 conn_emit_error(aTHX_ self, errbuf);
1329 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1330 0 conn_handle_disconnect(aTHX_ self, "SASL auth failed");
1331 0 return;
1332 }
1333  
1334 #ifdef HAVE_OPENSSL
1335 /* SCRAM multi-step handling */
1336 0 ▶ if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FIRST && auth_data) {
Branch: 3 branches
BranchTrueFalseTotal
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FIRST && auth_data) { 0 0 0
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FIRST && auth_data) { 0 0 0
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FIRST && auth_data) { 0 0 0
1337 /* Server-first-message: r=<nonce>,s=<salt>,i=<iterations> */
1338 /* Parse server response, compute proof, send client-final */
1339 0 const char *server_nonce = NULL;
1340 0 size_t server_nonce_len = 0;
1341 0 const char *salt_b64 = NULL;
1342 0 size_t salt_b64_len = 0;
1343 0 int iterations = 0;
1344 {
1345 0 const char *sp = auth_data;
1346 0 const char *se = auth_data + auth_data_len;
1347 0 ▶ while (sp < se) {
Branch: 1 branch
BranchTrueFalseTotal
while (sp < se) { 0 0 0
1348 0 ▶ if (sp + 2 <= se && sp[0] == 'r' && sp[1] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
if (sp + 2 <= se && sp[0] == 'r' && sp[1] == '=') { 0 0 0
if (sp + 2 <= se && sp[0] == 'r' && sp[1] == '=') { 0 0 0
if (sp + 2 <= se && sp[0] == 'r' && sp[1] == '=') { 0 0 0
1349 0 sp += 2; server_nonce = sp;
1350 0 ▶ while (sp < se && *sp != ',') sp++;
Branch: 2 branches
BranchTrueFalseTotal
while (sp < se && *sp != ',') sp++; 0 0 0
while (sp < se && *sp != ',') sp++; 0 0 0
1351 0 server_nonce_len = sp - server_nonce;
1352 0 ▶ } else if (sp + 2 <= se && sp[0] == 's' && sp[1] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (sp + 2 <= se && sp[0] == 's' && sp[1] == '=') { 0 0 0
} else if (sp + 2 <= se && sp[0] == 's' && sp[1] == '=') { 0 0 0
} else if (sp + 2 <= se && sp[0] == 's' && sp[1] == '=') { 0 0 0
1353 0 sp += 2; salt_b64 = sp;
1354 0 ▶ while (sp < se && *sp != ',') sp++;
Branch: 2 branches
BranchTrueFalseTotal
while (sp < se && *sp != ',') sp++; 0 0 0
while (sp < se && *sp != ',') sp++; 0 0 0
1355 0 salt_b64_len = sp - salt_b64;
1356 0 ▶ } else if (sp + 2 <= se && sp[0] == 'i' && sp[1] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (sp + 2 <= se && sp[0] == 'i' && sp[1] == '=') { 0 0 0
} else if (sp + 2 <= se && sp[0] == 'i' && sp[1] == '=') { 0 0 0
} else if (sp + 2 <= se && sp[0] == 'i' && sp[1] == '=') { 0 0 0
1357 0 sp += 2;
1358 0 iterations = atoi(sp);
1359 0 ▶ while (sp < se && *sp != ',') sp++;
Branch: 2 branches
BranchTrueFalseTotal
while (sp < se && *sp != ',') sp++; 0 0 0
while (sp < se && *sp != ',') sp++; 0 0 0
1360 }
1361 0 ▶ if (sp < se && *sp == ',') sp++;
Branch: 2 branches
BranchTrueFalseTotal
if (sp < se && *sp == ',') sp++; 0 0 0
if (sp < se && *sp == ',') sp++; 0 0 0
1362 0 else sp++;
1363 }
1364 }
1365  
1366 0 ▶ if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) {
Branch: 4 branches
BranchTrueFalseTotal
if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) { 0 0 0
if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) { 0 0 0
if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) { 0 0 0
if (!server_nonce || !salt_b64 || iterations <= 0 || iterations > 1048576) { 0 0 0
1367 0 conn_emit_error(aTHX_ self, "SCRAM: malformed server-first-message");
1368 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1369 0 conn_handle_disconnect(aTHX_ self, "SCRAM auth failed");
1370 0 return;
1371 }
1372  
1373 /* RFC 5802: server nonce must start with client nonce */
1374 0 ▶ if (server_nonce_len < 32 ||
Branch: 1 branch
BranchTrueFalseTotal
if (server_nonce_len < 32 || 0 0 0
1375 0 ▶ memcmp(server_nonce, self->scram_nonce, 32) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
memcmp(server_nonce, self->scram_nonce, 32) != 0) { 0 0 0
1376 0 conn_emit_error(aTHX_ self, "SCRAM: server nonce mismatch");
1377 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1378 0 conn_handle_disconnect(aTHX_ self, "SCRAM auth failed");
1379 0 return;
1380 }
1381  
1382 0 int is_sha512 = (strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0);
1383 0 ▶ const EVP_MD *md = is_sha512 ? EVP_sha512() : EVP_sha256();
Branch: 1 branch
BranchTrueFalseTotal
const EVP_MD *md = is_sha512 ? EVP_sha512() : EVP_sha256(); 0 0 0
1384 0 ▶ int digest_len = is_sha512 ? 64 : 32;
Branch: 1 branch
BranchTrueFalseTotal
int digest_len = is_sha512 ? 64 : 32; 0 0 0
1385  
1386 /* Decode salt from base64 */
1387 unsigned char salt[128];
1388 int salt_len;
1389 {
1390 0 BIO *b64 = BIO_new(BIO_f_base64());
1391 0 BIO *bmem = BIO_new_mem_buf(salt_b64, (int)salt_b64_len);
1392 0 bmem = BIO_push(b64, bmem);
1393 0 BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
1394 0 salt_len = BIO_read(bmem, salt, sizeof(salt));
1395 0 BIO_free_all(bmem);
1396 0 ▶ if (salt_len <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (salt_len <= 0) { 0 0 0
1397 0 conn_emit_error(aTHX_ self, "SCRAM: bad salt");
1398 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1399 0 conn_handle_disconnect(aTHX_ self, "SCRAM auth failed");
1400 0 return;
1401 }
1402 }
1403  
1404 /* SaltedPassword = Hi(password, salt, iterations) using PBKDF2 */
1405 unsigned char salted_password[64];
1406 0 PKCS5_PBKDF2_HMAC(self->sasl_password, strlen(self->sasl_password),
1407 salt, salt_len, iterations, md, digest_len, salted_password);
1408  
1409 /* ClientKey = HMAC(SaltedPassword, "Client Key") */
1410 unsigned char client_key[64];
1411 0 unsigned int ck_len = digest_len;
1412 0 HMAC(md, salted_password, digest_len,
1413 (unsigned char *)"Client Key", 10, client_key, &ck_len);
1414  
1415 /* ServerKey = HMAC(SaltedPassword, "Server Key") — saved for
1416 * server-final-message verification. */
1417 0 unsigned int sk_hmac_len = digest_len;
1418 0 HMAC(md, salted_password, digest_len,
1419 (unsigned char *)"Server Key", 10,
1420 0 self->scram_server_key, &sk_hmac_len);
1421 0 self->scram_server_key_len = (int)sk_hmac_len;
1422  
1423 /* StoredKey = H(ClientKey) */
1424 unsigned char stored_key[64];
1425 {
1426 0 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1427 unsigned int sk_len;
1428 0 EVP_DigestInit_ex(ctx, md, NULL);
1429 0 EVP_DigestUpdate(ctx, client_key, digest_len);
1430 0 EVP_DigestFinal_ex(ctx, stored_key, &sk_len);
1431 0 EVP_MD_CTX_free(ctx);
1432 }
1433  
1434 /* AuthMessage = client-first-bare + "," + server-first + "," + client-final-without-proof */
1435 0 char channel_binding_b64[] = "biws"; /* base64("n,,") */
1436 kf_buf_t auth_msg;
1437 0 kf_buf_init(&auth_msg);
1438 0 kf_buf_append(&auth_msg, self->scram_client_first, self->scram_client_first_len);
1439 0 kf_buf_append(&auth_msg, ",", 1);
1440 0 kf_buf_append(&auth_msg, auth_data, auth_data_len);
1441 0 kf_buf_append(&auth_msg, ",c=", 3);
1442 0 kf_buf_append(&auth_msg, channel_binding_b64, 4);
1443 0 kf_buf_append(&auth_msg, ",r=", 3);
1444 0 kf_buf_append(&auth_msg, server_nonce, server_nonce_len);
1445  
1446 /* ClientSignature = HMAC(StoredKey, AuthMessage) */
1447 unsigned char client_sig[64];
1448 0 unsigned int cs_len = digest_len;
1449 0 HMAC(md, stored_key, digest_len,
1450 0 (unsigned char *)auth_msg.data, auth_msg.len, client_sig, &cs_len);
1451  
1452 /* ClientProof = ClientKey XOR ClientSignature */
1453 unsigned char proof[64];
1454 int di;
1455 0 ▶ for (di = 0; di < digest_len; di++)
Branch: 1 branch
BranchTrueFalseTotal
for (di = 0; di < digest_len; di++) 0 0 0
1456 0 proof[di] = client_key[di] ^ client_sig[di];
1457  
1458 /* Save AuthMessage for server-signature verification at step 2. */
1459 0 ▶ if (self->scram_auth_message) Safefree(self->scram_auth_message);
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_auth_message) Safefree(self->scram_auth_message); 0 0 0
1460 0 self->scram_auth_message = savepvn(auth_msg.data, auth_msg.len);
1461 0 self->scram_auth_message_len = auth_msg.len;
1462  
1463 0 kf_buf_free(&auth_msg);
1464  
1465 /* Base64 encode proof */
1466 char proof_b64[256];
1467 {
1468 0 BIO *b64 = BIO_new(BIO_f_base64());
1469 0 BIO *bmem = BIO_new(BIO_s_mem());
1470 0 b64 = BIO_push(b64, bmem);
1471 0 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1472 0 BIO_write(b64, proof, digest_len);
1473 0 BIO_flush(b64);
1474 BUF_MEM *bptr;
1475 0 BIO_get_mem_ptr(b64, &bptr);
1476 0 ▶ int plen = bptr->length < 255 ? (int)bptr->length : 255;
Branch: 1 branch
BranchTrueFalseTotal
int plen = bptr->length < 255 ? (int)bptr->length : 255; 0 0 0
1477 0 memcpy(proof_b64, bptr->data, plen);
1478 0 proof_b64[plen] = '\0';
1479 0 BIO_free_all(b64);
1480 }
1481  
1482 /* Build client-final-message: c=biws,r=<server_nonce>,p=<proof> */
1483 kf_buf_t final_msg;
1484 0 kf_buf_init(&final_msg);
1485 0 kf_buf_append(&final_msg, "c=", 2);
1486 0 kf_buf_append(&final_msg, channel_binding_b64, 4);
1487 0 kf_buf_append(&final_msg, ",r=", 3);
1488 0 kf_buf_append(&final_msg, server_nonce, server_nonce_len);
1489 0 kf_buf_append(&final_msg, ",p=", 3);
1490 0 kf_buf_append(&final_msg, proof_b64, strlen(proof_b64));
1491  
1492 /* Send client-final via SaslAuthenticate */
1493 kf_buf_t body;
1494 0 kf_buf_init(&body);
1495 0 kf_buf_append_i32(&body, (int32_t)final_msg.len);
1496 0 kf_buf_append(&body, final_msg.data, final_msg.len);
1497  
1498 0 self->scram_step = SCRAM_STEP_CLIENT_FINAL;
1499 {
1500 0 int16_t ver = self->api_versions[API_SASL_AUTHENTICATE];
1501 0 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 0 0
1502 0 ▶ if (ver > 2) ver = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 2) ver = 2; 0 0 0
1503 0 conn_send_request(aTHX_ self, API_SASL_AUTHENTICATE, ver, &body, NULL, 1, 0);
1504 }
1505 0 kf_buf_free(&body);
1506 0 kf_buf_free(&final_msg);
1507 0 return;
1508 }
1509  
1510 0 ▶ if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FINAL) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FINAL) { 0 0 0
if (self->sasl_mechanism && self->scram_step == SCRAM_STEP_CLIENT_FINAL) { 0 0 0
1511 /* Server-final-message: v=<base64(ServerSignature)>
1512 * ServerSignature = HMAC(ServerKey, AuthMessage) — RFC 5802. */
1513 0 ▶ if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') {
Branch: 4 branches
BranchTrueFalseTotal
if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') { 0 0 0
if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') { 0 0 0
if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') { 0 0 0
if (!auth_data || auth_data_len < 2 || auth_data[0] != 'v' || auth_data[1] != '=') { 0 0 0
1514 0 conn_emit_error(aTHX_ self, "SCRAM: missing server signature");
1515 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1516 0 conn_handle_disconnect(aTHX_ self, "SCRAM verify failed");
1517 0 return;
1518 }
1519 0 const char *recv_b64 = auth_data + 2;
1520 0 int recv_b64_len = (int)(auth_data_len - 2);
1521  
1522 /* Compute expected server signature. */
1523 0 const EVP_MD *md = NULL;
1524 0 ▶ if (strcmp(self->sasl_mechanism, "SCRAM-SHA-256") == 0) md = EVP_sha256();
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(self->sasl_mechanism, "SCRAM-SHA-256") == 0) md = EVP_sha256(); 0 0 0
1525 0 ▶ else if (strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0) md = EVP_sha512();
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(self->sasl_mechanism, "SCRAM-SHA-512") == 0) md = EVP_sha512(); 0 0 0
1526 0 ▶ if (!md || self->scram_server_key_len <= 0 || !self->scram_auth_message) {
Branch: 3 branches
BranchTrueFalseTotal
if (!md || self->scram_server_key_len <= 0 || !self->scram_auth_message) { 0 0 0
if (!md || self->scram_server_key_len <= 0 || !self->scram_auth_message) { 0 0 0
if (!md || self->scram_server_key_len <= 0 || !self->scram_auth_message) { 0 0 0
1527 0 conn_emit_error(aTHX_ self, "SCRAM: missing state for verification");
1528 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1529 0 conn_handle_disconnect(aTHX_ self, "SCRAM verify failed");
1530 0 return;
1531 }
1532 unsigned char expected[64];
1533 0 unsigned int expected_len = (unsigned int)self->scram_server_key_len;
1534 0 HMAC(md, self->scram_server_key, self->scram_server_key_len,
1535 0 (unsigned char *)self->scram_auth_message,
1536 self->scram_auth_message_len, expected, &expected_len);
1537  
1538 /* Decode the received base64 signature. */
1539 unsigned char recv_sig[64];
1540 0 int recv_sig_len = 0;
1541 {
1542 0 BIO *b64 = BIO_new(BIO_f_base64());
1543 0 BIO *bmem = BIO_new_mem_buf(recv_b64, recv_b64_len);
1544 0 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
1545 0 b64 = BIO_push(b64, bmem);
1546 0 recv_sig_len = BIO_read(b64, recv_sig, (int)sizeof(recv_sig));
1547 0 BIO_free_all(b64);
1548 }
1549  
1550 0 int ok = (recv_sig_len == (int)expected_len)
1551 0 ▶ && (CRYPTO_memcmp(recv_sig, expected, expected_len) == 0);
Branch: 2 branches
BranchTrueFalseTotal
&& (CRYPTO_memcmp(recv_sig, expected, expected_len) == 0); 0 0 0
&& (CRYPTO_memcmp(recv_sig, expected, expected_len) == 0); 0 0 0
1552  
1553 /* Wipe ServerKey/AuthMessage now that verification is done. */
1554 0 OPENSSL_cleanse(self->scram_server_key, sizeof(self->scram_server_key));
1555 0 self->scram_server_key_len = 0;
1556 0 ▶ if (self->scram_auth_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->scram_auth_message) { 0 0 0
1557 0 OPENSSL_cleanse(self->scram_auth_message,
1558 self->scram_auth_message_len);
1559 0 Safefree(self->scram_auth_message);
1560 0 self->scram_auth_message = NULL;
1561 0 self->scram_auth_message_len = 0;
1562 }
1563  
1564 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
1565 0 conn_emit_error(aTHX_ self, "SCRAM: server signature mismatch");
1566 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1567 0 conn_handle_disconnect(aTHX_ self, "SCRAM verify failed");
1568 0 return;
1569 }
1570 0 self->scram_step = SCRAM_STEP_DONE;
1571 /* fall through to CONN_READY */
1572 }
1573 #endif
1574  
1575 #ifdef HAVE_OPENSSL
1576 /* SCRAM configured but the exchange never completed: no server
1577 * signature was verified — refuse. */
1578 0 ▶ if (self->sasl_mechanism
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_mechanism 0 0 0
1579 0 ▶ && strncmp(self->sasl_mechanism, "SCRAM-", 6) == 0
Branch: 1 branch
BranchTrueFalseTotal
&& strncmp(self->sasl_mechanism, "SCRAM-", 6) == 0 0 0 0
1580 0 ▶ && self->scram_step != SCRAM_STEP_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
&& self->scram_step != SCRAM_STEP_DONE) { 0 0 0
1581 0 conn_emit_error(aTHX_ self, "SASL: incomplete SCRAM exchange");
1582 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1583 0 conn_handle_disconnect(aTHX_ self, "SASL auth failed");
1584 0 return;
1585 }
1586 #endif
1587  
1588 /* Auth success — connection is ready */
1589 0 self->state = CONN_READY;
1590 0 conn_disarm_timer(self); /* handshake done */
1591 0 conn_emit_connect(aTHX_ self);
1592 0 return;
1593  
1594 0 err:
1595 0 conn_emit_error(aTHX_ self, "malformed SaslAuthenticate response");
1596 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
1597 0 conn_handle_disconnect(aTHX_ self, "malformed SaslAuthenticate response");
1598 }
1599  
1600 /* ================================================================
1601 * Response dispatch
1602 * ================================================================ */
1603  
1604 178 static void conn_dispatch_response(pTHX_ ev_kafka_conn_t *self,
1605 ev_kafka_conn_cb_t *cbt, const char *data, size_t len)
1606 {
1607 /* Internal handshake responses */
1608 178 ▶ if (cbt->internal) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->internal) { 64 114 178
1609 64 switch (cbt->api_key) {
1610 60 case API_API_VERSIONS:
1611 60 conn_parse_api_versions_response(aTHX_ self, data, len);
1612 60 break;
1613 4 case API_SASL_HANDSHAKE:
1614 4 conn_parse_sasl_handshake_response(aTHX_ self, data, len);
1615 4 break;
1616 0 case API_SASL_AUTHENTICATE:
1617 0 conn_parse_sasl_authenticate_response(aTHX_ self, data, len);
1618 0 break;
1619 0 default: break;
1620 }
1621 64 return;
1622 }
1623  
1624 /* User-facing responses: parse and invoke callback */
1625 114 ▶ if (!cbt->cb) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->cb) return; 0 114 114
1626  
1627 114 switch (cbt->api_key) {
1628 41 case API_METADATA: {
1629 /* Parse metadata response and return as Perl hash */
1630 41 SV *result = conn_parse_metadata_response(aTHX_ self, cbt->api_version, data, len);
1631 41 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1632 41 break;
1633 }
1634 14 case API_PRODUCE: {
1635 14 SV *result = conn_parse_produce_response(aTHX_ self, cbt->api_version, data, len);
1636 14 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1637 14 break;
1638 }
1639 12 case API_FETCH: {
1640 12 SV *result = conn_parse_fetch_response(aTHX_ self, cbt->api_version, data, len);
1641 12 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1642 12 break;
1643 }
1644 4 case API_LIST_OFFSETS: {
1645 4 SV *result = conn_parse_list_offsets_response(aTHX_ self, cbt->api_version, data, len);
1646 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1647 4 break;
1648 }
1649 9 case API_FIND_COORDINATOR: {
1650 9 SV *result = conn_parse_find_coordinator_response(aTHX_ self, cbt->api_version, data, len);
1651 9 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1652 9 break;
1653 }
1654 4 case API_JOIN_GROUP: {
1655 4 SV *result = conn_parse_join_group_response(aTHX_ self, cbt->api_version, data, len);
1656 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1657 4 break;
1658 }
1659 4 case API_SYNC_GROUP: {
1660 4 SV *result = conn_parse_sync_group_response(aTHX_ self, cbt->api_version, data, len);
1661 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1662 4 break;
1663 }
1664 1 case API_HEARTBEAT: {
1665 1 SV *result = conn_parse_heartbeat_response(aTHX_ self, cbt->api_version, data, len);
1666 1 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1667 1 break;
1668 }
1669 2 case API_OFFSET_COMMIT: {
1670 2 SV *result = conn_parse_offset_commit_response(aTHX_ self, cbt->api_version, data, len);
1671 2 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1672 2 break;
1673 }
1674 4 case API_OFFSET_FETCH: {
1675 4 SV *result = conn_parse_offset_fetch_response(aTHX_ self, cbt->api_version, data, len);
1676 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1677 4 break;
1678 }
1679 1 case API_LEAVE_GROUP: {
1680 1 SV *result = conn_parse_leave_group_response(aTHX_ self, cbt->api_version, data, len);
1681 1 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1682 1 break;
1683 }
1684 0 case API_CREATE_TOPICS: {
1685 0 SV *result = conn_parse_create_topics_response(aTHX_ self, cbt->api_version, data, len);
1686 0 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1687 0 break;
1688 }
1689 0 case API_DELETE_TOPICS: {
1690 0 SV *result = conn_parse_delete_topics_response(aTHX_ self, cbt->api_version, data, len);
1691 0 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1692 0 break;
1693 }
1694 10 case API_INIT_PRODUCER_ID: {
1695 10 SV *result = conn_parse_init_producer_id_response(aTHX_ self, cbt->api_version, data, len);
1696 10 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1697 10 break;
1698 }
1699 4 case API_ADD_PARTITIONS_TXN: {
1700 4 SV *result = conn_parse_add_partitions_to_txn_response(aTHX_ self, cbt->api_version, data, len);
1701 4 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1702 4 break;
1703 }
1704 2 case API_END_TXN: {
1705 2 SV *result = conn_parse_end_txn_response(aTHX_ self, cbt->api_version, data, len);
1706 2 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1707 2 break;
1708 }
1709 2 case API_TXN_OFFSET_COMMIT: {
1710 2 SV *result = conn_parse_txn_offset_commit_response(aTHX_ self, cbt->api_version, data, len);
1711 2 conn_invoke_cb(aTHX_ self, cbt->cb, result, NULL);
1712 2 break;
1713 }
1714 0 default: {
1715 /* Unknown API — return raw bytes */
1716 0 SV *result = newSVpvn(data, len);
1717 0 conn_invoke_cb(aTHX_ self, cbt->cb, sv_2mortal(result), NULL);
1718 0 break;
1719 }
1720 }
1721 }
1722  
1723 /* ================================================================
1724 * RecordBatch encoder (for Produce requests)
1725 * ================================================================ */
1726  
1727 /* Build a single Record (within a RecordBatch).
1728 * Format: length(varint), attributes(i8=0), timestampDelta(varint),
1729 * offsetDelta(varint), key(varint-bytes), value(varint-bytes),
1730 * headers(varint array of {key:varint-str, value:varint-bytes})
1731 */
1732 168 static void kf_encode_record(kf_buf_t *b, int offset_delta, int64_t ts_delta,
1733 const char *key, STRLEN key_len, const char *value, STRLEN value_len,
1734 HV *headers)
1735 {
1736 kf_buf_t rec;
1737 168 kf_buf_init(&rec);
1738  
1739 168 kf_buf_append_i8(&rec, 0); /* attributes */
1740 168 kf_buf_append_varint(&rec, ts_delta);
1741 168 kf_buf_append_varint(&rec, offset_delta);
1742  
1743 /* key */
1744 168 ▶ if (key) {
Branch: 1 branch
BranchTrueFalseTotal
if (key) { 167 1 168
1745 167 kf_buf_append_varint(&rec, (int64_t)key_len);
1746 167 kf_buf_append(&rec, key, key_len);
1747 } else {
1748 1 kf_buf_append_varint(&rec, -1);
1749 }
1750  
1751 /* value */
1752 168 ▶ if (value) {
Branch: 1 branch
BranchTrueFalseTotal
if (value) { 167 1 168
1753 167 kf_buf_append_varint(&rec, (int64_t)value_len);
1754 167 kf_buf_append(&rec, value, value_len);
1755 } else {
1756 1 kf_buf_append_varint(&rec, -1);
1757 }
1758  
1759 /* headers */
1760 169 ▶ if (headers && HvUSEDKEYS(headers) > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (headers && HvUSEDKEYS(headers) > 0) { 1 167 168
if (headers && HvUSEDKEYS(headers) > 0) { 0 1 1
if (headers && HvUSEDKEYS(headers) > 0) { 1 0 1
1761 1 ▶ kf_buf_append_varint(&rec, (int64_t)HvUSEDKEYS(headers));
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_varint(&rec, (int64_t)HvUSEDKEYS(headers)); 0 1 1
1762 HE *entry;
1763 1 hv_iterinit(headers);
1764 3 ▶ while ((entry = hv_iternext(headers))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(headers))) { 2 1 3
1765 I32 hklen;
1766 2 const char *hkey = hv_iterkey(entry, &hklen);
1767 2 SV *hval = hv_iterval(headers, entry);
1768 STRLEN hvlen;
1769 2 const char *hvstr = SvPV(hval, hvlen);
1770  
1771 2 kf_buf_append_varint(&rec, (int64_t)hklen);
1772 2 kf_buf_append(&rec, hkey, hklen);
1773 2 kf_buf_append_varint(&rec, (int64_t)hvlen);
1774 2 kf_buf_append(&rec, hvstr, hvlen);
1775 }
1776 } else {
1777 167 kf_buf_append_varint(&rec, 0); /* 0 headers */
1778 }
1779  
1780 /* Write record: length(varint) + body */
1781 168 kf_buf_append_varint(b, (int64_t)rec.len);
1782 168 kf_buf_append(b, rec.data, rec.len);
1783 168 kf_buf_free(&rec);
1784 168 }
1785  
1786 /* Build a RecordBatch from one or more records, with optional
1787 * compression and producer-ID/transactional state. Caller frees *out. */
1788 40 static void kf_encode_record_batch_multi(pTHX_ kf_buf_t *out,
1789 AV *records_av, int64_t timestamp, int compression,
1790 int64_t producer_id, int16_t producer_epoch, int32_t base_sequence,
1791 int is_transactional)
1792 {
1793 40 SSize_t i, count = av_len(records_av) + 1;
1794  
1795 /* Validate up front so croak() doesn't longjmp over kf_buf_free below. */
1796 208 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 168 40 208
1797 168 SV **elem = av_fetch(records_av, i, 0);
1798 168 ▶ if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 168 0 168
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 168 0 168
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 0 168 168
1799 0 croak("produce_batch: record element must be a hashref");
1800 }
1801  
1802 kf_buf_t records;
1803 40 kf_buf_init(&records);
1804  
1805 208 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 168 40 208
1806 168 SV **elem = av_fetch(records_av, i, 0);
1807 168 HV *rh = (HV*)SvRV(*elem);
1808 168 SV **key_sv = hv_fetch(rh, "key", 3, 0);
1809 168 SV **val_sv = hv_fetch(rh, "value", 5, 0);
1810 168 SV **hdr_sv = hv_fetch(rh, "headers", 7, 0);
1811 168 const char *key = NULL; STRLEN key_len = 0;
1812 168 const char *val = NULL; STRLEN val_len = 0;
1813 168 HV *hdrs = NULL;
1814 335 ▶ if (key_sv && SvOK(*key_sv)) key = SvPV(*key_sv, key_len);
Branch: 2 branches
BranchTrueFalseTotal
if (key_sv && SvOK(*key_sv)) key = SvPV(*key_sv, key_len); 168 0 168
if (key_sv && SvOK(*key_sv)) key = SvPV(*key_sv, key_len); 167 1 168
1815 335 ▶ if (val_sv && SvOK(*val_sv)) val = SvPV(*val_sv, val_len);
Branch: 2 branches
BranchTrueFalseTotal
if (val_sv && SvOK(*val_sv)) val = SvPV(*val_sv, val_len); 168 0 168
if (val_sv && SvOK(*val_sv)) val = SvPV(*val_sv, val_len); 167 1 168
1816 168 ▶ if (hdr_sv && SvROK(*hdr_sv) && SvTYPE(SvRV(*hdr_sv)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (hdr_sv && SvROK(*hdr_sv) && SvTYPE(SvRV(*hdr_sv)) == SVt_PVHV) 1 167 168
if (hdr_sv && SvROK(*hdr_sv) && SvTYPE(SvRV(*hdr_sv)) == SVt_PVHV) 1 0 1
if (hdr_sv && SvROK(*hdr_sv) && SvTYPE(SvRV(*hdr_sv)) == SVt_PVHV) 1 0 1
1817 1 hdrs = (HV*)SvRV(*hdr_sv);
1818 168 kf_encode_record(&records, (int)i, 0, key, key_len, val, val_len, hdrs);
1819 }
1820  
1821 kf_buf_t inner;
1822 40 kf_buf_init(&inner);
1823  
1824 40 int16_t attrs = (int16_t)(compression & 0x07);
1825 40 ▶ if (is_transactional) attrs |= 0x10; /* bit 4 = isTransactional */
Branch: 1 branch
BranchTrueFalseTotal
if (is_transactional) attrs |= 0x10; /* bit 4 = isTransactional */ 3 37 40
1826 40 kf_buf_append_i16(&inner, attrs);
1827 40 kf_buf_append_i32(&inner, (int32_t)(count - 1)); /* lastOffsetDelta */
1828 40 kf_buf_append_i64(&inner, timestamp);
1829 40 kf_buf_append_i64(&inner, timestamp);
1830 40 kf_buf_append_i64(&inner, producer_id);
1831 40 kf_buf_append_i16(&inner, producer_epoch);
1832 40 kf_buf_append_i32(&inner, base_sequence);
1833  
1834 #ifdef HAVE_LZ4
1835 if (compression == COMPRESS_LZ4) {
1836 /* Kafka's wire format is LZ4 FRAME (KIP-57), not the raw block API. */
1837 LZ4F_compressionContext_t cctx;
1838 size_t bound = LZ4F_compressFrameBound(records.len, NULL);
1839 char *compressed;
1840 Newx(compressed, bound, char);
1841 size_t clen = 0;
1842 if (!LZ4F_isError(LZ4F_createCompressionContext(&cctx, LZ4F_VERSION))) {
1843 size_t n = LZ4F_compressBegin(cctx, compressed, bound, NULL);
1844 if (!LZ4F_isError(n)) {
1845 clen += n;
1846 n = LZ4F_compressUpdate(cctx, compressed + clen, bound - clen,
1847 records.data, records.len, NULL);
1848 if (!LZ4F_isError(n)) {
1849 clen += n;
1850 n = LZ4F_compressEnd(cctx, compressed + clen, bound - clen,
1851 NULL);
1852 if (!LZ4F_isError(n))
1853 clen += n;
1854 else
1855 clen = 0;
1856 } else clen = 0;
1857 }
1858 LZ4F_freeCompressionContext(cctx);
1859 }
1860 if (clen > 0) {
1861 kf_buf_append_i32(&inner, (int32_t)count);
1862 kf_buf_append(&inner, compressed, clen);
1863 } else {
1864 /* compression failed: fall back to uncompressed. Clear only the
1865 * compression bits (0-2) in the big-endian attrs (low byte, at
1866 * offset 1), preserving isTransactional (bit 4) and other flags. */
1867 inner.data[1] &= (char)~0x07;
1868 kf_buf_append_i32(&inner, (int32_t)count);
1869 kf_buf_append(&inner, records.data, records.len);
1870 }
1871 Safefree(compressed);
1872 } else
1873 #endif
1874 #ifdef HAVE_ZLIB
1875 40 ▶ if (compression == COMPRESS_GZIP) {
Branch: 1 branch
BranchTrueFalseTotal
if (compression == COMPRESS_GZIP) { 2 38 40
1876 2 size_t dest_cap = compressBound((uLong)records.len) + 32;
1877 char *compressed;
1878 2 Newx(compressed, dest_cap, char);
1879 z_stream zs;
1880 2 Zero(&zs, 1, z_stream);
1881 2 int zinit = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
1882 MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY);
1883 2 int zok = 0;
1884 2 ▶ if (zinit == Z_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (zinit == Z_OK) { 2 0 2
1885 2 zs.next_in = (Bytef *)records.data;
1886 2 zs.avail_in = (uInt)records.len;
1887 2 zs.next_out = (Bytef *)compressed;
1888 2 zs.avail_out = (uInt)dest_cap;
1889 2 ▶ if (deflate(&zs, Z_FINISH) == Z_STREAM_END) zok = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (deflate(&zs, Z_FINISH) == Z_STREAM_END) zok = 1; 2 0 2
1890 2 deflateEnd(&zs);
1891 }
1892 2 ▶ if (zok) {
Branch: 1 branch
BranchTrueFalseTotal
if (zok) { 2 0 2
1893 2 kf_buf_append_i32(&inner, (int32_t)count);
1894 2 kf_buf_append(&inner, compressed, zs.total_out);
1895 } else {
1896 /* compression failed: fall back to uncompressed. Clear only the
1897 * compression bits (0-2) in the big-endian attrs (low byte, at
1898 * offset 1), preserving isTransactional (bit 4) and other flags. */
1899 0 inner.data[1] &= (char)~0x07;
1900 0 kf_buf_append_i32(&inner, (int32_t)count);
1901 0 kf_buf_append(&inner, records.data, records.len);
1902 }
1903 2 Safefree(compressed);
1904 } else
1905 #endif
1906 #ifdef HAVE_ZSTD
1907 if (compression == COMPRESS_ZSTD) {
1908 size_t dest_cap = ZSTD_compressBound(records.len);
1909 char *compressed;
1910 Newx(compressed, dest_cap, char);
1911 size_t clen = ZSTD_compress(compressed, dest_cap,
1912 records.data, records.len, ZSTD_CLEVEL_DEFAULT);
1913 if (!ZSTD_isError(clen)) {
1914 kf_buf_append_i32(&inner, (int32_t)count);
1915 kf_buf_append(&inner, compressed, clen);
1916 } else {
1917 /* compression failed: fall back to uncompressed. Clear only the
1918 * compression bits (0-2) in the big-endian attrs (low byte, at
1919 * offset 1), preserving isTransactional (bit 4) and other flags. */
1920 inner.data[1] &= (char)~0x07;
1921 kf_buf_append_i32(&inner, (int32_t)count);
1922 kf_buf_append(&inner, records.data, records.len);
1923 }
1924 Safefree(compressed);
1925 } else
1926 #endif
1927 #ifdef HAVE_SNAPPY
1928 if (compression == COMPRESS_SNAPPY) {
1929 size_t dest_cap = snappy_max_compressed_length(records.len);
1930 char *compressed;
1931 Newx(compressed, dest_cap, char);
1932 size_t clen = dest_cap;
1933 if (snappy_compress(records.data, records.len, compressed, &clen)
1934 == SNAPPY_OK) {
1935 kf_buf_append_i32(&inner, (int32_t)count);
1936 kf_buf_append(&inner, compressed, clen);
1937 } else {
1938 /* compression failed: fall back to uncompressed. Clear only the
1939 * compression bits (0-2) in the big-endian attrs (low byte, at
1940 * offset 1), preserving isTransactional (bit 4) and other flags. */
1941 inner.data[1] &= (char)~0x07;
1942 kf_buf_append_i32(&inner, (int32_t)count);
1943 kf_buf_append(&inner, records.data, records.len);
1944 }
1945 Safefree(compressed);
1946 } else
1947 #endif
1948 {
1949 /* Requested codec not built in (or COMPRESS_NONE): emit uncompressed
1950 * and clear the compression bits so the batch is not mislabeled. */
1951 38 ▶ if (compression != COMPRESS_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (compression != COMPRESS_NONE) 8 30 38
1952 8 inner.data[1] &= (char)~0x07;
1953 38 kf_buf_append_i32(&inner, (int32_t)count);
1954 38 kf_buf_append(&inner, records.data, records.len);
1955 }
1956  
1957 40 uint32_t crc_val = crc32c(inner.data, inner.len);
1958 40 int32_t batch_length = 4 + 1 + 4 + (int32_t)inner.len;
1959  
1960 40 kf_buf_init(out);
1961 40 kf_buf_append_i64(out, 0);
1962 40 kf_buf_append_i32(out, batch_length);
1963 40 kf_buf_append_i32(out, 0);
1964 40 kf_buf_append_i8(out, 2);
1965 40 kf_buf_append_i32(out, (int32_t)crc_val);
1966 40 kf_buf_append(out, inner.data, inner.len);
1967  
1968 40 kf_buf_free(&inner);
1969 40 kf_buf_free(&records);
1970 40 }
1971  
1972 /* ================================================================
1973 * Response parsers
1974 *
1975 * Ownership: every HV/AV is mortalized at birth and adopted by its
1976 * parent with newRV_inc; the root is sv_2mortal(newRV_noinc(root)).
1977 * The per-event SAVETMPS frame in the watcher wrappers reclaims all of
1978 * it, including nodes abandoned at "goto done" on malformed input.
1979 * ================================================================ */
1980  
1981 /* Metadata response parser (API 3) */
1982 52045 static SV* conn_parse_metadata_response(pTHX_ ev_kafka_conn_t *self,
1983 int16_t version, const char *data, size_t len)
1984 {
1985 52045 const char *p = data;
1986 52045 const char *end = data + len;
1987 52045 HV *result = newHV();
1988 52045 AV *brokers_av = (AV*)sv_2mortal((SV*)newAV());
1989 52045 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
1990 int n;
1991  
1992 (void)self;
1993  
1994 /* For v9+ (flexible versions), fields use compact encoding */
1995 52045 int flexible = (version >= 9);
1996  
1997 52045 ▶ if (flexible) {
Branch: 1 branch
BranchTrueFalseTotal
if (flexible) { 52002 43 52045
1998 /* throttle_time_ms */
1999 104004 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 52002 52002
2000 52002 /* int32_t throttle = kf_read_i32(p); */ p += 4;
2001  
2002 /* brokers: compact array */
2003 uint64_t raw;
2004 52002 n = kf_read_uvarint(p, end, &raw);
2005 52002 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 52002 52002
2006 52002 p += n;
2007 52002 int32_t broker_count = (int32_t)(raw - 1);
2008 int32_t i;
2009  
2010 52002 ▶ for (i = 0; i < broker_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < broker_count; i++) { 52002 0 52002
2011 52002 HV *bh = (HV*)sv_2mortal((SV*)newHV());
2012 104004 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 52002 52002
2013 52002 int32_t nid = kf_read_i32(p); p += 4;
2014 52002 hv_store(bh, "node_id", 7, newSViv(nid), 0);
2015  
2016 const char *host; int32_t hlen;
2017 52002 n = kf_read_compact_string(p, end, &host, &hlen);
2018 52002 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 52002 0 52002
2019 0 p += n;
2020 0 ▶ hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 0 0 0
hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 0 0 0
2021  
2022 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2023 0 int32_t port = kf_read_i32(p); p += 4;
2024 0 hv_store(bh, "port", 4, newSViv(port), 0);
2025  
2026 /* rack: compact nullable string */
2027 const char *rack; int32_t rlen;
2028 0 n = kf_read_compact_string(p, end, &rack, &rlen);
2029 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2030 0 p += n;
2031  
2032 /* tagged fields */
2033 0 n = kf_skip_tagged_fields(p, end);
2034 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2035 0 p += n;
2036  
2037 0 av_push(brokers_av, newRV_inc((SV*)bh));
2038 }
2039  
2040 /* cluster_id */
2041 const char *cid; int32_t cidlen;
2042 0 n = kf_read_compact_string(p, end, &cid, &cidlen);
2043 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2044 0 p += n;
2045  
2046 /* controller_id */
2047 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2048 0 int32_t controller_id = kf_read_i32(p); p += 4;
2049 0 hv_store(result, "controller_id", 13, newSViv(controller_id), 0);
2050  
2051 /* topics: compact array */
2052 0 n = kf_read_uvarint(p, end, &raw);
2053 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2054 0 p += n;
2055 0 int32_t topic_count = (int32_t)(raw - 1);
2056  
2057 0 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 0 0 0
2058 0 HV *th = (HV*)sv_2mortal((SV*)newHV());
2059 0 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 0 0
2060 0 int16_t terr = kf_read_i16(p); p += 2;
2061 0 hv_store(th, "error_code", 10, newSViv(terr), 0);
2062  
2063 const char *tname; int32_t tnlen;
2064 0 n = kf_read_compact_string(p, end, &tname, &tnlen);
2065 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2066 0 p += n;
2067 0 ▶ hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 0 0 0
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 0 0 0
2068  
2069 /* topic_id (UUID, 16 bytes) — v10+ */
2070 0 ▶ if (version >= 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 10) { 0 0 0
2071 0 ▶ if (end - p < 16) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 16) goto done; 0 0 0
2072 0 p += 16;
2073 }
2074  
2075 /* is_internal */
2076 0 ▶ if (end - p < 1) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 1) goto done; 0 0 0
2077 0 p += 1;
2078  
2079 /* partitions: compact array */
2080 0 n = kf_read_uvarint(p, end, &raw);
2081 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2082 0 p += n;
2083 0 int32_t part_count = (int32_t)(raw - 1);
2084 0 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2085 int32_t j;
2086  
2087 0 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 0 0 0
2088 0 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2089 0 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 0 0
2090 0 int16_t perr = kf_read_i16(p); p += 2;
2091 0 hv_store(ph, "error_code", 10, newSViv(perr), 0);
2092  
2093 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2094 0 int32_t pid = kf_read_i32(p); p += 4;
2095 0 hv_store(ph, "partition", 9, newSViv(pid), 0);
2096  
2097 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2098 0 int32_t leader = kf_read_i32(p); p += 4;
2099 0 hv_store(ph, "leader", 6, newSViv(leader), 0);
2100  
2101 /* leader_epoch — v7+ */
2102 0 ▶ if (version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 7) { 0 0 0
2103 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2104 0 p += 4;
2105 }
2106  
2107 /* replicas: compact array of i32 */
2108 0 n = kf_read_uvarint(p, end, &raw);
2109 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2110 0 p += n;
2111 0 int32_t rcount = (int32_t)(raw - 1);
2112 0 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
2113 0 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 0 0
2114 0 p += (int64_t)rcount * 4;
2115  
2116 /* isr: compact array of i32 */
2117 0 n = kf_read_uvarint(p, end, &raw);
2118 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2119 0 p += n;
2120 0 rcount = (int32_t)(raw - 1);
2121 0 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
2122 0 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 0 0
2123 0 p += (int64_t)rcount * 4;
2124  
2125 /* offline_replicas: compact array of i32 — v5+ */
2126 0 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 0 0 0
2127 0 n = kf_read_uvarint(p, end, &raw);
2128 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2129 0 p += n;
2130 0 rcount = (int32_t)(raw - 1);
2131 0 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
2132 0 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 0 0
2133 0 p += (int64_t)rcount * 4;
2134 }
2135  
2136 /* tagged fields */
2137 0 n = kf_skip_tagged_fields(p, end);
2138 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2139 0 p += n;
2140  
2141 0 av_push(parts_av, newRV_inc((SV*)ph));
2142 }
2143 0 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2144  
2145 /* topic authorized operations — v8+ */
2146 0 ▶ if (version >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 8) { 0 0 0
2147 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2148 0 p += 4;
2149 }
2150  
2151 /* tagged fields */
2152 0 n = kf_skip_tagged_fields(p, end);
2153 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2154 0 p += n;
2155  
2156 0 av_push(topics_av, newRV_inc((SV*)th));
2157 }
2158 } else {
2159 /* Non-flexible (v0-v8) — use classic STRING/ARRAY encoding */
2160 /* throttle_time_ms (v3+) */
2161 43 ▶ if (version >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 3) { 0 43 43
2162 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2163 0 p += 4;
2164 }
2165  
2166 /* brokers array */
2167 43 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 43 43
2168 43 int32_t broker_count = kf_read_i32(p); p += 4;
2169 43 ▶ if (broker_count < 0 || broker_count > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (broker_count < 0 || broker_count > 65536) goto done; 43 0 43
if (broker_count < 0 || broker_count > 65536) goto done; 0 43 43
2170 int32_t i;
2171 84 ▶ for (i = 0; i < broker_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < broker_count; i++) { 42 42 84
2172 42 HV *bh = (HV*)sv_2mortal((SV*)newHV());
2173 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 1 41 42
2174 41 int32_t nid = kf_read_i32(p); p += 4;
2175 41 hv_store(bh, "node_id", 7, newSViv(nid), 0);
2176  
2177 const char *host; int16_t hlen;
2178 41 n = kf_read_string(p, end, &host, &hlen);
2179 41 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 41 41
2180 41 p += n;
2181 41 ▶ hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 41 0 41
hv_store(bh, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 41 0 41
2182  
2183 41 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 41 41
2184 41 int32_t port = kf_read_i32(p); p += 4;
2185 41 hv_store(bh, "port", 4, newSViv(port), 0);
2186  
2187 /* rack (v1+) */
2188 41 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 40 1 41
2189 const char *r; int16_t rlen;
2190 40 n = kf_read_string(p, end, &r, &rlen);
2191 40 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 40 40
2192 40 p += n;
2193 }
2194  
2195 41 av_push(brokers_av, newRV_inc((SV*)bh));
2196 }
2197  
2198 /* cluster_id (v2+) */
2199 42 ▶ if (version >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 2) { 0 42 42
2200 const char *cid; int16_t cidlen;
2201 0 n = kf_read_string(p, end, &cid, &cidlen);
2202 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2203 0 p += n;
2204 }
2205  
2206 /* controller_id (v1+) */
2207 42 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 41 1 42
2208 41 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 41 41
2209 41 int32_t cid = kf_read_i32(p); p += 4;
2210 41 hv_store(result, "controller_id", 13, newSViv(cid), 0);
2211 }
2212  
2213 /* topics array */
2214 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2215 42 int32_t topic_count = kf_read_i32(p); p += 4;
2216 42 ▶ if (topic_count < 0 || topic_count > 1000000) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (topic_count < 0 || topic_count > 1000000) goto done; 42 0 42
if (topic_count < 0 || topic_count > 1000000) goto done; 0 42 42
2217 83 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 41 42 83
2218 41 HV *th = (HV*)sv_2mortal((SV*)newHV());
2219 41 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 41 41
2220 41 int16_t terr = kf_read_i16(p); p += 2;
2221 41 hv_store(th, "error_code", 10, newSViv(terr), 0);
2222  
2223 const char *tname; int16_t tnlen;
2224 41 n = kf_read_string(p, end, &tname, &tnlen);
2225 41 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 41 41
2226 41 p += n;
2227 41 ▶ hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 41 0 41
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 41 0 41
2228  
2229 /* is_internal (v1+) */
2230 41 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 40 1 41
2231 40 ▶ if (end - p < 1) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 1) goto done; 0 40 40
2232 40 p += 1;
2233 }
2234  
2235 /* partitions */
2236 41 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 41 41
2237 41 int32_t part_count = kf_read_i32(p); p += 4;
2238 41 ▶ if (part_count < 0 || part_count > 1000000) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (part_count < 0 || part_count > 1000000) goto done; 41 0 41
if (part_count < 0 || part_count > 1000000) goto done; 0 41 41
2239 41 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2240 int32_t j;
2241 83 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 42 41 83
2242 42 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2243 42 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 42 42
2244 42 int16_t perr = kf_read_i16(p); p += 2;
2245 42 hv_store(ph, "error_code", 10, newSViv(perr), 0);
2246  
2247 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2248 42 int32_t pid = kf_read_i32(p); p += 4;
2249 42 hv_store(ph, "partition", 9, newSViv(pid), 0);
2250  
2251 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2252 42 int32_t leader = kf_read_i32(p); p += 4;
2253 42 hv_store(ph, "leader", 6, newSViv(leader), 0);
2254  
2255 /* leader_epoch (v7+) */
2256 42 ▶ if (version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 7) { 0 42 42
2257 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2258 0 p += 4;
2259 }
2260  
2261 /* replicas */
2262 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2263 42 int32_t rcount = kf_read_i32(p); p += 4;
2264 42 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 42 0 42
if (rcount < 0 || rcount > 65536) goto done; 0 42 42
2265 42 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 42 42
2266 42 p += (int64_t)rcount * 4;
2267  
2268 /* isr */
2269 42 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 42 42
2270 42 rcount = kf_read_i32(p); p += 4;
2271 42 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 42 0 42
if (rcount < 0 || rcount > 65536) goto done; 0 42 42
2272 42 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 42 42
2273 42 p += (int64_t)rcount * 4;
2274  
2275 /* offline_replicas (v5+) */
2276 42 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 0 42 42
2277 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2278 0 rcount = kf_read_i32(p); p += 4;
2279 0 ▶ if (rcount < 0 || rcount > 65536) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
if (rcount < 0 || rcount > 65536) goto done; 0 0 0
2280 0 ▶ if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < (ptrdiff_t)((int64_t)rcount * 4)) goto done; 0 0 0
2281 0 p += (int64_t)rcount * 4;
2282 }
2283  
2284 42 av_push(parts_av, newRV_inc((SV*)ph));
2285 }
2286 41 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2287  
2288 41 av_push(topics_av, newRV_inc((SV*)th));
2289 }
2290 }
2291  
2292 42 done:
2293 52045 hv_store(result, "brokers", 7, newRV_inc((SV*)brokers_av), 0);
2294 52045 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
2295 52045 return sv_2mortal(newRV_noinc((SV*)result));
2296 }
2297  
2298 /* Produce response parser (API 0, v0-v7) */
2299 15 static SV* conn_parse_produce_response(pTHX_ ev_kafka_conn_t *self,
2300 int16_t version, const char *data, size_t len)
2301 {
2302 15 const char *p = data;
2303 15 const char *end = data + len;
2304 15 HV *result = newHV();
2305 15 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
2306 int n;
2307  
2308 (void)self;
2309  
2310 /* responses: ARRAY */
2311 15 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 15 15
2312 15 int32_t topic_count = kf_read_i32(p); p += 4;
2313 int32_t i;
2314  
2315 30 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 15 15 30
2316 15 HV *th = (HV*)sv_2mortal((SV*)newHV());
2317 const char *tname; int16_t tnlen;
2318 15 n = kf_read_string(p, end, &tname, &tnlen);
2319 15 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 15 15
2320 15 p += n;
2321 15 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 15 0 15
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 15 0 15
2322  
2323 /* partitions: ARRAY */
2324 15 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 15 15
2325 15 int32_t part_count = kf_read_i32(p); p += 4;
2326 15 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2327 int32_t j;
2328  
2329 30 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 15 15 30
2330 15 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2331 15 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 15 15
2332 15 int32_t pid = kf_read_i32(p); p += 4;
2333 15 hv_store(ph, "partition", 9, newSViv(pid), 0);
2334  
2335 15 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 15 15
2336 15 int16_t err = kf_read_i16(p); p += 2;
2337 15 hv_store(ph, "error_code", 10, newSViv(err), 0);
2338  
2339 15 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 15 15
2340 15 int64_t base_offset = kf_read_i64(p); p += 8;
2341 15 hv_store(ph, "base_offset", 11, newSViv(base_offset), 0);
2342  
2343 /* log_append_time (v2+) */
2344 15 ▶ if (version >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 2) { 14 1 15
2345 14 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 14 14
2346 14 p += 8;
2347 }
2348  
2349 /* log_start_offset (v5+) */
2350 15 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 14 1 15
2351 14 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 14 14
2352 14 p += 8;
2353 }
2354  
2355 15 av_push(parts_av, newRV_inc((SV*)ph));
2356 }
2357 15 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2358 15 av_push(topics_av, newRV_inc((SV*)th));
2359 }
2360  
2361 /* throttle_time_ms (v1+) */
2362 15 ▶ if (version >= 1 && end - p >= 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) { 1 14 15
if (version >= 1 && end - p >= 4) { 0 14 14
2363 14 int32_t throttle = kf_read_i32(p); p += 4;
2364 14 hv_store(result, "throttle_time_ms", 16, newSViv(throttle), 0);
2365 }
2366  
2367 1 done:
2368 15 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
2369 15 return sv_2mortal(newRV_noinc((SV*)result));
2370 }
2371  
2372 /* ================================================================
2373 * RecordBatch decoder (for Fetch responses)
2374 * ================================================================ */
2375  
2376 /* Decode records from a RecordBatch, push them as hashrefs onto records_av.
2377 * Returns number of records decoded, or -1 on error. */
2378 21 static int kf_decode_record_batch(pTHX_ const char *data, size_t len,
2379 AV *records_av, int64_t *out_base_offset)
2380 {
2381 21 const char *p = data;
2382 21 const char *end = data + len;
2383 int n;
2384  
2385 21 ▶ if (end - p < 12) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 12) return -1; 0 21 21
2386 21 int64_t base_offset = kf_read_i64(p); p += 8;
2387 21 ▶ if (out_base_offset) *out_base_offset = base_offset;
Branch: 1 branch
BranchTrueFalseTotal
if (out_base_offset) *out_base_offset = base_offset; 21 0 21
2388 21 int32_t batch_length = kf_read_i32(p); p += 4;
2389  
2390 /* compare in 64-bit so a huge batch_length cannot wrap the check */
2391 21 ▶ if (batch_length < 0 || (int64_t)(end - p) < (int64_t)batch_length) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (batch_length < 0 || (int64_t)(end - p) < (int64_t)batch_length) return -1; 21 0 21
if (batch_length < 0 || (int64_t)(end - p) < (int64_t)batch_length) return -1; 1 20 21
2392 20 const char *batch_end = p + batch_length;
2393  
2394 20 ▶ if (batch_end - p < 9) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (batch_end - p < 9) return -1; 0 20 20
2395 20 /* int32_t partition_leader_epoch = kf_read_i32(p); */ p += 4;
2396 20 int8_t magic = (int8_t)*p; p += 1;
2397 20 ▶ if (magic != 2) return -1; /* only support magic=2 (current format) */
Branch: 1 branch
BranchTrueFalseTotal
if (magic != 2) return -1; /* only support magic=2 (current format) */ 1 19 20
2398 19 uint32_t expected_crc = (uint32_t)kf_read_i32(p); p += 4;
2399 /* CRC32C covers the bytes from attributes to end of batch. */
2400 19 ▶ if (crc32c(p, (size_t)(batch_end - p)) != expected_crc) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (crc32c(p, (size_t)(batch_end - p)) != expected_crc) return -1; 1 18 19
2401  
2402 18 ▶ if (batch_end - p < 36) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (batch_end - p < 36) return -1; 0 18 18
2403 18 int16_t attributes = kf_read_i16(p); p += 2;
2404 18 int compression_type = attributes & 0x07;
2405 18 /* int32_t last_offset_delta = kf_read_i32(p); */ p += 4;
2406 18 int64_t first_timestamp = kf_read_i64(p); p += 8;
2407 18 /* int64_t max_timestamp = kf_read_i64(p); */ p += 8;
2408 18 /* int64_t producer_id = kf_read_i64(p); */ p += 8;
2409 18 /* int16_t producer_epoch = kf_read_i16(p); */ p += 2;
2410 18 /* int32_t base_sequence = kf_read_i32(p); */ p += 4;
2411  
2412 18 ▶ if (batch_end - p < 4) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (batch_end - p < 4) return -1; 0 18 18
2413 18 int32_t record_count = kf_read_i32(p); p += 4;
2414  
2415 /* Decompress if needed */
2416 18 const char *rec_data = p;
2417 18 const char *rec_end = batch_end;
2418 18 char *decompressed = NULL;
2419  
2420 18 ▶ if (compression_type != COMPRESS_NONE && batch_end > p) {
Branch: 2 branches
BranchTrueFalseTotal
if (compression_type != COMPRESS_NONE && batch_end > p) { 1 17 18
if (compression_type != COMPRESS_NONE && batch_end > p) { 1 0 1
2421 1 size_t compressed_len = batch_end - p;
2422 1 size_t decomp_cap = compressed_len * 4;
2423 1 ▶ if (decomp_cap < 4096) decomp_cap = 4096;
Branch: 1 branch
BranchTrueFalseTotal
if (decomp_cap < 4096) decomp_cap = 4096; 1 0 1
2424 /* set only by a codec branch that fully succeeded (see below) */
2425 1 int decompressed_ok = 0;
2426  
2427 #ifdef HAVE_ZLIB
2428 1 ▶ if (compression_type == COMPRESS_GZIP) {
Branch: 1 branch
BranchTrueFalseTotal
if (compression_type == COMPRESS_GZIP) { 1 0 1
2429 1 int zok = 0;
2430 2 ▶ while (!zok && decomp_cap < 64 * 1024 * 1024) {
Branch: 2 branches
BranchTrueFalseTotal
while (!zok && decomp_cap < 64 * 1024 * 1024) { 1 1 2
while (!zok && decomp_cap < 64 * 1024 * 1024) { 1 0 1
2431 1 Newx(decompressed, decomp_cap, char);
2432 z_stream zs;
2433 1 Zero(&zs, 1, z_stream);
2434 1 int zinit = inflateInit2(&zs, MAX_WBITS + 16);
2435 1 ▶ if (zinit != Z_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (zinit != Z_OK) { 0 1 1
2436 0 Safefree(decompressed);
2437 0 decompressed = NULL;
2438 0 break;
2439 }
2440 1 zs.next_in = (Bytef *)p;
2441 1 zs.avail_in = (uInt)compressed_len;
2442 1 zs.next_out = (Bytef *)decompressed;
2443 1 zs.avail_out = (uInt)decomp_cap;
2444 1 int zret = inflate(&zs, Z_FINISH);
2445 1 size_t dest_len = zs.total_out;
2446 1 inflateEnd(&zs);
2447 1 ▶ if (zret == Z_STREAM_END) {
Branch: 1 branch
BranchTrueFalseTotal
if (zret == Z_STREAM_END) { 1 0 1
2448 1 rec_data = decompressed;
2449 1 rec_end = decompressed + dest_len;
2450 1 zok = 1;
2451 1 decompressed_ok = 1;
2452 0 ▶ } else if (zret == Z_BUF_ERROR || zret == Z_OK) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (zret == Z_BUF_ERROR || zret == Z_OK) { 0 0 0
} else if (zret == Z_BUF_ERROR || zret == Z_OK) { 0 0 0
2453 0 Safefree(decompressed);
2454 0 decompressed = NULL;
2455 0 decomp_cap *= 2;
2456 } else {
2457 0 Safefree(decompressed);
2458 0 decompressed = NULL;
2459 0 break;
2460 }
2461 }
2462 }
2463 #endif
2464 #ifdef HAVE_LZ4
2465 if (compression_type == COMPRESS_LZ4) {
2466 /* LZ4 FRAME format (KIP-57); the context holds frame state,
2467 * so create a fresh one per attempt. */
2468 while (decomp_cap < 64 * 1024 * 1024) {
2469 LZ4F_decompressionContext_t dctx;
2470 if (LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION)))
2471 break;
2472 Newx(decompressed, decomp_cap, char);
2473 size_t produced = 0, consumed = 0;
2474 int err = 0, done = 0;
2475 while (consumed < compressed_len) {
2476 size_t dst_size = decomp_cap - produced;
2477 size_t src_size = compressed_len - consumed;
2478 size_t r = LZ4F_decompress(dctx,
2479 decompressed + produced, &dst_size,
2480 p + consumed, &src_size, NULL);
2481 produced += dst_size;
2482 consumed += src_size;
2483 if (LZ4F_isError(r)) { err = 1; break; }
2484 if (r == 0) { done = 1; break; } /* frame complete */
2485 if (dst_size == 0 && src_size == 0)
2486 break; /* no progress: output cap hit */
2487 }
2488 LZ4F_freeDecompressionContext(dctx);
2489 if (done) {
2490 /* produced==0 is a validly-empty batch: the record
2491 * loop sees zero records. */
2492 rec_data = decompressed;
2493 rec_end = decompressed + produced;
2494 decompressed_ok = 1;
2495 break;
2496 }
2497 Safefree(decompressed);
2498 decompressed = NULL;
2499 if (err || produced < decomp_cap)
2500 break; /* malformed or truncated frame */
2501 decomp_cap *= 2; /* output cap hit: grow, retry */
2502 }
2503 }
2504 #endif
2505 #ifdef HAVE_ZSTD
2506 if (compression_type == COMPRESS_ZSTD) {
2507 unsigned long long expected =
2508 ZSTD_getFrameContentSize(p, compressed_len);
2509 size_t dlen;
2510 if (expected != ZSTD_CONTENTSIZE_ERROR
2511 && expected != ZSTD_CONTENTSIZE_UNKNOWN
2512 && expected <= 64ULL * 1024 * 1024) {
2513 Newx(decompressed, (size_t)expected, char);
2514 dlen = ZSTD_decompress(decompressed, (size_t)expected,
2515 p, compressed_len);
2516 if (!ZSTD_isError(dlen)) {
2517 rec_data = decompressed;
2518 rec_end = decompressed + dlen;
2519 decompressed_ok = 1;
2520 } else {
2521 Safefree(decompressed);
2522 decompressed = NULL;
2523 }
2524 }
2525 }
2526 #endif
2527 #ifdef HAVE_SNAPPY
2528 if (compression_type == COMPRESS_SNAPPY) {
2529 size_t dlen;
2530 if (snappy_uncompressed_length(p, compressed_len, &dlen)
2531 == SNAPPY_OK
2532 && dlen <= 64UL * 1024 * 1024) {
2533 Newx(decompressed, dlen, char);
2534 if (snappy_uncompress(p, compressed_len,
2535 decompressed, &dlen) == SNAPPY_OK) {
2536 rec_data = decompressed;
2537 rec_end = decompressed + dlen;
2538 decompressed_ok = 1;
2539 } else {
2540 Safefree(decompressed);
2541 decompressed = NULL;
2542 }
2543 }
2544 }
2545 #endif
2546  
2547 1 ▶ if (!decompressed_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!decompressed_ok) { 0 1 1
2548 /* unknown type, codec missing, or decompression failed: do
2549 * NOT parse the compressed bytes as records. */
2550 0 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 0 0 0
2551 0 return -1;
2552 }
2553 }
2554  
2555 18 const char *rp = rec_data;
2556 int32_t i;
2557 163 ▶ for (i = 0; i < record_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < record_count; i++) { 145 18 163
2558 int64_t rec_len;
2559 145 n = kf_read_varint(rp, rec_end, &rec_len);
2560 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2561 145 rp += n;
2562 145 ▶ if (rec_end - rp < rec_len) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (rec_end - rp < rec_len) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (rec_end - rp < rec_len) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2563 145 const char *this_rec_end = rp + rec_len;
2564  
2565 145 ▶ if (this_rec_end - rp < 1) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (this_rec_end - rp < 1) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (this_rec_end - rp < 1) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2566 145 /* int8_t rec_attrs = (int8_t)*rp; */ rp += 1;
2567  
2568 int64_t ts_delta;
2569 145 n = kf_read_varint(rp, this_rec_end, &ts_delta);
2570 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2571 145 rp += n;
2572  
2573 int64_t offset_delta;
2574 145 n = kf_read_varint(rp, this_rec_end, &offset_delta);
2575 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2576 145 rp += n;
2577  
2578 /* key */
2579 int64_t key_len;
2580 145 n = kf_read_varint(rp, this_rec_end, &key_len);
2581 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2582 145 rp += n;
2583 145 const char *key_data = NULL;
2584 145 ▶ if (key_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (key_len >= 0) { 144 1 145
2585 144 ▶ if (this_rec_end - rp < key_len) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (this_rec_end - rp < key_len) { if (decompressed) Safefree(decompressed); return -1; } 0 144 144
if (this_rec_end - rp < key_len) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2586 144 key_data = rp;
2587 144 rp += key_len;
2588 }
2589  
2590 /* value */
2591 int64_t val_len;
2592 145 n = kf_read_varint(rp, this_rec_end, &val_len);
2593 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2594 145 rp += n;
2595 145 const char *val_data = NULL;
2596 145 ▶ if (val_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_len >= 0) { 144 1 145
2597 144 ▶ if (this_rec_end - rp < val_len) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (this_rec_end - rp < val_len) { if (decompressed) Safefree(decompressed); return -1; } 0 144 144
if (this_rec_end - rp < val_len) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2598 144 val_data = rp;
2599 144 rp += val_len;
2600 }
2601  
2602 /* headers */
2603 int64_t hdr_count;
2604 145 n = kf_read_varint(rp, this_rec_end, &hdr_count);
2605 145 ▶ if (n < 0) { if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 145 145
if (n < 0) { if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2606 145 rp += n;
2607 145 HV *hdr_hv = NULL;
2608 145 ▶ if (hdr_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hdr_count > 0) { 1 144 145
2609 1 hdr_hv = newHV();
2610 int64_t h;
2611 3 ▶ for (h = 0; h < hdr_count; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < hdr_count; h++) { 2 1 3
2612 int64_t hk_len;
2613 2 n = kf_read_varint(rp, this_rec_end, &hk_len);
2614 2 ▶ if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 2 2
if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2615 2 rp += n;
2616 2 const char *hk_data = rp;
2617 2 ▶ if (hk_len < 0 || this_rec_end - rp < hk_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; }
Branch: 3 branches
BranchTrueFalseTotal
if (hk_len < 0 || this_rec_end - rp < hk_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 2 0 2
if (hk_len < 0 || this_rec_end - rp < hk_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 2 2
if (hk_len < 0 || this_rec_end - rp < hk_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2618 2 rp += hk_len;
2619  
2620 int64_t hv_len;
2621 2 n = kf_read_varint(rp, this_rec_end, &hv_len);
2622 2 ▶ if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 2 2
if (n < 0) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2623 2 rp += n;
2624 2 const char *hv_data = rp;
2625 2 ▶ if (hv_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_len >= 0) { 2 0 2
2626 2 ▶ if (this_rec_end - rp < hv_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (this_rec_end - rp < hv_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 2 2
if (this_rec_end - rp < hv_len) { SvREFCNT_dec((SV*)hdr_hv); if (decompressed) Safefree(decompressed); return -1; } 0 0 0
2627 2 rp += hv_len;
2628 }
2629  
2630 2 ▶ hv_store(hdr_hv, hk_data, (I32)hk_len,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hdr_hv, hk_data, (I32)hk_len, 2 0 2
2631 hv_len >= 0 ? newSVpvn(hv_data, (STRLEN)hv_len) : newSV(0), 0);
2632 }
2633 }
2634  
2635 145 HV *rec_hv = newHV();
2636 145 hv_store(rec_hv, "offset", 6, newSViv(base_offset + offset_delta), 0);
2637 145 hv_store(rec_hv, "timestamp", 9, newSViv(first_timestamp + ts_delta), 0);
2638 145 ▶ hv_store(rec_hv, "key", 3,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(rec_hv, "key", 3, 144 1 145
2639 key_data ? newSVpvn(key_data, (STRLEN)key_len) : newSV(0), 0);
2640 145 ▶ hv_store(rec_hv, "value", 5,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(rec_hv, "value", 5, 144 1 145
2641 val_data ? newSVpvn(val_data, (STRLEN)val_len) : newSV(0), 0);
2642 145 ▶ if (hdr_hv)
Branch: 1 branch
BranchTrueFalseTotal
if (hdr_hv) 1 144 145
2643 1 hv_store(rec_hv, "headers", 7, newRV_noinc((SV*)hdr_hv), 0);
2644  
2645 145 av_push(records_av, newRV_noinc((SV*)rec_hv));
2646  
2647 145 rp = this_rec_end; /* skip any remaining bytes in the record */
2648 }
2649  
2650 18 ▶ if (decompressed) Safefree(decompressed);
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed) Safefree(decompressed); 1 17 18
2651 18 return record_count;
2652 }
2653  
2654 /* Fetch response parser (API 1, v4-v7 non-flexible) */
2655 13 static SV* conn_parse_fetch_response(pTHX_ ev_kafka_conn_t *self,
2656 int16_t version, const char *data, size_t len)
2657 {
2658 13 const char *p = data;
2659 13 const char *end = data + len;
2660 13 HV *result = newHV();
2661 13 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
2662 int n;
2663  
2664 (void)self;
2665  
2666 /* throttle_time_ms (v1+) */
2667 13 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 13 0 13
2668 13 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 13 13
2669 13 int32_t throttle = kf_read_i32(p); p += 4;
2670 13 hv_store(result, "throttle_time_ms", 16, newSViv(throttle), 0);
2671 }
2672  
2673 /* error_code (v7+) */
2674 13 ▶ if (version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 7) { 11 2 13
2675 11 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 11 11
2676 11 p += 2;
2677 }
2678  
2679 /* session_id (v7+) */
2680 13 ▶ if (version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 7) { 11 2 13
2681 11 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 11 11
2682 11 p += 4;
2683 }
2684  
2685 /* responses: ARRAY */
2686 13 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 13 13
2687 13 int32_t topic_count = kf_read_i32(p); p += 4;
2688 int32_t i;
2689  
2690 25 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 12 13 25
2691 12 HV *th = (HV*)sv_2mortal((SV*)newHV());
2692  
2693 const char *tname; int16_t tnlen;
2694 12 n = kf_read_string(p, end, &tname, &tnlen);
2695 12 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 12 12
2696 12 p += n;
2697 12 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 12 0 12
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 12 0 12
2698  
2699 /* partitions: ARRAY */
2700 12 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 12 12
2701 12 int32_t part_count = kf_read_i32(p); p += 4;
2702 12 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2703 int32_t j;
2704  
2705 24 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 12 12 24
2706 12 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2707  
2708 12 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 12 12
2709 12 int32_t pid = kf_read_i32(p); p += 4;
2710 12 hv_store(ph, "partition", 9, newSViv(pid), 0);
2711  
2712 12 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 12 12
2713 12 int16_t err = kf_read_i16(p); p += 2;
2714 12 hv_store(ph, "error_code", 10, newSViv(err), 0);
2715  
2716 12 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 12 12
2717 12 int64_t hw = kf_read_i64(p); p += 8;
2718 12 hv_store(ph, "high_watermark", 14, newSViv(hw), 0);
2719  
2720 /* last_stable_offset (v4+) */
2721 12 ▶ if (version >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 4) { 12 0 12
2722 12 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 12 12
2723 12 int64_t lso = kf_read_i64(p); p += 8;
2724 12 hv_store(ph, "last_stable_offset", 18, newSViv(lso), 0);
2725 }
2726  
2727 /* log_start_offset (v5+) */
2728 12 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 11 1 12
2729 11 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 11 11
2730 11 p += 8;
2731 }
2732  
2733 /* aborted_transactions (v4+) */
2734 12 ▶ if (version >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 4) { 12 0 12
2735 12 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 12 12
2736 12 int32_t at_count = kf_read_i32(p); p += 4;
2737 int32_t at;
2738 12 ▶ for (at = 0; at < at_count; at++) {
Branch: 1 branch
BranchTrueFalseTotal
for (at = 0; at < at_count; at++) { 0 12 12
2739 0 ▶ if (end - p < 16) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 16) goto done; 0 0 0
2740 0 p += 16; /* producer_id(i64) + first_offset(i64) */
2741 }
2742 }
2743  
2744 /* record_set: BYTES (records data) */
2745 12 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 12 12
2746 12 int32_t records_size = kf_read_i32(p); p += 4;
2747  
2748 12 AV *records_av = (AV*)sv_2mortal((SV*)newAV());
2749 12 ▶ if (records_size > 0 && end - p >= records_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (records_size > 0 && end - p >= records_size) { 6 6 12
if (records_size > 0 && end - p >= records_size) { 6 0 6
2750 6 const char *rp = p;
2751 6 const char *rend = p + records_size;
2752  
2753 /* May contain multiple RecordBatches */
2754 11 ▶ while (rp < rend && rend - rp >= 12) {
Branch: 2 branches
BranchTrueFalseTotal
while (rp < rend && rend - rp >= 12) { 6 5 11
while (rp < rend && rend - rp >= 12) { 6 0 6
2755 int64_t bo;
2756 6 int32_t bl = kf_read_i32(rp + 8);
2757 /* 12 + bl in 64-bit: a bl near INT32_MAX would wrap */
2758 6 ▶ if (bl < 0 || (int64_t)bl > (int64_t)(rend - rp) - 12) break;
Branch: 2 branches
BranchTrueFalseTotal
if (bl < 0 || (int64_t)bl > (int64_t)(rend - rp) - 12) break; 6 0 6
if (bl < 0 || (int64_t)bl > (int64_t)(rend - rp) - 12) break; 5 1 6
2759 5 kf_decode_record_batch(aTHX_ rp, (size_t)12 + (size_t)bl, records_av, &bo);
2760 5 rp += (ptrdiff_t)12 + (ptrdiff_t)bl;
2761 }
2762  
2763 6 p += records_size;
2764 6 ▶ } else if (records_size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (records_size > 0) { 0 6 6
2765 0 p = end; /* truncated */
2766 }
2767  
2768 12 hv_store(ph, "records", 7, newRV_inc((SV*)records_av), 0);
2769 12 av_push(parts_av, newRV_inc((SV*)ph));
2770 }
2771  
2772 12 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2773 12 av_push(topics_av, newRV_inc((SV*)th));
2774 }
2775  
2776 13 done:
2777 13 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
2778 13 return sv_2mortal(newRV_noinc((SV*)result));
2779 }
2780  
2781 /* ListOffsets response parser (API 2, v1+) */
2782 4 static SV* conn_parse_list_offsets_response(pTHX_ ev_kafka_conn_t *self,
2783 int16_t version, const char *data, size_t len)
2784 {
2785 4 const char *p = data;
2786 4 const char *end = data + len;
2787 4 HV *result = newHV();
2788 4 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
2789 int n;
2790  
2791 (void)self;
2792  
2793 /* throttle_time_ms (v2+) */
2794 4 ▶ if (version >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 2) { 0 4 4
2795 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2796 0 p += 4;
2797 }
2798  
2799 /* topics: ARRAY */
2800 4 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 4 4
2801 4 int32_t topic_count = kf_read_i32(p); p += 4;
2802 int32_t i;
2803  
2804 8 ▶ for (i = 0; i < topic_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < topic_count; i++) { 4 4 8
2805 4 HV *th = (HV*)sv_2mortal((SV*)newHV());
2806 const char *tname; int16_t tnlen;
2807 4 n = kf_read_string(p, end, &tname, &tnlen);
2808 4 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 4 4
2809 4 p += n;
2810 4 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 4 0 4
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 4 0 4
2811  
2812 4 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 4 4
2813 4 int32_t part_count = kf_read_i32(p); p += 4;
2814 4 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
2815 int32_t j;
2816  
2817 8 ▶ for (j = 0; j < part_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < part_count; j++) { 4 4 8
2818 4 HV *ph = (HV*)sv_2mortal((SV*)newHV());
2819 4 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 4 4
2820 4 int32_t pid = kf_read_i32(p); p += 4;
2821 4 hv_store(ph, "partition", 9, newSViv(pid), 0);
2822  
2823 4 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 4 4
2824 4 int16_t err = kf_read_i16(p); p += 2;
2825 4 hv_store(ph, "error_code", 10, newSViv(err), 0);
2826  
2827 4 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 4 0 4
2828 4 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 4 4
2829 4 int64_t ts = kf_read_i64(p); p += 8;
2830 4 hv_store(ph, "timestamp", 9, newSViv(ts), 0);
2831 }
2832  
2833 4 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 4 4
2834 4 int64_t offset = kf_read_i64(p); p += 8;
2835 4 hv_store(ph, "offset", 6, newSViv(offset), 0);
2836  
2837 /* leader_epoch (v4+) */
2838 4 ▶ if (version >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 4) { 0 4 4
2839 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2840 0 p += 4;
2841 }
2842  
2843 4 av_push(parts_av, newRV_inc((SV*)ph));
2844 }
2845 4 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
2846 4 av_push(topics_av, newRV_inc((SV*)th));
2847 }
2848  
2849 4 done:
2850 4 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
2851 4 return sv_2mortal(newRV_noinc((SV*)result));
2852 }
2853  
2854 /* FindCoordinator response parser (API 10, v0-v3) */
2855 10 static SV* conn_parse_find_coordinator_response(pTHX_ ev_kafka_conn_t *self,
2856 int16_t version, const char *data, size_t len)
2857 {
2858 10 const char *p = data;
2859 10 const char *end = data + len;
2860 10 HV *result = newHV();
2861 int n;
2862 (void)self;
2863  
2864 /* throttle_time_ms (v1+) */
2865 10 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 9 1 10
2866 9 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 9 9
2867 9 p += 4;
2868 }
2869  
2870 10 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 10 10
2871 10 int16_t err = kf_read_i16(p); p += 2;
2872 10 hv_store(result, "error_code", 10, newSViv(err), 0);
2873  
2874 /* error_message (v1+) */
2875 10 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 9 1 10
2876 const char *emsg; int16_t elen;
2877 9 n = kf_read_string(p, end, &emsg, &elen);
2878 9 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 9 9
2879 9 p += n;
2880 9 ▶ if (emsg && elen > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (emsg && elen > 0) 0 9 9
if (emsg && elen > 0) 0 0 0
2881 0 hv_store(result, "error_message", 13, newSVpvn(emsg, elen), 0);
2882 }
2883  
2884 10 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 10 10
2885 10 int32_t nid = kf_read_i32(p); p += 4;
2886 10 hv_store(result, "node_id", 7, newSViv(nid), 0);
2887  
2888 const char *host; int16_t hlen;
2889 10 n = kf_read_string(p, end, &host, &hlen);
2890 10 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 10 10
2891 10 p += n;
2892 10 ▶ hv_store(result, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(result, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 10 0 10
hv_store(result, "host", 4, newSVpvn(host ? host : "", host ? hlen : 0), 0); 10 0 10
2893  
2894 10 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 10 10
2895 10 int32_t port = kf_read_i32(p); p += 4;
2896 10 hv_store(result, "port", 4, newSViv(port), 0);
2897  
2898 10 done:
2899 10 return sv_2mortal(newRV_noinc((SV*)result));
2900 }
2901  
2902 /* JoinGroup response parser (API 11, v0-v5) */
2903 5 static SV* conn_parse_join_group_response(pTHX_ ev_kafka_conn_t *self,
2904 int16_t version, const char *data, size_t len)
2905 {
2906 5 const char *p = data;
2907 5 const char *end = data + len;
2908 5 HV *result = newHV();
2909 int n;
2910 (void)self;
2911  
2912 /* throttle_time_ms (v2+) */
2913 5 ▶ if (version >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 2) { 0 5 5
2914 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
2915 0 p += 4;
2916 }
2917  
2918 5 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 5 5
2919 5 int16_t err = kf_read_i16(p); p += 2;
2920 5 hv_store(result, "error_code", 10, newSViv(err), 0);
2921  
2922 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
2923 5 int32_t gen = kf_read_i32(p); p += 4;
2924 5 hv_store(result, "generation_id", 13, newSViv(gen), 0);
2925  
2926 /* protocol_type (v7+) — skip for now, we use v5 max */
2927  
2928 const char *proto; int16_t plen;
2929 5 n = kf_read_string(p, end, &proto, &plen);
2930 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
2931 5 p += n;
2932 5 ▶ if (proto)
Branch: 1 branch
BranchTrueFalseTotal
if (proto) 5 0 5
2933 5 hv_store(result, "protocol_name", 13, newSVpvn(proto, plen), 0);
2934  
2935 const char *leader; int16_t llen;
2936 5 n = kf_read_string(p, end, &leader, &llen);
2937 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
2938 5 p += n;
2939 5 ▶ if (leader)
Branch: 1 branch
BranchTrueFalseTotal
if (leader) 5 0 5
2940 5 hv_store(result, "leader", 6, newSVpvn(leader, llen), 0);
2941  
2942 /* skip_assignment (v9+) — not applicable */
2943  
2944 const char *member_id; int16_t mlen;
2945 5 n = kf_read_string(p, end, &member_id, &mlen);
2946 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
2947 5 p += n;
2948 5 ▶ if (member_id)
Branch: 1 branch
BranchTrueFalseTotal
if (member_id) 5 0 5
2949 5 hv_store(result, "member_id", 9, newSVpvn(member_id, mlen), 0);
2950  
2951 /* members array */
2952 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
2953 5 int32_t mcount = kf_read_i32(p); p += 4;
2954 5 AV *members_av = (AV*)sv_2mortal((SV*)newAV());
2955 int32_t i;
2956 10 ▶ for (i = 0; i < mcount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < mcount; i++) { 5 5 10
2957 5 HV *mh = (HV*)sv_2mortal((SV*)newHV());
2958  
2959 const char *mid; int16_t midlen;
2960 5 n = kf_read_string(p, end, &mid, &midlen);
2961 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
2962 5 p += n;
2963 5 ▶ if (mid)
Branch: 1 branch
BranchTrueFalseTotal
if (mid) 5 0 5
2964 5 hv_store(mh, "member_id", 9, newSVpvn(mid, midlen), 0);
2965  
2966 /* group_instance_id (v5+) */
2967 5 ▶ if (version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 5) { 0 5 5
2968 const char *gi; int16_t gilen;
2969 0 n = kf_read_string(p, end, &gi, &gilen);
2970 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
2971 0 p += n;
2972 }
2973  
2974 /* metadata: BYTES */
2975 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
2976 5 int32_t mdlen = kf_read_i32(p); p += 4;
2977 5 ▶ if (mdlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mdlen > 0) { 0 5 5
2978 0 ▶ if (end - p < mdlen) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < mdlen) goto done; 0 0 0
2979 0 hv_store(mh, "metadata", 8, newSVpvn(p, mdlen), 0);
2980 0 p += mdlen;
2981 }
2982  
2983 5 av_push(members_av, newRV_inc((SV*)mh));
2984 }
2985 5 hv_store(result, "members", 7, newRV_inc((SV*)members_av), 0);
2986  
2987 5 done:
2988 5 return sv_2mortal(newRV_noinc((SV*)result));
2989 }
2990  
2991 /* SyncGroup response parser (API 14, v0-v3) */
2992 5 static SV* conn_parse_sync_group_response(pTHX_ ev_kafka_conn_t *self,
2993 int16_t version, const char *data, size_t len)
2994 {
2995 5 const char *p = data;
2996 5 const char *end = data + len;
2997 5 HV *result = newHV();
2998 (void)self;
2999  
3000 /* throttle_time_ms (v1+) */
3001 5 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 0 5 5
3002 0 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 0 0
3003 0 p += 4;
3004 }
3005  
3006 5 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 5 5
3007 5 int16_t err = kf_read_i16(p); p += 2;
3008 5 hv_store(result, "error_code", 10, newSViv(err), 0);
3009  
3010 /* assignment: BYTES */
3011 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3012 5 int32_t alen = kf_read_i32(p); p += 4;
3013 5 ▶ if (alen > 0 && end - p >= alen) {
Branch: 2 branches
BranchTrueFalseTotal
if (alen > 0 && end - p >= alen) { 0 5 5
if (alen > 0 && end - p >= alen) { 0 5 5
3014 5 hv_store(result, "assignment", 10, newSVpvn(p, alen), 0);
3015 5 p += alen;
3016 }
3017  
3018 0 done:
3019 5 return sv_2mortal(newRV_noinc((SV*)result));
3020 }
3021  
3022 /* Heartbeat response parser (API 12) */
3023 2 static SV* conn_parse_heartbeat_response(pTHX_ ev_kafka_conn_t *self,
3024 int16_t version, const char *data, size_t len)
3025 {
3026 2 const char *p = data;
3027 2 const char *end = data + len;
3028 2 HV *result = newHV();
3029 (void)self;
3030  
3031 2 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 2 2
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3032 2 ▶ if (end - p >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 2) { 2 0 2
3033 2 int16_t err = kf_read_i16(p); p += 2;
3034 2 hv_store(result, "error_code", 10, newSViv(err), 0);
3035 }
3036  
3037 2 return sv_2mortal(newRV_noinc((SV*)result));
3038 }
3039  
3040 /* OffsetCommit response parser (API 8, v0-v7) */
3041 2 static SV* conn_parse_offset_commit_response(pTHX_ ev_kafka_conn_t *self,
3042 int16_t version, const char *data, size_t len)
3043 {
3044 2 const char *p = data;
3045 2 const char *end = data + len;
3046 2 HV *result = newHV();
3047 2 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3048 int n;
3049 (void)self;
3050  
3051 2 ▶ if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 2 2
if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3052  
3053 2 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 2 2
3054 2 int32_t tc = kf_read_i32(p); p += 4;
3055 int32_t i;
3056 4 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 2 2 4
3057 2 HV *th = (HV*)sv_2mortal((SV*)newHV());
3058 const char *tname; int16_t tnlen;
3059 2 n = kf_read_string(p, end, &tname, &tnlen);
3060 2 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 2 2
3061 2 p += n;
3062 2 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 2 0 2
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 2 0 2
3063  
3064 2 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 2 2
3065 2 int32_t pc = kf_read_i32(p); p += 4;
3066 2 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
3067 int32_t j;
3068 4 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 2 2 4
3069 2 HV *ph = (HV*)sv_2mortal((SV*)newHV());
3070 2 ▶ if (end - p < 6) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) goto done; 0 2 2
3071 2 int32_t pid = kf_read_i32(p); p += 4;
3072 2 int16_t err = kf_read_i16(p); p += 2;
3073 2 hv_store(ph, "partition", 9, newSViv(pid), 0);
3074 2 hv_store(ph, "error_code", 10, newSViv(err), 0);
3075 2 av_push(parts_av, newRV_inc((SV*)ph));
3076 }
3077 2 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
3078 2 av_push(topics_av, newRV_inc((SV*)th));
3079 }
3080  
3081 2 done:
3082 2 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3083 2 return sv_2mortal(newRV_noinc((SV*)result));
3084 }
3085  
3086 /* OffsetFetch response parser (API 9, v0-v5) */
3087 5 static SV* conn_parse_offset_fetch_response(pTHX_ ev_kafka_conn_t *self,
3088 int16_t version, const char *data, size_t len)
3089 {
3090 5 const char *p = data;
3091 5 const char *end = data + len;
3092 5 HV *result = newHV();
3093 5 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3094 int n;
3095 (void)self;
3096  
3097 5 ▶ if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 5 5
if (version >= 3 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3098  
3099 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3100 5 int32_t tc = kf_read_i32(p); p += 4;
3101 int32_t i;
3102 10 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 5 5 10
3103 5 HV *th = (HV*)sv_2mortal((SV*)newHV());
3104 const char *tname; int16_t tnlen;
3105 5 n = kf_read_string(p, end, &tname, &tnlen);
3106 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
3107 5 p += n;
3108 5 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 5 0 5
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 5 0 5
3109  
3110 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3111 5 int32_t pc = kf_read_i32(p); p += 4;
3112 5 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
3113 int32_t j;
3114 10 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 5 5 10
3115 5 HV *ph = (HV*)sv_2mortal((SV*)newHV());
3116 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3117 5 int32_t pid = kf_read_i32(p); p += 4;
3118 5 hv_store(ph, "partition", 9, newSViv(pid), 0);
3119  
3120 5 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 5 5
3121 5 int64_t offset = kf_read_i64(p); p += 8;
3122 5 hv_store(ph, "offset", 6, newSViv(offset), 0);
3123  
3124 /* leader_epoch (v5+) */
3125 5 ▶ if (version >= 5 && end - p >= 4) p += 4;
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 5 && end - p >= 4) p += 4; 0 5 5
if (version >= 5 && end - p >= 4) p += 4; 0 0 0
3126  
3127 const char *meta_str; int16_t meta_len;
3128 5 n = kf_read_string(p, end, &meta_str, &meta_len);
3129 5 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 5 5
3130 5 p += n;
3131  
3132 5 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 5 5
3133 5 int16_t err = kf_read_i16(p); p += 2;
3134 5 hv_store(ph, "error_code", 10, newSViv(err), 0);
3135  
3136 5 av_push(parts_av, newRV_inc((SV*)ph));
3137 }
3138 5 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
3139 5 av_push(topics_av, newRV_inc((SV*)th));
3140 }
3141  
3142 /* error_code (v2+) */
3143 5 ▶ if (version >= 2 && end - p >= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 2 && end - p >= 2) { 5 0 5
if (version >= 2 && end - p >= 2) { 0 0 0
3144 0 int16_t err = kf_read_i16(p); p += 2;
3145 0 hv_store(result, "error_code", 10, newSViv(err), 0);
3146 }
3147  
3148 5 done:
3149 5 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3150 5 return sv_2mortal(newRV_noinc((SV*)result));
3151 }
3152  
3153 /* LeaveGroup response parser (API 13, v0-v3) */
3154 2 static SV* conn_parse_leave_group_response(pTHX_ ev_kafka_conn_t *self,
3155 int16_t version, const char *data, size_t len)
3156 {
3157 2 const char *p = data;
3158 2 const char *end = data + len;
3159 2 HV *result = newHV();
3160 (void)self;
3161  
3162 2 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 2 2
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3163 2 ▶ if (end - p >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 2) { 2 0 2
3164 2 int16_t err = kf_read_i16(p); p += 2;
3165 2 hv_store(result, "error_code", 10, newSViv(err), 0);
3166 }
3167  
3168 2 return sv_2mortal(newRV_noinc((SV*)result));
3169 }
3170  
3171 /* CreateTopics response parser (API 19, v0-v4) */
3172 1 static SV* conn_parse_create_topics_response(pTHX_ ev_kafka_conn_t *self,
3173 int16_t version, const char *data, size_t len)
3174 {
3175 1 const char *p = data;
3176 1 const char *end = data + len;
3177 1 HV *result = newHV();
3178 1 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3179 int n;
3180 (void)self;
3181  
3182 1 ▶ if (version >= 2 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 2 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 1 1
if (version >= 2 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3183  
3184 1 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 1 1
3185 1 int32_t tc = kf_read_i32(p); p += 4;
3186 int32_t i;
3187 2 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 1 1 2
3188 1 HV *th = (HV*)sv_2mortal((SV*)newHV());
3189 const char *tname; int16_t tnlen;
3190 1 n = kf_read_string(p, end, &tname, &tnlen);
3191 1 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 1 1
3192 1 p += n;
3193 1 ▶ hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
3194  
3195 1 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 1 1
3196 1 int16_t err = kf_read_i16(p); p += 2;
3197 1 hv_store(th, "error_code", 10, newSViv(err), 0);
3198  
3199 /* error_message (v1+) */
3200 1 ▶ if (version >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (version >= 1) { 0 1 1
3201 const char *emsg; int16_t elen;
3202 0 n = kf_read_string(p, end, &emsg, &elen);
3203 0 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 0 0
3204 0 p += n;
3205 0 ▶ if (emsg && elen > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (emsg && elen > 0) 0 0 0
if (emsg && elen > 0) 0 0 0
3206 0 hv_store(th, "error_message", 13, newSVpvn(emsg, elen), 0);
3207 }
3208  
3209 1 av_push(topics_av, newRV_inc((SV*)th));
3210 }
3211  
3212 1 done:
3213 1 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3214 1 return sv_2mortal(newRV_noinc((SV*)result));
3215 }
3216  
3217 /* DeleteTopics response parser (API 20, v0-v3) */
3218 1 static SV* conn_parse_delete_topics_response(pTHX_ ev_kafka_conn_t *self,
3219 int16_t version, const char *data, size_t len)
3220 {
3221 1 const char *p = data;
3222 1 const char *end = data + len;
3223 1 HV *result = newHV();
3224 1 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3225 int n;
3226 (void)self;
3227  
3228 1 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 1 1
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3229  
3230 1 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 1 1
3231 1 int32_t tc = kf_read_i32(p); p += 4;
3232 int32_t i;
3233 2 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 1 1 2
3234 1 HV *th = (HV*)sv_2mortal((SV*)newHV());
3235 const char *tname; int16_t tnlen;
3236 1 n = kf_read_string(p, end, &tname, &tnlen);
3237 1 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 1 1
3238 1 p += n;
3239 1 ▶ hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
hv_store(th, "name", 4, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
3240  
3241 1 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 1 1
3242 1 int16_t err = kf_read_i16(p); p += 2;
3243 1 hv_store(th, "error_code", 10, newSViv(err), 0);
3244  
3245 1 av_push(topics_av, newRV_inc((SV*)th));
3246 }
3247  
3248 1 done:
3249 1 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3250 1 return sv_2mortal(newRV_noinc((SV*)result));
3251 }
3252  
3253 /* InitProducerId response parser (API 22, v0-v2) */
3254 11 static SV* conn_parse_init_producer_id_response(pTHX_ ev_kafka_conn_t *self,
3255 int16_t version, const char *data, size_t len)
3256 {
3257 11 const char *p = data;
3258 11 const char *end = data + len;
3259 11 HV *result = newHV();
3260 (void)self;
3261  
3262 11 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 11 0 11
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 11 0 11
3263  
3264 11 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 11 11
3265 11 int16_t err = kf_read_i16(p); p += 2;
3266 11 hv_store(result, "error_code", 10, newSViv(err), 0);
3267  
3268 11 ▶ if (end - p < 8) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 8) goto done; 0 11 11
3269 11 int64_t producer_id = kf_read_i64(p); p += 8;
3270 11 hv_store(result, "producer_id", 11, newSViv(producer_id), 0);
3271  
3272 11 ▶ if (end - p < 2) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 2) goto done; 0 11 11
3273 11 int16_t producer_epoch = kf_read_i16(p); p += 2;
3274 11 hv_store(result, "producer_epoch", 14, newSViv(producer_epoch), 0);
3275  
3276 11 done:
3277 11 return sv_2mortal(newRV_noinc((SV*)result));
3278 }
3279  
3280 /* AddPartitionsToTxn response parser (API 24, v0-v1) */
3281 5 static SV* conn_parse_add_partitions_to_txn_response(pTHX_ ev_kafka_conn_t *self,
3282 int16_t version, const char *data, size_t len)
3283 {
3284 5 const char *p = data;
3285 5 const char *end = data + len;
3286 5 HV *result = newHV();
3287 5 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3288 int n;
3289 (void)self; (void)version;
3290  
3291 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 5 5
3292 5 p += 4; /* throttle_time_ms */
3293  
3294 5 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 1 4 5
3295 4 int32_t tc = kf_read_i32(p); p += 4;
3296 int32_t i;
3297 8 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 4 4 8
3298 4 HV *th = (HV*)sv_2mortal((SV*)newHV());
3299 const char *tname; int16_t tnlen;
3300 4 n = kf_read_string(p, end, &tname, &tnlen);
3301 4 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 4 4
3302 4 p += n;
3303 4 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 4 0 4
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 4 0 4
3304  
3305 4 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 4 4
3306 4 int32_t pc = kf_read_i32(p); p += 4;
3307 4 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
3308 int32_t j;
3309 8 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 4 4 8
3310 4 HV *ph = (HV*)sv_2mortal((SV*)newHV());
3311 4 ▶ if (end - p < 6) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) goto done; 0 4 4
3312 4 int32_t pid = kf_read_i32(p); p += 4;
3313 4 int16_t err = kf_read_i16(p); p += 2;
3314 4 hv_store(ph, "partition", 9, newSViv(pid), 0);
3315 4 hv_store(ph, "error_code", 10, newSViv(err), 0);
3316 4 av_push(parts_av, newRV_inc((SV*)ph));
3317 }
3318 4 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
3319 4 av_push(topics_av, newRV_inc((SV*)th));
3320 }
3321  
3322 4 done:
3323 5 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3324 5 return sv_2mortal(newRV_noinc((SV*)result));
3325 }
3326  
3327 /* EndTxn response parser (API 26, v0-v1) */
3328 3 static SV* conn_parse_end_txn_response(pTHX_ ev_kafka_conn_t *self,
3329 int16_t version, const char *data, size_t len)
3330 {
3331 3 const char *p = data;
3332 3 const char *end = data + len;
3333 3 HV *result = newHV();
3334 (void)self; (void)version;
3335  
3336 3 ▶ if (end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 4) p += 4; /* throttle_time_ms */ 3 0 3
3337 3 ▶ if (end - p >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (end - p >= 2) { 3 0 3
3338 3 int16_t err = kf_read_i16(p); p += 2;
3339 3 hv_store(result, "error_code", 10, newSViv(err), 0);
3340 }
3341  
3342 3 return sv_2mortal(newRV_noinc((SV*)result));
3343 }
3344  
3345 /* TxnOffsetCommit response parser (API 28, v0-v2) */
3346 3 static SV* conn_parse_txn_offset_commit_response(pTHX_ ev_kafka_conn_t *self,
3347 int16_t version, const char *data, size_t len)
3348 {
3349 3 const char *p = data;
3350 3 const char *end = data + len;
3351 3 HV *result = newHV();
3352 3 AV *topics_av = (AV*)sv_2mortal((SV*)newAV());
3353 int n;
3354 (void)self;
3355  
3356 3 ▶ if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */
Branch: 2 branches
BranchTrueFalseTotal
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 3 3
if (version >= 1 && end - p >= 4) p += 4; /* throttle_time_ms */ 0 0 0
3357  
3358 3 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 3 3
3359 3 int32_t tc = kf_read_i32(p); p += 4;
3360 int32_t i;
3361 4 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 1 3 4
3362 1 HV *th = (HV*)sv_2mortal((SV*)newHV());
3363 const char *tname; int16_t tnlen;
3364 1 n = kf_read_string(p, end, &tname, &tnlen);
3365 1 ▶ if (n < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) goto done; 0 1 1
3366 1 p += n;
3367 1 ▶ hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
hv_store(th, "topic", 5, newSVpvn(tname ? tname : "", tname ? tnlen : 0), 0); 1 0 1
3368  
3369 1 ▶ if (end - p < 4) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 4) goto done; 0 1 1
3370 1 int32_t pc = kf_read_i32(p); p += 4;
3371 1 AV *parts_av = (AV*)sv_2mortal((SV*)newAV());
3372 int32_t j;
3373 2 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 1 1 2
3374 1 HV *ph = (HV*)sv_2mortal((SV*)newHV());
3375 1 ▶ if (end - p < 6) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (end - p < 6) goto done; 0 1 1
3376 1 int32_t pid = kf_read_i32(p); p += 4;
3377 1 int16_t err = kf_read_i16(p); p += 2;
3378 1 hv_store(ph, "partition", 9, newSViv(pid), 0);
3379 1 hv_store(ph, "error_code", 10, newSViv(err), 0);
3380 1 av_push(parts_av, newRV_inc((SV*)ph));
3381 }
3382 1 hv_store(th, "partitions", 10, newRV_inc((SV*)parts_av), 0);
3383 1 av_push(topics_av, newRV_inc((SV*)th));
3384 }
3385  
3386 3 done:
3387 3 hv_store(result, "topics", 6, newRV_inc((SV*)topics_av), 0);
3388 3 return sv_2mortal(newRV_noinc((SV*)result));
3389 }
3390  
3391 /* ================================================================
3392 * Response processing loop
3393 * ================================================================ */
3394  
3395 179 static void conn_process_responses(pTHX_ ev_kafka_conn_t *self) {
3396 532 ▶ while (self->rbuf_len >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->rbuf_len >= 4) { 180 173 353
3397 180 int32_t msg_size = kf_read_i32(self->rbuf);
3398 180 ▶ if (msg_size < 0 || msg_size > 256 * 1024 * 1024) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg_size < 0 || msg_size > 256 * 1024 * 1024) { 180 0 180
if (msg_size < 0 || msg_size > 256 * 1024 * 1024) { 1 179 180
3399 1 conn_emit_error(aTHX_ self, "invalid response size");
3400 1 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 1 1
3401 1 conn_handle_disconnect(aTHX_ self, "invalid response size");
3402 1 return;
3403 }
3404  
3405 179 ▶ if (self->rbuf_len < (size_t)(4 + msg_size))
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < (size_t)(4 + msg_size)) 0 179 179
3406 0 break; /* incomplete response */
3407  
3408 179 const char *msg = self->rbuf + 4;
3409  
3410 /* correlation_id is first 4 bytes of message */
3411 179 ▶ if (msg_size < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg_size < 4) { 0 179 179
3412 0 conn_emit_error(aTHX_ self, "response too short");
3413 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3414 0 conn_handle_disconnect(aTHX_ self, "response too short");
3415 0 return;
3416 }
3417  
3418 179 int32_t corr_id = kf_read_i32(msg);
3419 179 const char *payload = msg + 4;
3420 179 size_t payload_len = (size_t)msg_size - 4;
3421  
3422 /* Find matching callback (should be head of queue — Kafka guarantees ordering) */
3423 179 ev_kafka_conn_cb_t *cbt = NULL;
3424 179 ▶ if (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->cb_queue)) { 179 0 179
3425 179 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
3426 179 cbt = ngx_queue_data(q, ev_kafka_conn_cb_t, queue);
3427 179 ▶ if (cbt->correlation_id != corr_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->correlation_id != corr_id) { 1 178 179
3428 /* Out of order — shouldn't happen with Kafka, but handle gracefully */
3429 char errbuf[128];
3430 1 snprintf(errbuf, sizeof(errbuf),
3431 "correlation ID mismatch: expected %d, got %d",
3432 cbt->correlation_id, corr_id);
3433 1 conn_emit_error(aTHX_ self, errbuf);
3434 1 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 1 1
3435 1 conn_handle_disconnect(aTHX_ self, "correlation ID mismatch");
3436 1 return;
3437 }
3438 178 ngx_queue_remove(q);
3439 178 self->pending_count--;
3440 }
3441  
3442 /* Dispatch BEFORE compacting — payload points into rbuf */
3443 178 size_t consumed = 4 + (size_t)msg_size;
3444 178 ▶ if (cbt) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt) { 178 0 178
3445 178 conn_dispatch_response(aTHX_ self, cbt, payload, payload_len);
3446 178 ▶ if (cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) SvREFCNT_dec(cbt->cb); 114 64 178
3447 178 Safefree(cbt);
3448 178 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 178 178
3449 }
3450  
3451 /* Compact rbuf after dispatch — unless dispatch tore down the
3452 * conn (conn_cleanup zeroed rbuf_len; subtracting would underflow). */
3453 178 ▶ if (self->rbuf_len < consumed)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < consumed) 4 174 178
3454 4 return;
3455 174 self->rbuf_len -= consumed;
3456 174 ▶ if (self->rbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len > 0) 173 1 174
3457 1 memmove(self->rbuf, self->rbuf + consumed, self->rbuf_len);
3458 }
3459 }
3460  
3461 /* ================================================================
3462 * I/O callback
3463 * ================================================================ */
3464  
3465 441 static void conn_io_cb(EV_P_ ev_io *w, int revents) {
3466 441 ev_kafka_conn_t *self = (ev_kafka_conn_t *)w->data;
3467 dTHX;
3468 (void)loop;
3469 441 ▶ if (!self || self->magic != KF_MAGIC_ALIVE) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self || self->magic != KF_MAGIC_ALIVE) return; 441 0 441
if (!self || self->magic != KF_MAGIC_ALIVE) return; 0 441 441
3470 441 self->callback_depth++;
3471 {
3472 /* Per-event tmps frame: without it, mortals created below would
3473 * pile up in EV::run's frame. FREETMPS runs while callback_depth
3474 * >= 1, so a triggered destructor still defers conn_do_free. */
3475 441 ENTER;
3476 441 SAVETMPS;
3477 441 conn_io_cb_inner(loop, w, revents);
3478 441 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 121 320 441
3479 441 LEAVE;
3480 }
3481 441 ▶ if (--self->callback_depth == 0 && self->free_pending)
Branch: 2 branches
BranchTrueFalseTotal
if (--self->callback_depth == 0 && self->free_pending) 441 0 441
if (--self->callback_depth == 0 && self->free_pending) 1 440 441
3482 1 conn_do_free(aTHX_ self);
3483 }
3484  
3485 441 static void conn_io_cb_inner(EV_P_ ev_io *w, int revents) {
3486 441 ev_kafka_conn_t *self = (ev_kafka_conn_t *)w->data;
3487 dTHX;
3488 (void)loop;
3489  
3490 441 ▶ if (!self || self->magic != KF_MAGIC_ALIVE) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self || self->magic != KF_MAGIC_ALIVE) return; 441 0 441
if (!self || self->magic != KF_MAGIC_ALIVE) return; 0 441 441
3491  
3492 /* TCP connect in progress */
3493 441 ▶ if (self->state == CONN_CONNECTING) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == CONN_CONNECTING) { 69 372 441
3494 69 int err = 0;
3495 69 socklen_t errlen = sizeof(err);
3496  
3497 69 conn_stop_writing(self);
3498  
3499 69 ▶ if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) 0 69 69
3500 0 err = errno;
3501 69 ▶ if (err != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != 0) { 9 60 69
3502 char errbuf[256];
3503 9 snprintf(errbuf, sizeof(errbuf), "connect: %s", strerror(err));
3504 9 conn_emit_error(aTHX_ self, errbuf);
3505 9 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 1 8 9
3506 8 conn_handle_disconnect(aTHX_ self, errbuf);
3507 8 return;
3508 }
3509  
3510 /* TCP connected — keep the timer armed for the handshake phases. */
3511 60 conn_arm_timer(self);
3512 60 conn_on_connect_done(aTHX_ self);
3513 60 return;
3514 }
3515  
3516 #ifdef HAVE_OPENSSL
3517 /* TLS handshake in progress */
3518 372 ▶ if (self->state == CONN_TLS_HANDSHAKE) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == CONN_TLS_HANDSHAKE) { 0 372 372
3519 0 ERR_clear_error();
3520 0 int ret = SSL_connect(self->ssl);
3521 0 ▶ if (ret == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 1) { 0 0 0
3522 0 conn_stop_reading(self);
3523 0 conn_stop_writing(self);
3524  
3525 /* TLS done — proceed to ApiVersions (or SASL if needed) */
3526 0 conn_arm_timer(self);
3527 0 conn_send_api_versions(aTHX_ self);
3528 0 conn_start_reading(self);
3529 0 return;
3530 }
3531 0 int err = SSL_get_error(self->ssl, ret);
3532 0 ▶ if (err == SSL_ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_READ) { 0 0 0
3533 0 conn_stop_writing(self);
3534 0 conn_start_reading(self);
3535 0 ▶ } else if (err == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (err == SSL_ERROR_WANT_WRITE) { 0 0 0
3536 0 conn_stop_reading(self);
3537 0 conn_start_writing(self);
3538 } else {
3539 char errbuf[256];
3540 0 unsigned long e = ERR_peek_last_error();
3541 0 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 0 0 0
3542 0 snprintf(errbuf, sizeof(errbuf), "SSL_connect failed: %s",
3543 ERR_reason_error_string(e));
3544 } else {
3545 0 snprintf(errbuf, sizeof(errbuf), "SSL_connect failed (err=%d)", err);
3546 }
3547 0 ERR_clear_error();
3548 0 conn_emit_error(aTHX_ self, errbuf);
3549 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3550 0 conn_handle_disconnect(aTHX_ self, errbuf);
3551 }
3552 0 return;
3553 }
3554 #endif
3555  
3556 /* Write */
3557 372 ▶ if (revents & EV_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) { 183 189 372
3558 366 ▶ while (self->wbuf_off < self->wbuf_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->wbuf_off < self->wbuf_len) { 183 183 366
3559 183 ssize_t n = kf_io_write(self, self->wbuf + self->wbuf_off,
3560 183 self->wbuf_len - self->wbuf_off);
3561 183 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 183 183
3562 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
3563 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK) break;
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK) break; 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK) break; 0 0 0
3564 0 conn_emit_error(aTHX_ self, strerror(errno));
3565 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3566 0 conn_handle_disconnect(aTHX_ self, "write error");
3567 0 return;
3568 }
3569 183 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 183 183
3570 0 conn_handle_disconnect(aTHX_ self, "connection closed");
3571 0 return;
3572 }
3573 183 self->wbuf_off += n;
3574 }
3575  
3576 183 ▶ if (self->wbuf_off >= self->wbuf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_off >= self->wbuf_len) { 183 0 183
3577 183 self->wbuf_off = 0;
3578 183 self->wbuf_len = 0;
3579 183 conn_stop_writing(self);
3580 }
3581 }
3582  
3583 /* Read */
3584 372 ▶ if (revents & EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_READ) { 189 183 372
3585 189 conn_ensure_rbuf(self, self->rbuf_len + 8192);
3586 189 ssize_t n = kf_io_read(self, self->rbuf + self->rbuf_len,
3587 189 self->rbuf_cap - self->rbuf_len);
3588 189 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 189 189
3589 0 ▶ if (errno == EINTR) return; /* level-triggered watcher re-fires */
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) return; /* level-triggered watcher re-fires */ 0 0 0
3590 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK) return;
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK) return; 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK) return; 0 0 0
3591 0 conn_emit_error(aTHX_ self, strerror(errno));
3592 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3593 0 conn_handle_disconnect(aTHX_ self, "read error");
3594 0 return;
3595 }
3596 189 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 10 179 189
3597 10 conn_handle_disconnect(aTHX_ self, "connection closed by broker");
3598 10 return;
3599 }
3600 179 self->rbuf_len += n;
3601  
3602 179 conn_process_responses(aTHX_ self);
3603 }
3604 }
3605  
3606 /* ================================================================
3607 * Connect timer (timeout)
3608 * ================================================================ */
3609  
3610 1 static void conn_timer_cb(EV_P_ ev_timer *w, int revents) {
3611 1 ev_kafka_conn_t *self = (ev_kafka_conn_t *)w->data;
3612 dTHX;
3613 (void)loop;
3614 1 self->timing = 0;
3615 1 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 1 1
3616 1 self->callback_depth++;
3617 {
3618 /* Per-event tmps frame: see conn_io_cb. */
3619 1 ENTER;
3620 1 SAVETMPS;
3621 1 conn_timer_cb_inner(loop, w, revents);
3622 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 1 1
3623 1 LEAVE;
3624 }
3625 1 ▶ if (--self->callback_depth == 0 && self->free_pending)
Branch: 2 branches
BranchTrueFalseTotal
if (--self->callback_depth == 0 && self->free_pending) 1 0 1
if (--self->callback_depth == 0 && self->free_pending) 0 1 1
3626 0 conn_do_free(aTHX_ self);
3627 }
3628  
3629 1 static void conn_timer_cb_inner(EV_P_ ev_timer *w, int revents) {
3630 1 ev_kafka_conn_t *self = (ev_kafka_conn_t *)w->data;
3631 dTHX;
3632 (void)loop;
3633 (void)revents;
3634  
3635 1 self->timing = 0;
3636 1 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 1 1
3637  
3638 2 const char *msg = self->state == CONN_CONNECTING
3639 1 ▶ ? "connect timeout" : "handshake timeout";
Branch: 1 branch
BranchTrueFalseTotal
? "connect timeout" : "handshake timeout"; 1 0 1
3640 1 conn_emit_error(aTHX_ self, msg);
3641 1 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 1 1
3642 1 conn_handle_disconnect(aTHX_ self, msg);
3643 }
3644  
3645 /* ================================================================
3646 * TCP connect + handshake initiation
3647 * ================================================================ */
3648  
3649 #ifdef HAVE_OPENSSL
3650 0 static int is_ip_literal(const char *host) {
3651 struct in_addr addr4;
3652 struct in6_addr addr6;
3653 0 return (inet_pton(AF_INET, host, &addr4) == 1 ||
3654 0 inet_pton(AF_INET6, host, &addr6) == 1);
3655 }
3656 #endif
3657  
3658 60 static void conn_on_connect_done(pTHX_ ev_kafka_conn_t *self) {
3659 /* TCP connected — set up I/O watchers if not already */
3660  
3661 60 self->reconnect_attempts = 0;
3662  
3663 #ifdef HAVE_OPENSSL
3664 60 ▶ if (self->tls_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_enabled) { 0 60 60
3665 0 self->ssl_ctx = SSL_CTX_new(TLS_client_method());
3666 0 ▶ if (!self->ssl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->ssl_ctx) { 0 0 0
3667 0 conn_emit_error(aTHX_ self, "SSL_CTX_new failed");
3668 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3669 0 conn_handle_disconnect(aTHX_ self, "SSL_CTX_new failed");
3670 0 return;
3671 }
3672 0 SSL_CTX_set_default_verify_paths(self->ssl_ctx);
3673 0 ▶ if (self->tls_skip_verify)
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_skip_verify) 0 0 0
3674 0 SSL_CTX_set_verify(self->ssl_ctx, SSL_VERIFY_NONE, NULL);
3675 else
3676 0 SSL_CTX_set_verify(self->ssl_ctx, SSL_VERIFY_PEER, NULL);
3677  
3678 0 ▶ if (self->tls_ca_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_ca_file) { 0 0 0
3679 0 ▶ if (SSL_CTX_load_verify_locations(self->ssl_ctx, self->tls_ca_file, NULL) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (SSL_CTX_load_verify_locations(self->ssl_ctx, self->tls_ca_file, NULL) != 1) { 0 0 0
3680 0 conn_emit_error(aTHX_ self, "SSL_CTX_load_verify_locations failed");
3681 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3682 0 conn_handle_disconnect(aTHX_ self, "SSL_CTX_load_verify_locations failed");
3683 0 return;
3684 }
3685 }
3686  
3687 0 self->ssl = SSL_new(self->ssl_ctx);
3688 0 ▶ if (!self->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->ssl) { 0 0 0
3689 0 conn_emit_error(aTHX_ self, "SSL_new failed");
3690 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3691 0 conn_handle_disconnect(aTHX_ self, "SSL_new failed");
3692 0 return;
3693 }
3694 0 SSL_set_fd(self->ssl, self->fd);
3695  
3696 0 ▶ if (!is_ip_literal(self->host))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ip_literal(self->host)) 0 0 0
3697 0 SSL_set_tlsext_host_name(self->ssl, self->host);
3698  
3699 0 ▶ if (!self->tls_skip_verify) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->tls_skip_verify) { 0 0 0
3700 0 X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl);
3701 0 X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
3702 0 ▶ if (is_ip_literal(self->host))
Branch: 1 branch
BranchTrueFalseTotal
if (is_ip_literal(self->host)) 0 0 0
3703 0 X509_VERIFY_PARAM_set1_ip_asc(param, self->host);
3704 else
3705 0 X509_VERIFY_PARAM_set1_host(param, self->host, 0);
3706 }
3707  
3708 0 self->state = CONN_TLS_HANDSHAKE;
3709 0 ERR_clear_error();
3710 0 int ret = SSL_connect(self->ssl);
3711 0 ▶ if (ret == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 1) { 0 0 0
3712 /* Immediate success */
3713 0 conn_send_api_versions(aTHX_ self);
3714 0 conn_start_reading(self);
3715 0 return;
3716 }
3717 0 int err = SSL_get_error(self->ssl, ret);
3718 0 ▶ if (err == SSL_ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == SSL_ERROR_WANT_READ) { 0 0 0
3719 0 conn_start_reading(self);
3720 0 ▶ } else if (err == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (err == SSL_ERROR_WANT_WRITE) { 0 0 0
3721 0 conn_start_writing(self);
3722 } else {
3723 char errbuf[256];
3724 0 unsigned long e = ERR_peek_last_error();
3725 0 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 0 0 0
3726 0 snprintf(errbuf, sizeof(errbuf), "SSL_connect failed: %s",
3727 ERR_reason_error_string(e));
3728 } else {
3729 0 snprintf(errbuf, sizeof(errbuf), "SSL_connect failed (err=%d)", err);
3730 }
3731 0 ERR_clear_error();
3732 0 conn_emit_error(aTHX_ self, errbuf);
3733 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3734 0 conn_handle_disconnect(aTHX_ self, errbuf);
3735 }
3736 0 return;
3737 }
3738 #endif
3739  
3740 /* No TLS — send ApiVersions directly */
3741 60 conn_send_api_versions(aTHX_ self);
3742 60 conn_start_reading(self);
3743 }
3744  
3745 72 static void conn_start_connect(pTHX_ ev_kafka_conn_t *self,
3746 const char *host, int port, double timeout)
3747 {
3748 struct addrinfo hints, *res, *rp;
3749 char port_str[16];
3750 72 int fd = -1;
3751  
3752 72 ▶ if (self->state != CONN_DISCONNECTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_DISCONNECTED) { 0 72 72
3753 0 conn_cleanup(aTHX_ self);
3754 /* fail old-conn callbacks: their correlation IDs must not match the new wire */
3755 0 conn_cancel_pending(aTHX_ self, "reconnecting");
3756 72 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3757 }
3758  
3759 /* Save host/port (skip if already pointing to same string, e.g. reconnect) */
3760 72 ▶ if (host != self->host) {
Branch: 1 branch
BranchTrueFalseTotal
if (host != self->host) { 60 12 72
3761 60 ▶ if (self->host) Safefree(self->host);
Branch: 1 branch
BranchTrueFalseTotal
if (self->host) Safefree(self->host); 0 60 60
3762 60 self->host = savepv(host);
3763 }
3764 72 self->port = port;
3765 72 self->intentional_disconnect = 0;
3766 /* default timeout so a silent peer cannot wedge the conn forever */
3767 72 ▶ self->connect_timeout = (timeout > 0) ? timeout : 10.0;
Branch: 1 branch
BranchTrueFalseTotal
self->connect_timeout = (timeout > 0) ? timeout : 10.0; 72 0 72
3768  
3769 72 snprintf(port_str, sizeof(port_str), "%d", port);
3770  
3771 72 memset(&hints, 0, sizeof(hints));
3772 72 hints.ai_family = AF_UNSPEC;
3773 72 hints.ai_socktype = SOCK_STREAM;
3774  
3775 /* Fast path: if host is a numeric IP literal, AI_NUMERICHOST avoids any
3776 * DNS resolver work and returns synchronously without blocking the EV
3777 * loop. For non-literal hostnames getaddrinfo still blocks; users that
3778 * need fully-async DNS should resolve in Perl-land before connecting. */
3779 72 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
3780 72 int gai_err = getaddrinfo(host, port_str, &hints, &res);
3781 72 ▶ if (gai_err != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gai_err != 0) { 2 70 72
3782 2 hints.ai_flags = 0;
3783 2 gai_err = getaddrinfo(host, port_str, &hints, &res);
3784 }
3785 72 ▶ if (gai_err != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gai_err != 0) { 2 70 72
3786 char errbuf[256];
3787 2 snprintf(errbuf, sizeof(errbuf), "resolve: %s", gai_strerror(gai_err));
3788 2 conn_emit_error(aTHX_ self, errbuf);
3789 2 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 2 2
3790 /* already DISCONNECTED: conn_handle_disconnect would no-op, so
3791 * schedule the retry directly. */
3792 2 ▶ if (!self->intentional_disconnect && self->auto_reconnect)
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->auto_reconnect) 2 0 2
if (!self->intentional_disconnect && self->auto_reconnect) 2 0 2
3793 2 conn_schedule_reconnect(aTHX_ self);
3794 2 return;
3795 }
3796  
3797 70 ▶ for (rp = res; rp; rp = rp->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (rp = res; rp; rp = rp->ai_next) { 70 0 70
3798 70 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
3799 70 ▶ if (fd < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) continue; 0 70 70
3800  
3801 #ifdef SO_NOSIGPIPE
3802 /* BSD/macOS: no SIGPIPE from writes on this socket */
3803 {
3804 int one = 1;
3805 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one));
3806 }
3807 #endif
3808  
3809 /* Non-blocking */
3810 70 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
3811  
3812 /* TCP_NODELAY */
3813 {
3814 70 int one = 1;
3815 70 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
3816 }
3817  
3818 70 int ret = connect(fd, rp->ai_addr, rp->ai_addrlen);
3819 70 ▶ if (ret == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) { 0 70 70
3820 /* Immediate connect */
3821 0 self->fd = fd;
3822 0 self->state = CONN_CONNECTING; /* will be advanced in on_connect_done */
3823 0 freeaddrinfo(res);
3824  
3825 0 ev_io_init(&self->rio, conn_io_cb, fd, EV_READ);
3826 0 self->rio.data = (void *)self;
3827 0 ev_io_init(&self->wio, conn_io_cb, fd, EV_WRITE);
3828 0 self->wio.data = (void *)self;
3829  
3830 0 conn_arm_timer(self); /* cover the handshake phases */
3831 0 conn_on_connect_done(aTHX_ self);
3832 0 return;
3833 }
3834  
3835 70 ▶ if (errno == EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINPROGRESS) { 70 0 70
3836 70 self->fd = fd;
3837 70 self->state = CONN_CONNECTING;
3838 70 freeaddrinfo(res);
3839  
3840 70 ev_io_init(&self->rio, conn_io_cb, fd, EV_READ);
3841 70 self->rio.data = (void *)self;
3842 70 ev_io_init(&self->wio, conn_io_cb, fd, EV_WRITE);
3843 70 self->wio.data = (void *)self;
3844  
3845 70 conn_start_writing(self); /* wait for connect to complete */
3846  
3847 70 conn_arm_timer(self);
3848 70 return;
3849 }
3850  
3851 0 close(fd);
3852 }
3853  
3854 0 freeaddrinfo(res);
3855 0 conn_emit_error(aTHX_ self, "connect: all addresses failed");
3856 0 ▶ if (conn_check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (conn_check_destroyed(self)) return; 0 0 0
3857 /* See the resolve-failure branch above: schedule the retry directly. */
3858 0 ▶ if (!self->intentional_disconnect && self->auto_reconnect)
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->auto_reconnect) 0 0 0
if (!self->intentional_disconnect && self->auto_reconnect) 0 0 0
3859 0 conn_schedule_reconnect(aTHX_ self);
3860 }
3861  
3862 /* Pre-validate the topics ARRAY so croak() doesn't longjmp over
3863 * kf_buf_free in the caller; check_part_records also requires
3864 * {partition, offset} hashrefs per partition. */
3865 40408 static void kf_validate_txn_topics(const char *who, SV *topics_sv,
3866 int check_part_records)
3867 {
3868 40408 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 40408 0 40408
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 40408 40408
3869 0 croak("%s: expected arrayref", who);
3870 40408 AV *topics = (AV*)SvRV(topics_sv);
3871 40408 SSize_t i, tc = av_len(topics) + 1;
3872 40414 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 40408 6 40414
3873 40408 SV **elem = av_fetch(topics, i, 0);
3874 40408 ▶ if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 40408 0 40408
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 40408 0 40408
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 0 40408 40408
3875 0 croak("%s: bad element", who);
3876 40408 HV *th = (HV*)SvRV(*elem);
3877 40408 ▶ if (!hv_fetch(th, "topic", 5, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetch(th, "topic", 5, 0)) 20201 20207 40408
3878 20201 croak("%s: missing topic", who);
3879 20207 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
3880 20207 ▶ if (!parts_sv || !SvROK(*parts_sv) || SvTYPE(SvRV(*parts_sv)) != SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (!parts_sv || !SvROK(*parts_sv) || SvTYPE(SvRV(*parts_sv)) != SVt_PVAV) 7 20200 20207
if (!parts_sv || !SvROK(*parts_sv) || SvTYPE(SvRV(*parts_sv)) != SVt_PVAV) 7 0 7
if (!parts_sv || !SvROK(*parts_sv) || SvTYPE(SvRV(*parts_sv)) != SVt_PVAV) 0 7 7
3881 20200 croak("%s: missing partitions", who);
3882 7 ▶ if (check_part_records) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_part_records) { 3 4 7
3883 3 AV *parts = (AV*)SvRV(*parts_sv);
3884 3 SSize_t j, pc = av_len(parts) + 1;
3885 5 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 3 2 5
3886 3 SV **pelem = av_fetch(parts, j, 0);
3887 3 ▶ if (!pelem || !SvROK(*pelem) || SvTYPE(SvRV(*pelem)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!pelem || !SvROK(*pelem) || SvTYPE(SvRV(*pelem)) != SVt_PVHV) 3 0 3
if (!pelem || !SvROK(*pelem) || SvTYPE(SvRV(*pelem)) != SVt_PVHV) 2 1 3
if (!pelem || !SvROK(*pelem) || SvTYPE(SvRV(*pelem)) != SVt_PVHV) 0 2 2
3888 1 croak("%s: bad partition", who);
3889 }
3890 }
3891 }
3892 6 }
3893  
3894 /* ================================================================
3895 * XS INTERFACE
3896 * ================================================================ */
3897  
3898 MODULE = EV::Kafka PACKAGE = EV::Kafka::Conn
3899  
3900 PROTOTYPES: DISABLE
3901  
3902 EV::Kafka::Conn
3903 _new(char *cls, SV *loop_sv)
3904 CODE:
3905 {
3906 struct ev_loop *loop;
3907 ev_kafka_conn_t *self;
3908  
3909 69 ▶ if (SvOK(loop_sv) && sv_derived_from(loop_sv, "EV::Loop")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(loop_sv) && sv_derived_from(loop_sv, "EV::Loop")) { 1 68 69
if (SvOK(loop_sv) && sv_derived_from(loop_sv, "EV::Loop")) { 1 0 1
3910 1 loop = (struct ev_loop *)SvIV(SvRV(loop_sv));
3911 }
3912 else
3913 68 loop = EV_DEFAULT;
3914  
3915 69 Newxz(self, 1, ev_kafka_conn_t);
3916 69 self->magic = KF_MAGIC_ALIVE;
3917 69 self->loop = loop;
3918 69 ▶ if (loop != EV_DEFAULT)
Branch: 1 branch
BranchTrueFalseTotal
if (loop != EV_DEFAULT) 1 68 69
3919 1 self->loop_sv = newSVsv(loop_sv); /* freed in conn_do_free */
3920 69 self->fd = -1;
3921 69 self->state = CONN_DISCONNECTED;
3922 69 self->next_correlation_id = 1;
3923  
3924 69 Newx(self->rbuf, KF_BUF_INIT, char);
3925 69 self->rbuf_cap = KF_BUF_INIT;
3926 69 self->rbuf_len = 0;
3927 69 Newx(self->wbuf, KF_BUF_INIT, char);
3928 69 self->wbuf_cap = KF_BUF_INIT;
3929 69 self->wbuf_len = 0;
3930 69 self->wbuf_off = 0;
3931  
3932 69 ngx_queue_init(&self->cb_queue);
3933  
3934 /* Default client_id */
3935 69 self->client_id = savepv("ev-kafka");
3936 69 self->client_id_len = 8;
3937  
3938 /* Default: no API versions known */
3939 {
3940 int i;
3941 4485 ▶ for (i = 0; i < API_VERSIONS_MAX_KEY; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < API_VERSIONS_MAX_KEY; i++) 4416 69 4485
3942 4416 self->api_versions[i] = -1;
3943 }
3944  
3945 69 self->reconnect_delay_ms = 1000;
3946  
3947 69 RETVAL = self;
3948 }
3949 OUTPUT:
3950 RETVAL
3951  
3952 void
3953 DESTROY(EV::Kafka::Conn self)
3954 CODE:
3955 {
3956 86 ▶ if (!self) return; /* IV already nulled by an earlier DESTROY */
Branch: 1 branch
BranchTrueFalseTotal
if (!self) return; /* IV already nulled by an earlier DESTROY */ 17 69 86
3957 69 ▶ if (self->magic != KF_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != KF_MAGIC_ALIVE) return; 0 69 69
3958 69 ▶ if (self->free_pending) return; /* already scheduled for deferred free */
Branch: 1 branch
BranchTrueFalseTotal
if (self->free_pending) return; /* already scheduled for deferred free */ 0 69 69
3959  
3960 69 self->intentional_disconnect = 1;
3961 69 conn_cleanup(aTHX_ self);
3962  
3963 /* Mark dead BEFORE firing any callback (conn_cancel_pending fires the
3964 * pending command callbacks): a re-entrant DESTROY then hits the
3965 * free_pending guard, and conn_check_destroyed reports the conn gone. */
3966 69 self->magic = KF_MAGIC_FREED;
3967 69 self->free_pending = 1;
3968  
3969 /* Null the IV in the blessed ref BEFORE the drain: every copy of
3970 * the ref shares it, so teardown callbacks calling methods on the
3971 * dying conn get a clean croak (KF_REQUIRE_CONN). */
3972 69 sv_setiv(SvRV(ST(0)), 0);
3973  
3974 69 conn_cancel_pending(aTHX_ self, "destroyed");
3975  
3976 /* If we are inside an event-loop watcher callback (callback_depth>0),
3977 * that outermost frame runs conn_do_free when it unwinds to depth 0.
3978 * Otherwise free now. conn_cancel_pending only fires callbacks and a
3979 * nested DESTROY is blocked above, so self is still valid here. */
3980 69 ▶ if (self->callback_depth == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->callback_depth == 0) 68 1 69
3981 68 conn_do_free(aTHX_ self);
3982 }
3983  
3984 void
3985 connect(EV::Kafka::Conn self, const char *host, int port, double timeout = 0)
3986 CODE:
3987 {
3988 61 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 1 60 61
3989 60 conn_start_connect(aTHX_ self, host, port, timeout);
3990 }
3991  
3992 void
3993 disconnect(EV::Kafka::Conn self)
3994 CODE:
3995 {
3996 40 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 40 40
3997 40 self->intentional_disconnect = 1;
3998 40 ▶ if (self->reconnect_timing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timing) { 0 40 40
3999 0 ev_timer_stop(self->loop, &self->reconnect_timer);
4000 0 self->reconnect_timing = 0;
4001 }
4002 40 conn_handle_disconnect(aTHX_ self, "disconnected");
4003 }
4004  
4005 int
4006 connected(EV::Kafka::Conn self)
4007 CODE:
4008 {
4009 436 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 436 436
4010 436 ▶ RETVAL = (self->state == CONN_READY) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (self->state == CONN_READY) ? 1 : 0; 365 71 436
4011 }
4012 OUTPUT:
4013 RETVAL
4014  
4015 int
4016 state(EV::Kafka::Conn self)
4017 CODE:
4018 {
4019 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 1 0 1
4020 0 ▶ RETVAL = self->state;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->state; 0 0 0
4021 }
4022 OUTPUT:
4023 RETVAL
4024  
4025 int
4026 pending(EV::Kafka::Conn self)
4027 CODE:
4028 {
4029 44 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 1 43 44
4030 43 ▶ RETVAL = self->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->pending_count; 36 7 43
4031 }
4032 OUTPUT:
4033 RETVAL
4034  
4035 void
4036 on_error(EV::Kafka::Conn self, SV *cb = NULL)
4037 CODE:
4038 {
4039 155 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 155 155
4040 155 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 96 59 155
4041 155 ▶ if (cb && SvOK(cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvOK(cb)) { 155 0 155
if (cb && SvOK(cb)) { 123 32 155
4042 123 self->on_error = newSVsv(cb);
4043 }
4044 }
4045  
4046 void
4047 on_connect(EV::Kafka::Conn self, SV *cb = NULL)
4048 CODE:
4049 {
4050 80 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 80 80
4051 80 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 32 48 80
4052 80 ▶ if (cb && SvOK(cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvOK(cb)) { 80 0 80
if (cb && SvOK(cb)) { 48 32 80
4053 48 self->on_connect = newSVsv(cb);
4054 }
4055 }
4056  
4057 void
4058 on_disconnect(EV::Kafka::Conn self, SV *cb = NULL)
4059 CODE:
4060 {
4061 106 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 106 106
4062 106 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 63 43 106
4063 106 ▶ if (cb && SvOK(cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvOK(cb)) { 106 0 106
if (cb && SvOK(cb)) { 74 32 106
4064 74 self->on_disconnect = newSVsv(cb);
4065 }
4066 }
4067  
4068 void
4069 client_id(EV::Kafka::Conn self, const char *id = NULL)
4070 CODE:
4071 {
4072 33 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 33 33
4073 33 ▶ if (id) {
Branch: 1 branch
BranchTrueFalseTotal
if (id) { 33 0 33
4074 33 ▶ if (self->client_id) Safefree(self->client_id);
Branch: 1 branch
BranchTrueFalseTotal
if (self->client_id) Safefree(self->client_id); 33 0 33
4075 33 self->client_id = savepv(id);
4076 33 self->client_id_len = strlen(id);
4077 }
4078 }
4079  
4080 void
4081 tls(EV::Kafka::Conn self, int enable, const char *ca_file = NULL, int skip_verify = 0)
4082 CODE:
4083 {
4084 0 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 0 0
4085 0 self->tls_enabled = enable;
4086 0 ▶ if (self->tls_ca_file) { Safefree(self->tls_ca_file); self->tls_ca_file = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->tls_ca_file) { Safefree(self->tls_ca_file); self->tls_ca_file = NULL; } 0 0 0
4087 0 ▶ if (ca_file && *ca_file) self->tls_ca_file = savepv(ca_file);
Branch: 2 branches
BranchTrueFalseTotal
if (ca_file && *ca_file) self->tls_ca_file = savepv(ca_file); 0 0 0
if (ca_file && *ca_file) self->tls_ca_file = savepv(ca_file); 0 0 0
4088 0 self->tls_skip_verify = skip_verify;
4089 }
4090  
4091 void
4092 sasl(EV::Kafka::Conn self, SV *mechanism_sv, SV *username_sv = NULL, SV *password_sv = NULL)
4093 CODE:
4094 {
4095 5 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 5 5
4096 5 ▶ if (self->sasl_mechanism) { Safefree(self->sasl_mechanism); self->sasl_mechanism = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_mechanism) { Safefree(self->sasl_mechanism); self->sasl_mechanism = NULL; } 0 5 5
4097 5 ▶ if (self->sasl_username) { Safefree(self->sasl_username); self->sasl_username = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_username) { Safefree(self->sasl_username); self->sasl_username = NULL; } 0 5 5
4098 5 ▶ if (self->sasl_password) { Safefree(self->sasl_password); self->sasl_password = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->sasl_password) { Safefree(self->sasl_password); self->sasl_password = NULL; } 0 5 5
4099 5 ▶ SvGETMAGIC(mechanism_sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(mechanism_sv); 0 5 5
SvGETMAGIC(mechanism_sv); 0 0 0
4100 5 ▶ if (SvOK(mechanism_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(mechanism_sv)) { 4 1 5
4101 STRLEN mlen;
4102 4 self->sasl_mechanism = savepv(SvPV_nomg(mechanism_sv, mlen));
4103 4 ▶ if (username_sv && (SvGETMAGIC(username_sv), SvOK(username_sv))) {
Branch: 4 branches
BranchTrueFalseTotal
if (username_sv && (SvGETMAGIC(username_sv), SvOK(username_sv))) { 4 0 4
if (username_sv && (SvGETMAGIC(username_sv), SvOK(username_sv))) { 1 3 4
if (username_sv && (SvGETMAGIC(username_sv), SvOK(username_sv))) { 0 1 1
if (username_sv && (SvGETMAGIC(username_sv), SvOK(username_sv))) { 3 1 4
4104 STRLEN len;
4105 3 const char *u = SvPV_nomg(username_sv, len);
4106 3 ▶ if (len) self->sasl_username = savepv(u);
Branch: 1 branch
BranchTrueFalseTotal
if (len) self->sasl_username = savepv(u); 2 1 3
4107 }
4108 4 ▶ if (password_sv && (SvGETMAGIC(password_sv), SvOK(password_sv))) {
Branch: 4 branches
BranchTrueFalseTotal
if (password_sv && (SvGETMAGIC(password_sv), SvOK(password_sv))) { 4 0 4
if (password_sv && (SvGETMAGIC(password_sv), SvOK(password_sv))) { 1 3 4
if (password_sv && (SvGETMAGIC(password_sv), SvOK(password_sv))) { 0 1 1
if (password_sv && (SvGETMAGIC(password_sv), SvOK(password_sv))) { 3 1 4
4109 STRLEN len;
4110 3 const char *p = SvPV_nomg(password_sv, len);
4111 3 ▶ if (len) self->sasl_password = savepv(p);
Branch: 1 branch
BranchTrueFalseTotal
if (len) self->sasl_password = savepv(p); 2 1 3
4112 }
4113 }
4114 }
4115  
4116 void
4117 auto_reconnect(EV::Kafka::Conn self, int enable, int delay_ms = 1000)
4118 CODE:
4119 {
4120 34 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 34 34
4121 34 self->auto_reconnect = enable;
4122 34 self->reconnect_delay_ms = delay_ms;
4123 }
4124  
4125 void
4126 metadata(EV::Kafka::Conn self, SV *topics_sv, SV *cb)
4127 CODE:
4128 {
4129 50 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 1 49 50
4130 49 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 1 48 49
4131 1 croak("not connected");
4132  
4133 kf_buf_t body;
4134 48 kf_buf_init(&body);
4135  
4136 /* Metadata v1-v4 (non-flexible) */
4137 48 int16_t ver = self->api_versions[API_METADATA];
4138 48 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 7 41 48
4139 48 ▶ if (ver > 4) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 4) ver = 4; 0 48 48
4140  
4141 48 ▶ if (SvOK(topics_sv) && SvROK(topics_sv) && SvTYPE(SvRV(topics_sv)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvOK(topics_sv) && SvROK(topics_sv) && SvTYPE(SvRV(topics_sv)) == SVt_PVAV) { 0 48 48
if (SvOK(topics_sv) && SvROK(topics_sv) && SvTYPE(SvRV(topics_sv)) == SVt_PVAV) { 0 0 0
if (SvOK(topics_sv) && SvROK(topics_sv) && SvTYPE(SvRV(topics_sv)) == SVt_PVAV) { 0 0 0
4142 0 AV *topics = (AV*)SvRV(topics_sv);
4143 0 SSize_t i, count = av_len(topics) + 1;
4144 0 kf_buf_append_i32(&body, (int32_t)count);
4145 0 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 0 0 0
4146 0 SV **elem = av_fetch(topics, i, 0);
4147 STRLEN tlen;
4148 0 const char *tname = SvPV(*elem, tlen);
4149 0 kf_buf_append_string(&body, tname, (int16_t)tlen);
4150 }
4151 } else {
4152 48 kf_buf_append_i32(&body, -1); /* null array = all topics */
4153 }
4154  
4155 /* allow_auto_topic_creation (v4+) */
4156 48 ▶ if (ver >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 4) 0 48 48
4157 0 kf_buf_append_i8(&body, 1);
4158  
4159 48 conn_send_request(aTHX_ self, API_METADATA, ver, &body, cb, 0, 0);
4160 48 kf_buf_free(&body);
4161 }
4162  
4163 void
4164 api_versions(EV::Kafka::Conn self)
4165 PPCODE:
4166 {
4167 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
4168 1 ▶ if (!self->api_versions_known)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->api_versions_known) 0 1 1
4169 0 XSRETURN_UNDEF;
4170  
4171 1 HV *hv = newHV();
4172 int i;
4173 65 ▶ for (i = 0; i < API_VERSIONS_MAX_KEY; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < API_VERSIONS_MAX_KEY; i++) { 64 1 65
4174 64 ▶ if (self->api_versions[i] >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->api_versions[i] >= 0) { 16 48 64
4175 char key[8];
4176 16 int klen = snprintf(key, sizeof(key), "%d", i);
4177 16 hv_store(hv, key, klen, newSViv(self->api_versions[i]), 0);
4178 }
4179 }
4180 1 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 1 1
4181 1 mPUSHs(newRV_noinc((SV*)hv));
4182 1 XSRETURN(1);
4183 }
4184  
4185 void
4186 fetch(EV::Kafka::Conn self, const char *topic, int partition, SV *offset_sv, SV *arg1 = NULL, SV *arg2 = NULL)
4187 CODE:
4188 {
4189 2 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 2 2
4190 2 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 1 1 2
4191 1 croak("not connected");
4192  
4193 /* Accept either fetch(..., $cb) or fetch(..., \%opts, $cb).
4194 * $opts may set max_bytes, max_wait_ms, min_bytes. */
4195 1 SV *cb = NULL;
4196 1 HV *opts = NULL;
4197 1 ▶ if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 1 0 1
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 1 0 1
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 0 1 1
4198 0 opts = (HV*)SvRV(arg1);
4199 0 cb = arg2;
4200 } else {
4201 1 cb = arg1;
4202 }
4203  
4204 1 int32_t max_bytes = 1048576;
4205 1 int32_t max_wait_ms = 500;
4206 1 int32_t min_bytes = 1;
4207 1 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 0 1 1
4208 SV **v;
4209 0 ▶ if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v)) 0 0 0
4210 0 max_bytes = (int32_t)SvIV(*v);
4211 0 ▶ if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v)) 0 0 0
4212 0 max_wait_ms = (int32_t)SvIV(*v);
4213 0 ▶ if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v)) 0 0 0
4214 0 min_bytes = (int32_t)SvIV(*v);
4215 }
4216  
4217 1 int64_t offset = SvIV(offset_sv);
4218 1 STRLEN topic_len = strlen(topic);
4219  
4220 1 int16_t ver = self->api_versions[API_FETCH];
4221 1 ▶ if (ver < 0) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 4; 0 1 1
4222 1 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 1 1
4223  
4224 kf_buf_t body;
4225 1 kf_buf_init(&body);
4226  
4227 1 kf_buf_append_i32(&body, -1); /* replica_id = -1 (consumer) */
4228 1 kf_buf_append_i32(&body, max_wait_ms);
4229 1 kf_buf_append_i32(&body, min_bytes);
4230  
4231 /* max_bytes (v3+) */
4232 1 ▶ if (ver >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) 1 0 1
4233 1 kf_buf_append_i32(&body, max_bytes);
4234  
4235 /* isolation_level (v4+) */
4236 1 ▶ if (ver >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 4) 1 0 1
4237 1 kf_buf_append_i8(&body, 0); /* READ_UNCOMMITTED */
4238  
4239 /* session_id + session_epoch (v7+) */
4240 1 ▶ if (ver >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) { 1 0 1
4241 1 kf_buf_append_i32(&body, 0); /* session_id */
4242 1 kf_buf_append_i32(&body, -1); /* session_epoch */
4243 }
4244  
4245 /* topics: ARRAY(1) */
4246 1 kf_buf_append_i32(&body, 1);
4247 1 kf_buf_append_string(&body, topic, (int16_t)topic_len);
4248  
4249 /* partitions: ARRAY(1) */
4250 1 kf_buf_append_i32(&body, 1);
4251 1 kf_buf_append_i32(&body, (int32_t)partition);
4252  
4253 /* fetch_offset */
4254 1 kf_buf_append_i64(&body, offset);
4255  
4256 /* log_start_offset (v5+) */
4257 1 ▶ if (ver >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 5) 1 0 1
4258 1 kf_buf_append_i64(&body, -1);
4259  
4260 /* partition_max_bytes */
4261 1 kf_buf_append_i32(&body, max_bytes);
4262  
4263 /* forgotten_topics_data (v7+) */
4264 1 ▶ if (ver >= 7)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) 1 0 1
4265 1 kf_buf_append_i32(&body, 0); /* empty array */
4266  
4267 1 conn_send_request(aTHX_ self, API_FETCH, ver, &body, cb, 0, 0);
4268 1 kf_buf_free(&body);
4269 }
4270  
4271 void
4272 fetch_multi(EV::Kafka::Conn self, SV *topics_sv, SV *arg1 = NULL, SV *arg2 = NULL)
4273 CODE:
4274 {
4275 20214 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 20214 20214
4276 20214 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 20214 20214
4277 0 croak("not connected");
4278  
4279 /* topics_sv: { topic => [{partition => N, offset => N}, ...], ... }
4280 * accept fetch_multi(\%topics, $cb) or fetch_multi(\%topics, \%opts, $cb). */
4281 20214 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVHV) 20214 0 20214
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVHV) 0 20214 20214
4282 0 croak("fetch_multi: expected hashref");
4283 20214 HV *topics_hv = (HV*)SvRV(topics_sv);
4284  
4285 20214 SV *cb = NULL;
4286 20214 HV *opts = NULL;
4287 20214 ▶ if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 20214 0 20214
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 20214 0 20214
if (arg1 && SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVHV) { 11 20203 20214
4288 11 opts = (HV*)SvRV(arg1);
4289 11 cb = arg2;
4290 } else {
4291 20203 cb = arg1;
4292 }
4293  
4294 20214 int32_t max_bytes = 1048576;
4295 20214 int32_t max_wait_ms = 500;
4296 20214 int32_t min_bytes = 1;
4297 20214 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 11 20203 20214
4298 SV **v;
4299 11 ▶ if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v)) 11 0 11
if ((v = hv_fetchs(opts, "max_bytes", 0)) && SvOK(*v)) 11 0 11
4300 11 max_bytes = (int32_t)SvIV(*v);
4301 11 ▶ if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v)) 11 0 11
if ((v = hv_fetchs(opts, "max_wait_ms", 0)) && SvOK(*v)) 11 0 11
4302 11 max_wait_ms = (int32_t)SvIV(*v);
4303 11 ▶ if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v)) 11 0 11
if ((v = hv_fetchs(opts, "min_bytes", 0)) && SvOK(*v)) 11 0 11
4304 11 min_bytes = (int32_t)SvIV(*v);
4305 }
4306  
4307 20214 int16_t ver = self->api_versions[API_FETCH];
4308 20214 ▶ if (ver < 0) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 4; 20203 11 20214
4309 20214 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 20214 20214
4310  
4311 /* Validate up front so croak() doesn't longjmp over kf_buf_free. */
4312 {
4313 20214 hv_iterinit(topics_hv);
4314 HE *entry;
4315 20226 ▶ while ((entry = hv_iternext(topics_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(topics_hv))) { 20214 12 20226
4316 20214 SV *parts_sv = hv_iterval(topics_hv, entry);
4317 20214 ▶ if (!SvROK(parts_sv) || SvTYPE(SvRV(parts_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(parts_sv) || SvTYPE(SvRV(parts_sv)) != SVt_PVAV) 20213 1 20214
if (!SvROK(parts_sv) || SvTYPE(SvRV(parts_sv)) != SVt_PVAV) 0 20213 20213
4318 1 croak("fetch_multi: value must be an arrayref");
4319 20213 AV *parts_av = (AV*)SvRV(parts_sv);
4320 20213 SSize_t i, pc = av_len(parts_av) + 1;
4321 20225 ▶ for (i = 0; i < pc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pc; i++) { 20213 12 20225
4322 20213 SV **elem = av_fetch(parts_av, i, 0);
4323 20213 ▶ if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 20213 0 20213
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 12 20201 20213
if (!elem || !SvROK(*elem) || SvTYPE(SvRV(*elem)) != SVt_PVHV) 0 12 12
4324 20201 croak("fetch_multi: partition entry must be a hashref");
4325 }
4326 }
4327 }
4328  
4329 kf_buf_t body;
4330 12 kf_buf_init(&body);
4331  
4332 12 kf_buf_append_i32(&body, -1); /* replica_id */
4333 12 kf_buf_append_i32(&body, max_wait_ms);
4334 12 kf_buf_append_i32(&body, min_bytes);
4335 12 ▶ if (ver >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) 12 0 12
4336 12 kf_buf_append_i32(&body, max_bytes);
4337 12 ▶ if (ver >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 4) 12 0 12
4338 12 kf_buf_append_i8(&body, 0); /* isolation_level */
4339 12 ▶ if (ver >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) { 11 1 12
4340 11 kf_buf_append_i32(&body, 0); /* session_id */
4341 11 kf_buf_append_i32(&body, -1); /* session_epoch */
4342 }
4343  
4344 /* topics array */
4345 12 ▶ kf_buf_append_i32(&body, (int32_t)HvUSEDKEYS(topics_hv));
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, (int32_t)HvUSEDKEYS(topics_hv)); 0 12 12
4346  
4347 12 hv_iterinit(topics_hv);
4348 HE *entry;
4349 24 ▶ while ((entry = hv_iternext(topics_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(topics_hv))) { 12 12 24
4350 I32 tlen;
4351 12 const char *tname = hv_iterkey(entry, &tlen);
4352 12 kf_buf_append_string(&body, tname, (int16_t)tlen);
4353  
4354 12 SV *parts_sv = hv_iterval(topics_hv, entry);
4355 12 AV *parts_av = (AV*)SvRV(parts_sv); /* validated above */
4356 12 SSize_t i, pc = av_len(parts_av) + 1;
4357 12 kf_buf_append_i32(&body, (int32_t)pc);
4358  
4359 24 ▶ for (i = 0; i < pc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pc; i++) { 12 12 24
4360 12 SV **elem = av_fetch(parts_av, i, 0);
4361 12 HV *ph = (HV*)SvRV(*elem); /* validated above */
4362  
4363 12 SV **pid_sv = hv_fetch(ph, "partition", 9, 0);
4364 12 ▶ int32_t pid = pid_sv ? (int32_t)SvIV(*pid_sv) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int32_t pid = pid_sv ? (int32_t)SvIV(*pid_sv) : 0; 12 0 12
4365 12 kf_buf_append_i32(&body, pid);
4366  
4367 12 SV **off_sv = hv_fetch(ph, "offset", 6, 0);
4368 12 ▶ int64_t offset = off_sv ? (int64_t)SvIV(*off_sv) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int64_t offset = off_sv ? (int64_t)SvIV(*off_sv) : 0; 12 0 12
4369 12 kf_buf_append_i64(&body, offset);
4370  
4371 12 ▶ if (ver >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 5) 11 1 12
4372 11 kf_buf_append_i64(&body, -1); /* log_start_offset */
4373  
4374 12 kf_buf_append_i32(&body, max_bytes); /* partition_max_bytes */
4375 }
4376 }
4377  
4378 12 ▶ if (ver >= 7)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) 11 1 12
4379 11 kf_buf_append_i32(&body, 0); /* forgotten_topics_data */
4380  
4381 12 conn_send_request(aTHX_ self, API_FETCH, ver, &body, cb, 0, 0);
4382 12 kf_buf_free(&body);
4383 }
4384  
4385 void
4386 produce_batch(EV::Kafka::Conn self, const char *topic, int partition, SV *records_sv, SV *opts_sv = NULL, SV *cb = NULL)
4387 CODE:
4388 {
4389 16 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 16 16
4390 16 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 16 16
4391 0 croak("not connected");
4392  
4393 16 ▶ if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV) 16 0 16
if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV) 0 16 16
4394 0 croak("produce_batch: expected arrayref of records");
4395 16 AV *records_av = (AV*)SvRV(records_sv);
4396  
4397 16 int16_t acks = 1;
4398 16 int compression = COMPRESS_NONE;
4399 16 int64_t producer_id = -1;
4400 16 int16_t producer_epoch = -1;
4401 16 int32_t base_sequence = -1;
4402 16 const char *txn_id = NULL;
4403 16 STRLEN txn_id_len = 0;
4404  
4405 32 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 16 0 16
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 16 0 16
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 16 0 16
4406 16 HV *opts = (HV*)SvRV(opts_sv);
4407 SV **tmp;
4408 16 ▶ if ((tmp = hv_fetch(opts, "acks", 4, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "acks", 4, 0))) 16 0 16
4409 16 acks = (int16_t)SvIV(*tmp);
4410 16 ▶ if ((tmp = hv_fetch(opts, "transactional_id", 16, 0)) && SvOK(*tmp))
Branch: 2 branches
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "transactional_id", 16, 0)) && SvOK(*tmp)) 3 13 16
if ((tmp = hv_fetch(opts, "transactional_id", 16, 0)) && SvOK(*tmp)) 3 0 3
4411 6 txn_id = SvPV(*tmp, txn_id_len);
4412 16 ▶ if ((tmp = hv_fetch(opts, "compression", 11, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "compression", 11, 0))) { 0 16 16
4413 STRLEN clen;
4414 0 const char *cstr = SvPV(*tmp, clen);
4415 0 ▶ if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE;
Branch: 2 branches
BranchTrueFalseTotal
if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE; 0 0 0
if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE; 0 0 0
4416 #ifdef HAVE_LZ4
4417 else if (clen == 3 && memcmp(cstr, "lz4", 3) == 0) compression = COMPRESS_LZ4;
4418 #endif
4419 #ifdef HAVE_ZLIB
4420 0 ▶ else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP;
Branch: 2 branches
BranchTrueFalseTotal
else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP; 0 0 0
else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP; 0 0 0
4421 #endif
4422 #ifdef HAVE_ZSTD
4423 else if (clen == 4 && memcmp(cstr, "zstd", 4) == 0) compression = COMPRESS_ZSTD;
4424 #endif
4425 #ifdef HAVE_SNAPPY
4426 else if (clen == 6 && memcmp(cstr, "snappy", 6) == 0) compression = COMPRESS_SNAPPY;
4427 #endif
4428 0 else croak("unsupported compression: %.*s", (int)clen, cstr);
4429 }
4430 16 ▶ if ((tmp = hv_fetch(opts, "producer_id", 11, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "producer_id", 11, 0))) 9 7 16
4431 9 producer_id = (int64_t)SvIV(*tmp);
4432 16 ▶ if ((tmp = hv_fetch(opts, "producer_epoch", 14, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "producer_epoch", 14, 0))) 9 7 16
4433 9 producer_epoch = (int16_t)SvIV(*tmp);
4434 16 ▶ if ((tmp = hv_fetch(opts, "base_sequence", 13, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "base_sequence", 13, 0))) 9 7 16
4435 9 base_sequence = (int32_t)SvIV(*tmp);
4436 0 ▶ } else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 0 0 0
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 0 0 0
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 0 0 0
4437 0 cb = opts_sv;
4438 0 opts_sv = NULL;
4439 }
4440  
4441 struct timeval tv;
4442 16 gettimeofday(&tv, NULL);
4443 16 int64_t timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
4444  
4445 kf_buf_t batch;
4446 16 kf_encode_record_batch_multi(aTHX_ &batch, records_av, timestamp,
4447 compression, producer_id, producer_epoch, base_sequence,
4448 txn_id != NULL ? 1 : 0);
4449  
4450 16 int16_t ver = self->api_versions[API_PRODUCE];
4451 16 ▶ if (ver < 0) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 3; 0 16 16
4452 16 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 16 16
4453  
4454 16 STRLEN topic_len = strlen(topic);
4455  
4456 kf_buf_t body;
4457 16 kf_buf_init(&body);
4458  
4459 16 ▶ if (ver >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) 16 0 16
4460 16 ▶ kf_buf_append_nullable_string(&body, txn_id, txn_id ? (int16_t)txn_id_len : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_nullable_string(&body, txn_id, txn_id ? (int16_t)txn_id_len : 0); 3 13 16
4461  
4462 16 kf_buf_append_i16(&body, acks);
4463 16 kf_buf_append_i32(&body, 30000);
4464  
4465 16 kf_buf_append_i32(&body, 1);
4466 16 kf_buf_append_string(&body, topic, (int16_t)topic_len);
4467 16 kf_buf_append_i32(&body, 1);
4468 16 kf_buf_append_i32(&body, (int32_t)partition);
4469 16 kf_buf_append_i32(&body, (int32_t)batch.len);
4470 16 kf_buf_append(&body, batch.data, batch.len);
4471  
4472 32 ▶ conn_send_request(aTHX_ self, API_PRODUCE, ver, &body,
Branch: 1 branch
BranchTrueFalseTotal
conn_send_request(aTHX_ self, API_PRODUCE, ver, &body, 16 0 16
4473 16 ▶ (cb && SvOK(cb)) ? cb : NULL, 0, (acks == 0) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
(cb && SvOK(cb)) ? cb : NULL, 0, (acks == 0) ? 1 : 0); 16 0 16
4474  
4475 16 kf_buf_free(&body);
4476 16 kf_buf_free(&batch);
4477 }
4478  
4479 void
4480 list_offsets(EV::Kafka::Conn self, const char *topic, int partition, SV *timestamp_sv, SV *cb)
4481 CODE:
4482 {
4483 5 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 5 5
4484 5 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 5 5
4485 0 croak("not connected");
4486  
4487 5 int64_t timestamp = SvIV(timestamp_sv);
4488 5 STRLEN topic_len = strlen(topic);
4489  
4490 /* -2 = earliest, -1 = latest */
4491 5 int16_t ver = self->api_versions[API_LIST_OFFSETS];
4492 5 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 5 5
4493 5 ▶ if (ver > 5) ver = 5;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 5) ver = 5; 0 5 5
4494  
4495 kf_buf_t body;
4496 5 kf_buf_init(&body);
4497  
4498 5 kf_buf_append_i32(&body, -1); /* replica_id */
4499  
4500 /* isolation_level (v2+) */
4501 5 ▶ if (ver >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 2) 0 5 5
4502 0 kf_buf_append_i8(&body, 0);
4503  
4504 /* topics: ARRAY(1) */
4505 5 kf_buf_append_i32(&body, 1);
4506 5 kf_buf_append_string(&body, topic, (int16_t)topic_len);
4507  
4508 /* partitions: ARRAY(1) */
4509 5 kf_buf_append_i32(&body, 1);
4510 5 kf_buf_append_i32(&body, (int32_t)partition);
4511  
4512 /* current_leader_epoch (v4+) */
4513 5 ▶ if (ver >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 4) 0 5 5
4514 0 kf_buf_append_i32(&body, -1);
4515  
4516 5 kf_buf_append_i64(&body, timestamp);
4517  
4518 5 conn_send_request(aTHX_ self, API_LIST_OFFSETS, ver, &body, cb, 0, 0);
4519 5 kf_buf_free(&body);
4520 }
4521  
4522 void
4523 produce(EV::Kafka::Conn self, const char *topic, int partition, SV *key_sv, SV *value_sv, SV *opts_sv = NULL, SV *cb = NULL)
4524 CODE:
4525 {
4526 4 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 4 4
4527 4 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 1 3 4
4528 1 croak("not connected");
4529  
4530 /* Handle optional opts hash: produce($topic, $part, $key, $val, \%opts, $cb)
4531 * or produce($topic, $part, $key, $val, $cb)
4532 */
4533 3 HV *headers = NULL;
4534 3 int16_t acks = 1;
4535 3 int compression = COMPRESS_NONE;
4536  
4537 4 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 3 0 3
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 3 0 3
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 1 2 3
4538 1 HV *opts = (HV*)SvRV(opts_sv);
4539 SV **tmp;
4540 1 ▶ if ((tmp = hv_fetch(opts, "headers", 7, 0)) && SvROK(*tmp) && SvTYPE(SvRV(*tmp)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "headers", 7, 0)) && SvROK(*tmp) && SvTYPE(SvRV(*tmp)) == SVt_PVHV) 0 1 1
if ((tmp = hv_fetch(opts, "headers", 7, 0)) && SvROK(*tmp) && SvTYPE(SvRV(*tmp)) == SVt_PVHV) 0 0 0
if ((tmp = hv_fetch(opts, "headers", 7, 0)) && SvROK(*tmp) && SvTYPE(SvRV(*tmp)) == SVt_PVHV) 0 0 0
4541 0 headers = (HV*)SvRV(*tmp);
4542 1 ▶ if ((tmp = hv_fetch(opts, "acks", 4, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "acks", 4, 0))) 1 0 1
4543 1 acks = (int16_t)SvIV(*tmp);
4544 1 ▶ if ((tmp = hv_fetch(opts, "compression", 11, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((tmp = hv_fetch(opts, "compression", 11, 0))) { 0 1 1
4545 STRLEN clen;
4546 0 const char *cstr = SvPV(*tmp, clen);
4547 0 ▶ if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE;
Branch: 2 branches
BranchTrueFalseTotal
if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE; 0 0 0
if (clen == 4 && memcmp(cstr, "none", 4) == 0) compression = COMPRESS_NONE; 0 0 0
4548 #ifdef HAVE_LZ4
4549 else if (clen == 3 && memcmp(cstr, "lz4", 3) == 0) compression = COMPRESS_LZ4;
4550 #endif
4551 #ifdef HAVE_ZLIB
4552 0 ▶ else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP;
Branch: 2 branches
BranchTrueFalseTotal
else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP; 0 0 0
else if (clen == 4 && memcmp(cstr, "gzip", 4) == 0) compression = COMPRESS_GZIP; 0 0 0
4553 #endif
4554 #ifdef HAVE_ZSTD
4555 else if (clen == 4 && memcmp(cstr, "zstd", 4) == 0) compression = COMPRESS_ZSTD;
4556 #endif
4557 #ifdef HAVE_SNAPPY
4558 else if (clen == 6 && memcmp(cstr, "snappy", 6) == 0) compression = COMPRESS_SNAPPY;
4559 #endif
4560 0 else croak("unsupported compression: %.*s", (int)clen, cstr);
4561 }
4562 2 ▶ } else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 2 0 2
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 2 0 2
} else if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVCV) { 2 0 2
4563 /* opts_sv is actually the callback */
4564 2 cb = opts_sv;
4565 2 opts_sv = NULL;
4566 }
4567  
4568 3 const char *key = NULL;
4569 3 STRLEN key_len = 0;
4570 3 ▶ if (SvOK(key_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(key_sv)) 3 0 3
4571 3 key = SvPV(key_sv, key_len);
4572  
4573 3 const char *value = NULL;
4574 3 STRLEN value_len = 0;
4575 3 ▶ if (SvOK(value_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(value_sv)) 3 0 3
4576 3 value = SvPV(value_sv, value_len);
4577  
4578 /* Build RecordBatch */
4579 int64_t timestamp;
4580 {
4581 3 SV **ts_tmp = NULL;
4582 3 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 1 2 3
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 1 0 1
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 1 0 1
4583 1 ts_tmp = hv_fetch((HV*)SvRV(opts_sv), "timestamp", 9, 0);
4584 3 ▶ if (ts_tmp && SvOK(*ts_tmp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ts_tmp && SvOK(*ts_tmp)) { 0 3 3
if (ts_tmp && SvOK(*ts_tmp)) { 0 0 0
4585 0 timestamp = (int64_t)SvIV(*ts_tmp);
4586 } else {
4587 struct timeval tv;
4588 3 gettimeofday(&tv, NULL);
4589 3 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
4590 }
4591 }
4592  
4593 /* Build a single-element AV and dispatch to the multi encoder so
4594 * the compression / CRC framing logic lives in one place. */
4595 kf_buf_t batch;
4596 {
4597 3 HV *rec = newHV();
4598 3 ▶ if (key) hv_stores(rec, "key", newSVpvn(key, key_len));
Branch: 1 branch
BranchTrueFalseTotal
if (key) hv_stores(rec, "key", newSVpvn(key, key_len)); 3 0 3
4599 3 ▶ if (value) hv_stores(rec, "value", newSVpvn(value, value_len));
Branch: 1 branch
BranchTrueFalseTotal
if (value) hv_stores(rec, "value", newSVpvn(value, value_len)); 3 0 3
4600 3 ▶ if (headers) hv_stores(rec, "headers", newRV_inc((SV*)headers));
Branch: 1 branch
BranchTrueFalseTotal
if (headers) hv_stores(rec, "headers", newRV_inc((SV*)headers)); 0 3 3
4601 3 AV *records_av = newAV();
4602 3 av_push(records_av, newRV_noinc((SV*)rec));
4603 3 kf_encode_record_batch_multi(aTHX_ &batch, records_av, timestamp,
4604 compression, -1, -1, -1, 0);
4605 3 SvREFCNT_dec((SV*)records_av);
4606 }
4607  
4608 /* Use Produce version — cap at v7 */
4609 3 int16_t ver = self->api_versions[API_PRODUCE];
4610 3 ▶ if (ver < 0) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 3; 0 3 3
4611 3 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 3 3
4612  
4613 3 STRLEN topic_len = strlen(topic);
4614  
4615 kf_buf_t body;
4616 3 kf_buf_init(&body);
4617  
4618 /* transactional_id (v3+): nullable string = null */
4619 3 ▶ if (ver >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) 3 0 3
4620 3 kf_buf_append_nullable_string(&body, NULL, 0);
4621  
4622 3 kf_buf_append_i16(&body, acks); /* acks */
4623 3 kf_buf_append_i32(&body, 30000); /* timeout_ms = 30s */
4624  
4625 /* topic_data: ARRAY(1) */
4626 3 kf_buf_append_i32(&body, 1); /* 1 topic */
4627 3 kf_buf_append_string(&body, topic, (int16_t)topic_len);
4628  
4629 /* partition_data: ARRAY(1) */
4630 3 kf_buf_append_i32(&body, 1); /* 1 partition */
4631 3 kf_buf_append_i32(&body, partition);
4632  
4633 /* record_set: BYTES (i32 length + record_batch) */
4634 3 kf_buf_append_i32(&body, (int32_t)batch.len);
4635 3 kf_buf_append(&body, batch.data, batch.len);
4636  
4637 6 ▶ conn_send_request(aTHX_ self, API_PRODUCE, ver, &body,
Branch: 1 branch
BranchTrueFalseTotal
conn_send_request(aTHX_ self, API_PRODUCE, ver, &body, 3 0 3
4638 3 ▶ (cb && SvOK(cb)) ? cb : NULL, 0, (acks == 0) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
(cb && SvOK(cb)) ? cb : NULL, 0, (acks == 0) ? 1 : 0); 3 0 3
4639  
4640 3 kf_buf_free(&body);
4641 3 kf_buf_free(&batch);
4642 }
4643  
4644 void
4645 find_coordinator(EV::Kafka::Conn self, const char *group_id, SV *cb, int key_type = 0)
4646 CODE:
4647 {
4648 9 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 9 9
4649 9 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 9 9
4650 0 croak("not connected");
4651  
4652 9 int16_t ver = self->api_versions[API_FIND_COORDINATOR];
4653 9 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 9 9
4654 9 ▶ if (ver > 2) ver = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 2) ver = 2; 0 9 9
4655  
4656 kf_buf_t body;
4657 9 kf_buf_init(&body);
4658  
4659 9 STRLEN glen = strlen(group_id);
4660 9 kf_buf_append_string(&body, group_id, (int16_t)glen);
4661  
4662 /* key_type (v1+): 0=group, 1=transaction */
4663 9 ▶ if (ver >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) 9 0 9
4664 9 kf_buf_append_i8(&body, (int8_t)key_type);
4665  
4666 9 conn_send_request(aTHX_ self, API_FIND_COORDINATOR, ver, &body, cb, 0, 0);
4667 9 kf_buf_free(&body);
4668 }
4669  
4670 void
4671 join_group(EV::Kafka::Conn self, const char *group_id, const char *member_id, SV *topics_sv, SV *cb, int session_timeout_ms = 30000, int rebalance_timeout_ms = 60000, SV *group_instance_id_sv = NULL)
4672 CODE:
4673 {
4674 9 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 9 9
4675 9 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 9 9
4676 0 croak("not connected");
4677  
4678 9 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 8 1 9
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 8 8
4679 1 croak("join_group: expected arrayref");
4680  
4681 8 int16_t ver = self->api_versions[API_JOIN_GROUP];
4682 8 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 8 8
4683 8 ▶ if (ver > 5) ver = 5;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 5) ver = 5; 0 8 8
4684  
4685 kf_buf_t body;
4686 8 kf_buf_init(&body);
4687  
4688 8 STRLEN glen = strlen(group_id);
4689 8 kf_buf_append_string(&body, group_id, (int16_t)glen);
4690 8 kf_buf_append_i32(&body, session_timeout_ms);
4691  
4692 /* rebalance_timeout_ms (v1+) */
4693 8 ▶ if (ver >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) 8 0 8
4694 8 kf_buf_append_i32(&body, rebalance_timeout_ms);
4695  
4696 8 STRLEN mlen = strlen(member_id);
4697 8 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4698  
4699 /* group_instance_id (v5+) */
4700 8 ▶ if (ver >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 5) { 0 8 8
4701 0 ▶ if (group_instance_id_sv && SvOK(group_instance_id_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
4702 STRLEN gilen;
4703 0 const char *gi = SvPV(group_instance_id_sv, gilen);
4704 0 kf_buf_append_nullable_string(&body, gi, (int16_t)gilen);
4705 } else {
4706 0 kf_buf_append_nullable_string(&body, NULL, 0);
4707 }
4708 }
4709  
4710 /* protocol_type = "consumer" */
4711 8 kf_buf_append_string(&body, "consumer", 8);
4712  
4713 /* protocols: ARRAY(1) */
4714 8 kf_buf_append_i32(&body, 1);
4715  
4716 /* protocol name = "sticky" */
4717 8 kf_buf_append_string(&body, "sticky", 6);
4718  
4719 /* protocol metadata (ConsumerProtocol subscription) */
4720 /* Version:0, Topics:array, UserData:null */
4721 kf_buf_t meta;
4722 8 kf_buf_init(&meta);
4723 8 kf_buf_append_i16(&meta, 0); /* version */
4724  
4725 8 AV *topics = (AV*)SvRV(topics_sv);
4726 8 SSize_t i, tc = av_len(topics) + 1;
4727 8 kf_buf_append_i32(&meta, (int32_t)tc);
4728 16 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 8 8 16
4729 8 SV **elem = av_fetch(topics, i, 0);
4730 STRLEN tlen;
4731 8 const char *tname = SvPV(*elem, tlen);
4732 8 kf_buf_append_string(&meta, tname, (int16_t)tlen);
4733 }
4734 8 kf_buf_append_nullable_bytes(&meta, NULL, 0); /* user_data = null */
4735  
4736 8 kf_buf_append_bytes(&body, meta.data, (int32_t)meta.len);
4737 8 kf_buf_free(&meta);
4738  
4739 8 conn_send_request(aTHX_ self, API_JOIN_GROUP, ver, &body, cb, 0, 0);
4740 8 kf_buf_free(&body);
4741 }
4742  
4743 void
4744 sync_group(EV::Kafka::Conn self, const char *group_id, int generation_id, const char *member_id, SV *assignments_sv, SV *cb, SV *group_instance_id_sv = NULL)
4745 CODE:
4746 {
4747 4 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 4 4
4748 4 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 4 4
4749 0 croak("not connected");
4750  
4751 4 int16_t ver = self->api_versions[API_SYNC_GROUP];
4752 4 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 4 4
4753 4 ▶ if (ver > 3) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 3) ver = 3; 0 4 4
4754  
4755 kf_buf_t body;
4756 4 kf_buf_init(&body);
4757  
4758 4 STRLEN glen = strlen(group_id);
4759 4 kf_buf_append_string(&body, group_id, (int16_t)glen);
4760 4 kf_buf_append_i32(&body, generation_id);
4761  
4762 4 STRLEN mlen = strlen(member_id);
4763 4 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4764  
4765 /* group_instance_id (v3+) */
4766 4 ▶ if (ver >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) { 0 4 4
4767 0 ▶ if (group_instance_id_sv && SvOK(group_instance_id_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
4768 STRLEN gilen;
4769 0 const char *gi = SvPV(group_instance_id_sv, gilen);
4770 0 kf_buf_append_nullable_string(&body, gi, (int16_t)gilen);
4771 } else {
4772 0 kf_buf_append_nullable_string(&body, NULL, 0);
4773 }
4774 }
4775  
4776 /* assignments: ARRAY of {member_id, assignment_bytes} */
4777 4 ▶ if (SvOK(assignments_sv) && SvROK(assignments_sv)
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(assignments_sv) && SvROK(assignments_sv) 4 0 4
if (SvOK(assignments_sv) && SvROK(assignments_sv) 4 0 4
4778 8 ▶ && SvTYPE(SvRV(assignments_sv)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(assignments_sv)) == SVt_PVAV) { 4 0 4
4779 4 AV *assigns = (AV*)SvRV(assignments_sv);
4780 4 SSize_t i, ac = av_len(assigns) + 1;
4781 4 kf_buf_append_i32(&body, (int32_t)ac);
4782  
4783 8 ▶ for (i = 0; i < ac; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ac; i++) { 4 4 8
4784 4 SV **elem = av_fetch(assigns, i, 0);
4785 4 ▶ if (!elem || !SvROK(*elem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem)) continue; 4 0 4
if (!elem || !SvROK(*elem)) continue; 0 4 4
4786 4 HV *ah = (HV*)SvRV(*elem);
4787  
4788 4 SV **mid_sv = hv_fetch(ah, "member_id", 9, 0);
4789 4 ▶ if (!mid_sv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!mid_sv) continue; 0 4 4
4790 STRLEN mid_len;
4791 4 const char *mid = SvPV(*mid_sv, mid_len);
4792 4 kf_buf_append_string(&body, mid, (int16_t)mid_len);
4793  
4794 4 SV **data_sv = hv_fetch(ah, "assignment", 10, 0);
4795 4 ▶ if (!data_sv) { kf_buf_append_bytes(&body, NULL, 0); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!data_sv) { kf_buf_append_bytes(&body, NULL, 0); continue; } 0 4 4
4796 STRLEN dlen;
4797 4 const char *ddata = SvPV(*data_sv, dlen);
4798 4 kf_buf_append_bytes(&body, ddata, (int32_t)dlen);
4799 }
4800 } else {
4801 0 kf_buf_append_i32(&body, 0); /* empty array */
4802 }
4803  
4804 4 conn_send_request(aTHX_ self, API_SYNC_GROUP, ver, &body, cb, 0, 0);
4805 4 kf_buf_free(&body);
4806 }
4807  
4808 void
4809 heartbeat(EV::Kafka::Conn self, const char *group_id, int generation_id, const char *member_id, SV *cb, SV *group_instance_id_sv = NULL)
4810 CODE:
4811 {
4812 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
4813 1 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 1 1
4814 0 croak("not connected");
4815  
4816 1 int16_t ver = self->api_versions[API_HEARTBEAT];
4817 1 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 1 1
4818 1 ▶ if (ver > 4) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 4) ver = 4; 0 1 1
4819  
4820 kf_buf_t body;
4821 1 kf_buf_init(&body);
4822  
4823 1 STRLEN glen = strlen(group_id);
4824 1 kf_buf_append_string(&body, group_id, (int16_t)glen);
4825 1 kf_buf_append_i32(&body, generation_id);
4826  
4827 1 STRLEN mlen = strlen(member_id);
4828 1 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4829  
4830 /* group_instance_id (v3+) */
4831 1 ▶ if (ver >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) { 0 1 1
4832 0 ▶ if (group_instance_id_sv && SvOK(group_instance_id_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
if (group_instance_id_sv && SvOK(group_instance_id_sv)) { 0 0 0
4833 STRLEN gilen;
4834 0 const char *gi = SvPV(group_instance_id_sv, gilen);
4835 0 kf_buf_append_nullable_string(&body, gi, (int16_t)gilen);
4836 } else {
4837 0 kf_buf_append_nullable_string(&body, NULL, 0);
4838 }
4839 }
4840  
4841 1 conn_send_request(aTHX_ self, API_HEARTBEAT, ver, &body, cb, 0, 0);
4842 1 kf_buf_free(&body);
4843 }
4844  
4845 void
4846 offset_commit(EV::Kafka::Conn self, const char *group_id, int generation_id, const char *member_id, SV *offsets_sv, SV *cb)
4847 CODE:
4848 {
4849 3 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 3 3
4850 3 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 3 3
4851 0 croak("not connected");
4852  
4853 3 ▶ if (!SvROK(offsets_sv) || SvTYPE(SvRV(offsets_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(offsets_sv) || SvTYPE(SvRV(offsets_sv)) != SVt_PVAV) 2 1 3
if (!SvROK(offsets_sv) || SvTYPE(SvRV(offsets_sv)) != SVt_PVAV) 0 2 2
4854 1 croak("offset_commit: expected arrayref");
4855  
4856 2 int16_t ver = self->api_versions[API_OFFSET_COMMIT];
4857 2 ▶ if (ver < 0) ver = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 2; 0 2 2
4858 2 ▶ if (ver > 7) ver = 7;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 7) ver = 7; 0 2 2
4859  
4860 kf_buf_t body;
4861 2 kf_buf_init(&body);
4862  
4863 2 STRLEN glen = strlen(group_id);
4864 2 kf_buf_append_string(&body, group_id, (int16_t)glen);
4865  
4866 /* generation_id (v1+) */
4867 2 ▶ if (ver >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) 2 0 2
4868 2 kf_buf_append_i32(&body, generation_id);
4869  
4870 /* member_id (v1+) */
4871 2 ▶ if (ver >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) { 2 0 2
4872 2 STRLEN mlen = strlen(member_id);
4873 2 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4874 }
4875  
4876 /* group_instance_id (v7+): null */
4877 2 ▶ if (ver >= 7)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 7) 0 2 2
4878 0 kf_buf_append_nullable_string(&body, NULL, 0);
4879  
4880 /* topics: ARRAY of {topic, partitions: [{partition, committed_offset, metadata}]} */
4881 2 AV *topics = (AV*)SvRV(offsets_sv);
4882 2 SSize_t i, tc = av_len(topics) + 1;
4883 2 kf_buf_append_i32(&body, (int32_t)tc);
4884  
4885 4 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 2 2 4
4886 2 SV **elem = av_fetch(topics, i, 0);
4887 2 ▶ if (!elem || !SvROK(*elem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem)) continue; 2 0 2
if (!elem || !SvROK(*elem)) continue; 0 2 2
4888 2 HV *th = (HV*)SvRV(*elem);
4889 2 SV **tname_sv = hv_fetch(th, "topic", 5, 0);
4890 2 ▶ if (!tname_sv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!tname_sv) continue; 0 2 2
4891 STRLEN tnlen;
4892 2 const char *tname = SvPV(*tname_sv, tnlen);
4893 2 kf_buf_append_string(&body, tname, (int16_t)tnlen);
4894  
4895 2 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
4896 2 ▶ if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; } 2 0 2
if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; } 0 2 2
4897 2 AV *parts = (AV*)SvRV(*parts_sv);
4898 2 SSize_t j, pc = av_len(parts) + 1;
4899 2 kf_buf_append_i32(&body, (int32_t)pc);
4900  
4901 4 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 2 2 4
4902 2 SV **pelem = av_fetch(parts, j, 0);
4903 2 ▶ if (!pelem || !SvROK(*pelem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!pelem || !SvROK(*pelem)) continue; 2 0 2
if (!pelem || !SvROK(*pelem)) continue; 0 2 2
4904 2 HV *ph = (HV*)SvRV(*pelem);
4905  
4906 2 SV **pid_sv = hv_fetch(ph, "partition", 9, 0);
4907 2 ▶ kf_buf_append_i32(&body, pid_sv ? (int32_t)SvIV(*pid_sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, pid_sv ? (int32_t)SvIV(*pid_sv) : 0); 2 0 2
4908  
4909 2 SV **off_sv = hv_fetch(ph, "offset", 6, 0);
4910 2 ▶ kf_buf_append_i64(&body, off_sv ? (int64_t)SvIV(*off_sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i64(&body, off_sv ? (int64_t)SvIV(*off_sv) : 0); 2 0 2
4911  
4912 /* leader_epoch (v6+) */
4913 2 ▶ if (ver >= 6)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 6) 0 2 2
4914 0 kf_buf_append_i32(&body, -1);
4915  
4916 /* metadata: nullable string = empty */
4917 2 kf_buf_append_nullable_string(&body, "", 0);
4918 }
4919 }
4920  
4921 2 conn_send_request(aTHX_ self, API_OFFSET_COMMIT, ver, &body, cb, 0, 0);
4922 2 kf_buf_free(&body);
4923 }
4924  
4925 void
4926 offset_fetch(EV::Kafka::Conn self, const char *group_id, SV *topics_sv, SV *cb)
4927 CODE:
4928 {
4929 5 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 5 5
4930 5 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 5 5
4931 0 croak("not connected");
4932  
4933 5 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 4 1 5
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 4 4
4934 1 croak("offset_fetch: expected arrayref");
4935  
4936 4 int16_t ver = self->api_versions[API_OFFSET_FETCH];
4937 4 ▶ if (ver < 0) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 1; 0 4 4
4938 4 ▶ if (ver > 5) ver = 5;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 5) ver = 5; 0 4 4
4939  
4940 kf_buf_t body;
4941 4 kf_buf_init(&body);
4942  
4943 4 STRLEN glen = strlen(group_id);
4944 4 kf_buf_append_string(&body, group_id, (int16_t)glen);
4945  
4946 /* topics: ARRAY */
4947 4 AV *topics = (AV*)SvRV(topics_sv);
4948 4 SSize_t i, tc = av_len(topics) + 1;
4949 4 kf_buf_append_i32(&body, (int32_t)tc);
4950  
4951 8 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 4 4 8
4952 4 SV **elem = av_fetch(topics, i, 0);
4953 4 ▶ if (!elem || !SvROK(*elem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem)) continue; 4 0 4
if (!elem || !SvROK(*elem)) continue; 0 4 4
4954 4 HV *th = (HV*)SvRV(*elem);
4955 4 SV **tname_sv = hv_fetch(th, "topic", 5, 0);
4956 4 ▶ if (!tname_sv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!tname_sv) continue; 0 4 4
4957 STRLEN tnlen;
4958 4 const char *tname = SvPV(*tname_sv, tnlen);
4959 4 kf_buf_append_string(&body, tname, (int16_t)tnlen);
4960  
4961 4 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
4962 4 ▶ if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; } 4 0 4
if (!parts_sv || !SvROK(*parts_sv)) { kf_buf_append_i32(&body, 0); continue; } 0 4 4
4963 4 AV *parts = (AV*)SvRV(*parts_sv);
4964 4 SSize_t j, pc = av_len(parts) + 1;
4965 4 kf_buf_append_i32(&body, (int32_t)pc);
4966  
4967 8 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 4 4 8
4968 4 SV **pelem = av_fetch(parts, j, 0);
4969 4 ▶ kf_buf_append_i32(&body, pelem ? (int32_t)SvIV(*pelem) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, pelem ? (int32_t)SvIV(*pelem) : 0); 4 0 4
4970 }
4971 }
4972  
4973 4 conn_send_request(aTHX_ self, API_OFFSET_FETCH, ver, &body, cb, 0, 0);
4974 4 kf_buf_free(&body);
4975 }
4976  
4977 void
4978 leave_group(EV::Kafka::Conn self, const char *group_id, const char *member_id, SV *cb)
4979 CODE:
4980 {
4981 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
4982 1 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 1 1
4983 0 croak("not connected");
4984  
4985 1 int16_t ver = self->api_versions[API_LEAVE_GROUP];
4986 1 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 1 1
4987 1 ▶ if (ver > 3) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 3) ver = 3; 0 1 1
4988  
4989 kf_buf_t body;
4990 1 kf_buf_init(&body);
4991  
4992 1 STRLEN glen = strlen(group_id);
4993 1 kf_buf_append_string(&body, group_id, (int16_t)glen);
4994  
4995 1 STRLEN mlen = strlen(member_id);
4996 1 kf_buf_append_string(&body, member_id, (int16_t)mlen);
4997  
4998 1 conn_send_request(aTHX_ self, API_LEAVE_GROUP, ver, &body, cb, 0, 0);
4999 1 kf_buf_free(&body);
5000 }
5001  
5002 void
5003 create_topics(EV::Kafka::Conn self, SV *topics_sv, int timeout_ms, SV *cb)
5004 CODE:
5005 {
5006 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
5007 1 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 1 1
5008 0 croak("not connected");
5009  
5010 1 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 1 1
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 0 0
5011 1 croak("create_topics: expected arrayref");
5012  
5013 0 int16_t ver = self->api_versions[API_CREATE_TOPICS];
5014 0 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 0 0
5015 0 ▶ if (ver > 4) ver = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 4) ver = 4; 0 0 0
5016  
5017 kf_buf_t body;
5018 0 kf_buf_init(&body);
5019  
5020 0 AV *topics = (AV*)SvRV(topics_sv);
5021 0 SSize_t i, tc = av_len(topics) + 1;
5022 0 kf_buf_append_i32(&body, (int32_t)tc);
5023  
5024 0 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 0 0 0
5025 0 SV **elem = av_fetch(topics, i, 0);
5026 0 ▶ if (!elem || !SvROK(*elem)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !SvROK(*elem)) continue; 0 0 0
if (!elem || !SvROK(*elem)) continue; 0 0 0
5027 0 HV *th = (HV*)SvRV(*elem);
5028  
5029 0 SV **name_sv = hv_fetch(th, "name", 4, 0);
5030 0 ▶ if (!name_sv) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!name_sv) continue; 0 0 0
5031 STRLEN nlen;
5032 0 const char *name = SvPV(*name_sv, nlen);
5033 0 kf_buf_append_string(&body, name, (int16_t)nlen);
5034  
5035 0 SV **np_sv = hv_fetch(th, "num_partitions", 14, 0);
5036 0 ▶ int32_t num_partitions = np_sv ? (int32_t)SvIV(*np_sv) : 1;
Branch: 1 branch
BranchTrueFalseTotal
int32_t num_partitions = np_sv ? (int32_t)SvIV(*np_sv) : 1; 0 0 0
5037 0 kf_buf_append_i32(&body, num_partitions);
5038  
5039 0 SV **rf_sv = hv_fetch(th, "replication_factor", 18, 0);
5040 0 ▶ int16_t replication_factor = rf_sv ? (int16_t)SvIV(*rf_sv) : 1;
Branch: 1 branch
BranchTrueFalseTotal
int16_t replication_factor = rf_sv ? (int16_t)SvIV(*rf_sv) : 1; 0 0 0
5041 0 kf_buf_append_i16(&body, replication_factor);
5042  
5043 /* assignments: empty array */
5044 0 kf_buf_append_i32(&body, 0);
5045  
5046 /* configs: empty array */
5047 0 kf_buf_append_i32(&body, 0);
5048 }
5049  
5050 0 kf_buf_append_i32(&body, timeout_ms);
5051  
5052 /* validate_only (v1+) */
5053 0 ▶ if (ver >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 1) 0 0 0
5054 0 kf_buf_append_i8(&body, 0);
5055  
5056 0 conn_send_request(aTHX_ self, API_CREATE_TOPICS, ver, &body, cb, 0, 0);
5057 0 kf_buf_free(&body);
5058 }
5059  
5060 void
5061 delete_topics(EV::Kafka::Conn self, SV *topics_sv, int timeout_ms, SV *cb)
5062 CODE:
5063 {
5064 1 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 1 1
5065 1 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 1 1
5066 0 croak("not connected");
5067  
5068 1 ▶ if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 1 1
if (!SvROK(topics_sv) || SvTYPE(SvRV(topics_sv)) != SVt_PVAV) 0 0 0
5069 1 croak("delete_topics: expected arrayref");
5070  
5071 0 int16_t ver = self->api_versions[API_DELETE_TOPICS];
5072 0 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 0 0
5073 0 ▶ if (ver > 3) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 3) ver = 3; 0 0 0
5074  
5075 kf_buf_t body;
5076 0 kf_buf_init(&body);
5077  
5078 0 AV *topics = (AV*)SvRV(topics_sv);
5079 0 SSize_t i, tc = av_len(topics) + 1;
5080 0 kf_buf_append_i32(&body, (int32_t)tc);
5081  
5082 0 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 0 0 0
5083 0 SV **elem = av_fetch(topics, i, 0);
5084 0 ▶ if (!elem) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!elem) continue; 0 0 0
5085 STRLEN tlen;
5086 0 const char *tname = SvPV(*elem, tlen);
5087 0 kf_buf_append_string(&body, tname, (int16_t)tlen);
5088 }
5089  
5090 0 kf_buf_append_i32(&body, timeout_ms);
5091  
5092 0 conn_send_request(aTHX_ self, API_DELETE_TOPICS, ver, &body, cb, 0, 0);
5093 0 kf_buf_free(&body);
5094 }
5095  
5096 void
5097 init_producer_id(EV::Kafka::Conn self, SV *transactional_id_sv, int txn_timeout_ms, SV *cb)
5098 CODE:
5099 {
5100 10 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 10 10
5101 10 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 10 10
5102 0 croak("not connected");
5103  
5104 10 int16_t ver = self->api_versions[API_INIT_PRODUCER_ID];
5105 10 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 10 10
5106 10 ▶ if (ver > 1) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 1) ver = 1; 0 10 10
5107  
5108 kf_buf_t body;
5109 10 kf_buf_init(&body);
5110  
5111 10 ▶ if (SvOK(transactional_id_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(transactional_id_sv)) { 3 7 10
5112 STRLEN tlen;
5113 3 const char *tid = SvPV(transactional_id_sv, tlen);
5114 3 kf_buf_append_string(&body, tid, (int16_t)tlen);
5115 } else {
5116 7 kf_buf_append_nullable_string(&body, NULL, 0);
5117 }
5118  
5119 10 kf_buf_append_i32(&body, txn_timeout_ms);
5120  
5121 /* v2+: producer_id(i64=-1), producer_epoch(i16=-1) */
5122 10 ▶ if (ver >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 2) { 0 10 10
5123 0 kf_buf_append_i64(&body, -1);
5124 0 kf_buf_append_i16(&body, -1);
5125 }
5126  
5127 10 conn_send_request(aTHX_ self, API_INIT_PRODUCER_ID, ver, &body, cb, 0, 0);
5128 10 kf_buf_free(&body);
5129 }
5130  
5131 void
5132 add_partitions_to_txn(EV::Kafka::Conn self, const char *transactional_id, SV *producer_id_sv, int producer_epoch, SV *topics_sv, SV *cb)
5133 CODE:
5134 {
5135 20205 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 20205 20205
5136 20205 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 20205 20205
5137 0 croak("not connected");
5138  
5139 20205 int16_t ver = self->api_versions[API_ADD_PARTITIONS_TXN];
5140 20205 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 20202 3 20205
5141 20205 ▶ if (ver > 1) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 1) ver = 1; 0 20205 20205
5142  
5143 20205 int64_t pid = SvIV(producer_id_sv);
5144  
5145 20205 kf_validate_txn_topics("add_partitions_to_txn", topics_sv, 0);
5146  
5147 kf_buf_t body;
5148 4 kf_buf_init(&body);
5149  
5150 4 STRLEN tid_len = strlen(transactional_id);
5151 4 kf_buf_append_string(&body, transactional_id, (int16_t)tid_len);
5152 4 kf_buf_append_i64(&body, pid);
5153 4 kf_buf_append_i16(&body, (int16_t)producer_epoch);
5154  
5155 /* topics: ARRAY of {topic, partitions: ARRAY(i32)} */
5156 4 AV *topics = (AV*)SvRV(topics_sv); /* validated above */
5157 4 SSize_t i, tc = av_len(topics) + 1;
5158 4 kf_buf_append_i32(&body, (int32_t)tc);
5159  
5160 8 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 4 4 8
5161 4 SV **elem = av_fetch(topics, i, 0);
5162 4 HV *th = (HV*)SvRV(*elem);
5163 4 SV **tname_sv = hv_fetch(th, "topic", 5, 0);
5164 STRLEN tnlen;
5165 4 const char *tname = SvPV(*tname_sv, tnlen);
5166 4 kf_buf_append_string(&body, tname, (int16_t)tnlen);
5167  
5168 4 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
5169 4 AV *parts = (AV*)SvRV(*parts_sv); /* validated above */
5170 4 SSize_t j, pc = av_len(parts) + 1;
5171 4 kf_buf_append_i32(&body, (int32_t)pc);
5172 8 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 4 4 8
5173 4 SV **pelem = av_fetch(parts, j, 0);
5174 4 ▶ kf_buf_append_i32(&body, pelem ? (int32_t)SvIV(*pelem) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, pelem ? (int32_t)SvIV(*pelem) : 0); 4 0 4
5175 }
5176 }
5177  
5178 4 conn_send_request(aTHX_ self, API_ADD_PARTITIONS_TXN, ver, &body, cb, 0, 0);
5179 4 kf_buf_free(&body);
5180 }
5181  
5182 void
5183 end_txn(EV::Kafka::Conn self, const char *transactional_id, SV *producer_id_sv, int producer_epoch, int committed, SV *cb)
5184 CODE:
5185 {
5186 3 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 3 3
5187 3 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 3 3
5188 0 croak("not connected");
5189  
5190 3 int16_t ver = self->api_versions[API_END_TXN];
5191 3 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 0 3 3
5192 3 ▶ if (ver > 1) ver = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 1) ver = 1; 0 3 3
5193  
5194 3 int64_t pid = SvIV(producer_id_sv);
5195  
5196 kf_buf_t body;
5197 3 kf_buf_init(&body);
5198  
5199 3 STRLEN tid_len = strlen(transactional_id);
5200 3 kf_buf_append_string(&body, transactional_id, (int16_t)tid_len);
5201 3 kf_buf_append_i64(&body, pid);
5202 3 kf_buf_append_i16(&body, (int16_t)producer_epoch);
5203 3 kf_buf_append_i8(&body, committed ? 1 : 0);
5204  
5205 3 conn_send_request(aTHX_ self, API_END_TXN, ver, &body, cb, 0, 0);
5206 3 kf_buf_free(&body);
5207 }
5208  
5209 void
5210 txn_offset_commit(EV::Kafka::Conn self, const char *transactional_id, const char *group_id, SV *producer_id_sv, int producer_epoch, int generation_id, const char *member_id, SV *offsets_sv, SV *cb)
5211 CODE:
5212 {
5213 20203 ▶ KF_REQUIRE_CONN(self);
Branch: 1 branch
BranchTrueFalseTotal
KF_REQUIRE_CONN(self); 0 20203 20203
5214 20203 ▶ if (self->state != CONN_READY)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != CONN_READY) 0 20203 20203
5215 0 croak("not connected");
5216  
5217 20203 int16_t ver = self->api_versions[API_TXN_OFFSET_COMMIT];
5218 20203 ▶ if (ver < 0) ver = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ver < 0) ver = 0; 20202 1 20203
5219 20203 ▶ if (ver > 3) ver = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (ver > 3) ver = 3; 0 20203 20203
5220  
5221 20203 int64_t pid = SvIV(producer_id_sv);
5222  
5223 20203 kf_validate_txn_topics("txn_offset_commit", offsets_sv, 1);
5224  
5225 kf_buf_t body;
5226 2 kf_buf_init(&body);
5227  
5228 2 STRLEN tid_len = strlen(transactional_id);
5229 2 kf_buf_append_string(&body, transactional_id, (int16_t)tid_len);
5230  
5231 2 STRLEN gid_len = strlen(group_id);
5232 2 kf_buf_append_string(&body, group_id, (int16_t)gid_len);
5233  
5234 2 kf_buf_append_i64(&body, pid);
5235 2 kf_buf_append_i16(&body, (int16_t)producer_epoch);
5236  
5237 /* v3+: generation_id, member_id, group_instance_id */
5238 2 ▶ if (ver >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 3) { 0 2 2
5239 0 kf_buf_append_i32(&body, generation_id);
5240 0 STRLEN mid_len = strlen(member_id);
5241 0 kf_buf_append_string(&body, member_id, (int16_t)mid_len);
5242 0 kf_buf_append_nullable_string(&body, NULL, 0); /* group_instance_id */
5243 }
5244  
5245 /* offsets: ARRAY of {topic, partitions: [{partition, offset}]} */
5246 2 AV *topics = (AV*)SvRV(offsets_sv); /* validated above */
5247 2 SSize_t i, tc = av_len(topics) + 1;
5248 2 kf_buf_append_i32(&body, (int32_t)tc);
5249  
5250 4 ▶ for (i = 0; i < tc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tc; i++) { 2 2 4
5251 2 SV **elem = av_fetch(topics, i, 0);
5252 2 HV *th = (HV*)SvRV(*elem);
5253  
5254 2 SV **tname_sv = hv_fetch(th, "topic", 5, 0);
5255 STRLEN tnlen;
5256 2 const char *tname = SvPV(*tname_sv, tnlen);
5257 2 kf_buf_append_string(&body, tname, (int16_t)tnlen);
5258  
5259 2 SV **parts_sv = hv_fetch(th, "partitions", 10, 0);
5260 2 AV *parts = (AV*)SvRV(*parts_sv); /* validated above */
5261 2 SSize_t j, pc = av_len(parts) + 1;
5262 2 kf_buf_append_i32(&body, (int32_t)pc);
5263  
5264 4 ▶ for (j = 0; j < pc; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pc; j++) { 2 2 4
5265 2 SV **pelem = av_fetch(parts, j, 0);
5266 2 HV *ph = (HV*)SvRV(*pelem); /* validated above */
5267  
5268 2 SV **ppid_sv = hv_fetch(ph, "partition", 9, 0);
5269 2 ▶ kf_buf_append_i32(&body, ppid_sv ? (int32_t)SvIV(*ppid_sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i32(&body, ppid_sv ? (int32_t)SvIV(*ppid_sv) : 0); 2 0 2
5270  
5271 2 SV **off_sv = hv_fetch(ph, "offset", 6, 0);
5272 2 ▶ kf_buf_append_i64(&body, off_sv ? (int64_t)SvIV(*off_sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
kf_buf_append_i64(&body, off_sv ? (int64_t)SvIV(*off_sv) : 0); 2 0 2
5273  
5274 /* leader_epoch (v2+) */
5275 2 ▶ if (ver >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= 2) 0 2 2
5276 0 kf_buf_append_i32(&body, -1);
5277  
5278 2 kf_buf_append_nullable_string(&body, "", 0); /* metadata */
5279 }
5280 }
5281  
5282 2 conn_send_request(aTHX_ self, API_TXN_OFFSET_COMMIT, ver, &body, cb, 0, 0);
5283 2 kf_buf_free(&body);
5284 }
5285  
5286 MODULE = EV::Kafka PACKAGE = EV::Kafka
5287  
5288 int
5289 _murmur2(SV *data_sv)
5290 CODE:
5291 {
5292 STRLEN len;
5293 32 const unsigned char *data = (const unsigned char *)SvPV(data_sv, len);
5294 32 uint32_t h = 0x9747b28c ^ (uint32_t)len;
5295 32 const uint32_t m = 0x5bd1e995;
5296 32 size_t i = 0;
5297 32 size_t remaining = len;
5298  
5299 291 ▶ while (remaining >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
while (remaining >= 4) { 259 32 291
5300 uint32_t k;
5301 259 memcpy(&k, data + i, 4); /* little-endian on x86, matches Java */
5302 259 k *= m;
5303 259 k ^= k >> 24;
5304 259 k *= m;
5305 259 h *= m;
5306 259 h ^= k;
5307 259 i += 4;
5308 259 remaining -= 4;
5309 }
5310  
5311 32 switch (remaining) {
5312 5 case 3: h ^= (uint32_t)data[i + 2] << 16; /* fallthrough */
5313 7 case 2: h ^= (uint32_t)data[i + 1] << 8; /* fallthrough */
5314 26 case 1: h ^= (uint32_t)data[i]; h *= m;
5315 }
5316  
5317 32 h ^= h >> 13;
5318 32 h *= m;
5319 32 h ^= h >> 15;
5320  
5321 32 ▶ RETVAL = (int)(h & 0x7FFFFFFF);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)(h & 0x7FFFFFFF); 19 13 32
5322 }
5323 OUTPUT:
5324 RETVAL
5325  
5326 unsigned int
5327 _crc32c(SV *data_sv)
5328 CODE:
5329 {
5330 STRLEN len;
5331 7 const char *data = SvPV(data_sv, len);
5332 7 RETVAL = crc32c(data, len);
5333 }
5334 OUTPUT:
5335 RETVAL
5336  
5337 void
5338 _error_name(int code)
5339 PPCODE:
5340 {
5341 14 const char *name = NULL;
5342 14 switch (code) {
5343 1 case 0: name = "NONE"; break;
5344 2 case 1: name = "OFFSET_OUT_OF_RANGE"; break;
5345 0 case 2: name = "CORRUPT_MESSAGE"; break;
5346 2 case 3: name = "UNKNOWN_TOPIC_OR_PARTITION"; break;
5347 0 case 5: name = "LEADER_NOT_AVAILABLE"; break;
5348 1 case 6: name = "NOT_LEADER_OR_FOLLOWER"; break;
5349 0 case 7: name = "REQUEST_TIMED_OUT"; break;
5350 0 case 10: name = "MESSAGE_TOO_LARGE"; break;
5351 1 case 15: name = "COORDINATOR_NOT_AVAILABLE"; break;
5352 1 case 16: name = "NOT_COORDINATOR"; break;
5353 0 case 17: name = "INVALID_TOPIC_EXCEPTION"; break;
5354 0 case 19: name = "NOT_ENOUGH_REPLICAS"; break;
5355 0 case 20: name = "NOT_ENOUGH_REPLICAS_AFTER_APPEND"; break;
5356 0 case 22: name = "ILLEGAL_GENERATION"; break;
5357 0 case 25: name = "UNKNOWN_MEMBER_ID"; break;
5358 0 case 26: name = "INVALID_SESSION_TIMEOUT"; break;
5359 1 case 27: name = "REBALANCE_IN_PROGRESS"; break;
5360 0 case 35: name = "UNSUPPORTED_VERSION"; break;
5361 1 case 36: name = "TOPIC_ALREADY_EXISTS"; break;
5362 0 case 39: name = "REASSIGNMENT_IN_PROGRESS"; break;
5363 0 case 41: name = "NOT_CONTROLLER"; break;
5364 1 case 45: name = "OUT_OF_ORDER_SEQUENCE_NUMBER"; break;
5365 1 case 46: name = "DUPLICATE_SEQUENCE_NUMBER"; break;
5366 0 case 47: name = "INVALID_REPLICATION_FACTOR"; break;
5367 0 case 58: name = "SASL_AUTHENTICATION_FAILED"; break;
5368 0 case 72: name = "LISTENER_NOT_FOUND"; break;
5369 1 case 79: name = "MEMBER_ID_REQUIRED"; break;
5370 1 default: name = "UNKNOWN"; break;
5371 }
5372 14 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 14 14
5373 14 mPUSHp(name, strlen(name));
5374 14 XSRETURN(1);
5375 }
5376  
5377 # ---- internal test helpers (not part of the public API) ----
5378  
5379 SV*
5380 _test_zigzag_i32(int v)
5381 CODE:
5382 {
5383 5 int32_t v32 = (int32_t)v;
5384 5 uint32_t z = (uint32_t)((v32 << 1) ^ (v32 >> 31));
5385 5 RETVAL = newSVuv(z);
5386 }
5387 OUTPUT:
5388 RETVAL
5389  
5390 SV*
5391 _test_zigzag_i64(SV *v_sv)
5392 CODE:
5393 {
5394 5 int64_t v = (int64_t)SvIV(v_sv);
5395 5 uint64_t z = (uint64_t)((v << 1) ^ (v >> 63));
5396 5 RETVAL = newSVuv(z);
5397 }
5398 OUTPUT:
5399 RETVAL
5400  
5401 # Encode then decode a varint; returns the round-tripped i64 or undef on
5402 # malformed input.
5403 SV*
5404 _test_varint_roundtrip(SV *v_sv)
5405 CODE:
5406 {
5407 14 int64_t v = (int64_t)SvIV(v_sv);
5408 kf_buf_t b;
5409 14 kf_buf_init(&b);
5410 14 kf_buf_append_varint(&b, v);
5411 int64_t out;
5412 14 int n = kf_read_varint(b.data, b.data + b.len, &out);
5413 14 ▶ if (n < 0) RETVAL = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) RETVAL = &PL_sv_undef; 0 14 14
5414 14 else RETVAL = newSViv((IV)out);
5415 14 kf_buf_free(&b);
5416 }
5417 OUTPUT:
5418 RETVAL
5419  
5420 # Encode a single-record RecordBatch v2 with given options.
5421 # opts: { compression => 0|1|3, producer_id, producer_epoch,
5422 # base_sequence, is_transactional, timestamp }.
5423 SV*
5424 _test_encode_batch(SV *records_sv, SV *opts_sv = NULL)
5425 CODE:
5426 {
5427 21 ▶ if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV) 21 0 21
if (!SvROK(records_sv) || SvTYPE(SvRV(records_sv)) != SVt_PVAV) 0 21 21
5428 0 croak("_test_encode_batch: arrayref required");
5429 21 AV *records = (AV*)SvRV(records_sv);
5430  
5431 21 int compression = 0;
5432 21 int64_t producer_id = -1;
5433 21 int16_t producer_epoch = -1;
5434 21 int32_t base_sequence = -1;
5435 21 int is_txn = 0;
5436 21 int64_t ts = 0;
5437 21 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 11 10 21
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 11 0 11
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 11 0 11
5438 11 HV *opts = (HV*)SvRV(opts_sv);
5439 SV **v;
5440 11 ▶ if ((v = hv_fetchs(opts, "compression", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "compression", 0)) && SvOK(*v)) 10 1 11
if ((v = hv_fetchs(opts, "compression", 0)) && SvOK(*v)) 10 0 10
5441 10 compression = (int)SvIV(*v);
5442 11 ▶ if ((v = hv_fetchs(opts, "producer_id", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "producer_id", 0)) && SvOK(*v)) 1 10 11
if ((v = hv_fetchs(opts, "producer_id", 0)) && SvOK(*v)) 1 0 1
5443 1 producer_id = (int64_t)SvIV(*v);
5444 11 ▶ if ((v = hv_fetchs(opts, "producer_epoch", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "producer_epoch", 0)) && SvOK(*v)) 1 10 11
if ((v = hv_fetchs(opts, "producer_epoch", 0)) && SvOK(*v)) 1 0 1
5445 1 producer_epoch = (int16_t)SvIV(*v);
5446 11 ▶ if ((v = hv_fetchs(opts, "base_sequence", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "base_sequence", 0)) && SvOK(*v)) 1 10 11
if ((v = hv_fetchs(opts, "base_sequence", 0)) && SvOK(*v)) 1 0 1
5447 1 base_sequence = (int32_t)SvIV(*v);
5448 11 ▶ if ((v = hv_fetchs(opts, "is_transactional", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "is_transactional", 0)) && SvOK(*v)) 0 11 11
if ((v = hv_fetchs(opts, "is_transactional", 0)) && SvOK(*v)) 0 0 0
5449 0 is_txn = (int)SvIV(*v);
5450 11 ▶ if ((v = hv_fetchs(opts, "timestamp", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "timestamp", 0)) && SvOK(*v)) 0 11 11
if ((v = hv_fetchs(opts, "timestamp", 0)) && SvOK(*v)) 0 0 0
5451 0 ts = (int64_t)SvIV(*v);
5452 }
5453  
5454 kf_buf_t out;
5455 21 kf_encode_record_batch_multi(aTHX_ &out, records, ts, compression,
5456 producer_id, producer_epoch, base_sequence, is_txn);
5457 21 RETVAL = newSVpvn(out.data, out.len);
5458 21 kf_buf_free(&out);
5459 }
5460 OUTPUT:
5461 RETVAL
5462  
5463 # Parse a captured response body of a known API. Returns the parsed hash
5464 # (same shape user callbacks see) or undef if the parser rejected the bytes.
5465 # api: 'metadata' | 'produce' | 'fetch' | 'list_offsets' | 'find_coordinator'.
5466 SV*
5467 _test_parse_response(const char *api, int version, SV *bytes_sv)
5468 CODE:
5469 {
5470 STRLEN len;
5471 52018 const char *data = SvPV(bytes_sv, len);
5472 ev_kafka_conn_t stub;
5473 52018 memset(&stub, 0, sizeof(stub));
5474 52018 stub.magic = KF_MAGIC_ALIVE;
5475 52018 stub.fd = -1;
5476 52018 SV *result = NULL;
5477 52018 ▶ if (strcmp(api, "metadata") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(api, "metadata") == 0) { 52004 14 52018
5478 52004 result = conn_parse_metadata_response(aTHX_ &stub, (int16_t)version, data, len);
5479 14 ▶ } else if (strcmp(api, "produce") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "produce") == 0) { 1 13 14
5480 1 result = conn_parse_produce_response(aTHX_ &stub, (int16_t)version, data, len);
5481 13 ▶ } else if (strcmp(api, "fetch") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "fetch") == 0) { 1 12 13
5482 1 result = conn_parse_fetch_response(aTHX_ &stub, (int16_t)version, data, len);
5483 12 ▶ } else if (strcmp(api, "list_offsets") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "list_offsets") == 0) { 0 12 12
5484 0 result = conn_parse_list_offsets_response(aTHX_ &stub, (int16_t)version, data, len);
5485 12 ▶ } else if (strcmp(api, "find_coordinator") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "find_coordinator") == 0) { 1 11 12
5486 1 result = conn_parse_find_coordinator_response(aTHX_ &stub, (int16_t)version, data, len);
5487 11 ▶ } else if (strcmp(api, "join_group") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "join_group") == 0) { 1 10 11
5488 1 result = conn_parse_join_group_response(aTHX_ &stub, (int16_t)version, data, len);
5489 10 ▶ } else if (strcmp(api, "sync_group") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "sync_group") == 0) { 1 9 10
5490 1 result = conn_parse_sync_group_response(aTHX_ &stub, (int16_t)version, data, len);
5491 9 ▶ } else if (strcmp(api, "heartbeat") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "heartbeat") == 0) { 1 8 9
5492 1 result = conn_parse_heartbeat_response(aTHX_ &stub, (int16_t)version, data, len);
5493 8 ▶ } else if (strcmp(api, "offset_commit") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "offset_commit") == 0) { 0 8 8
5494 0 result = conn_parse_offset_commit_response(aTHX_ &stub, (int16_t)version, data, len);
5495 8 ▶ } else if (strcmp(api, "offset_fetch") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "offset_fetch") == 0) { 1 7 8
5496 1 result = conn_parse_offset_fetch_response(aTHX_ &stub, (int16_t)version, data, len);
5497 7 ▶ } else if (strcmp(api, "leave_group") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "leave_group") == 0) { 1 6 7
5498 1 result = conn_parse_leave_group_response(aTHX_ &stub, (int16_t)version, data, len);
5499 6 ▶ } else if (strcmp(api, "create_topics") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "create_topics") == 0) { 1 5 6
5500 1 result = conn_parse_create_topics_response(aTHX_ &stub, (int16_t)version, data, len);
5501 5 ▶ } else if (strcmp(api, "delete_topics") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "delete_topics") == 0) { 1 4 5
5502 1 result = conn_parse_delete_topics_response(aTHX_ &stub, (int16_t)version, data, len);
5503 4 ▶ } else if (strcmp(api, "init_producer_id") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "init_producer_id") == 0) { 1 3 4
5504 1 result = conn_parse_init_producer_id_response(aTHX_ &stub, (int16_t)version, data, len);
5505 3 ▶ } else if (strcmp(api, "add_partitions_to_txn") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "add_partitions_to_txn") == 0) { 1 2 3
5506 1 result = conn_parse_add_partitions_to_txn_response(aTHX_ &stub, (int16_t)version, data, len);
5507 2 ▶ } else if (strcmp(api, "end_txn") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "end_txn") == 0) { 1 1 2
5508 1 result = conn_parse_end_txn_response(aTHX_ &stub, (int16_t)version, data, len);
5509 1 ▶ } else if (strcmp(api, "txn_offset_commit") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(api, "txn_offset_commit") == 0) { 1 0 1
5510 1 result = conn_parse_txn_offset_commit_response(aTHX_ &stub, (int16_t)version, data, len);
5511 } else {
5512 0 croak("_test_parse_response: unknown api '%s'", api);
5513 }
5514 52018 ▶ RETVAL = result ? SvREFCNT_inc(result) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result ? SvREFCNT_inc(result) : &PL_sv_undef; 52018 0 52018
5515 }
5516 OUTPUT:
5517 RETVAL
5518  
5519 # Decode a RecordBatch v2 blob. Returns arrayref of {offset, key, value,
5520 # headers} on success, or undef on malformed input (CRC mismatch, etc.).
5521 SV*
5522 _test_decode_batch(SV *bytes_sv)
5523 CODE:
5524 {
5525 STRLEN len;
5526 16 const char *data = SvPV(bytes_sv, len);
5527 16 AV *records_av = newAV();
5528 int64_t base_offset;
5529 16 int n = kf_decode_record_batch(aTHX_ data, len, records_av, &base_offset);
5530 16 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 3 13 16
5531 3 SvREFCNT_dec((SV*)records_av);
5532 3 RETVAL = &PL_sv_undef;
5533 } else {
5534 13 RETVAL = newRV_noinc((SV*)records_av);
5535 }
5536 }
5537 OUTPUT:
5538 RETVAL
5539  
5540 BOOT:
5541 {
5542 34 ▶ I_EV_API("EV::Kafka");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::Kafka"); 0 34 34
I_EV_API("EV::Kafka"); 34 0 34
I_EV_API("EV::Kafka"); 0 34 34
5543 34 crc32c_init_table();
5544 }
« blib/lib/EV/Kafka.pm ↑ Summary