Kafka.xs

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