| 1 |
|
| /* po_ingest.h - the OTLP receiver. |
| 2 |
|
| * |
| 3 |
|
| * The whole PSGI path: route, authenticate, read, bound, decompress, decode, |
| 4 |
|
| * store, answer. It is one code path for all three transports, because the |
| 5 |
|
| * three differ only in how bytes become records and sharing everything after |
| 6 |
|
| * that is what keeps them from drifting into three behaviours. |
| 7 |
|
| * |
| 8 |
|
| * Three things reach back into Perl, and only three: the tenant resolver, the |
| 9 |
|
| * store callback and - on the JSON path - the parse. Everything else, down to |
| 10 |
|
| * the response bytes, is here. The parse stays in Perl on purpose: JSON is the |
| 11 |
|
| * debuggable transport and protobuf is the production one, so a second JSON |
| 12 |
|
| * parser to save allocations on a path nobody benchmarks is the wrong trade. |
| 13 |
|
| * The WALK of what it returns is C, so nothing is allocated per record. |
| 14 |
|
| * |
| 15 |
|
| * Perl headers must be included before this file. |
| 16 |
|
| */ |
| 17 |
|
| #ifndef PO_INGEST_H |
| 18 |
|
| #define PO_INGEST_H |
| 19 |
|
| |
| 20 |
|
| #include "punk_observe/po_compat.h" |
| 21 |
|
| #include "punk_observe/po_otlp_in.h" |
| 22 |
|
| #include "punk_observe/po_json_in.h" |
| 23 |
|
| |
| 24 |
|
| #ifdef PO_HAVE_ZLIB |
| 25 |
|
| # include <zlib.h> |
| 26 |
|
| #endif |
| 27 |
|
| |
| 28 |
|
| /* Perl 5.15 added XS_INTERNAL. Before it, XS() carried __declspec(dllexport) |
| 29 |
|
| * on Windows, which cannot be combined with `static`, so the prototype is |
| 30 |
|
| * spelled out rather than borrowed. */ |
| 31 |
|
| #ifndef XS_INTERNAL |
| 32 |
|
| # define XS_INTERNAL(name) static void name(pTHX_ CV *cv) |
| 33 |
|
| #endif |
| 34 |
|
| |
| 35 |
|
| #define PO_SIG_NONE 0 |
| 36 |
|
| #define PO_SIG_TRACES 1 |
| 37 |
|
| #define PO_SIG_METRICS 2 |
| 38 |
|
| #define PO_SIG_LOGS 3 |
| 39 |
|
| |
| 40 |
|
| #define PO_ENC_NONE 0 |
| 41 |
|
| #define PO_ENC_PROTOBUF 1 |
| 42 |
|
| #define PO_ENC_JSON 2 |
| 43 |
|
| |
| 44 |
|
| #define PO_CE_NONE 0 |
| 45 |
|
| #define PO_CE_GZIP 1 |
| 46 |
|
| #define PO_CE_DEFLATE 2 |
| 47 |
|
| |
| 48 |
|
| /* Decompressed-size ceiling as a multiple of the compressed body. A 40KB body |
| 49 |
|
| * that inflates to 4GB is a denial of service with no exploit needed, so the |
| 50 |
|
| * limit applies to the DECOMPRESSED size and not only to what arrived. */ |
| 51 |
|
| #define PO_MAX_RATIO 20 |
| 52 |
|
| |
| 53 |
|
| /* The largest body this perl can address. max_body is clamped to it, so that |
| 54 |
|
| * CONTENT_LENGTH is checked against a number the buffer about to be filled can |
| 55 |
|
| * actually hold: on a 32-bit perl STRLEN is four bytes, and a max_body set |
| 56 |
|
| * above that would let a length truncate on the way in and overrun what was |
| 57 |
|
| * allocated for it. */ |
| 58 |
|
| #define PO_BODY_MAX ((po_u64)(STRLEN)~(STRLEN)0 - 1) |
| 59 |
|
| |
| 60 |
|
| typedef struct { |
| 61 |
|
| po_u64 max_body; |
| 62 |
|
| po_u64 max_ratio; |
| 63 |
|
| po_u64 max_records; /* 0 = uncapped */ |
| 64 |
|
| SV *auth; /* coderef: $env -> tenant id or undef, or NULL */ |
| 65 |
|
| SV *on_batch; /* coderef: (tenant, signal, buf, enc, out) */ |
| 66 |
|
| } po_ingest; |
| 67 |
|
| |
| 68 |
|
| /* --- the routing table ---------------------------------------------------- */ |
| 69 |
|
| |
| 70 |
43 |
| static int po_signal_of(const char *p, size_t len) { |
| 71 |
43 |
▶ | if (len == 10 && memcmp(p, "/v1/traces", 10) == 0) return PO_SIG_TRACES; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 10 && memcmp(p, "/v1/traces", 10) == 0) return PO_SIG_TRACES; |
39 |
4 |
43 |
| if (len == 10 && memcmp(p, "/v1/traces", 10) == 0) return PO_SIG_TRACES; |
39 |
0 |
39 |
|
| 72 |
4 |
▶ | if (len == 11 && memcmp(p, "/v1/metrics", 11) == 0) return PO_SIG_METRICS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 11 && memcmp(p, "/v1/metrics", 11) == 0) return PO_SIG_METRICS; |
1 |
3 |
4 |
| if (len == 11 && memcmp(p, "/v1/metrics", 11) == 0) return PO_SIG_METRICS; |
1 |
0 |
1 |
|
| 73 |
3 |
▶ | if (len == 8 && memcmp(p, "/v1/logs", 8) == 0) return PO_SIG_LOGS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 8 && memcmp(p, "/v1/logs", 8) == 0) return PO_SIG_LOGS; |
2 |
1 |
3 |
| if (len == 8 && memcmp(p, "/v1/logs", 8) == 0) return PO_SIG_LOGS; |
2 |
0 |
2 |
|
| 74 |
1 |
| return PO_SIG_NONE; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
19 |
| static const char *po_signal_name(int sig) { |
| 78 |
19 |
| switch (sig) { |
| 79 |
17 |
| case PO_SIG_TRACES: return "traces"; |
| 80 |
1 |
| case PO_SIG_METRICS: return "metrics"; |
| 81 |
1 |
| default: return "logs"; |
| 82 |
|
| } |
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
|
| /* The field a rejected count is reported under, per signal. */ |
| 86 |
1 |
| static const char *po_rejected_field(int sig) { |
| 87 |
1 |
| switch (sig) { |
| 88 |
1 |
| case PO_SIG_TRACES: return "rejectedSpans"; |
| 89 |
0 |
| case PO_SIG_METRICS: return "rejectedDataPoints"; |
| 90 |
0 |
| default: return "rejectedLogRecords"; |
| 91 |
|
| } |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| /* Normalise a Content-Type: lowercase, and cut at the first parameter along |
| 95 |
|
| * with the whitespace before it. Everything else is left alone, so a leading |
| 96 |
|
| * space is still a mismatch - a header with one is malformed, and quietly |
| 97 |
|
| * accepting it here would mean two receivers disagreeing about what is valid. |
| 98 |
|
| * |
| 99 |
|
| * Returns the length written; the buffer is always NUL-terminated. */ |
| 100 |
37 |
| static size_t po_ctype_norm(const char *p, size_t len, char *out, size_t cap) { |
| 101 |
|
| size_t n, i; |
| 102 |
|
| |
| 103 |
|
| /* The cut is found in the INPUT, not while filling the output. Bounding |
| 104 |
|
| * the search by the buffer would let a long parameter list push the |
| 105 |
|
| * semicolon out of reach and turn a perfectly good application/json into |
| 106 |
|
| * a 415. */ |
| 107 |
797 |
▶ | for (i = 0; i < len && p[i] != ';'; i++) ; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < len && p[i] != ';'; i++) ; |
762 |
35 |
797 |
| for (i = 0; i < len && p[i] != ';'; i++) ; |
760 |
2 |
762 |
|
| 108 |
37 |
▶ | while (i && (p[i-1] == ' ' || p[i-1] == '\t')) i--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (i && (p[i-1] == ' ' || p[i-1] == '\t')) i--; |
37 |
0 |
37 |
| while (i && (p[i-1] == ' ' || p[i-1] == '\t')) i--; |
0 |
37 |
37 |
| while (i && (p[i-1] == ' ' || p[i-1] == '\t')) i--; |
0 |
37 |
37 |
|
| 109 |
|
| |
| 110 |
37 |
▶ | if (i > cap - 1) i = cap - 1; /* longer than any type we serve */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > cap - 1) i = cap - 1; /* longer than any type we serve */ |
0 |
37 |
37 |
|
| 111 |
797 |
▶ | for (n = 0; n < i; n++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (n = 0; n < i; n++) { |
760 |
37 |
797 |
|
| 112 |
760 |
| char c = p[n]; |
| 113 |
760 |
▶ | out[n] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
Branch: 2 branches
| Branch | True | False | Total |
| out[n] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
693 |
67 |
760 |
| out[n] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
20 |
673 |
693 |
|
| 114 |
|
| } |
| 115 |
37 |
| out[n] = '\0'; |
| 116 |
37 |
| return n; |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
37 |
| static int po_enc_of(const char *p, size_t len) { |
| 120 |
37 |
▶ | if (len == 22 && memcmp(p, "application/x-protobuf", 22) == 0) return PO_ENC_PROTOBUF; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 22 && memcmp(p, "application/x-protobuf", 22) == 0) return PO_ENC_PROTOBUF; |
30 |
7 |
37 |
| if (len == 22 && memcmp(p, "application/x-protobuf", 22) == 0) return PO_ENC_PROTOBUF; |
30 |
0 |
30 |
|
| 121 |
7 |
▶ | if (len == 16 && memcmp(p, "application/json", 16) == 0) return PO_ENC_JSON; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 16 && memcmp(p, "application/json", 16) == 0) return PO_ENC_JSON; |
5 |
2 |
7 |
| if (len == 16 && memcmp(p, "application/json", 16) == 0) return PO_ENC_JSON; |
5 |
0 |
5 |
|
| 122 |
2 |
| return PO_ENC_NONE; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
9 |
| static int po_ce_of(const char *p, size_t len) { |
| 126 |
|
| size_t i; |
| 127 |
|
| char b[16]; |
| 128 |
9 |
▶ | if (len >= sizeof(b)) return PO_CE_NONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= sizeof(b)) return PO_CE_NONE; |
0 |
9 |
9 |
|
| 129 |
51 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
42 |
9 |
51 |
|
| 130 |
42 |
| char c = p[i]; |
| 131 |
42 |
▶ | b[i] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
Branch: 2 branches
| Branch | True | False | Total |
| b[i] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
42 |
0 |
42 |
| b[i] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
0 |
42 |
42 |
|
| 132 |
|
| } |
| 133 |
9 |
▶ | if (len == 4 && memcmp(b, "gzip", 4) == 0) return PO_CE_GZIP; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 4 && memcmp(b, "gzip", 4) == 0) return PO_CE_GZIP; |
7 |
2 |
9 |
| if (len == 4 && memcmp(b, "gzip", 4) == 0) return PO_CE_GZIP; |
7 |
0 |
7 |
|
| 134 |
2 |
▶ | if (len == 7 && memcmp(b, "deflate", 7) == 0) return PO_CE_DEFLATE; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 7 && memcmp(b, "deflate", 7) == 0) return PO_CE_DEFLATE; |
2 |
0 |
2 |
| if (len == 7 && memcmp(b, "deflate", 7) == 0) return PO_CE_DEFLATE; |
2 |
0 |
2 |
|
| 135 |
0 |
| return PO_CE_NONE; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| /* --- decompression -------------------------------------------------------- */ |
| 139 |
|
| |
| 140 |
|
| #ifdef PO_HAVE_ZLIB |
| 141 |
|
| /* Inflate with a hard ceiling, refusing the moment it is crossed rather than |
| 142 |
|
| * after. That ordering is the whole point: a forty-kilobyte body expanding to |
| 143 |
|
| * gigabytes must be stopped while it is expanding, and any interface that can |
| 144 |
|
| * be asked to produce an unbounded amount in one call cannot do that. |
| 145 |
|
| * |
| 146 |
|
| * windowBits selects the framing, and the two HTTP encodings are not the same |
| 147 |
|
| * stream: 15+16 is gzip only, 15 is the zlib wrapper that `deflate` means. |
| 148 |
|
| * Neither is 15+32, which auto-detects - a body labelled gzip that is really |
| 149 |
|
| * zlib is a mislabelled encoding, and saying so beats guessing. |
| 150 |
|
| * |
| 151 |
|
| * There is no transparent fallback. Passing non-compressed data through, which |
| 152 |
|
| * is what the Perl decompressors do by default, would accept a body whose gzip |
| 153 |
|
| * framing is subtly wrong and then report it as a malformed OTLP payload - |
| 154 |
|
| * losing the actual cause on the way. |
| 155 |
|
| */ |
| 156 |
9 |
| static int po_inflate_bounded(const char *src, size_t srclen, int gzip, |
| 157 |
|
| size_t max, char **out, size_t *outlen) { |
| 158 |
|
| z_stream z; |
| 159 |
9 |
| char *buf = NULL; |
| 160 |
9 |
| size_t cap = 0, len = 0; |
| 161 |
|
| int rc; |
| 162 |
|
| |
| 163 |
|
| /* avail_in is a uInt. A compressed body over 4GB is not a request anyone |
| 164 |
|
| * is making, and refusing it is a 413, which is the truthful answer. */ |
| 165 |
9 |
▶ | if (srclen > (size_t)0xFFFFFFFFUL) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (srclen > (size_t)0xFFFFFFFFUL) return 0; |
0 |
9 |
9 |
|
| 166 |
|
| |
| 167 |
9 |
| memset(&z, 0, sizeof(z)); |
| 168 |
9 |
▶ | if (inflateInit2(&z, gzip ? 15 + 16 : 15) != Z_OK) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (inflateInit2(&z, gzip ? 15 + 16 : 15) != Z_OK) return 0; |
7 |
2 |
9 |
| if (inflateInit2(&z, gzip ? 15 + 16 : 15) != Z_OK) return 0; |
0 |
9 |
9 |
|
| 169 |
9 |
| z.next_in = (Bytef *)(void *)src; |
| 170 |
9 |
| z.avail_in = (uInt)srclen; |
| 171 |
|
| |
| 172 |
2 |
| for (;;) { |
| 173 |
|
| size_t room; |
| 174 |
11 |
▶ | if (len == cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == cap) { |
11 |
0 |
11 |
|
| 175 |
|
| size_t ncap; |
| 176 |
|
| char *nb; |
| 177 |
11 |
▶ | if (len > max) goto fail; /* the ceiling, crossed */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > max) goto fail; /* the ceiling, crossed */ |
0 |
11 |
11 |
|
| 178 |
11 |
▶ | ncap = cap ? cap * 2 : 65536; |
Branch: 1 branch
| Branch | True | False | Total |
| ncap = cap ? cap * 2 : 65536; |
2 |
9 |
11 |
|
| 179 |
|
| /* One byte past the ceiling is enough to notice the overrun. */ |
| 180 |
11 |
▶ | if (ncap > max + 1) ncap = max + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ncap > max + 1) ncap = max + 1; |
8 |
3 |
11 |
|
| 181 |
11 |
▶ | if (ncap <= cap) goto fail; /* cannot grow: at the ceiling */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ncap <= cap) goto fail; /* cannot grow: at the ceiling */ |
0 |
11 |
11 |
|
| 182 |
11 |
| nb = (char *)realloc(buf, ncap); |
| 183 |
11 |
▶ | if (!nb) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nb) goto fail; |
0 |
11 |
11 |
|
| 184 |
11 |
| buf = nb; cap = ncap; |
| 185 |
|
| } |
| 186 |
11 |
| room = cap - len; |
| 187 |
11 |
▶ | if (room > (size_t)0x40000000UL) room = (size_t)0x40000000UL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (room > (size_t)0x40000000UL) room = (size_t)0x40000000UL; |
0 |
11 |
11 |
|
| 188 |
11 |
| z.next_out = (Bytef *)(buf + len); |
| 189 |
11 |
| z.avail_out = (uInt)room; |
| 190 |
11 |
| rc = inflate(&z, Z_NO_FLUSH); |
| 191 |
11 |
| len += room - (size_t)z.avail_out; |
| 192 |
11 |
▶ | if (rc == Z_STREAM_END) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == Z_STREAM_END) break; |
3 |
8 |
11 |
|
| 193 |
8 |
▶ | if (rc != Z_OK) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != Z_OK) goto fail; |
3 |
5 |
8 |
|
| 194 |
5 |
▶ | if (len > max) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > max) goto fail; |
2 |
3 |
5 |
|
| 195 |
|
| /* Room left and nothing left to feed it: the stream stops mid-member. |
| 196 |
|
| * The Perl decompressor hands back what it got so far, which arrives |
| 197 |
|
| * later as a malformed OTLP payload with the real cause thrown away. */ |
| 198 |
3 |
▶ | if (z.avail_in == 0 && z.avail_out != 0) goto fail; |
Branch: 2 branches
| Branch | True | False | Total |
| if (z.avail_in == 0 && z.avail_out != 0) goto fail; |
1 |
2 |
3 |
| if (z.avail_in == 0 && z.avail_out != 0) goto fail; |
1 |
0 |
1 |
|
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
3 |
| inflateEnd(&z); |
| 202 |
3 |
▶ | if (!buf) { buf = (char *)malloc(1); if (!buf) return 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!buf) { buf = (char *)malloc(1); if (!buf) return 0; } |
0 |
3 |
3 |
| if (!buf) { buf = (char *)malloc(1); if (!buf) return 0; } |
0 |
0 |
0 |
|
| 203 |
3 |
| *out = buf; *outlen = len; |
| 204 |
3 |
| return 1; |
| 205 |
|
| |
| 206 |
6 |
| fail: |
| 207 |
6 |
| inflateEnd(&z); |
| 208 |
6 |
| free(buf); |
| 209 |
6 |
| return 0; |
| 210 |
|
| } |
| 211 |
|
| #endif /* PO_HAVE_ZLIB */ |
| 212 |
|
| |
| 213 |
|
| /* --- the partial-success body --------------------------------------------- */ |
| 214 |
|
| |
| 215 |
|
| /* ExportTraceServiceResponse { partial_success = 1 } |
| 216 |
|
| * ExportTracePartialSuccess { rejected_* = 1, error_message = 2 } |
| 217 |
|
| * |
| 218 |
|
| * Tiny, and on the response path rather than the hot one, but it has to be |
| 219 |
|
| * exactly right: this is the channel that says "I kept 9,600 of these and |
| 220 |
|
| * rejected 400", and Punk::OpenTelemetry's exporter reads it. `out` needs 80 |
| 221 |
|
| * bytes; the message is truncated to 40 so the inner length stays one byte. */ |
| 222 |
2 |
| static size_t po_partial_success_pb(char *out, po_u64 rej, |
| 223 |
|
| const char *msg, size_t mlen) { |
| 224 |
|
| char inner[64]; |
| 225 |
2 |
| size_t n = 0, o = 0; |
| 226 |
|
| |
| 227 |
2 |
▶ | if (mlen > 40) mlen = 40; |
Branch: 1 branch
| Branch | True | False | Total |
| if (mlen > 40) mlen = 40; |
0 |
2 |
2 |
|
| 228 |
|
| |
| 229 |
2 |
▶ | if (rej) { /* field 1, varint */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (rej) { /* field 1, varint */ |
2 |
0 |
2 |
|
| 230 |
2 |
| po_u64 v = rej; |
| 231 |
2 |
| inner[n++] = (char)0x08; |
| 232 |
2 |
| do { unsigned char c = (unsigned char)(v & 0x7F); v >>= 7; |
| 233 |
2 |
▶ | if (v) c |= 0x80; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) c |= 0x80; |
0 |
2 |
2 |
|
| 234 |
2 |
▶ | inner[n++] = (char)c; } while (v); |
Branch: 1 branch
| Branch | True | False | Total |
| inner[n++] = (char)c; } while (v); |
0 |
2 |
2 |
|
| 235 |
|
| } |
| 236 |
2 |
▶ | if (msg && mlen) { /* field 2, length-delimited */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (msg && mlen) { /* field 2, length-delimited */ |
2 |
0 |
2 |
| if (msg && mlen) { /* field 2, length-delimited */ |
2 |
0 |
2 |
|
| 237 |
2 |
| inner[n++] = (char)0x12; |
| 238 |
2 |
| inner[n++] = (char)mlen; |
| 239 |
2 |
| memcpy(inner + n, msg, mlen); n += mlen; |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
2 |
| out[o++] = (char)0x0A; /* field 1, length-delimited */ |
| 243 |
2 |
| out[o++] = (char)n; |
| 244 |
2 |
| memcpy(out + o, inner, n); o += n; |
| 245 |
2 |
| return o; |
| 246 |
|
| } |
| 247 |
|
| |
| 248 |
|
| /* --- the response --------------------------------------------------------- */ |
| 249 |
|
| |
| 250 |
43 |
| static SV *po_psgi_res(pTHX_ int status, const char *ctype, SV *body, |
| 251 |
|
| const char *xk, SV *xv) { |
| 252 |
43 |
| AV *hdr = newAV(); |
| 253 |
43 |
| AV *bod = newAV(); |
| 254 |
43 |
| AV *res = newAV(); |
| 255 |
|
| STRLEN blen; |
| 256 |
|
| |
| 257 |
|
| (void)SvPV(body, blen); |
| 258 |
43 |
| av_push(hdr, newSVpvs("Content-Type")); |
| 259 |
43 |
| av_push(hdr, newSVpv(ctype, 0)); |
| 260 |
43 |
| av_push(hdr, newSVpvs("Content-Length")); |
| 261 |
43 |
| av_push(hdr, newSVuv((UV)blen)); |
| 262 |
43 |
▶ | if (xk) { av_push(hdr, newSVpv(xk, 0)); av_push(hdr, xv); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (xk) { av_push(hdr, newSVpv(xk, 0)); av_push(hdr, xv); } |
2 |
41 |
43 |
|
| 263 |
|
| |
| 264 |
43 |
| av_push(bod, body); |
| 265 |
43 |
| av_push(res, newSViv((IV)status)); |
| 266 |
43 |
| av_push(res, newRV_noinc((SV *)hdr)); |
| 267 |
43 |
| av_push(res, newRV_noinc((SV *)bod)); |
| 268 |
43 |
| return newRV_noinc((SV *)res); |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
|
| /* The status codes are a data-retention decision rather than a formality: an |
| 272 |
|
| * OTLP client retries 429/502/503/504 and DROPS everything else, so getting |
| 273 |
|
| * them backwards means either losing telemetry or being retried forever. */ |
| 274 |
20 |
| static SV *po_err(pTHX_ int status, const char *msg, const char *xk, SV *xv) { |
| 275 |
20 |
| SV *body = newSVpv(msg, 0); |
| 276 |
20 |
| sv_catpvs(body, "\n"); |
| 277 |
20 |
| return po_psgi_res(aTHX_ status, "text/plain", body, xk, xv); |
| 278 |
|
| } |
| 279 |
|
| |
| 280 |
|
| /* Strip perl's " at FILE line N." from a die message, so what reaches the |
| 281 |
|
| * client is the reason and not this file's line numbering. */ |
| 282 |
1 |
| static SV *po_trim_die(pTHX_ SV *err) { |
| 283 |
|
| STRLEN len; |
| 284 |
|
| const char *p; |
| 285 |
|
| const char *end; |
| 286 |
|
| const char *q; |
| 287 |
|
| |
| 288 |
1 |
| p = SvPV(err, len); |
| 289 |
1 |
| end = p + len; |
| 290 |
|
| |
| 291 |
2 |
▶ | while (end > p && isSPACE(end[-1])) end--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (end > p && isSPACE(end[-1])) end--; |
2 |
0 |
2 |
| while (end > p && isSPACE(end[-1])) end--; |
1 |
1 |
2 |
|
| 292 |
1 |
▶ | if (end > p && end[-1] == '.') end--; |
Branch: 2 branches
| Branch | True | False | Total |
| if (end > p && end[-1] == '.') end--; |
1 |
0 |
1 |
| if (end > p && end[-1] == '.') end--; |
1 |
0 |
1 |
|
| 293 |
|
| |
| 294 |
1 |
| q = end; |
| 295 |
3 |
▶ | while (q > p && q[-1] >= '0' && q[-1] <= '9') q--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (q > p && q[-1] >= '0' && q[-1] <= '9') q--; |
3 |
0 |
3 |
| while (q > p && q[-1] >= '0' && q[-1] <= '9') q--; |
2 |
1 |
3 |
| while (q > p && q[-1] >= '0' && q[-1] <= '9') q--; |
2 |
0 |
2 |
|
| 296 |
1 |
▶ | if (q < end) { /* there was a number */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (q < end) { /* there was a number */ |
1 |
0 |
1 |
|
| 297 |
1 |
| const char *r = q; |
| 298 |
2 |
▶ | while (r > p && isSPACE(r[-1])) r--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (r > p && isSPACE(r[-1])) r--; |
2 |
0 |
2 |
| while (r > p && isSPACE(r[-1])) r--; |
1 |
1 |
2 |
|
| 299 |
1 |
▶ | if (r - p >= 4 && memcmp(r - 4, "line", 4) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (r - p >= 4 && memcmp(r - 4, "line", 4) == 0) { |
1 |
0 |
1 |
| if (r - p >= 4 && memcmp(r - 4, "line", 4) == 0) { |
1 |
0 |
1 |
|
| 300 |
1 |
| r -= 4; |
| 301 |
2 |
▶ | while (r > p && isSPACE(r[-1])) r--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (r > p && isSPACE(r[-1])) r--; |
2 |
0 |
2 |
| while (r > p && isSPACE(r[-1])) r--; |
1 |
1 |
2 |
|
| 302 |
16 |
▶ | while (r > p && !isSPACE(r[-1])) r--; /* \S+, the file name */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (r > p && !isSPACE(r[-1])) r--; /* \S+, the file name */ |
16 |
0 |
16 |
| while (r > p && !isSPACE(r[-1])) r--; /* \S+, the file name */ |
15 |
1 |
16 |
|
| 303 |
2 |
▶ | while (r > p && isSPACE(r[-1])) r--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (r > p && isSPACE(r[-1])) r--; |
2 |
0 |
2 |
| while (r > p && isSPACE(r[-1])) r--; |
1 |
1 |
2 |
|
| 304 |
1 |
▶ | if (r - p >= 2 && memcmp(r - 2, "at", 2) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (r - p >= 2 && memcmp(r - 2, "at", 2) == 0) { |
1 |
0 |
1 |
| if (r - p >= 2 && memcmp(r - 2, "at", 2) == 0) { |
1 |
0 |
1 |
|
| 305 |
1 |
| r -= 2; |
| 306 |
2 |
▶ | while (r > p && isSPACE(r[-1])) r--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (r > p && isSPACE(r[-1])) r--; |
2 |
0 |
2 |
| while (r > p && isSPACE(r[-1])) r--; |
1 |
1 |
2 |
|
| 307 |
1 |
| end = r; |
| 308 |
|
| } |
| 309 |
|
| } |
| 310 |
|
| } |
| 311 |
1 |
| return newSVpvn(p, (STRLEN)(end - p)); |
| 312 |
|
| } |
| 313 |
|
| |
| 314 |
|
| /* --- the body ------------------------------------------------------------- */ |
| 315 |
|
| |
| 316 |
|
| /* Returns 1 with *out set, or 0 with *why set (413) or *why NULL (400). |
| 317 |
|
| * |
| 318 |
|
| * The limit is enforced BEFORE the read completes, not after. A hundred |
| 319 |
|
| * megabytes decoded into an arena and then measured is a memory exhaustion |
| 320 |
|
| * with extra steps. */ |
| 321 |
35 |
| static int po_read_body(pTHX_ po_ingest *ing, HV *env, SV **out, SV **why) { |
| 322 |
|
| SV **e; |
| 323 |
|
| SV *fh; |
| 324 |
35 |
| po_u64 len = 0; |
| 325 |
35 |
| STRLEN got = 0; |
| 326 |
|
| SV *buf; |
| 327 |
|
| int ce; |
| 328 |
|
| |
| 329 |
35 |
| *out = NULL; *why = NULL; |
| 330 |
|
| |
| 331 |
35 |
| e = hv_fetchs(env, "CONTENT_LENGTH", 0); |
| 332 |
35 |
▶ | if (!e || !SvOK(*e)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !SvOK(*e)) return 0; |
35 |
0 |
35 |
| if (!e || !SvOK(*e)) return 0; |
0 |
35 |
35 |
|
| 333 |
35 |
| (void)po_sv_to_u64(aTHX_ *e, &len); |
| 334 |
|
| |
| 335 |
35 |
▶ | if (len > ing->max_body) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > ing->max_body) { |
2 |
33 |
35 |
|
| 336 |
2 |
| *why = newSVpvs("body exceeds max_body ("); |
| 337 |
2 |
| sv_catsv(*why, sv_2mortal(po_u64_to_sv(ing->max_body))); |
| 338 |
2 |
| sv_catpvs(*why, ")"); |
| 339 |
2 |
| return 0; |
| 340 |
|
| } |
| 341 |
|
| |
| 342 |
33 |
| e = hv_fetchs(env, "psgi.input", 0); |
| 343 |
33 |
▶ | if (!e || !SvOK(*e)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !SvOK(*e)) return 0; |
33 |
0 |
33 |
| if (!e || !SvOK(*e)) return 0; |
0 |
33 |
33 |
|
| 344 |
33 |
| fh = *e; |
| 345 |
|
| |
| 346 |
33 |
▶ | buf = newSV(len ? (STRLEN)len + 1 : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| buf = newSV(len ? (STRLEN)len + 1 : 1); |
32 |
1 |
33 |
|
| 347 |
33 |
| sv_setpvs(buf, ""); |
| 348 |
|
| |
| 349 |
|
| /* An unblessed handle is read through PerlIO directly - Hyperman's |
| 350 |
|
| * psgi.input is exactly that, a :scalar or temp-file handle. An OBJECT |
| 351 |
|
| * keeps its method call, because a server that wraps the body in a class |
| 352 |
|
| * of its own may mean something by `read` that PerlIO does not. */ |
| 353 |
33 |
▶ | if (!sv_isobject(fh)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(fh)) { |
33 |
0 |
33 |
|
| 354 |
33 |
| IO *io = NULL; |
| 355 |
33 |
▶ | SV *rv = SvROK(fh) ? SvRV(fh) : fh; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *rv = SvROK(fh) ? SvRV(fh) : fh; |
33 |
0 |
33 |
|
| 356 |
33 |
▶ | if (SvTYPE(rv) == SVt_PVGV) io = GvIO((GV *)rv); |
Branch: 5 branches
| Branch | True | False | Total |
| if (SvTYPE(rv) == SVt_PVGV) io = GvIO((GV *)rv); |
33 |
0 |
33 |
| if (SvTYPE(rv) == SVt_PVGV) io = GvIO((GV *)rv); |
33 |
0 |
33 |
| if (SvTYPE(rv) == SVt_PVGV) io = GvIO((GV *)rv); |
0 |
33 |
33 |
| if (SvTYPE(rv) == SVt_PVGV) io = GvIO((GV *)rv); |
0 |
0 |
0 |
| if (SvTYPE(rv) == SVt_PVGV) io = GvIO((GV *)rv); |
33 |
0 |
33 |
|
| 357 |
0 |
▶ | else if (SvTYPE(rv) == SVt_PVIO) io = (IO *)rv; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvTYPE(rv) == SVt_PVIO) io = (IO *)rv; |
0 |
0 |
0 |
|
| 358 |
33 |
▶ | if (io && IoIFP(io)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (io && IoIFP(io)) { |
33 |
0 |
33 |
| if (io && IoIFP(io)) { |
33 |
0 |
33 |
|
| 359 |
33 |
▶ | SvGROW(buf, (STRLEN)len + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(buf, (STRLEN)len + 1); |
33 |
0 |
33 |
| SvGROW(buf, (STRLEN)len + 1); |
0 |
33 |
33 |
|
| 360 |
65 |
▶ | while (got < (STRLEN)len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (got < (STRLEN)len) { |
32 |
33 |
65 |
|
| 361 |
32 |
| SSize_t k = PerlIO_read(IoIFP(io), SvPVX(buf) + got, |
| 362 |
|
| (STRLEN)len - got); |
| 363 |
32 |
▶ | if (k <= 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k <= 0) break; |
0 |
32 |
32 |
|
| 364 |
32 |
| got += (STRLEN)k; |
| 365 |
|
| } |
| 366 |
33 |
| SvCUR_set(buf, got); |
| 367 |
33 |
| *SvEND(buf) = '\0'; |
| 368 |
33 |
| goto have_body; |
| 369 |
|
| } |
| 370 |
|
| } |
| 371 |
|
| |
| 372 |
|
| { /* $fh->read(my $chunk, $n) - the same call the Perl made. */ |
| 373 |
0 |
| SV *chunk = newSV(0); |
| 374 |
0 |
▶ | while (got < (STRLEN)len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (got < (STRLEN)len) { |
0 |
0 |
0 |
|
| 375 |
0 |
| STRLEN want = (STRLEN)len - got; |
| 376 |
|
| SSize_t k; |
| 377 |
0 |
| dSP; |
| 378 |
0 |
▶ | if (want > 65536) want = 65536; |
Branch: 1 branch
| Branch | True | False | Total |
| if (want > 65536) want = 65536; |
0 |
0 |
0 |
|
| 379 |
0 |
| ENTER; SAVETMPS; |
| 380 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 381 |
0 |
▶ | XPUSHs(fh); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(fh); |
0 |
0 |
0 |
|
| 382 |
0 |
▶ | XPUSHs(chunk); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(chunk); |
0 |
0 |
0 |
|
| 383 |
0 |
▶ | XPUSHs(sv_2mortal(newSVuv((UV)want))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVuv((UV)want))); |
0 |
0 |
0 |
|
| 384 |
0 |
| PUTBACK; |
| 385 |
0 |
| call_method("read", G_SCALAR | G_EVAL); |
| 386 |
0 |
| SPAGAIN; |
| 387 |
|
| { |
| 388 |
0 |
| SV *r = POPs; /* never SvTRUE around a POP */ |
| 389 |
0 |
▶ | k = SvOK(r) ? (SSize_t)SvIV(r) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| k = SvOK(r) ? (SSize_t)SvIV(r) : 0; |
0 |
0 |
0 |
|
| 390 |
|
| } |
| 391 |
0 |
| PUTBACK; |
| 392 |
0 |
▶ | if (SvTRUE(ERRSV)) k = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) k = 0; |
0 |
0 |
0 |
| if (SvTRUE(ERRSV)) k = 0; |
0 |
0 |
0 |
|
| 393 |
0 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 394 |
0 |
▶ | if (k <= 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k <= 0) break; |
0 |
0 |
0 |
|
| 395 |
0 |
| sv_catsv(buf, chunk); |
| 396 |
0 |
| got += (STRLEN)k; |
| 397 |
|
| } |
| 398 |
0 |
| SvREFCNT_dec(chunk); |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
33 |
| have_body: |
| 402 |
33 |
| e = hv_fetchs(env, "HTTP_CONTENT_ENCODING", 0); |
| 403 |
33 |
| ce = PO_CE_NONE; |
| 404 |
33 |
▶ | if (e && SvOK(*e)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && SvOK(*e)) { |
9 |
24 |
33 |
| if (e && SvOK(*e)) { |
9 |
0 |
9 |
|
| 405 |
|
| STRLEN cl; |
| 406 |
|
| const char *cp; |
| 407 |
9 |
| cp = SvPV(*e, cl); |
| 408 |
9 |
| ce = po_ce_of(cp, cl); |
| 409 |
|
| } |
| 410 |
|
| |
| 411 |
33 |
▶ | if (ce != PO_CE_NONE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ce != PO_CE_NONE) { |
9 |
24 |
33 |
|
| 412 |
9 |
| po_u64 max = ing->max_body; |
| 413 |
9 |
▶ | po_u64 room = (len && ing->max_ratio > PO_U64_MAX / len) |
Branch: 1 branch
| Branch | True | False | Total |
| po_u64 room = (len && ing->max_ratio > PO_U64_MAX / len) |
9 |
0 |
9 |
|
| 414 |
18 |
▶ | ? PO_U64_MAX : len * ing->max_ratio; |
Branch: 1 branch
| Branch | True | False | Total |
| ? PO_U64_MAX : len * ing->max_ratio; |
9 |
0 |
9 |
|
| 415 |
9 |
▶ | if (room < max) max = room; |
Branch: 1 branch
| Branch | True | False | Total |
| if (room < max) max = room; |
9 |
0 |
9 |
|
| 416 |
|
| #ifdef PO_HAVE_ZLIB |
| 417 |
|
| { |
| 418 |
|
| STRLEN clen; |
| 419 |
|
| const char *cp; |
| 420 |
9 |
| char *raw = NULL; |
| 421 |
9 |
| size_t rawlen = 0; |
| 422 |
9 |
| cp = SvPV(buf, clen); |
| 423 |
9 |
▶ | if (!po_inflate_bounded(cp, (size_t)clen, ce == PO_CE_GZIP, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_inflate_bounded(cp, (size_t)clen, ce == PO_CE_GZIP, |
6 |
3 |
9 |
|
| 424 |
|
| (size_t)max, &raw, &rawlen)) { |
| 425 |
6 |
| SvREFCNT_dec(buf); |
| 426 |
6 |
| *why = newSVpvs("compressed body expands beyond the limit"); |
| 427 |
6 |
| return 0; |
| 428 |
|
| } |
| 429 |
3 |
| sv_setpvn(buf, raw, (STRLEN)rawlen); |
| 430 |
3 |
| free(raw); |
| 431 |
|
| } |
| 432 |
|
| #else |
| 433 |
|
| /* No zlib at build time. The fallback is the core decompressors, |
| 434 |
|
| * driven from the .pm, and it is the ONLY thing in this path that is |
| 435 |
|
| * not C. It reads in bounded chunks for the same reason as above. */ |
| 436 |
|
| { |
| 437 |
|
| dSP; |
| 438 |
|
| SV *res; |
| 439 |
|
| int ok; |
| 440 |
|
| ENTER; SAVETMPS; |
| 441 |
|
| PUSHMARK(SP); |
| 442 |
|
| XPUSHs(buf); |
| 443 |
|
| XPUSHs(sv_2mortal(newSVpv(ce == PO_CE_GZIP ? "gzip" : "deflate", 0))); |
| 444 |
|
| XPUSHs(sv_2mortal(po_u64_to_sv(max))); |
| 445 |
|
| PUTBACK; |
| 446 |
|
| call_pv("Punk::Observe::Ingest::_inflate", G_SCALAR | G_EVAL); |
| 447 |
|
| SPAGAIN; |
| 448 |
|
| res = POPs; |
| 449 |
|
| ok = SvOK(res) ? 1 : 0; |
| 450 |
|
| if (ok) sv_setsv(buf, res); |
| 451 |
|
| PUTBACK; |
| 452 |
|
| if (SvTRUE(ERRSV)) ok = 0; |
| 453 |
|
| FREETMPS; LEAVE; |
| 454 |
|
| if (!ok) { |
| 455 |
|
| SvREFCNT_dec(buf); |
| 456 |
|
| *why = newSVpvs("compressed body expands beyond the limit"); |
| 457 |
|
| return 0; |
| 458 |
|
| } |
| 459 |
|
| } |
| 460 |
|
| #endif |
| 461 |
|
| } |
| 462 |
|
| |
| 463 |
27 |
| *out = buf; |
| 464 |
27 |
| return 1; |
| 465 |
|
| } |
| 466 |
|
| |
| 467 |
|
| /* --- decoding ------------------------------------------------------------- */ |
| 468 |
|
| |
| 469 |
|
| /* Returns 1 on a decodable batch. *errmsg, when set, is the detail appended to |
| 470 |
|
| * "malformed OTLP: "; the caller owns it. */ |
| 471 |
27 |
| static int po_decode_body(pTHX_ SV *body, int sig, int enc, po_batch *b, |
| 472 |
|
| SV **errmsg) { |
| 473 |
27 |
| *errmsg = NULL; |
| 474 |
|
| |
| 475 |
27 |
▶ | if (enc == PO_ENC_PROTOBUF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc == PO_ENC_PROTOBUF) { |
22 |
5 |
27 |
|
| 476 |
|
| STRLEN len; |
| 477 |
|
| const char *p; |
| 478 |
|
| int ok; |
| 479 |
22 |
| p = SvPV(body, len); |
| 480 |
22 |
| switch (sig) { |
| 481 |
20 |
| case PO_SIG_TRACES: ok = po_otlp_traces(p, (size_t)len, b); break; |
| 482 |
1 |
| case PO_SIG_METRICS: ok = po_otlp_metrics(p, (size_t)len, b); break; |
| 483 |
1 |
| default: ok = po_otlp_logs(p, (size_t)len, b); break; |
| 484 |
|
| } |
| 485 |
22 |
▶ | return ok && !b->err; |
Branch: 2 branches
| Branch | True | False | Total |
| return ok && !b->err; |
21 |
1 |
22 |
| return ok && !b->err; |
21 |
0 |
21 |
|
| 486 |
|
| } |
| 487 |
|
| |
| 488 |
|
| { |
| 489 |
5 |
| dSP; |
| 490 |
5 |
| int count, rc = 0; |
| 491 |
|
| SV *doc; |
| 492 |
5 |
| ENTER; SAVETMPS; |
| 493 |
5 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
5 |
5 |
|
| 494 |
5 |
▶ | XPUSHs(body); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(body); |
0 |
5 |
5 |
|
| 495 |
5 |
| PUTBACK; |
| 496 |
5 |
| count = call_pv("File::Raw::JSON::file_json_decode", G_SCALAR | G_EVAL); |
| 497 |
5 |
| SPAGAIN; |
| 498 |
5 |
▶ | doc = count ? POPs : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| doc = count ? POPs : &PL_sv_undef; |
5 |
0 |
5 |
|
| 499 |
5 |
| PUTBACK; |
| 500 |
|
| |
| 501 |
5 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
5 |
0 |
5 |
| if (SvTRUE(ERRSV)) { |
1 |
4 |
5 |
|
| 502 |
1 |
▶ | *errmsg = po_trim_die(aTHX_ ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| *errmsg = po_trim_die(aTHX_ ERRSV); |
1 |
0 |
1 |
|
| 503 |
|
| } |
| 504 |
4 |
▶ | else if (!SvROK(doc) || SvTYPE(SvRV(doc)) != SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!SvROK(doc) || SvTYPE(SvRV(doc)) != SVt_PVHV) { |
4 |
0 |
4 |
| else if (!SvROK(doc) || SvTYPE(SvRV(doc)) != SVt_PVHV) { |
0 |
4 |
4 |
|
| 505 |
0 |
| *errmsg = newSVpvs("not a JSON object"); |
| 506 |
|
| } |
| 507 |
|
| else { |
| 508 |
4 |
| switch (sig) { |
| 509 |
4 |
| case PO_SIG_TRACES: po_json_traces(aTHX_ doc, b); break; |
| 510 |
0 |
| case PO_SIG_METRICS: po_json_metrics(aTHX_ doc, b); break; |
| 511 |
0 |
| default: po_json_logs(aTHX_ doc, b); break; |
| 512 |
|
| } |
| 513 |
4 |
| rc = !b->err; |
| 514 |
|
| } |
| 515 |
5 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
5 |
0 |
5 |
|
| 516 |
5 |
| return rc; |
| 517 |
|
| } |
| 518 |
|
| } |
| 519 |
|
| |
| 520 |
|
| /* --- the request ---------------------------------------------------------- */ |
| 521 |
|
| |
| 522 |
44 |
| static SV *po_ingest_call(pTHX_ po_ingest *ing, SV *envrv) { |
| 523 |
|
| HV *env; |
| 524 |
|
| SV **e; |
| 525 |
44 |
| SV *tenant = NULL; |
| 526 |
44 |
| SV *body = NULL; |
| 527 |
44 |
| SV *why = NULL; |
| 528 |
44 |
| SV *errmsg = NULL; |
| 529 |
|
| po_batch b; |
| 530 |
|
| int sig, enc; |
| 531 |
44 |
| po_u64 n, rejected = 0; |
| 532 |
|
| SV *res; |
| 533 |
|
| |
| 534 |
44 |
▶ | if (!SvROK(envrv) || SvTYPE(SvRV(envrv)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(envrv) || SvTYPE(SvRV(envrv)) != SVt_PVHV) |
44 |
0 |
44 |
| if (!SvROK(envrv) || SvTYPE(SvRV(envrv)) != SVt_PVHV) |
1 |
43 |
44 |
|
| 535 |
1 |
| croak("Punk::Observe::Ingest::call: the PSGI environment must be a hashref"); |
| 536 |
43 |
| env = (HV *)SvRV(envrv); |
| 537 |
|
| |
| 538 |
43 |
| e = hv_fetchs(env, "PATH_INFO", 0); |
| 539 |
|
| { |
| 540 |
43 |
| STRLEN l = 0; |
| 541 |
43 |
| const char *p = ""; |
| 542 |
86 |
▶ | if (e && SvOK(*e)) p = SvPV(*e, l); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && SvOK(*e)) p = SvPV(*e, l); |
43 |
0 |
43 |
| if (e && SvOK(*e)) p = SvPV(*e, l); |
43 |
0 |
43 |
|
| 543 |
43 |
| sig = po_signal_of(p, (size_t)l); |
| 544 |
|
| } |
| 545 |
43 |
▶ | if (!sig) return po_err(aTHX_ 404, "not an OTLP endpoint", NULL, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sig) return po_err(aTHX_ 404, "not an OTLP endpoint", NULL, NULL); |
1 |
42 |
43 |
|
| 546 |
|
| |
| 547 |
42 |
| e = hv_fetchs(env, "REQUEST_METHOD", 0); |
| 548 |
|
| { |
| 549 |
42 |
| STRLEN l = 0; |
| 550 |
42 |
| const char *p = ""; |
| 551 |
84 |
▶ | if (e && SvOK(*e)) p = SvPV(*e, l); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && SvOK(*e)) p = SvPV(*e, l); |
42 |
0 |
42 |
| if (e && SvOK(*e)) p = SvPV(*e, l); |
42 |
0 |
42 |
|
| 552 |
42 |
▶ | if (!(l == 4 && memcmp(p, "POST", 4) == 0)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(l == 4 && memcmp(p, "POST", 4) == 0)) |
41 |
1 |
42 |
| if (!(l == 4 && memcmp(p, "POST", 4) == 0)) |
0 |
41 |
41 |
|
| 553 |
1 |
| return po_err(aTHX_ 405, "POST only", NULL, NULL); |
| 554 |
|
| } |
| 555 |
|
| |
| 556 |
|
| /* Auth first, so an unauthenticated caller cannot make the server spend |
| 557 |
|
| * anything on decoding. The resolver returns a tenant id or undef; the |
| 558 |
|
| * engine's default resolver is absent entirely, which is what makes "no |
| 559 |
|
| * key on a private network" a supported configuration rather than a |
| 560 |
|
| * hole. A resolver that dies is a 401, not a 500. */ |
| 561 |
41 |
▶ | if (ing->auth) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ing->auth) { |
6 |
35 |
41 |
|
| 562 |
6 |
| dSP; |
| 563 |
|
| int count; |
| 564 |
6 |
| ENTER; SAVETMPS; |
| 565 |
6 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
6 |
6 |
|
| 566 |
6 |
▶ | XPUSHs(envrv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(envrv); |
0 |
6 |
6 |
|
| 567 |
6 |
| PUTBACK; |
| 568 |
6 |
| count = call_sv(ing->auth, G_SCALAR | G_EVAL); |
| 569 |
6 |
| SPAGAIN; |
| 570 |
|
| { |
| 571 |
6 |
▶ | SV *r = count ? POPs : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *r = count ? POPs : &PL_sv_undef; |
6 |
0 |
6 |
|
| 572 |
6 |
▶ | if (SvOK(r)) tenant = newSVsv(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(r)) tenant = newSVsv(r); |
3 |
3 |
6 |
|
| 573 |
|
| } |
| 574 |
6 |
| PUTBACK; |
| 575 |
6 |
▶ | if (SvTRUE(ERRSV)) { SvREFCNT_dec(tenant); tenant = NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { SvREFCNT_dec(tenant); tenant = NULL; } |
6 |
0 |
6 |
| if (SvTRUE(ERRSV)) { SvREFCNT_dec(tenant); tenant = NULL; } |
1 |
5 |
6 |
|
| 576 |
6 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
6 |
0 |
6 |
|
| 577 |
|
| |
| 578 |
6 |
▶ | if (tenant) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tenant) { |
3 |
3 |
6 |
|
| 579 |
|
| STRLEN tl; |
| 580 |
|
| (void)SvPV(tenant, tl); |
| 581 |
3 |
▶ | if (!tl) { SvREFCNT_dec(tenant); tenant = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tl) { SvREFCNT_dec(tenant); tenant = NULL; } |
1 |
2 |
3 |
|
| 582 |
|
| } |
| 583 |
6 |
▶ | if (!tenant) return po_err(aTHX_ 401, "unauthorized", NULL, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tenant) return po_err(aTHX_ 401, "unauthorized", NULL, NULL); |
4 |
2 |
6 |
|
| 584 |
|
| } |
| 585 |
35 |
| else tenant = newSVpvs("default"); |
| 586 |
|
| |
| 587 |
|
| { |
| 588 |
|
| char ct[128]; |
| 589 |
37 |
| size_t ctlen = 0; |
| 590 |
37 |
| e = hv_fetchs(env, "CONTENT_TYPE", 0); |
| 591 |
74 |
▶ | if (e && SvOK(*e)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && SvOK(*e)) { |
37 |
0 |
37 |
| if (e && SvOK(*e)) { |
37 |
0 |
37 |
|
| 592 |
|
| STRLEN l; |
| 593 |
|
| const char *p; |
| 594 |
37 |
| p = SvPV(*e, l); |
| 595 |
37 |
| ctlen = po_ctype_norm(p, (size_t)l, ct, sizeof(ct)); |
| 596 |
|
| } |
| 597 |
0 |
| else ct[0] = '\0'; |
| 598 |
37 |
| enc = po_enc_of(ct, ctlen); |
| 599 |
37 |
▶ | if (!enc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!enc) { |
2 |
35 |
37 |
|
| 600 |
2 |
| SV *m = newSVpvs("unsupported content type '"); |
| 601 |
2 |
| sv_catpvn(m, ct, (STRLEN)ctlen); |
| 602 |
2 |
| sv_catpvs(m, "'"); |
| 603 |
2 |
| SvREFCNT_dec(tenant); |
| 604 |
2 |
| res = po_err(aTHX_ 415, SvPV_nolen(m), NULL, NULL); |
| 605 |
2 |
| SvREFCNT_dec(m); |
| 606 |
2 |
| return res; |
| 607 |
|
| } |
| 608 |
|
| } |
| 609 |
|
| |
| 610 |
35 |
▶ | if (!po_read_body(aTHX_ ing, env, &body, &why)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_read_body(aTHX_ ing, env, &body, &why)) { |
8 |
27 |
35 |
|
| 611 |
8 |
| SvREFCNT_dec(tenant); |
| 612 |
8 |
▶ | if (why) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (why) { |
8 |
0 |
8 |
|
| 613 |
16 |
| res = po_err(aTHX_ 413, SvPV_nolen(why), NULL, NULL); |
| 614 |
8 |
| SvREFCNT_dec(why); |
| 615 |
8 |
| return res; |
| 616 |
|
| } |
| 617 |
0 |
| return po_err(aTHX_ 400, "no body", NULL, NULL); |
| 618 |
|
| } |
| 619 |
|
| |
| 620 |
27 |
▶ | if (!po_batch_init(&b, enc == PO_ENC_PROTOBUF ? 64 : 16)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!po_batch_init(&b, enc == PO_ENC_PROTOBUF ? 64 : 16)) { |
22 |
5 |
27 |
| if (!po_batch_init(&b, enc == PO_ENC_PROTOBUF ? 64 : 16)) { |
0 |
27 |
27 |
|
| 621 |
0 |
| SvREFCNT_dec(tenant); SvREFCNT_dec(body); |
| 622 |
0 |
| croak("out of memory"); |
| 623 |
|
| } |
| 624 |
|
| |
| 625 |
27 |
▶ | if (!po_decode_body(aTHX_ body, sig, enc, &b, &errmsg)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_decode_body(aTHX_ body, sig, enc, &b, &errmsg)) { |
2 |
25 |
27 |
|
| 626 |
2 |
| SV *m = newSVpvs("malformed OTLP"); |
| 627 |
2 |
▶ | if (errmsg) { sv_catpvs(m, ": "); sv_catsv(m, errmsg); |
Branch: 1 branch
| Branch | True | False | Total |
| if (errmsg) { sv_catpvs(m, ": "); sv_catsv(m, errmsg); |
1 |
1 |
2 |
|
| 628 |
1 |
| SvREFCNT_dec(errmsg); } |
| 629 |
2 |
| po_batch_free(&b); |
| 630 |
2 |
| SvREFCNT_dec(tenant); SvREFCNT_dec(body); |
| 631 |
2 |
| res = po_err(aTHX_ 400, SvPV_nolen(m), NULL, NULL); |
| 632 |
2 |
| SvREFCNT_dec(m); |
| 633 |
2 |
| return res; |
| 634 |
|
| } |
| 635 |
|
| |
| 636 |
|
| /* Over the per-batch record cap the excess is REJECTED and reported |
| 637 |
|
| * through partial_success rather than refused with a 4xx. A 4xx makes the |
| 638 |
|
| * exporter re-send the whole batch, forever, at exactly the moment the |
| 639 |
|
| * server is already under pressure. */ |
| 640 |
25 |
| n = (po_u64)b.n; |
| 641 |
25 |
▶ | if (ing->max_records && n > ing->max_records) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ing->max_records && n > ing->max_records) { |
3 |
22 |
25 |
| if (ing->max_records && n > ing->max_records) { |
3 |
0 |
3 |
|
| 642 |
3 |
| rejected = n - ing->max_records; |
| 643 |
3 |
| n = ing->max_records; |
| 644 |
|
| } |
| 645 |
|
| |
| 646 |
|
| /* A batch that decodes to zero records is a 200 and no append. An empty |
| 647 |
|
| * frame would carry a nonsense timestamp span, and a reader pruning on it |
| 648 |
|
| * skips exactly the wrong frames. */ |
| 649 |
25 |
▶ | if (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) { |
20 |
5 |
25 |
|
| 650 |
20 |
| int stored = 1; |
| 651 |
20 |
▶ | if (ing->on_batch) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ing->on_batch) { |
19 |
1 |
20 |
|
| 652 |
19 |
| dSP; |
| 653 |
|
| int count; |
| 654 |
19 |
| HV *out = newHV(); |
| 655 |
19 |
| hv_stores(out, "ok", newSViv(1)); |
| 656 |
19 |
| hv_stores(out, "records", po_u64_to_sv(n)); |
| 657 |
19 |
| hv_stores(out, "rejected", po_u64_to_sv(rejected)); |
| 658 |
19 |
| hv_stores(out, "dropped_bad_trace", newSViv((IV)b.dropped_bad_trace)); |
| 659 |
19 |
| hv_stores(out, "clamped_durations", newSViv((IV)b.clamped_durations)); |
| 660 |
|
| |
| 661 |
19 |
| stored = 0; |
| 662 |
19 |
| ENTER; SAVETMPS; |
| 663 |
19 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
19 |
19 |
|
| 664 |
19 |
▶ | XPUSHs(tenant); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(tenant); |
0 |
19 |
19 |
|
| 665 |
19 |
▶ | XPUSHs(sv_2mortal(newSVpv(po_signal_name(sig), 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(po_signal_name(sig), 0))); |
0 |
19 |
19 |
|
| 666 |
19 |
▶ | XPUSHs(body); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(body); |
0 |
19 |
19 |
|
| 667 |
19 |
▶ | XPUSHs(sv_2mortal(newSVpv(enc == PO_ENC_PROTOBUF ? "protobuf" |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(enc == PO_ENC_PROTOBUF ? "protobuf" |
0 |
19 |
19 |
| XPUSHs(sv_2mortal(newSVpv(enc == PO_ENC_PROTOBUF ? "protobuf" |
18 |
1 |
19 |
|
| 668 |
|
| : "json", 0))); |
| 669 |
19 |
▶ | XPUSHs(sv_2mortal(newRV_noinc((SV *)out))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newRV_noinc((SV *)out))); |
0 |
19 |
19 |
|
| 670 |
19 |
| PUTBACK; |
| 671 |
19 |
| count = call_sv(ing->on_batch, G_SCALAR | G_EVAL); |
| 672 |
19 |
| SPAGAIN; |
| 673 |
|
| { |
| 674 |
19 |
▶ | SV *r = count ? POPs : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *r = count ? POPs : &PL_sv_undef; |
19 |
0 |
19 |
|
| 675 |
19 |
| stored = SvTRUE(r) ? 1 : 0; |
| 676 |
|
| } |
| 677 |
19 |
| PUTBACK; |
| 678 |
19 |
▶ | if (SvTRUE(ERRSV)) stored = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) stored = 0; |
19 |
0 |
19 |
| if (SvTRUE(ERRSV)) stored = 0; |
1 |
18 |
19 |
|
| 679 |
|
| |
| 680 |
|
| /* `out` IS BIDIRECTIONAL. The callback owns the store, so it is |
| 681 |
|
| * the only party that knows how much of the batch the ingest rate |
| 682 |
|
| * limit truncated - and a partial success has to name a number |
| 683 |
|
| * that matches what was actually kept. Reading it back here is |
| 684 |
|
| * what keeps the response honest about the write. */ |
| 685 |
19 |
▶ | if (stored) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stored) { |
17 |
2 |
19 |
|
| 686 |
17 |
| SV **rj = hv_fetchs(out, "rejected", 0); |
| 687 |
17 |
| po_u64 back = 0; |
| 688 |
17 |
▶ | if (rj && SvOK(*rj) && po_sv_to_u64(aTHX_ *rj, &back) |
Branch: 3 branches
| Branch | True | False | Total |
| if (rj && SvOK(*rj) && po_sv_to_u64(aTHX_ *rj, &back) |
17 |
0 |
17 |
| if (rj && SvOK(*rj) && po_sv_to_u64(aTHX_ *rj, &back) |
17 |
0 |
17 |
| if (rj && SvOK(*rj) && po_sv_to_u64(aTHX_ *rj, &back) |
17 |
0 |
17 |
|
| 689 |
17 |
▶ | && back > rejected) rejected = back; |
Branch: 1 branch
| Branch | True | False | Total |
| && back > rejected) rejected = back; |
0 |
17 |
17 |
|
| 690 |
|
| } |
| 691 |
19 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
19 |
0 |
19 |
|
| 692 |
|
| } |
| 693 |
20 |
▶ | if (!stored) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!stored) { |
2 |
18 |
20 |
|
| 694 |
2 |
| po_batch_free(&b); |
| 695 |
2 |
| SvREFCNT_dec(tenant); SvREFCNT_dec(body); |
| 696 |
|
| /* Retryable: the data is recoverable after a cleanup, and an OTLP |
| 697 |
|
| * client backs off on 503 and drops on 500. */ |
| 698 |
2 |
| return po_err(aTHX_ 503, "store unavailable", |
| 699 |
|
| "Retry-After", newSViv(1)); |
| 700 |
|
| } |
| 701 |
|
| } |
| 702 |
|
| |
| 703 |
23 |
| po_batch_free(&b); |
| 704 |
23 |
| SvREFCNT_dec(tenant); |
| 705 |
23 |
| SvREFCNT_dec(body); |
| 706 |
|
| |
| 707 |
|
| /* A full accept is an EMPTY 200, and it must only be sent when it is one. */ |
| 708 |
23 |
▶ | if (!rejected) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rejected) |
20 |
3 |
23 |
|
| 709 |
40 |
▶ | return po_psgi_res(aTHX_ 200, |
Branch: 2 branches
| Branch | True | False | Total |
| return po_psgi_res(aTHX_ 200, |
17 |
3 |
20 |
| return po_psgi_res(aTHX_ 200, |
17 |
3 |
20 |
|
| 710 |
|
| enc == PO_ENC_PROTOBUF ? "application/x-protobuf" |
| 711 |
|
| : "application/json", |
| 712 |
17 |
| enc == PO_ENC_PROTOBUF ? newSVpvs("") |
| 713 |
3 |
| : newSVpvs("{}"), |
| 714 |
|
| NULL, NULL); |
| 715 |
|
| |
| 716 |
|
| { |
| 717 |
3 |
| SV *msg = newSVpvs("rejected "); |
| 718 |
3 |
| sv_catsv(msg, sv_2mortal(po_u64_to_sv(rejected))); |
| 719 |
3 |
| sv_catpvs(msg, " over the per-batch limit"); |
| 720 |
|
| |
| 721 |
3 |
▶ | if (enc == PO_ENC_PROTOBUF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc == PO_ENC_PROTOBUF) { |
2 |
1 |
3 |
|
| 722 |
|
| char pb[80]; |
| 723 |
|
| STRLEN ml; |
| 724 |
|
| const char *mp; |
| 725 |
|
| size_t o; |
| 726 |
2 |
| mp = SvPV(msg, ml); |
| 727 |
2 |
| o = po_partial_success_pb(pb, rejected, mp, (size_t)ml); |
| 728 |
2 |
| res = po_psgi_res(aTHX_ 200, "application/x-protobuf", |
| 729 |
|
| newSVpvn(pb, (STRLEN)o), NULL, NULL); |
| 730 |
|
| } |
| 731 |
|
| else { |
| 732 |
|
| /* Emitted here rather than through an encoder. The shape is fixed |
| 733 |
|
| * and the only variable parts are a decimal count and a message |
| 734 |
|
| * this file wrote, so there is nothing to escape - and the key |
| 735 |
|
| * order comes out stable instead of a hash's. |
| 736 |
|
| * |
| 737 |
|
| * Rule 3: a 64-bit count is a STRING in OTLP/JSON. */ |
| 738 |
1 |
| SV *j = newSVpvs("{\"partialSuccess\":{\""); |
| 739 |
1 |
| sv_catpv(j, po_rejected_field(sig)); |
| 740 |
1 |
| sv_catpvs(j, "\":\""); |
| 741 |
1 |
| sv_catsv(j, sv_2mortal(po_u64_to_sv(rejected))); |
| 742 |
1 |
| sv_catpvs(j, "\",\"errorMessage\":\""); |
| 743 |
1 |
| sv_catsv(j, msg); |
| 744 |
1 |
| sv_catpvs(j, "\"}}"); |
| 745 |
1 |
| res = po_psgi_res(aTHX_ 200, "application/json", j, NULL, NULL); |
| 746 |
|
| } |
| 747 |
3 |
| SvREFCNT_dec(msg); |
| 748 |
3 |
| return res; |
| 749 |
|
| } |
| 750 |
|
| } |
| 751 |
|
| |
| 752 |
|
| /* --- the object ----------------------------------------------------------- */ |
| 753 |
|
| |
| 754 |
37 |
| static po_ingest *po_ingest_new(void) { |
| 755 |
37 |
| po_ingest *ing = (po_ingest *)malloc(sizeof(po_ingest)); |
| 756 |
37 |
▶ | if (!ing) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ing) return NULL; |
0 |
37 |
37 |
|
| 757 |
37 |
| memset(ing, 0, sizeof(*ing)); |
| 758 |
37 |
| ing->max_body = (po_u64)16 * 1024 * 1024; |
| 759 |
37 |
▶ | if (ing->max_body > PO_BODY_MAX) ing->max_body = PO_BODY_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ing->max_body > PO_BODY_MAX) ing->max_body = PO_BODY_MAX; |
0 |
37 |
37 |
|
| 760 |
37 |
| ing->max_ratio = PO_MAX_RATIO; |
| 761 |
37 |
| ing->max_records = 0; |
| 762 |
37 |
| return ing; |
| 763 |
|
| } |
| 764 |
|
| |
| 765 |
37 |
| static void po_ingest_free(pTHX_ po_ingest *ing) { |
| 766 |
37 |
▶ | if (!ing) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ing) return; |
0 |
37 |
37 |
|
| 767 |
37 |
| SvREFCNT_dec(ing->auth); |
| 768 |
37 |
| SvREFCNT_dec(ing->on_batch); |
| 769 |
37 |
| free(ing); |
| 770 |
|
| } |
| 771 |
|
| |
| 772 |
78 |
| static po_ingest *po_ingest_of(pTHX_ SV *self, const char *who) { |
| 773 |
78 |
▶ | if (!SvROK(self) || !SvIOK(SvRV(self)) || !SvIVX(SvRV(self))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvROK(self) || !SvIOK(SvRV(self)) || !SvIVX(SvRV(self))) |
78 |
0 |
78 |
| if (!SvROK(self) || !SvIOK(SvRV(self)) || !SvIVX(SvRV(self))) |
78 |
0 |
78 |
| if (!SvROK(self) || !SvIOK(SvRV(self)) || !SvIVX(SvRV(self))) |
0 |
78 |
78 |
|
| 774 |
0 |
| croak("%s: not a Punk::Observe::Ingest", who); |
| 775 |
78 |
| return INT2PTR(po_ingest *, SvIVX(SvRV(self))); |
| 776 |
|
| } |
| 777 |
|
| |
| 778 |
|
| /* to_app returns a real CV rather than a Perl closure, with the object hung |
| 779 |
|
| * off it as ext magic: refcounted by perl, so the app holding the coderef is |
| 780 |
|
| * what keeps the engine alive and nothing has to be freed by hand. */ |
| 781 |
|
| static MGVTBL po_app_vtbl; /* static: zeroed, and the right member count |
| 782 |
|
| * whatever this perl's MGVTBL looks like */ |
| 783 |
|
| |
| 784 |
|
| XS_INTERNAL(po_ingest_app_xs); |
| 785 |
41 |
| XS_INTERNAL(po_ingest_app_xs) { |
| 786 |
41 |
| dXSARGS; |
| 787 |
|
| MAGIC *mg; |
| 788 |
|
| po_ingest *ing; |
| 789 |
|
| |
| 790 |
41 |
▶ | if (items != 1) croak("Usage: $app->($env)"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items != 1) croak("Usage: $app->($env)"); |
0 |
41 |
41 |
|
| 791 |
41 |
| mg = mg_find((SV *)cv, PERL_MAGIC_ext); |
| 792 |
41 |
▶ | if (!mg || !mg->mg_obj) croak("Punk::Observe::Ingest: app has no engine"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!mg || !mg->mg_obj) croak("Punk::Observe::Ingest: app has no engine"); |
41 |
0 |
41 |
| if (!mg || !mg->mg_obj) croak("Punk::Observe::Ingest: app has no engine"); |
0 |
41 |
41 |
|
| 793 |
41 |
| ing = po_ingest_of(aTHX_ mg->mg_obj, "app"); |
| 794 |
|
| |
| 795 |
41 |
| ST(0) = sv_2mortal(po_ingest_call(aTHX_ ing, ST(0))); |
| 796 |
41 |
| XSRETURN(1); |
| 797 |
|
| } |
| 798 |
|
| |
| 799 |
|
| #endif /* PO_INGEST_H */ |