HTTP3.xs

filter: Statementstmt 80.5% 1214 / 1508 Branchbran 59.7% 485 / 812 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.2% 1699 / 2320 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Unblock/HTTP3/NativeABI.pm ↑ Summary blib/lib/Unblock/HTTP3.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "uniform_http_fastpath.h"
5 #include "lib/Unblock/HTTP3/NativeABI/unblock_http3_consumer.h"
6  
7 #include <nghttp3/nghttp3.h>
8  
9 #include <inttypes.h>
10 #include <string.h>
11  
12 #define UNBLOCK_HTTP3_MAX_VARINT UINT64_C(0x3fffffffffffffff)
13  
14 typedef struct {
15 uhttp_native_api uniform_api;
16 } my_cxt_t;
17  
18 START_MY_CXT
19  
20 typedef struct unblock_http3_body_chunk {
21 SV *storage;
22 uint8_t *data;
23 size_t len;
24 size_t acked;
25 int supplied;
26 struct unblock_http3_body_chunk *next;
27 } unblock_http3_body_chunk;
28  
29 typedef struct unblock_http3_body {
30 int64_t stream_id;
31 int streaming;
32 int eof;
33 uint64_t discarded_unacked;
34 unblock_http3_body_chunk *head;
35 unblock_http3_body_chunk *tail;
36 struct unblock_http3_body *next;
37 } unblock_http3_body;
38  
39 typedef struct {
40 uint8_t *name;
41 size_t namelen;
42 uint8_t *value;
43 size_t valuelen;
44 } unblock_http3_captured_field;
45  
46 typedef struct unblock_http3_header_block {
47 int64_t stream_id;
48 unblock_http3_captured_field *fields;
49 size_t count;
50 size_t capacity;
51 size_t field_section_size;
52 struct unblock_http3_header_block *next;
53 } unblock_http3_header_block;
54  
55 typedef struct {
56 nghttp3_conn *conn;
57 AV *events;
58 uint8_t *origin_list_data;
59 nghttp3_vec origin_list;
60 unblock_http3_body *bodies;
61 unblock_http3_header_block *header_blocks;
62 size_t streaming_retained_bytes;
63 int is_server;
64 int fatal;
65 } unblock_http3_native_conn;
66  
67  
68 static void
69 58 unblock_http3_header_block_free(unblock_http3_header_block *block)
70 {
71 size_t i;
72  
73 58 ▶ if (block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block == NULL) { 1 57 58
74 1 return;
75 }
76  
77 57 ▶ if (block->fields != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->fields != NULL) { 57 0 57
78 229 ▶ for (i = 0; i < block->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; ++i) { 172 57 229
79 172 ▶ if (block->fields[i].name != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->fields[i].name != NULL) { 172 0 172
80 172 Safefree(block->fields[i].name);
81 }
82 172 ▶ if (block->fields[i].value != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->fields[i].value != NULL) { 172 0 172
83 172 Safefree(block->fields[i].value);
84 }
85 }
86  
87 57 Safefree(block->fields);
88 }
89  
90 57 Safefree(block);
91 }
92  
93 static unblock_http3_header_block *
94 559 unblock_http3_header_block_find(
95 unblock_http3_native_conn *native,
96 int64_t stream_id
97 )
98 {
99 unblock_http3_header_block *block;
100  
101 559 for (
102 559 block = native->header_blocks;
103 559 ▶ block != NULL;
Branch: 1 branch
BranchTrueFalseTotal
block != NULL; 502 57 559
104 0 block = block->next
105 ) {
106 502 ▶ if (block->stream_id == stream_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->stream_id == stream_id) { 502 0 502
107 502 return block;
108 }
109 }
110  
111 57 return NULL;
112 }
113  
114 static unblock_http3_header_block *
115 57 unblock_http3_header_block_create(
116 unblock_http3_native_conn *native,
117 int64_t stream_id
118 )
119 {
120 unblock_http3_header_block *block;
121  
122 57 ▶ if (unblock_http3_header_block_find(native, stream_id) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_http3_header_block_find(native, stream_id) != NULL) { 0 57 57
123 0 croak("HTTP/3 stream already has an active header block");
124 }
125  
126 57 Newxz(block, 1, unblock_http3_header_block);
127 57 block->stream_id = stream_id;
128 57 block->next = native->header_blocks;
129 57 native->header_blocks = block;
130  
131 57 return block;
132 }
133  
134 static void
135 172 unblock_http3_header_block_append(
136 unblock_http3_header_block *block,
137 const uint8_t *name,
138 size_t namelen,
139 const uint8_t *value,
140 size_t valuelen
141 )
142 {
143 unblock_http3_captured_field *field;
144 size_t next_size;
145  
146 172 ▶ if (block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block == NULL) { 0 172 172
147 0 croak("HTTP/3 header arrived without an active header block");
148 }
149  
150 172 ▶ if (block->count == block->capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->count == block->capacity) { 58 114 172
151 116 size_t capacity = block->capacity == 0
152 ? 8
153 58 ▶ : block->capacity * 2;
Branch: 1 branch
BranchTrueFalseTotal
: block->capacity * 2; 1 57 58
154  
155 58 ▶ if (capacity < block->capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < block->capacity) { 0 58 58
156 0 croak("HTTP/3 header block capacity overflow");
157 }
158  
159 58 ▶ if (block->fields == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->fields == NULL) { 57 1 58
160 57 ▶ Newxz(
Branch: 1 branch
BranchTrueFalseTotal
Newxz( 0 57 57
161 block->fields,
162 capacity,
163 unblock_http3_captured_field
164 );
165 } else {
166 1 size_t old_capacity = block->capacity;
167  
168 1 ▶ Renew(
Branch: 1 branch
BranchTrueFalseTotal
Renew( 0 1 1
169 block->fields,
170 capacity,
171 unblock_http3_captured_field
172 );
173 1 ▶ Zero(
Branch: 1 branch
BranchTrueFalseTotal
Zero( 0 1 1
174 block->fields + old_capacity,
175 capacity - old_capacity,
176 unblock_http3_captured_field
177 );
178 }
179  
180 58 block->capacity = capacity;
181 }
182  
183 172 field = &block->fields[block->count];
184  
185 172 ▶ Newx(field->name, namelen ? namelen : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(field->name, namelen ? namelen : 1, uint8_t); 172 0 172
186 172 ▶ Newx(field->value, valuelen ? valuelen : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(field->value, valuelen ? valuelen : 1, uint8_t); 172 0 172
187  
188 172 ▶ if (namelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (namelen) { 172 0 172
189 172 Copy(name, field->name, namelen, uint8_t);
190 }
191  
192 172 ▶ if (valuelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (valuelen) { 172 0 172
193 172 Copy(value, field->value, valuelen, uint8_t);
194 }
195  
196 172 field->namelen = namelen;
197 172 field->valuelen = valuelen;
198 172 ++block->count;
199  
200 172 if (
201 172 ▶ block->field_section_size > (size_t)-1 - namelen
Branch: 1 branch
BranchTrueFalseTotal
block->field_section_size > (size_t)-1 - namelen 172 0 172
202 172 ▶ || block->field_section_size + namelen > (size_t)-1 - valuelen
Branch: 1 branch
BranchTrueFalseTotal
|| block->field_section_size + namelen > (size_t)-1 - valuelen 172 0 172
203 172 ▶ || block->field_section_size + namelen + valuelen > (size_t)-1 - 32
Branch: 1 branch
BranchTrueFalseTotal
|| block->field_section_size + namelen + valuelen > (size_t)-1 - 32 0 172 172
204 ) {
205 0 croak("HTTP/3 field section size overflow");
206 }
207  
208 172 next_size =
209 172 block->field_section_size
210 172 + namelen
211 172 + valuelen
212 + 32;
213 172 block->field_section_size = next_size;
214 172 }
215  
216 static unblock_http3_header_block *
217 55 unblock_http3_header_block_remove(
218 unblock_http3_native_conn *native,
219 int64_t stream_id
220 )
221 {
222 55 unblock_http3_header_block **link = &native->header_blocks;
223  
224 55 ▶ while (*link != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (*link != NULL) { 54 1 55
225 54 unblock_http3_header_block *block = *link;
226  
227 54 ▶ if (block->stream_id != stream_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->stream_id != stream_id) { 0 54 54
228 0 link = &block->next;
229 0 continue;
230 }
231  
232 54 *link = block->next;
233 54 block->next = NULL;
234 54 return block;
235 }
236  
237 1 return NULL;
238 }
239  
240 static void
241 60 unblock_http3_header_block_free_all(
242 unblock_http3_native_conn *native
243 )
244 {
245 60 unblock_http3_header_block *block = native->header_blocks;
246  
247 63 ▶ while (block != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (block != NULL) { 3 60 63
248 3 unblock_http3_header_block *next = block->next;
249 3 unblock_http3_header_block_free(block);
250 3 block = next;
251 }
252  
253 60 native->header_blocks = NULL;
254 60 }
255  
256 static int
257 655 unblock_http3_captured_name_equal(
258 const unblock_http3_captured_field *field,
259 const char *name,
260 size_t namelen
261 )
262 {
263 655 return field->namelen == namelen
264 655 ▶ && memcmp(field->name, name, namelen) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(field->name, name, namelen) == 0; 194 461 655
&& memcmp(field->name, name, namelen) == 0; 140 54 194
265 }
266  
267 static int
268 58 unblock_http3_captured_name_equal_ci(
269 const unblock_http3_captured_field *field,
270 const char *name,
271 size_t namelen
272 )
273 {
274 size_t i;
275  
276 58 ▶ if (field->namelen != namelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (field->namelen != namelen) { 40 18 58
277 40 return 0;
278 }
279  
280 144 ▶ for (i = 0; i < namelen; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < namelen; ++i) { 126 18 144
281 126 uint8_t left = field->name[i];
282 126 uint8_t right = (uint8_t)name[i];
283  
284 126 ▶ if (left >= 'A' && left <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (left >= 'A' && left <= 'Z') { 124 2 126
if (left >= 'A' && left <= 'Z') { 0 124 124
285 0 left = (uint8_t)(left + ('a' - 'A'));
286 }
287  
288 126 ▶ if (right >= 'A' && right <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (right >= 'A' && right <= 'Z') { 124 2 126
if (right >= 'A' && right <= 'Z') { 0 124 124
289 0 right = (uint8_t)(right + ('a' - 'A'));
290 }
291  
292 126 ▶ if (left != right) {
Branch: 1 branch
BranchTrueFalseTotal
if (left != right) { 0 126 126
293 0 return 0;
294 }
295 }
296  
297 18 return 1;
298 }
299  
300 static const unblock_http3_captured_field *
301 166 unblock_http3_header_block_pseudo(
302 const unblock_http3_header_block *block,
303 const char *name,
304 size_t namelen
305 )
306 {
307 166 const unblock_http3_captured_field *found = NULL;
308 size_t i;
309  
310 821 ▶ for (i = 0; i < block->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; ++i) { 655 166 821
311 655 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 140 515 655
312 655 unblock_http3_captured_name_equal(
313 655 &block->fields[i],
314 name,
315 namelen
316 )
317 ) {
318 140 found = &block->fields[i];
319 }
320 }
321  
322 166 return found;
323 }
324  
325 static size_t
326 53 unblock_http3_header_block_regular_count(
327 const unblock_http3_header_block *block
328 )
329 {
330 size_t i;
331 53 size_t count = 0;
332  
333 207 ▶ for (i = 0; i < block->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; ++i) { 154 53 207
334 154 if (
335 154 ▶ block->fields[i].namelen == 0
Branch: 1 branch
BranchTrueFalseTotal
block->fields[i].namelen == 0 154 0 154
336 154 ▶ || block->fields[i].name[0] != ':'
Branch: 1 branch
BranchTrueFalseTotal
|| block->fields[i].name[0] != ':' 18 136 154
337 ) {
338 18 ++count;
339 }
340 }
341  
342 53 return count;
343 }
344  
345 static void
346 53 unblock_http3_header_block_uniform_fields(
347 pTHX_ const unblock_http3_header_block *block,
348 uhttp_native_field **pfields,
349 Size_t *pcount,
350 uint8_t **pcookie
351 )
352 {
353 size_t regular_count =
354 53 unblock_http3_header_block_regular_count(block);
355 53 size_t cookie_count = 0;
356 53 size_t cookie_length = 0;
357 53 size_t first_cookie = (size_t)-1;
358 size_t i;
359 53 size_t out_index = 0;
360 53 uhttp_native_field *fields = NULL;
361 53 uint8_t *cookie = NULL;
362  
363 207 ▶ for (i = 0; i < block->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; ++i) { 154 53 207
364 154 const unblock_http3_captured_field *field =
365 154 &block->fields[i];
366  
367 154 if (
368 154 ▶ field->namelen != 0
Branch: 1 branch
BranchTrueFalseTotal
field->namelen != 0 154 0 154
369 154 ▶ && field->name[0] == ':'
Branch: 1 branch
BranchTrueFalseTotal
&& field->name[0] == ':' 136 18 154
370 ) {
371 136 continue;
372 }
373  
374 18 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 5 13 18
375 18 unblock_http3_captured_name_equal_ci(
376 field,
377 "cookie",
378 6
379 )
380 ) {
381 5 ▶ if (first_cookie == (size_t)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (first_cookie == (size_t)-1) { 2 3 5
382 2 first_cookie = i;
383 }
384  
385 5 if (
386 5 ▶ cookie_length > (size_t)-1 - field->valuelen
Branch: 1 branch
BranchTrueFalseTotal
cookie_length > (size_t)-1 - field->valuelen 5 0 5
387 5 ▶ || (
Branch: 1 branch
BranchTrueFalseTotal
|| ( 3 2 5
388 cookie_count != 0
389 3 ▶ && cookie_length + field->valuelen
Branch: 1 branch
BranchTrueFalseTotal
&& cookie_length + field->valuelen 0 3 3
390 > (size_t)-1 - 2
391 )
392 ) {
393 0 croak("HTTP/3 Cookie field coalescing overflow");
394 }
395  
396 5 cookie_length += field->valuelen;
397 5 ▶ if (cookie_count != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cookie_count != 0) { 3 2 5
398 3 cookie_length += 2;
399 }
400 5 ++cookie_count;
401 }
402 }
403  
404 53 ▶ if (cookie_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cookie_count > 1) { 2 51 53
405 2 regular_count -= cookie_count - 1;
406 2 ▶ Newx(cookie, cookie_length ? cookie_length : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(cookie, cookie_length ? cookie_length : 1, uint8_t); 2 0 2
407 }
408  
409 53 ▶ if (regular_count != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (regular_count != 0) { 12 41 53
410 12 ▶ Newxz(fields, regular_count, uhttp_native_field);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(fields, regular_count, uhttp_native_field); 0 12 12
411 }
412  
413 53 ▶ if (cookie_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cookie_count > 1) { 2 51 53
414 2 size_t cookie_offset = 0;
415  
416 15 ▶ for (i = 0; i < block->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; ++i) { 13 2 15
417 13 const unblock_http3_captured_field *field =
418 13 &block->fields[i];
419  
420 13 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 8 5 13
421 13 !unblock_http3_captured_name_equal_ci(
422 field,
423 "cookie",
424 6
425 )
426 ) {
427 8 continue;
428 }
429  
430 5 ▶ if (cookie_offset != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cookie_offset != 0) { 3 2 5
431 3 cookie[cookie_offset++] = ';';
432 3 cookie[cookie_offset++] = ' ';
433 }
434  
435 5 ▶ if (field->valuelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (field->valuelen) { 5 0 5
436 5 Copy(
437 field->value,
438 cookie + cookie_offset,
439 field->valuelen,
440 uint8_t
441 );
442 5 cookie_offset += field->valuelen;
443 }
444 }
445 }
446  
447 207 ▶ for (i = 0; i < block->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; ++i) { 154 53 207
448 154 const unblock_http3_captured_field *field =
449 154 &block->fields[i];
450  
451 154 if (
452 154 ▶ field->namelen != 0
Branch: 1 branch
BranchTrueFalseTotal
field->namelen != 0 154 0 154
453 154 ▶ && field->name[0] == ':'
Branch: 1 branch
BranchTrueFalseTotal
&& field->name[0] == ':' 136 18 154
454 ) {
455 136 continue;
456 }
457  
458 18 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 8 10 18
459 cookie_count > 1
460 8 ▶ && unblock_http3_captured_name_equal_ci(
Branch: 1 branch
BranchTrueFalseTotal
&& unblock_http3_captured_name_equal_ci( 5 3 8
461 field,
462 "cookie",
463 6
464 )
465 ) {
466 5 ▶ if (i != first_cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if (i != first_cookie) { 3 2 5
467 3 continue;
468 }
469  
470 2 fields[out_index].name.data = "cookie";
471 2 fields[out_index].name.len = 6;
472 2 fields[out_index].value.data =
473 (const char *)cookie;
474 2 fields[out_index].value.len =
475 (STRLEN)cookie_length;
476 2 ++out_index;
477 2 continue;
478 }
479  
480 13 fields[out_index].name.data =
481 13 (const char *)field->name;
482 13 fields[out_index].name.len =
483 13 (STRLEN)field->namelen;
484 13 fields[out_index].value.data =
485 13 (const char *)field->value;
486 13 fields[out_index].value.len =
487 13 (STRLEN)field->valuelen;
488 13 ++out_index;
489 }
490  
491 53 *pfields = fields;
492 53 *pcount = (Size_t)regular_count;
493 53 *pcookie = cookie;
494 53 }
495  
496 static uhttp_native_bytes
497 135 unblock_http3_uniform_bytes_from_sv(SV *value)
498 {
499 uhttp_native_bytes bytes;
500 STRLEN len;
501 const char *data;
502  
503 135 ▶ if (value == NULL || !SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (value == NULL || !SvOK(value)) { 135 0 135
if (value == NULL || !SvOK(value)) { 23 112 135
504 23 bytes.data = NULL;
505 23 bytes.len = 0;
506 23 return bytes;
507 }
508  
509 112 data = SvPVbyte(value, len);
510 112 bytes.data = data;
511 112 bytes.len = len;
512 112 return bytes;
513 }
514  
515 static U32
516 53 unblock_http3_received_uniform_flags(
517 U32 kind,
518 int fin
519 )
520 {
521 53 U32 flags =
522 UHTTP_HEADERS_LOSSLESS
523 | UHTTP_TRAILERS_LOSSLESS;
524  
525 53 ▶ if (kind == UHTTP_KIND_REQUEST) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind == UHTTP_KIND_REQUEST) { 27 26 53
526 27 flags |= UHTTP_TARGET_EXACT;
527 }
528  
529 53 ▶ if (fin) {
Branch: 1 branch
BranchTrueFalseTotal
if (fin) { 0 53 53
530 0 flags |= UHTTP_COMPLETE;
531 } else {
532 53 flags |=
533 UHTTP_MUTABLE
534 | UHTTP_BODY_MUTABLE
535 | UHTTP_TRAILERS_MUTABLE;
536 }
537  
538 53 return flags;
539 }
540  
541  
542 static SV *
543 27 unblock_http3_header_block_uniform_request(
544 pTHX_ unblock_http3_header_block *block,
545 SV *method,
546 SV *target,
547 SV *scheme,
548 SV *authority,
549 SV *protocol,
550 int fin
551 )
552 {
553 dMY_CXT;
554 uhttp_native_input input;
555 27 uhttp_native_field *fields = NULL;
556 27 Size_t field_count = 0;
557 27 uint8_t *cookie = NULL;
558 SV *object;
559  
560 27 unblock_http3_header_block_uniform_fields(
561 aTHX_ block,
562 &fields,
563 &field_count,
564 &cookie
565 );
566  
567 27 uhttp_native_input_init(
568 &input,
569 UHTTP_KIND_REQUEST
570 );
571 27 input.flags = unblock_http3_received_uniform_flags(
572 UHTTP_KIND_REQUEST,
573 fin
574 );
575 27 input.version.data = "3";
576 27 input.version.len = 1;
577 input.method =
578 27 unblock_http3_uniform_bytes_from_sv(method);
579 input.target =
580 27 unblock_http3_uniform_bytes_from_sv(target);
581 input.scheme =
582 27 unblock_http3_uniform_bytes_from_sv(scheme);
583 input.authority =
584 27 unblock_http3_uniform_bytes_from_sv(authority);
585 input.protocol =
586 27 unblock_http3_uniform_bytes_from_sv(protocol);
587 27 input.headers = fields;
588 27 input.header_count = field_count;
589  
590 27 object = uhttp_native_from_validated(
591 aTHX_ &MY_CXT.uniform_api,
592 &input,
593 UHTTP_NATIVE_TRUSTED
594 );
595  
596 27 ▶ if (cookie != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cookie != NULL) { 1 26 27
597 1 Safefree(cookie);
598 }
599 27 ▶ if (fields != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fields != NULL) { 5 22 27
600 5 Safefree(fields);
601 }
602  
603 27 return object;
604 }
605  
606 static SV *
607 26 unblock_http3_header_block_uniform_response(
608 pTHX_ unblock_http3_header_block *block,
609 IV status,
610 int fin
611 )
612 {
613 dMY_CXT;
614 uhttp_native_input input;
615 26 uhttp_native_field *fields = NULL;
616 26 Size_t field_count = 0;
617 26 uint8_t *cookie = NULL;
618 SV *object;
619  
620 26 unblock_http3_header_block_uniform_fields(
621 aTHX_ block,
622 &fields,
623 &field_count,
624 &cookie
625 );
626  
627 26 uhttp_native_input_init(
628 &input,
629 UHTTP_KIND_RESPONSE
630 );
631 26 input.flags = unblock_http3_received_uniform_flags(
632 UHTTP_KIND_RESPONSE,
633 fin
634 );
635 26 input.version.data = "3";
636 26 input.version.len = 1;
637 26 input.status = status;
638 26 input.headers = fields;
639 26 input.header_count = field_count;
640  
641 26 object = uhttp_native_from_validated(
642 aTHX_ &MY_CXT.uniform_api,
643 &input,
644 UHTTP_NATIVE_TRUSTED
645 );
646  
647 26 ▶ if (cookie != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cookie != NULL) { 1 25 26
648 1 Safefree(cookie);
649 }
650 26 ▶ if (fields != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fields != NULL) { 7 19 26
651 7 Safefree(fields);
652 }
653  
654 26 return object;
655 }
656  
657 static void
658 0 unblock_http3_fail(const char *operation, int rv)
659 {
660 0 croak("%s: %s (%d)", operation, nghttp3_strerror(rv), rv);
661 }
662  
663 static unblock_http3_body *
664 79 unblock_http3_body_find(
665 unblock_http3_native_conn *native,
666 int64_t stream_id
667 )
668 {
669 unblock_http3_body *body;
670  
671 92 ▶ for (body = native->bodies; body != NULL; body = body->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (body = native->bodies; body != NULL; body = body->next) { 55 37 92
672 55 ▶ if (body->stream_id == stream_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->stream_id == stream_id) { 42 13 55
673 42 return body;
674 }
675 }
676  
677 37 return NULL;
678 }
679  
680 static unblock_http3_body *
681 32 unblock_http3_body_create(
682 unblock_http3_native_conn *native,
683 int64_t stream_id,
684 int streaming
685 )
686 {
687 unblock_http3_body *body;
688  
689 32 ▶ if (unblock_http3_body_find(native, stream_id) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_http3_body_find(native, stream_id) != NULL) { 0 32 32
690 0 croak("HTTP/3 stream already has an outgoing body");
691 }
692  
693 32 Newxz(body, 1, unblock_http3_body);
694  
695 32 body->stream_id = stream_id;
696 32 body->streaming = streaming ? 1 : 0;
697 32 body->next = native->bodies;
698 32 native->bodies = body;
699  
700 32 return body;
701 }
702  
703 static void
704 36 unblock_http3_body_chunk_free(unblock_http3_body_chunk *chunk)
705 {
706 36 ▶ if (chunk == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunk == NULL) { 0 36 36
707 0 return;
708 }
709  
710 36 ▶ if (chunk->storage != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunk->storage != NULL) { 36 0 36
711 36 SvREFCNT_dec(chunk->storage);
712 36 chunk->storage = NULL;
713 }
714  
715 36 chunk->data = NULL;
716 36 Safefree(chunk);
717 }
718  
719 static size_t
720 47 unblock_http3_body_append(
721 unblock_http3_native_conn *native,
722 unblock_http3_body *body,
723 SV *body_sv
724 )
725 {
726 unblock_http3_body_chunk *chunk;
727 SV *storage;
728 STRLEN len;
729 const char *bytes;
730  
731 47 storage = newSVsv(body_sv);
732 47 bytes = SvPVbyte(storage, len);
733  
734 47 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 11 36 47
735 11 SvREFCNT_dec(storage);
736 11 return 0;
737 }
738  
739 36 Newxz(chunk, 1, unblock_http3_body_chunk);
740  
741 36 chunk->storage = storage;
742 36 chunk->data = (uint8_t *)bytes;
743 36 chunk->len = (size_t)len;
744  
745 36 ▶ if (body->tail != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->tail != NULL) { 8 28 36
746 8 body->tail->next = chunk;
747 } else {
748 28 body->head = chunk;
749 }
750  
751 36 body->tail = chunk;
752  
753 36 ▶ if (body->streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->streaming) { 27 9 36
754 27 native->streaming_retained_bytes += (size_t)len;
755 }
756  
757 36 return (size_t)len;
758 }
759  
760 static void
761 9 unblock_http3_body_discard(
762 unblock_http3_native_conn *native,
763 unblock_http3_body *body
764 )
765 {
766 unblock_http3_body_chunk *chunk;
767  
768 9 ▶ if (body == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (body == NULL) { 5 4 9
769 5 return;
770 }
771  
772 4 chunk = body->head;
773  
774 6 ▶ while (chunk != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (chunk != NULL) { 2 4 6
775 2 unblock_http3_body_chunk *next_chunk = chunk->next;
776  
777 {
778 2 size_t retained = chunk->len - chunk->acked;
779  
780 2 body->discarded_unacked += (uint64_t)retained;
781  
782 2 ▶ if (body->streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->streaming) { 2 0 2
783 2 ▶ if (native->streaming_retained_bytes < retained) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->streaming_retained_bytes < retained) { 0 2 2
784 0 croak("HTTP/3 streaming retained-byte accounting underflow");
785 }
786  
787 2 native->streaming_retained_bytes -= retained;
788 }
789 }
790  
791 2 unblock_http3_body_chunk_free(chunk);
792 2 chunk = next_chunk;
793 }
794  
795 4 body->head = NULL;
796 4 body->tail = NULL;
797 4 body->eof = 1;
798 }
799  
800 static void
801 47 unblock_http3_body_remove(
802 unblock_http3_native_conn *native,
803 int64_t stream_id
804 )
805 {
806 47 unblock_http3_body **link = &native->bodies;
807  
808 57 ▶ while (*link != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (*link != NULL) { 37 20 57
809 37 unblock_http3_body *body = *link;
810  
811 37 ▶ if (body->stream_id != stream_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->stream_id != stream_id) { 10 27 37
812 10 link = &body->next;
813 10 continue;
814 }
815  
816 27 *link = body->next;
817  
818 {
819 27 unblock_http3_body_chunk *chunk = body->head;
820  
821 27 ▶ while (chunk != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (chunk != NULL) { 0 27 27
822 0 unblock_http3_body_chunk *next_chunk = chunk->next;
823 0 size_t retained = chunk->len - chunk->acked;
824  
825 0 ▶ if (body->streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->streaming) { 0 0 0
826 0 ▶ if (native->streaming_retained_bytes < retained) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->streaming_retained_bytes < retained) { 0 0 0
827 0 croak("HTTP/3 streaming retained-byte accounting underflow");
828 }
829  
830 0 native->streaming_retained_bytes -= retained;
831 }
832  
833 0 unblock_http3_body_chunk_free(chunk);
834 0 chunk = next_chunk;
835 }
836 }
837  
838 27 Safefree(body);
839 27 return;
840 }
841 }
842  
843 static void
844 60 unblock_http3_body_free_all(unblock_http3_native_conn *native)
845 {
846 60 unblock_http3_body *body = native->bodies;
847  
848 65 ▶ while (body != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (body != NULL) { 5 60 65
849 5 unblock_http3_body *next_body = body->next;
850 5 unblock_http3_body_chunk *chunk = body->head;
851  
852 7 ▶ while (chunk != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (chunk != NULL) { 2 5 7
853 2 unblock_http3_body_chunk *next_chunk = chunk->next;
854  
855 2 unblock_http3_body_chunk_free(chunk);
856 2 chunk = next_chunk;
857 }
858  
859 5 Safefree(body);
860 5 body = next_body;
861 }
862  
863 60 native->bodies = NULL;
864 60 native->streaming_retained_bytes = 0;
865 60 }
866  
867 static nghttp3_ssize
868 89 unblock_http3_read_body_cb(
869 nghttp3_conn *conn,
870 int64_t stream_id,
871 nghttp3_vec *vec,
872 size_t veccnt,
873 uint32_t *pflags,
874 void *conn_user_data,
875 void *stream_user_data
876 )
877 {
878 89 unblock_http3_body *body = (unblock_http3_body *)stream_user_data;
879 unblock_http3_body_chunk *chunk;
880 89 size_t count = 0;
881 89 int more_unsupplied = 0;
882  
883 (void)conn;
884 (void)stream_id;
885 (void)conn_user_data;
886  
887 89 ▶ if (body == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (body == NULL) { 0 89 89
888 0 return NGHTTP3_ERR_CALLBACK_FAILURE;
889 }
890  
891 89 *pflags = NGHTTP3_DATA_FLAG_NONE;
892  
893 158 ▶ for (chunk = body->head; chunk != NULL; chunk = chunk->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (chunk = body->head; chunk != NULL; chunk = chunk->next) { 69 89 158
894 69 ▶ if (chunk->supplied) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunk->supplied) { 33 36 69
895 33 continue;
896 }
897  
898 36 ▶ if (count == veccnt) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == veccnt) { 0 36 36
899 0 more_unsupplied = 1;
900 0 break;
901 }
902  
903 36 vec[count].base = chunk->data;
904 36 vec[count].len = chunk->len;
905 36 chunk->supplied = 1;
906 36 ++count;
907 }
908  
909 89 ▶ if (count != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 0) { 36 53 89
910 36 ▶ if (body->eof && !more_unsupplied) {
Branch: 2 branches
BranchTrueFalseTotal
if (body->eof && !more_unsupplied) { 16 20 36
if (body->eof && !more_unsupplied) { 16 0 16
911 39 ▶ for (chunk = body->head; chunk != NULL; chunk = chunk->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (chunk = body->head; chunk != NULL; chunk = chunk->next) { 23 16 39
912 23 ▶ if (!chunk->supplied) {
Branch: 1 branch
BranchTrueFalseTotal
if (!chunk->supplied) { 0 23 23
913 0 more_unsupplied = 1;
914 0 break;
915 }
916 }
917  
918 16 ▶ if (!more_unsupplied) {
Branch: 1 branch
BranchTrueFalseTotal
if (!more_unsupplied) { 16 0 16
919 16 *pflags |= NGHTTP3_DATA_FLAG_EOF;
920 }
921 }
922  
923 36 return (nghttp3_ssize)count;
924 }
925  
926 53 ▶ if (body->eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->eof) { 11 42 53
927 11 *pflags |= NGHTTP3_DATA_FLAG_EOF;
928 11 return 0;
929 }
930  
931 42 return NGHTTP3_ERR_WOULDBLOCK;
932 }
933  
934 static const nghttp3_data_reader unblock_http3_body_reader = {
935 unblock_http3_read_body_cb
936 };
937  
938 static int
939 39 unblock_http3_acked_stream_data_cb(
940 nghttp3_conn *conn,
941 int64_t stream_id,
942 uint64_t datalen,
943 void *conn_user_data,
944 void *stream_user_data
945 )
946 {
947 39 unblock_http3_native_conn *native =
948 (unblock_http3_native_conn *)conn_user_data;
949 39 unblock_http3_body *body = (unblock_http3_body *)stream_user_data;
950 39 uint64_t remaining = datalen;
951  
952 (void)conn;
953 (void)stream_id;
954  
955 39 ▶ if (body == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (body == NULL) { 0 39 39
956 0 return 0;
957 }
958  
959 39 ▶ if (remaining != 0 && body->discarded_unacked != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (remaining != 0 && body->discarded_unacked != 0) { 39 0 39
if (remaining != 0 && body->discarded_unacked != 0) { 2 37 39
960 2 uint64_t ignored = remaining < body->discarded_unacked
961 ? remaining
962 : body->discarded_unacked;
963  
964 2 body->discarded_unacked -= ignored;
965 2 remaining -= ignored;
966 }
967  
968 76 ▶ while (remaining != 0 && body->head != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
while (remaining != 0 && body->head != NULL) { 37 39 76
while (remaining != 0 && body->head != NULL) { 37 0 37
969 37 unblock_http3_body_chunk *chunk = body->head;
970 size_t available;
971 size_t take;
972  
973 37 ▶ if (!chunk->supplied) {
Branch: 1 branch
BranchTrueFalseTotal
if (!chunk->supplied) { 0 37 37
974 0 return NGHTTP3_ERR_CALLBACK_FAILURE;
975 }
976  
977 37 available = chunk->len - chunk->acked;
978 37 take = remaining < (uint64_t)available
979 ? (size_t)remaining
980 : available;
981  
982 37 chunk->acked += take;
983 37 remaining -= take;
984  
985 37 ▶ if (body->streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->streaming) { 29 8 37
986 29 ▶ if (native->streaming_retained_bytes < take) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->streaming_retained_bytes < take) { 0 29 29
987 0 return NGHTTP3_ERR_CALLBACK_FAILURE;
988 }
989 29 native->streaming_retained_bytes -= take;
990 }
991  
992 37 ▶ if (chunk->acked == chunk->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunk->acked == chunk->len) { 32 5 37
993 32 body->head = chunk->next;
994 32 ▶ if (body->head == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->head == NULL) { 24 8 32
995 24 body->tail = NULL;
996 }
997  
998 32 unblock_http3_body_chunk_free(chunk);
999 }
1000 }
1001  
1002 39 ▶ if (remaining != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining != 0) { 0 39 39
1003 0 return NGHTTP3_ERR_CALLBACK_FAILURE;
1004 }
1005  
1006 39 return 0;
1007 }
1008  
1009 static AV *
1010 244 unblock_http3_event_new(const char *type, int64_t stream_id)
1011 {
1012 244 AV *event = newAV();
1013  
1014 244 av_push(event, newSVpv(type, 0));
1015 244 av_push(event, newSViv((IV)stream_id));
1016  
1017 244 return event;
1018 }
1019  
1020 static void
1021 244 unblock_http3_push_event(unblock_http3_native_conn *native, AV *event)
1022 {
1023 244 av_push(native->events, newRV_noinc((SV *)event));
1024 244 }
1025  
1026 static int
1027 28 unblock_http3_recv_settings2_cb(
1028 nghttp3_conn *conn,
1029 const nghttp3_proto_settings *settings,
1030 void *conn_user_data
1031 )
1032 {
1033 28 unblock_http3_native_conn *native =
1034 (unblock_http3_native_conn *)conn_user_data;
1035 28 AV *event = unblock_http3_event_new("settings", -1);
1036 char max_field_section_size[32];
1037  
1038 (void)conn;
1039  
1040 28 snprintf(
1041 max_field_section_size,
1042 sizeof(max_field_section_size),
1043 "%" PRIu64,
1044 28 settings->max_field_section_size
1045 );
1046  
1047 28 av_push(event, newSVpv(max_field_section_size, 0));
1048 28 av_push(event, newSViv(settings->enable_connect_protocol ? 1 : 0));
1049 28 av_push(event, newSViv(settings->h3_datagram ? 1 : 0));
1050 28 unblock_http3_push_event(native, event);
1051  
1052 28 return 0;
1053 }
1054  
1055 static int
1056 2 unblock_http3_recv_origin_cb(
1057 nghttp3_conn *conn,
1058 const uint8_t *origin,
1059 size_t originlen,
1060 void *conn_user_data
1061 )
1062 {
1063 2 unblock_http3_native_conn *native =
1064 (unblock_http3_native_conn *)conn_user_data;
1065 2 AV *event = unblock_http3_event_new("origin", -1);
1066  
1067 (void)conn;
1068  
1069 2 av_push(event, newSVpvn((const char *)origin, originlen));
1070 2 unblock_http3_push_event(native, event);
1071  
1072 2 return 0;
1073 }
1074  
1075 static int
1076 1 unblock_http3_end_origin_cb(
1077 nghttp3_conn *conn,
1078 void *conn_user_data
1079 )
1080 {
1081 1 unblock_http3_native_conn *native =
1082 (unblock_http3_native_conn *)conn_user_data;
1083  
1084 (void)conn;
1085  
1086 1 unblock_http3_push_event(
1087 native,
1088 unblock_http3_event_new("end_origin", -1)
1089 );
1090  
1091 1 return 0;
1092 }
1093  
1094 static int
1095 44 unblock_http3_recv_data_cb(
1096 nghttp3_conn *conn,
1097 int64_t stream_id,
1098 const uint8_t *data,
1099 size_t datalen,
1100 void *conn_user_data,
1101 void *stream_user_data
1102 )
1103 {
1104 44 unblock_http3_native_conn *native =
1105 (unblock_http3_native_conn *)conn_user_data;
1106 44 AV *event = unblock_http3_event_new("data", stream_id);
1107  
1108 (void)conn;
1109 (void)stream_user_data;
1110  
1111 44 av_push(event, newSVpvn((const char *)data, datalen));
1112 44 unblock_http3_push_event(native, event);
1113  
1114 44 return 0;
1115 }
1116  
1117 static int
1118 0 unblock_http3_deferred_consume_cb(
1119 nghttp3_conn *conn,
1120 int64_t stream_id,
1121 size_t consumed,
1122 void *conn_user_data,
1123 void *stream_user_data
1124 )
1125 {
1126 0 unblock_http3_native_conn *native =
1127 (unblock_http3_native_conn *)conn_user_data;
1128 0 AV *event = unblock_http3_event_new("deferred_consume", stream_id);
1129  
1130 (void)conn;
1131 (void)stream_user_data;
1132  
1133 0 av_push(event, newSVuv((UV)consumed));
1134 0 unblock_http3_push_event(native, event);
1135  
1136 0 return 0;
1137 }
1138  
1139 static int
1140 57 unblock_http3_begin_headers_cb(
1141 nghttp3_conn *conn,
1142 int64_t stream_id,
1143 void *conn_user_data,
1144 void *stream_user_data
1145 )
1146 {
1147 57 unblock_http3_native_conn *native =
1148 (unblock_http3_native_conn *)conn_user_data;
1149  
1150 (void)conn;
1151 (void)stream_user_data;
1152  
1153 57 (void)unblock_http3_header_block_create(
1154 native,
1155 stream_id
1156 );
1157  
1158 57 return 0;
1159 }
1160  
1161 static int
1162 172 unblock_http3_recv_header_cb(
1163 nghttp3_conn *conn,
1164 int64_t stream_id,
1165 int32_t token,
1166 nghttp3_rcbuf *name,
1167 nghttp3_rcbuf *value,
1168 uint8_t flags,
1169 void *conn_user_data,
1170 void *stream_user_data
1171 )
1172 {
1173 172 unblock_http3_native_conn *native =
1174 (unblock_http3_native_conn *)conn_user_data;
1175 unblock_http3_header_block *block =
1176 172 unblock_http3_header_block_find(native, stream_id);
1177 172 nghttp3_vec nbuf = nghttp3_rcbuf_get_buf(name);
1178 172 nghttp3_vec vbuf = nghttp3_rcbuf_get_buf(value);
1179  
1180 (void)conn;
1181 (void)token;
1182 (void)flags;
1183 (void)stream_user_data;
1184  
1185 172 unblock_http3_header_block_append(
1186 block,
1187 172 nbuf.base,
1188 nbuf.len,
1189 172 vbuf.base,
1190 vbuf.len
1191 );
1192  
1193 172 return 0;
1194 }
1195  
1196 static int
1197 57 unblock_http3_end_headers_cb(
1198 nghttp3_conn *conn,
1199 int64_t stream_id,
1200 int fin,
1201 void *conn_user_data,
1202 void *stream_user_data
1203 )
1204 {
1205 57 unblock_http3_native_conn *native =
1206 (unblock_http3_native_conn *)conn_user_data;
1207 unblock_http3_header_block *block =
1208 57 unblock_http3_header_block_find(native, stream_id);
1209 AV *event;
1210  
1211 (void)conn;
1212 (void)stream_user_data;
1213  
1214 57 ▶ if (block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block == NULL) { 0 57 57
1215 0 croak("HTTP/3 header section ended without beginning");
1216 }
1217  
1218 57 event = unblock_http3_event_new("headers", stream_id);
1219 57 av_push(event, newSVuv((UV)block->field_section_size));
1220 57 av_push(event, newSViv(fin ? 1 : 0));
1221 57 unblock_http3_push_event(native, event);
1222  
1223 57 return 0;
1224 }
1225  
1226 static int
1227 4 unblock_http3_begin_trailers_cb(
1228 nghttp3_conn *conn,
1229 int64_t stream_id,
1230 void *conn_user_data,
1231 void *stream_user_data
1232 )
1233 {
1234 4 unblock_http3_native_conn *native =
1235 (unblock_http3_native_conn *)conn_user_data;
1236  
1237 (void)conn;
1238 (void)stream_user_data;
1239  
1240 4 unblock_http3_push_event(
1241 native,
1242 unblock_http3_event_new("begin_trailers", stream_id)
1243 );
1244  
1245 4 return 0;
1246 }
1247  
1248 static int
1249 6 unblock_http3_recv_trailer_cb(
1250 nghttp3_conn *conn,
1251 int64_t stream_id,
1252 int32_t token,
1253 nghttp3_rcbuf *name,
1254 nghttp3_rcbuf *value,
1255 uint8_t flags,
1256 void *conn_user_data,
1257 void *stream_user_data
1258 )
1259 {
1260 6 unblock_http3_native_conn *native =
1261 (unblock_http3_native_conn *)conn_user_data;
1262 6 nghttp3_vec nbuf = nghttp3_rcbuf_get_buf(name);
1263 6 nghttp3_vec vbuf = nghttp3_rcbuf_get_buf(value);
1264 6 AV *event = unblock_http3_event_new("trailer", stream_id);
1265  
1266 (void)conn;
1267 (void)token;
1268 (void)flags;
1269 (void)stream_user_data;
1270  
1271 6 av_push(event, newSVpvn((const char *)nbuf.base, nbuf.len));
1272 6 av_push(event, newSVpvn((const char *)vbuf.base, vbuf.len));
1273 6 unblock_http3_push_event(native, event);
1274  
1275 6 return 0;
1276 }
1277  
1278 static int
1279 4 unblock_http3_end_trailers_cb(
1280 nghttp3_conn *conn,
1281 int64_t stream_id,
1282 int fin,
1283 void *conn_user_data,
1284 void *stream_user_data
1285 )
1286 {
1287 4 unblock_http3_native_conn *native =
1288 (unblock_http3_native_conn *)conn_user_data;
1289 4 AV *event = unblock_http3_event_new("end_trailers", stream_id);
1290  
1291 (void)conn;
1292 (void)stream_user_data;
1293  
1294 4 av_push(event, newSViv(fin ? 1 : 0));
1295 4 unblock_http3_push_event(native, event);
1296  
1297 4 return 0;
1298 }
1299  
1300 static int
1301 48 unblock_http3_end_stream_cb(
1302 nghttp3_conn *conn,
1303 int64_t stream_id,
1304 void *conn_user_data,
1305 void *stream_user_data
1306 )
1307 {
1308 48 unblock_http3_native_conn *native =
1309 (unblock_http3_native_conn *)conn_user_data;
1310  
1311 (void)conn;
1312 (void)stream_user_data;
1313  
1314 48 unblock_http3_push_event(
1315 native,
1316 unblock_http3_event_new("end_stream", stream_id)
1317 );
1318  
1319 48 return 0;
1320 }
1321  
1322 static int
1323 0 unblock_http3_stop_sending_cb(
1324 nghttp3_conn *conn,
1325 int64_t stream_id,
1326 uint64_t app_error_code,
1327 void *conn_user_data,
1328 void *stream_user_data
1329 )
1330 {
1331 0 unblock_http3_native_conn *native =
1332 (unblock_http3_native_conn *)conn_user_data;
1333 0 AV *event = unblock_http3_event_new("stop_sending", stream_id);
1334  
1335 (void)conn;
1336 (void)stream_user_data;
1337  
1338 0 av_push(event, newSVuv((UV)app_error_code));
1339 0 unblock_http3_push_event(native, event);
1340  
1341 0 return 0;
1342 }
1343  
1344 static int
1345 0 unblock_http3_reset_stream_cb(
1346 nghttp3_conn *conn,
1347 int64_t stream_id,
1348 uint64_t app_error_code,
1349 void *conn_user_data,
1350 void *stream_user_data
1351 )
1352 {
1353 0 unblock_http3_native_conn *native =
1354 (unblock_http3_native_conn *)conn_user_data;
1355 0 AV *event = unblock_http3_event_new("reset_stream", stream_id);
1356  
1357 (void)conn;
1358 (void)stream_user_data;
1359  
1360 0 av_push(event, newSVuv((UV)app_error_code));
1361 0 unblock_http3_push_event(native, event);
1362  
1363 0 return 0;
1364 }
1365  
1366 static int
1367 3 unblock_http3_shutdown_cb(
1368 nghttp3_conn *conn,
1369 int64_t id,
1370 void *conn_user_data
1371 )
1372 {
1373 3 unblock_http3_native_conn *native =
1374 (unblock_http3_native_conn *)conn_user_data;
1375 3 AV *event = unblock_http3_event_new("shutdown", id);
1376  
1377 (void)conn;
1378  
1379 3 unblock_http3_push_event(native, event);
1380  
1381 3 return 0;
1382 }
1383  
1384 static int
1385 47 unblock_http3_stream_close2_cb(
1386 nghttp3_conn *conn,
1387 uint32_t flags,
1388 int64_t stream_id,
1389 uint64_t rx_app_error_code,
1390 uint64_t tx_app_error_code,
1391 void *conn_user_data,
1392 void *stream_user_data
1393 )
1394 {
1395 47 unblock_http3_native_conn *native =
1396 (unblock_http3_native_conn *)conn_user_data;
1397 47 AV *event = unblock_http3_event_new("stream_close", stream_id);
1398  
1399 (void)conn;
1400 (void)stream_user_data;
1401  
1402 47 av_push(event, newSVuv((UV)flags));
1403 47 av_push(event, newSVuv((UV)rx_app_error_code));
1404 47 av_push(event, newSVuv((UV)tx_app_error_code));
1405 47 unblock_http3_push_event(native, event);
1406  
1407 47 return 0;
1408 }
1409  
1410 static SV *
1411 60 unblock_http3_bless_conn(unblock_http3_native_conn *native)
1412 {
1413 60 SV *inner = newSViv(PTR2IV(native));
1414 60 SV *rv = newRV_noinc(inner);
1415  
1416 60 sv_bless(
1417 rv,
1418 gv_stashpv("Unblock::HTTP3::_Native::Connection", GV_ADD)
1419 );
1420  
1421 60 return rv;
1422 }
1423  
1424 static unblock_http3_native_conn *
1425 3969 unblock_http3_conn_from_sv(SV *self)
1426 {
1427 unblock_http3_native_conn *native;
1428  
1429 3969 ▶ if (!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) { 0 3969 3969
1430 0 croak("native HTTP/3 connection is not a reference");
1431 }
1432  
1433 3969 native = INT2PTR(
1434 unblock_http3_native_conn *,
1435 SvIV(SvRV(self))
1436 );
1437  
1438 3969 ▶ if (native == NULL || native->conn == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (native == NULL || native->conn == NULL) { 3969 0 3969
if (native == NULL || native->conn == NULL) { 0 3969 3969
1439 0 croak("native HTTP/3 connection has been destroyed");
1440 }
1441  
1442 3969 ▶ if (native->fatal) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->fatal) { 1 3968 3969
1443 1 croak("native HTTP/3 connection is no longer usable");
1444 }
1445  
1446 3968 return native;
1447 }
1448  
1449 static SV *
1450 60 unblock_http3_new_conn(
1451 int is_server,
1452 uint64_t max_field_section_size,
1453 uint64_t qpack_max_table_capacity,
1454 uint64_t qpack_blocked_streams,
1455 int enable_connect_protocol,
1456 int h3_datagram,
1457 SV *origin_list_sv
1458 )
1459 {
1460 unblock_http3_native_conn *native;
1461 nghttp3_callbacks callbacks;
1462 nghttp3_settings settings;
1463 int rv;
1464  
1465 60 Newxz(native, 1, unblock_http3_native_conn);
1466 60 Zero(&callbacks, 1, nghttp3_callbacks);
1467  
1468 60 native->events = newAV();
1469  
1470 60 callbacks.acked_stream_data = unblock_http3_acked_stream_data_cb;
1471 60 callbacks.recv_settings2 = unblock_http3_recv_settings2_cb;
1472 60 callbacks.recv_origin = unblock_http3_recv_origin_cb;
1473 60 callbacks.end_origin = unblock_http3_end_origin_cb;
1474 60 callbacks.recv_data = unblock_http3_recv_data_cb;
1475 60 callbacks.deferred_consume = unblock_http3_deferred_consume_cb;
1476 60 callbacks.begin_headers = unblock_http3_begin_headers_cb;
1477 60 callbacks.recv_header = unblock_http3_recv_header_cb;
1478 60 callbacks.end_headers = unblock_http3_end_headers_cb;
1479 60 callbacks.begin_trailers = unblock_http3_begin_trailers_cb;
1480 60 callbacks.recv_trailer = unblock_http3_recv_trailer_cb;
1481 60 callbacks.end_trailers = unblock_http3_end_trailers_cb;
1482 60 callbacks.end_stream = unblock_http3_end_stream_cb;
1483 60 callbacks.stop_sending = unblock_http3_stop_sending_cb;
1484 60 callbacks.reset_stream = unblock_http3_reset_stream_cb;
1485 60 callbacks.shutdown = unblock_http3_shutdown_cb;
1486 60 callbacks.stream_close2 = unblock_http3_stream_close2_cb;
1487  
1488 60 nghttp3_settings_default(&settings);
1489 60 settings.max_field_section_size = max_field_section_size;
1490 60 settings.qpack_max_dtable_capacity = qpack_max_table_capacity;
1491 60 settings.qpack_blocked_streams = qpack_blocked_streams;
1492 60 settings.enable_connect_protocol = enable_connect_protocol ? 1 : 0;
1493 60 settings.h3_datagram = h3_datagram ? 1 : 0;
1494  
1495 60 ▶ if (is_server && origin_list_sv != NULL && SvOK(origin_list_sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_server && origin_list_sv != NULL && SvOK(origin_list_sv)) { 38 22 60
if (is_server && origin_list_sv != NULL && SvOK(origin_list_sv)) { 38 0 38
if (is_server && origin_list_sv != NULL && SvOK(origin_list_sv)) { 2 36 38
1496 STRLEN origin_list_len;
1497 const char *origin_list_bytes =
1498 2 SvPVbyte(origin_list_sv, origin_list_len);
1499  
1500 2 ▶ if (origin_list_len != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (origin_list_len != 0) { 1 1 2
1501 1 Newx(native->origin_list_data, origin_list_len, uint8_t);
1502 1 Copy(
1503 origin_list_bytes,
1504 native->origin_list_data,
1505 origin_list_len,
1506 uint8_t
1507 );
1508 }
1509  
1510 2 native->origin_list.base = native->origin_list_data;
1511 2 native->origin_list.len = (size_t)origin_list_len;
1512 2 settings.origin_list = &native->origin_list;
1513 }
1514  
1515 60 ▶ if (is_server) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_server) { 38 22 60
1516 38 rv = nghttp3_conn_server_new(
1517 &native->conn,
1518 &callbacks,
1519 &settings,
1520 NULL,
1521 native
1522 );
1523 } else {
1524 22 rv = nghttp3_conn_client_new(
1525 &native->conn,
1526 &callbacks,
1527 &settings,
1528 NULL,
1529 native
1530 );
1531 }
1532  
1533 60 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 60 60
1534 0 SvREFCNT_dec((SV *)native->events);
1535 0 ▶ if (native->origin_list_data != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->origin_list_data != NULL) { 0 0 0
1536 0 Safefree(native->origin_list_data);
1537 }
1538 0 Safefree(native);
1539 0 unblock_http3_fail("could not create libnghttp3 connection", rv);
1540 }
1541  
1542 60 native->is_server = is_server ? 1 : 0;
1543  
1544 60 return unblock_http3_bless_conn(native);
1545 }
1546  
1547  
1548 typedef struct {
1549 nghttp3_nv *nva;
1550 uint8_t **owned_names;
1551 size_t nvlen;
1552 } unblock_http3_uniform_fields;
1553  
1554 static int
1555 0 unblock_http3_bytes_equal(
1556 const uint8_t *left,
1557 size_t left_len,
1558 const char *right
1559 )
1560 {
1561 0 size_t right_len = strlen(right);
1562  
1563 return left_len == right_len
1564 0 ▶ && memcmp(left, right, right_len) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(left, right, right_len) == 0; 0 0 0
&& memcmp(left, right, right_len) == 0; 0 0 0
1565 }
1566  
1567 static int
1568 318 unblock_http3_ascii_equal_ci(
1569 const uint8_t *left,
1570 size_t left_len,
1571 const char *right
1572 )
1573 {
1574 size_t i;
1575 318 size_t right_len = strlen(right);
1576  
1577 318 ▶ if (left_len != right_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (left_len != right_len) { 303 15 318
1578 303 return 0;
1579 }
1580  
1581 15 ▶ for (i = 0; i < left_len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < left_len; ++i) { 15 0 15
1582 15 uint8_t ch = left[i];
1583 15 uint8_t expected = (uint8_t)right[i];
1584  
1585 15 ▶ if (ch >= 'A' && ch <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'A' && ch <= 'Z') { 15 0 15
if (ch >= 'A' && ch <= 'Z') { 0 15 15
1586 0 ch = (uint8_t)(ch + ('a' - 'A'));
1587 }
1588  
1589 15 ▶ if (expected >= 'A' && expected <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (expected >= 'A' && expected <= 'Z') { 15 0 15
if (expected >= 'A' && expected <= 'Z') { 0 15 15
1590 0 expected = (uint8_t)(expected + ('a' - 'A'));
1591 }
1592  
1593 15 ▶ if (ch != expected) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch != expected) { 15 0 15
1594 15 return 0;
1595 }
1596 }
1597  
1598 0 return 1;
1599 }
1600  
1601 static void
1602 106 unblock_http3_uniform_fields_free(unblock_http3_uniform_fields *fields)
1603 {
1604 size_t i;
1605  
1606 106 ▶ if (fields == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fields == NULL) { 0 106 106
1607 0 return;
1608 }
1609  
1610 106 ▶ if (fields->owned_names != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fields->owned_names != NULL) { 58 48 106
1611 222 ▶ for (i = 0; i < fields->nvlen; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < fields->nvlen; ++i) { 164 58 222
1612 164 ▶ if (fields->owned_names[i] != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fields->owned_names[i] != NULL) { 1 163 164
1613 1 Safefree(fields->owned_names[i]);
1614 }
1615 }
1616  
1617 58 Safefree(fields->owned_names);
1618 }
1619  
1620 106 ▶ if (fields->nva != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fields->nva != NULL) { 58 48 106
1621 58 Safefree(fields->nva);
1622 }
1623  
1624 106 fields->nva = NULL;
1625 106 fields->owned_names = NULL;
1626 106 fields->nvlen = 0;
1627 }
1628  
1629 static void
1630 140 unblock_http3_uniform_nv_static(
1631 nghttp3_nv *nv,
1632 const char *name,
1633 const char *value,
1634 size_t value_len
1635 )
1636 {
1637 140 nv->name = (const uint8_t *)name;
1638 140 nv->namelen = strlen(name);
1639 140 nv->value = (const uint8_t *)value;
1640 140 nv->valuelen = value_len;
1641 140 nv->flags = NGHTTP3_NV_FLAG_NONE;
1642 140 }
1643  
1644 static void
1645 114 unblock_http3_uniform_nv_sv(
1646 nghttp3_nv *nv,
1647 const char *name,
1648 SV *value
1649 )
1650 {
1651 STRLEN len;
1652 114 const char *bytes = SvPVbyte(value, len);
1653  
1654 114 unblock_http3_uniform_nv_static(
1655 nv,
1656 name,
1657 bytes,
1658 (size_t)len
1659 );
1660 114 }
1661  
1662 static void
1663 49 unblock_http3_validate_uniform_field(
1664 int context,
1665 const uint8_t *name,
1666 size_t namelen,
1667 const uint8_t *value,
1668 size_t valuelen
1669 )
1670 {
1671 size_t start;
1672 size_t end;
1673  
1674 49 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 49 0 49
1675 49 unblock_http3_ascii_equal_ci(name, namelen, "connection")
1676 49 ▶ || unblock_http3_ascii_equal_ci(name, namelen, "keep-alive")
Branch: 1 branch
BranchTrueFalseTotal
|| unblock_http3_ascii_equal_ci(name, namelen, "keep-alive") 49 0 49
1677 49 ▶ || unblock_http3_ascii_equal_ci(name, namelen, "proxy-connection")
Branch: 1 branch
BranchTrueFalseTotal
|| unblock_http3_ascii_equal_ci(name, namelen, "proxy-connection") 49 0 49
1678 49 ▶ || unblock_http3_ascii_equal_ci(name, namelen, "transfer-encoding")
Branch: 1 branch
BranchTrueFalseTotal
|| unblock_http3_ascii_equal_ci(name, namelen, "transfer-encoding") 49 0 49
1679 49 ▶ || unblock_http3_ascii_equal_ci(name, namelen, "upgrade")
Branch: 1 branch
BranchTrueFalseTotal
|| unblock_http3_ascii_equal_ci(name, namelen, "upgrade") 0 49 49
1680 ) {
1681 0 croak("HTTP/3 does not allow connection-specific field '%.*s'",
1682 (int)namelen, (const char *)name);
1683 }
1684  
1685 49 ▶ if (unblock_http3_ascii_equal_ci(name, namelen, "te")) {
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_http3_ascii_equal_ci(name, namelen, "te")) { 0 49 49
1686 0 ▶ if (context != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (context != 0) { 0 0 0
1687 0 croak("HTTP/3 TE is only allowed in request headers");
1688 }
1689  
1690 0 start = 0;
1691 0 end = valuelen;
1692  
1693 0 while (
1694 start < end
1695 0 ▶ && (value[start] == ' ' || value[start] == '\t')
Branch: 3 branches
BranchTrueFalseTotal
&& (value[start] == ' ' || value[start] == '\t') 0 0 0
&& (value[start] == ' ' || value[start] == '\t') 0 0 0
&& (value[start] == ' ' || value[start] == '\t') 0 0 0
1696 ) {
1697 0 ++start;
1698 }
1699  
1700 0 while (
1701 end > start
1702 0 ▶ && (value[end - 1] == ' ' || value[end - 1] == '\t')
Branch: 3 branches
BranchTrueFalseTotal
&& (value[end - 1] == ' ' || value[end - 1] == '\t') 0 0 0
&& (value[end - 1] == ' ' || value[end - 1] == '\t') 0 0 0
&& (value[end - 1] == ' ' || value[end - 1] == '\t') 0 0 0
1703 ) {
1704 0 --end;
1705 }
1706  
1707 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 0 0
1708 0 !unblock_http3_ascii_equal_ci(
1709 value + start,
1710 end - start,
1711 "trailers"
1712 )
1713 ) {
1714 0 croak("HTTP/3 TE field may contain only 'trailers'");
1715 }
1716 }
1717  
1718 49 ▶ if (context == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (context == 2) { 12 37 49
1719 12 ▶ if (unblock_http3_ascii_equal_ci(name, namelen, "content-length")) {
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_http3_ascii_equal_ci(name, namelen, "content-length")) { 0 12 12
1720 0 croak("HTTP/3 Content-Length is not allowed in trailers");
1721 }
1722  
1723 12 ▶ if (unblock_http3_ascii_equal_ci(name, namelen, "host")) {
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_http3_ascii_equal_ci(name, namelen, "host")) { 0 12 12
1724 0 croak("HTTP/3 Host is not allowed in trailers");
1725 }
1726 }
1727 49 }
1728  
1729  
1730 static void
1731 213 unblock_http3_uniform_validate_section(
1732 pTHX_ const uhttp_native_section *section,
1733 int context
1734 )
1735 {
1736 Size_t i;
1737 213 Size_t count = uhttp_native_field_count(aTHX_ section);
1738  
1739 262 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 49 213 262
1740 SV *name_sv;
1741 SV *value_sv;
1742 STRLEN namelen;
1743 STRLEN valuelen;
1744 const char *name;
1745 const char *value;
1746  
1747 49 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 49 49
1748 49 !uhttp_native_field_at(
1749 aTHX_ section,
1750 i,
1751 &name_sv,
1752 &value_sv
1753 )
1754 ) {
1755 0 croak("Uniform field inspection failed");
1756 }
1757  
1758 49 name = SvPVbyte(name_sv, namelen);
1759 49 ▶ value = SvPVbyte(value_sv, valuelen);
Branch: 1 branch
BranchTrueFalseTotal
value = SvPVbyte(value_sv, valuelen); 49 0 49
1760  
1761 49 unblock_http3_validate_uniform_field(
1762 context,
1763 (const uint8_t *)name,
1764 (size_t)namelen,
1765 (const uint8_t *)value,
1766 (size_t)valuelen
1767 );
1768 }
1769 213 }
1770  
1771 static size_t
1772 107 unblock_http3_uniform_section_size(
1773 pTHX_ const uhttp_native_section *section
1774 )
1775 {
1776 Size_t i;
1777 107 Size_t count = uhttp_native_field_count(aTHX_ section);
1778 107 size_t total = 0;
1779  
1780 132 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 25 107 132
1781 SV *name_sv;
1782 SV *value_sv;
1783 STRLEN namelen;
1784 STRLEN valuelen;
1785  
1786 25 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 25 25
1787 25 !uhttp_native_field_at(
1788 aTHX_ section,
1789 i,
1790 &name_sv,
1791 &value_sv
1792 )
1793 ) {
1794 0 croak("Uniform header inspection failed");
1795 }
1796  
1797 25 (void)SvPVbyte(name_sv, namelen);
1798 25 ▶ (void)SvPVbyte(value_sv, valuelen);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvPVbyte(value_sv, valuelen); 25 0 25
1799  
1800 25 if (
1801 25 ▶ total > SIZE_MAX - (size_t)namelen
Branch: 1 branch
BranchTrueFalseTotal
total > SIZE_MAX - (size_t)namelen 25 0 25
1802 25 ▶ || total + (size_t)namelen > SIZE_MAX - (size_t)valuelen
Branch: 1 branch
BranchTrueFalseTotal
|| total + (size_t)namelen > SIZE_MAX - (size_t)valuelen 25 0 25
1803 25 ▶ || total + (size_t)namelen + (size_t)valuelen > SIZE_MAX - 32
Branch: 1 branch
BranchTrueFalseTotal
|| total + (size_t)namelen + (size_t)valuelen > SIZE_MAX - 32 0 25 25
1804 ) {
1805 0 croak("HTTP/3 field section size overflow");
1806 }
1807  
1808 25 total += (size_t)namelen + (size_t)valuelen + 32;
1809 }
1810  
1811 107 return total;
1812 }
1813  
1814 static size_t
1815 118 unblock_http3_uniform_pseudo_size(
1816 const char *name,
1817 SV *value
1818 )
1819 {
1820 STRLEN valuelen;
1821  
1822 (void)SvPVbyte(value, valuelen);
1823 118 return strlen(name) + (size_t)valuelen + 32;
1824 }
1825  
1826 static void
1827 58 unblock_http3_uniform_add_section(
1828 pTHX_ unblock_http3_uniform_fields *out,
1829 size_t *index,
1830 const uhttp_native_section *section,
1831 int context
1832 )
1833 {
1834 Size_t i;
1835 58 Size_t count = uhttp_native_field_count(aTHX_ section);
1836  
1837 82 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 24 58 82
1838 SV *name_sv;
1839 SV *value_sv;
1840 STRLEN namelen;
1841 STRLEN valuelen;
1842 const char *name;
1843 const char *value;
1844 24 uint8_t *lower = NULL;
1845 size_t j;
1846 24 int needs_lower = 0;
1847  
1848 24 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 24 24
1849 24 !uhttp_native_field_at(
1850 aTHX_ section,
1851 i,
1852 &name_sv,
1853 &value_sv
1854 )
1855 ) {
1856 0 croak("Uniform field inspection failed");
1857 }
1858  
1859 24 name = SvPVbyte(name_sv, namelen);
1860 24 ▶ value = SvPVbyte(value_sv, valuelen);
Branch: 1 branch
BranchTrueFalseTotal
value = SvPVbyte(value_sv, valuelen); 24 0 24
1861  
1862 282 ▶ for (j = 0; j < (size_t)namelen; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < (size_t)namelen; ++j) { 259 23 282
1863 259 uint8_t ch = (uint8_t)name[j];
1864  
1865 259 ▶ if (ch >= 'A' && ch <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'A' && ch <= 'Z') { 238 21 259
if (ch >= 'A' && ch <= 'Z') { 1 237 238
1866 1 needs_lower = 1;
1867 1 break;
1868 }
1869 }
1870  
1871 24 ▶ if (needs_lower) {
Branch: 1 branch
BranchTrueFalseTotal
if (needs_lower) { 1 23 24
1872 1 ▶ Newx(lower, namelen ? namelen : 1, uint8_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(lower, namelen ? namelen : 1, uint8_t); 1 0 1
1873  
1874 9 ▶ for (j = 0; j < (size_t)namelen; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < (size_t)namelen; ++j) { 8 1 9
1875 8 uint8_t ch = (uint8_t)name[j];
1876  
1877 8 ▶ if (ch >= 'A' && ch <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'A' && ch <= 'Z') { 8 0 8
if (ch >= 'A' && ch <= 'Z') { 1 7 8
1878 1 ch = (uint8_t)(ch + ('a' - 'A'));
1879 }
1880  
1881 8 lower[j] = ch;
1882 }
1883  
1884 1 out->nva[*index].name = lower;
1885 } else {
1886 23 out->nva[*index].name = (const uint8_t *)name;
1887 }
1888 24 out->nva[*index].namelen = (size_t)namelen;
1889 24 out->nva[*index].value = (const uint8_t *)value;
1890 24 out->nva[*index].valuelen = (size_t)valuelen;
1891 24 out->nva[*index].flags = NGHTTP3_NV_FLAG_NONE;
1892 24 out->owned_names[*index] = lower;
1893 24 ++*index;
1894 }
1895 58 }
1896  
1897 static int
1898 57 unblock_http3_uniform_request_shape(
1899 pTHX_ const uhttp_native_view *view,
1900 int *is_connect,
1901 int *is_extended
1902 )
1903 {
1904 STRLEN method_len;
1905 const char *method;
1906  
1907 57 ▶ if (!SvOK(view->method) || !SvOK(view->target)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(view->method) || !SvOK(view->target)) { 57 0 57
if (!SvOK(view->method) || !SvOK(view->target)) { 0 57 57
1908 0 croak("canonical Uniform request is missing method or target");
1909 }
1910  
1911 57 method = SvPVbyte(view->method, method_len);
1912 57 *is_connect =
1913 73 method_len == 7 && memcmp(method, "CONNECT", 7) == 0
1914 57 ▶ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
? 1 : 0; 16 41 57
? 1 : 0; 16 0 16
1915 57 ▶ *is_extended = *is_connect && SvOK(view->protocol) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
*is_extended = *is_connect && SvOK(view->protocol) ? 1 : 0; 16 41 57
*is_extended = *is_connect && SvOK(view->protocol) ? 1 : 0; 12 4 16
1916  
1917 57 ▶ if (*is_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (*is_connect) { 16 41 57
1918 16 ▶ if (!SvOK(view->authority)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(view->authority)) { 0 16 16
1919 0 croak("HTTP/3 CONNECT request requires authority");
1920 }
1921  
1922 16 ▶ if (*is_extended && !SvOK(view->scheme)) {
Branch: 2 branches
BranchTrueFalseTotal
if (*is_extended && !SvOK(view->scheme)) { 12 4 16
if (*is_extended && !SvOK(view->scheme)) { 0 12 12
1923 0 croak("HTTP/3 Extended CONNECT request requires scheme");
1924 }
1925 } else {
1926 41 ▶ if (!SvOK(view->scheme)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(view->scheme)) { 0 41 41
1927 0 croak("HTTP/3 request requires scheme");
1928 }
1929  
1930 41 ▶ if (!SvOK(view->authority)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(view->authority)) { 0 41 41
1931 0 croak("HTTP/3 request requires authority");
1932 }
1933 }
1934  
1935 57 return 1;
1936 }
1937  
1938 static size_t
1939 29 unblock_http3_uniform_request_size(
1940 pTHX_ const uhttp_native_view *view
1941 )
1942 {
1943 int is_connect;
1944 int is_extended;
1945 size_t total;
1946  
1947 29 unblock_http3_uniform_request_shape(
1948 aTHX_ view,
1949 &is_connect,
1950 &is_extended
1951 );
1952  
1953 29 total = unblock_http3_uniform_section_size(
1954 aTHX_ &view->headers
1955 );
1956  
1957 58 total += unblock_http3_uniform_pseudo_size(
1958 ":method",
1959 29 view->method
1960 );
1961  
1962 29 ▶ if (is_extended) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_extended) { 6 23 29
1963 12 total += unblock_http3_uniform_pseudo_size(
1964 ":protocol",
1965 6 view->protocol
1966 );
1967 12 total += unblock_http3_uniform_pseudo_size(
1968 ":scheme",
1969 6 view->scheme
1970 );
1971 12 total += unblock_http3_uniform_pseudo_size(
1972 ":authority",
1973 6 view->authority
1974 );
1975 6 total += unblock_http3_uniform_pseudo_size(
1976 ":path",
1977 6 view->target
1978 );
1979 23 ▶ } else if (is_connect) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_connect) { 2 21 23
1980 2 total += unblock_http3_uniform_pseudo_size(
1981 ":authority",
1982 2 view->authority
1983 );
1984 } else {
1985 42 total += unblock_http3_uniform_pseudo_size(
1986 ":scheme",
1987 21 view->scheme
1988 );
1989 42 total += unblock_http3_uniform_pseudo_size(
1990 ":authority",
1991 21 view->authority
1992 );
1993 21 total += unblock_http3_uniform_pseudo_size(
1994 ":path",
1995 21 view->target
1996 );
1997 }
1998  
1999 29 return total;
2000 }
2001  
2002 static size_t
2003 26 unblock_http3_uniform_response_size(
2004 pTHX_ const uhttp_native_view *view
2005 )
2006 {
2007 IV status;
2008 char status_buffer[4];
2009  
2010 26 ▶ if (!SvOK(view->status)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(view->status)) { 0 26 26
2011 0 croak("canonical Uniform response is missing status");
2012 }
2013  
2014 26 status = SvIV(view->status);
2015  
2016 26 ▶ if (status < 100 || status > 599) {
Branch: 2 branches
BranchTrueFalseTotal
if (status < 100 || status > 599) { 26 0 26
if (status < 100 || status > 599) { 0 26 26
2017 0 croak("canonical Uniform response status is invalid");
2018 }
2019  
2020 26 snprintf(status_buffer, sizeof(status_buffer), "%ld", (long)status);
2021  
2022 26 return strlen(":status") + strlen(status_buffer) + 32
2023 26 + unblock_http3_uniform_section_size(
2024 aTHX_ &view->headers
2025 );
2026 }
2027  
2028 static void
2029 28 unblock_http3_uniform_request_fields(
2030 pTHX_ const uhttp_native_view *view,
2031 unblock_http3_uniform_fields *out
2032 )
2033 {
2034 28 Size_t header_count = uhttp_native_field_count(
2035 aTHX_ &view->headers
2036 );
2037 int is_connect;
2038 int is_extended;
2039 size_t pseudo_count;
2040 28 size_t index = 0;
2041  
2042 28 unblock_http3_uniform_request_shape(
2043 aTHX_ view,
2044 &is_connect,
2045 &is_extended
2046 );
2047 28 unblock_http3_uniform_validate_section(
2048 aTHX_ &view->headers,
2049 0
2050 );
2051  
2052 28 ▶ pseudo_count = is_extended ? 5 : (is_connect ? 2 : 4);
Branch: 2 branches
BranchTrueFalseTotal
pseudo_count = is_extended ? 5 : (is_connect ? 2 : 4); 22 6 28
pseudo_count = is_extended ? 5 : (is_connect ? 2 : 4); 2 20 22
2053  
2054 28 Zero(out, 1, unblock_http3_uniform_fields);
2055 28 out->nvlen = pseudo_count + (size_t)header_count;
2056  
2057 28 ▶ if (out->nvlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (out->nvlen == 0) { 0 28 28
2058 0 return;
2059 }
2060  
2061 28 ▶ Newxz(out->nva, out->nvlen, nghttp3_nv);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(out->nva, out->nvlen, nghttp3_nv); 0 28 28
2062 28 ▶ Newxz(out->owned_names, out->nvlen, uint8_t *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(out->owned_names, out->nvlen, uint8_t *); 0 28 28
2063  
2064 28 unblock_http3_uniform_nv_sv(
2065 28 &out->nva[index++],
2066 ":method",
2067 28 view->method
2068 );
2069  
2070 28 ▶ if (is_extended) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_extended) { 6 22 28
2071 6 unblock_http3_uniform_nv_sv(
2072 6 &out->nva[index++],
2073 ":protocol",
2074 6 view->protocol
2075 );
2076 6 unblock_http3_uniform_nv_sv(
2077 6 &out->nva[index++],
2078 ":scheme",
2079 6 view->scheme
2080 );
2081 6 unblock_http3_uniform_nv_sv(
2082 6 &out->nva[index++],
2083 ":authority",
2084 6 view->authority
2085 );
2086 6 unblock_http3_uniform_nv_sv(
2087 6 &out->nva[index++],
2088 ":path",
2089 6 view->target
2090 );
2091 22 ▶ } else if (is_connect) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_connect) { 2 20 22
2092 2 unblock_http3_uniform_nv_sv(
2093 2 &out->nva[index++],
2094 ":authority",
2095 2 view->authority
2096 );
2097 } else {
2098 20 unblock_http3_uniform_nv_sv(
2099 20 &out->nva[index++],
2100 ":scheme",
2101 20 view->scheme
2102 );
2103 20 unblock_http3_uniform_nv_sv(
2104 20 &out->nva[index++],
2105 ":authority",
2106 20 view->authority
2107 );
2108 20 unblock_http3_uniform_nv_sv(
2109 20 &out->nva[index++],
2110 ":path",
2111 20 view->target
2112 );
2113 }
2114  
2115 28 unblock_http3_uniform_add_section(
2116 aTHX_ out,
2117 &index,
2118 &view->headers,
2119 0
2120 );
2121 }
2122  
2123 static void
2124 26 unblock_http3_uniform_response_fields(
2125 pTHX_ const uhttp_native_view *view,
2126 unblock_http3_uniform_fields *out,
2127 char status_buffer[4]
2128 )
2129 {
2130 26 Size_t header_count = uhttp_native_field_count(
2131 aTHX_ &view->headers
2132 );
2133 IV status;
2134 26 size_t index = 0;
2135  
2136 26 ▶ if (!SvOK(view->status)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(view->status)) { 0 26 26
2137 0 croak("canonical Uniform response is missing status");
2138 }
2139  
2140 26 status = SvIV(view->status);
2141  
2142 26 ▶ if (status < 100 || status > 599) {
Branch: 2 branches
BranchTrueFalseTotal
if (status < 100 || status > 599) { 26 0 26
if (status < 100 || status > 599) { 0 26 26
2143 0 croak("canonical Uniform response status is invalid");
2144 }
2145  
2146 26 snprintf(status_buffer, 4, "%ld", (long)status);
2147 26 unblock_http3_uniform_validate_section(
2148 aTHX_ &view->headers,
2149 1
2150 );
2151  
2152 26 Zero(out, 1, unblock_http3_uniform_fields);
2153 26 out->nvlen = 1 + (size_t)header_count;
2154  
2155 26 ▶ Newxz(out->nva, out->nvlen, nghttp3_nv);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(out->nva, out->nvlen, nghttp3_nv); 0 26 26
2156 26 ▶ Newxz(out->owned_names, out->nvlen, uint8_t *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(out->owned_names, out->nvlen, uint8_t *); 0 26 26
2157  
2158 26 unblock_http3_uniform_nv_static(
2159 26 &out->nva[index++],
2160 ":status",
2161 status_buffer,
2162 strlen(status_buffer)
2163 );
2164  
2165 26 unblock_http3_uniform_add_section(
2166 aTHX_ out,
2167 &index,
2168 &view->headers,
2169 1
2170 );
2171 26 }
2172  
2173 static void
2174 52 unblock_http3_uniform_trailer_fields(
2175 pTHX_ const uhttp_native_view *view,
2176 unblock_http3_uniform_fields *out
2177 )
2178 {
2179 52 Size_t count = uhttp_native_field_count(
2180 aTHX_ &view->trailers
2181 );
2182 52 size_t index = 0;
2183  
2184 52 unblock_http3_uniform_validate_section(
2185 aTHX_ &view->trailers,
2186 2
2187 );
2188 52 Zero(out, 1, unblock_http3_uniform_fields);
2189 52 out->nvlen = (size_t)count;
2190  
2191 52 ▶ if (out->nvlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (out->nvlen == 0) { 48 4 52
2192 48 return;
2193 }
2194  
2195 4 ▶ Newxz(out->nva, out->nvlen, nghttp3_nv);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(out->nva, out->nvlen, nghttp3_nv); 0 4 4
2196 4 ▶ Newxz(out->owned_names, out->nvlen, uint8_t *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(out->owned_names, out->nvlen, uint8_t *); 0 4 4
2197  
2198 4 unblock_http3_uniform_add_section(
2199 aTHX_ out,
2200 &index,
2201 &view->trailers,
2202 2
2203 );
2204 }
2205  
2206 static void
2207 161 unblock_http3_uniform_any_view(
2208 pTHX_ SV *message,
2209 uhttp_native_view *view
2210 )
2211 {
2212 dMY_CXT;
2213  
2214 161 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 161 161
2215 161 !uhttp_native_inspect(
2216 aTHX_ &MY_CXT.uniform_api,
2217 message,
2218 view
2219 )
2220 ) {
2221 0 croak("native Uniform fast path requires an exact canonical message");
2222 }
2223 161 }
2224  
2225 static void
2226 109 unblock_http3_uniform_view(
2227 pTHX_ SV *message,
2228 U32 expected_kind,
2229 uhttp_native_view *view
2230 )
2231 {
2232 109 unblock_http3_uniform_any_view(
2233 aTHX_ message,
2234 view
2235 );
2236  
2237 109 ▶ if (view->kind != expected_kind) {
Branch: 1 branch
BranchTrueFalseTotal
if (view->kind != expected_kind) { 0 109 109
2238 0 croak("canonical Uniform message has the wrong kind");
2239 }
2240 109 }
2241  
2242  
2243 typedef struct {
2244 SV *connection;
2245 HV *connection_hv;
2246 CV *service_cv;
2247 CV *request_cv;
2248 CV *respond_cv;
2249 CV *send_informational_cv;
2250 void *interpreter;
2251 } unblock_http3_consumer_context;
2252  
2253 static void *
2254 13 unblock_http3_consumer_interpreter(pTHX)
2255 {
2256 #ifdef MULTIPLICITY
2257 return (void *)aTHX;
2258 #else
2259 13 return NULL;
2260 #endif
2261 }
2262  
2263 static SV *
2264 14 unblock_http3_consumer_fetch(
2265 pTHX_ HV *hv,
2266 const char *key,
2267 I32 key_len,
2268 int required
2269 )
2270 {
2271 14 SV **slot = hv_fetch(hv, key, key_len, 0);
2272  
2273 14 ▶ if (slot == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot == NULL) { 0 14 14
2274 0 ▶ if (required) {
Branch: 1 branch
BranchTrueFalseTotal
if (required) { 0 0 0
2275 0 croak("Unblock::HTTP3 native ABI object is missing '%.*s'",
2276 (int)key_len, key);
2277 }
2278  
2279 0 return NULL;
2280 }
2281  
2282 14 return *slot;
2283 }
2284  
2285 static HV *
2286 5 unblock_http3_consumer_exact_object(
2287 pTHX_
2288 SV *object,
2289 const char *class_name
2290 )
2291 {
2292 HV *stash;
2293  
2294 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 5 0 5
2295 object == NULL
2296 5 ▶ || !SvROK(object)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(object) 5 0 5
2297 5 ▶ || SvTYPE(SvRV(object)) != SVt_PVHV
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(object)) != SVt_PVHV 5 0 5
2298 5 ▶ || !SvOBJECT(SvRV(object))
Branch: 1 branch
BranchTrueFalseTotal
|| !SvOBJECT(SvRV(object)) 5 0 5
2299 5 ▶ || SvMAGICAL(SvRV(object))
Branch: 1 branch
BranchTrueFalseTotal
|| SvMAGICAL(SvRV(object)) 0 5 5
2300 ) {
2301 0 croak("Unblock::HTTP3 native ABI requires a plain blessed hash object");
2302 }
2303  
2304 5 stash = gv_stashpv(class_name, 0);
2305  
2306 5 ▶ if (stash == NULL || SvSTASH(SvRV(object)) != stash) {
Branch: 2 branches
BranchTrueFalseTotal
if (stash == NULL || SvSTASH(SvRV(object)) != stash) { 5 0 5
if (stash == NULL || SvSTASH(SvRV(object)) != stash) { 0 5 5
2307 0 croak("Unblock::HTTP3 native ABI requires exact class %s", class_name);
2308 }
2309  
2310 5 return (HV *)SvRV(object);
2311 }
2312  
2313 static HV *
2314 5 unblock_http3_consumer_exact_connection(
2315 pTHX_ SV *object
2316 )
2317 {
2318 HV *client_stash;
2319 HV *server_stash;
2320 HV *object_stash;
2321  
2322 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 5 0 5
2323 object == NULL
2324 5 ▶ || !SvROK(object)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(object) 5 0 5
2325 5 ▶ || SvTYPE(SvRV(object)) != SVt_PVHV
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(object)) != SVt_PVHV 5 0 5
2326 5 ▶ || !SvOBJECT(SvRV(object))
Branch: 1 branch
BranchTrueFalseTotal
|| !SvOBJECT(SvRV(object)) 5 0 5
2327 5 ▶ || SvMAGICAL(SvRV(object))
Branch: 1 branch
BranchTrueFalseTotal
|| SvMAGICAL(SvRV(object)) 0 5 5
2328 ) {
2329 0 croak("Unblock::HTTP3 native ABI requires a plain blessed hash object");
2330 }
2331  
2332 5 client_stash = gv_stashpv("Unblock::HTTP3::Client", 0);
2333 5 server_stash = gv_stashpv("Unblock::HTTP3::Server", 0);
2334 5 object_stash = SvSTASH(SvRV(object));
2335  
2336 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 5 0 5
2337 5 ▶ (client_stash == NULL || object_stash != client_stash)
Branch: 1 branch
BranchTrueFalseTotal
(client_stash == NULL || object_stash != client_stash) 2 3 5
2338 2 ▶ && (server_stash == NULL || object_stash != server_stash)
Branch: 2 branches
BranchTrueFalseTotal
&& (server_stash == NULL || object_stash != server_stash) 2 0 2
&& (server_stash == NULL || object_stash != server_stash) 1 1 2
2339 ) {
2340 1 croak(
2341 "Unblock::HTTP3 native ABI requires exact class "
2342 "Unblock::HTTP3::Client or Unblock::HTTP3::Server"
2343 );
2344 }
2345  
2346 4 return (HV *)SvRV(object);
2347 }
2348  
2349 static unblock_http3_consumer_context *
2350 5 unblock_http3_consumer_require_context(
2351 pTHX_ void *opaque
2352 )
2353 {
2354 5 unblock_http3_consumer_context *context =
2355 (unblock_http3_consumer_context *)opaque;
2356  
2357 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 5 0 5
2358 context == NULL
2359 5 ▶ || context->connection == NULL
Branch: 1 branch
BranchTrueFalseTotal
|| context->connection == NULL 5 0 5
2360 5 ▶ || context->connection_hv == NULL
Branch: 1 branch
BranchTrueFalseTotal
|| context->connection_hv == NULL 5 0 5
2361 5 ▶ || context->interpreter != unblock_http3_consumer_interpreter(aTHX)
Branch: 1 branch
BranchTrueFalseTotal
|| context->interpreter != unblock_http3_consumer_interpreter(aTHX) 0 5 5
2362 ) {
2363 0 croak("Unblock::HTTP3 native consumer context is invalid");
2364 }
2365  
2366 5 return context;
2367 }
2368  
2369 static SV *
2370 0 unblock_http3_consumer_call_scalar_2(
2371 pTHX_
2372 CV *cv,
2373 SV *first,
2374 SV *second
2375 )
2376 {
2377 0 dSP;
2378 int count;
2379 SV *value;
2380 0 SV *owned = NULL;
2381  
2382 0 ENTER;
2383 0 SAVETMPS;
2384  
2385 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
2386 0 ▶ XPUSHs(first);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(first); 0 0 0
2387 0 ▶ XPUSHs(second);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(second); 0 0 0
2388 0 PUTBACK;
2389  
2390 0 count = call_sv((SV *)cv, G_SCALAR);
2391  
2392 0 SPAGAIN;
2393  
2394 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
2395 0 PUTBACK;
2396 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
2397 0 LEAVE;
2398 0 croak("Unblock::HTTP3 native ABI callback returned an invalid result");
2399 }
2400  
2401 0 value = POPs;
2402  
2403 0 ▶ if (SvOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(value)) { 0 0 0
2404 0 owned = newSVsv(value);
2405 }
2406  
2407 0 PUTBACK;
2408 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
2409 0 LEAVE;
2410  
2411 0 return owned;
2412 }
2413  
2414 static void
2415 0 unblock_http3_consumer_call_void_1(
2416 pTHX_
2417 CV *cv,
2418 SV *first
2419 )
2420 {
2421 0 dSP;
2422  
2423 0 ENTER;
2424 0 SAVETMPS;
2425  
2426 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
2427 0 ▶ XPUSHs(first);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(first); 0 0 0
2428 0 PUTBACK;
2429  
2430 0 (void)call_sv((SV *)cv, G_VOID);
2431  
2432 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
2433 0 LEAVE;
2434 0 }
2435  
2436 static void
2437 0 unblock_http3_consumer_call_void_2(
2438 pTHX_
2439 CV *cv,
2440 SV *first,
2441 SV *second
2442 )
2443 {
2444 0 dSP;
2445  
2446 0 ENTER;
2447 0 SAVETMPS;
2448  
2449 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
2450 0 ▶ XPUSHs(first);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(first); 0 0 0
2451 0 ▶ XPUSHs(second);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(second); 0 0 0
2452 0 PUTBACK;
2453  
2454 0 (void)call_sv((SV *)cv, G_VOID);
2455  
2456 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
2457 0 LEAVE;
2458 0 }
2459  
2460 static SV *
2461 3 unblock_http3_consumer_shift_queue(
2462 pTHX_
2463 HV *connection_hv,
2464 const char *key,
2465 I32 key_len
2466 )
2467 {
2468 SV *queue_sv;
2469 AV *queue;
2470  
2471 3 queue_sv = unblock_http3_consumer_fetch(
2472 aTHX_ connection_hv,
2473 key,
2474 key_len,
2475 1
2476 );
2477  
2478 3 if (
2479 3 ▶ !SvROK(queue_sv)
Branch: 1 branch
BranchTrueFalseTotal
!SvROK(queue_sv) 3 0 3
2480 3 ▶ || SvTYPE(SvRV(queue_sv)) != SVt_PVAV
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(queue_sv)) != SVt_PVAV 3 0 3
2481 3 ▶ || SvMAGICAL(SvRV(queue_sv))
Branch: 1 branch
BranchTrueFalseTotal
|| SvMAGICAL(SvRV(queue_sv)) 3 0 3
2482 3 ▶ || SvOBJECT(SvRV(queue_sv))
Branch: 1 branch
BranchTrueFalseTotal
|| SvOBJECT(SvRV(queue_sv)) 0 3 3
2483 ) {
2484 0 croak("Unblock::HTTP3 native ABI queue storage is invalid");
2485 }
2486  
2487 3 queue = (AV *)SvRV(queue_sv);
2488  
2489 3 ▶ if (av_len(queue) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(queue) < 0) { 0 3 3
2490 0 return NULL;
2491 }
2492  
2493 3 return av_shift(queue);
2494 }
2495  
2496 static void *
2497 5 unblock_http3_consumer_create(
2498 pTHX_ SV *connection
2499 )
2500 {
2501 unblock_http3_consumer_context *context;
2502  
2503 5 (void)unblock_http3_consumer_exact_connection(
2504 aTHX_ connection
2505 );
2506  
2507 4 Newxz(context, 1, unblock_http3_consumer_context);
2508  
2509 4 context->connection = newSVsv(connection);
2510 4 context->connection_hv = (HV *)SvRV(context->connection);
2511 4 context->service_cv =
2512 4 get_cv("Unblock::HTTP3::Connection::_service", 0);
2513 4 context->request_cv =
2514 4 get_cv("Unblock::HTTP3::Connection::request", 0);
2515 4 context->respond_cv =
2516 4 get_cv("Unblock::HTTP3::Transaction::respond", 0);
2517 4 context->send_informational_cv =
2518 4 get_cv("Unblock::HTTP3::Transaction::send_informational", 0);
2519 4 context->interpreter =
2520 4 unblock_http3_consumer_interpreter(aTHX);
2521  
2522 4 if (
2523 4 ▶ context->service_cv == NULL
Branch: 1 branch
BranchTrueFalseTotal
context->service_cv == NULL 4 0 4
2524 4 ▶ || context->request_cv == NULL
Branch: 1 branch
BranchTrueFalseTotal
|| context->request_cv == NULL 4 0 4
2525 4 ▶ || context->respond_cv == NULL
Branch: 1 branch
BranchTrueFalseTotal
|| context->respond_cv == NULL 4 0 4
2526 4 ▶ || context->send_informational_cv == NULL
Branch: 1 branch
BranchTrueFalseTotal
|| context->send_informational_cv == NULL 0 4 4
2527 ) {
2528 0 SvREFCNT_dec(context->connection);
2529 0 Safefree(context);
2530 0 croak("Unblock::HTTP3 native ABI could not resolve provider callbacks");
2531 }
2532  
2533 4 return context;
2534 }
2535  
2536 static void
2537 0 unblock_http3_consumer_service(
2538 pTHX_ void *opaque
2539 )
2540 {
2541 unblock_http3_consumer_context *context =
2542 0 unblock_http3_consumer_require_context(aTHX_ opaque);
2543  
2544 0 unblock_http3_consumer_call_void_1(
2545 aTHX_ context->service_cv,
2546 context->connection
2547 );
2548 0 }
2549  
2550 static int
2551 2 unblock_http3_consumer_should_service(
2552 pTHX_ unblock_http3_consumer_context *context
2553 )
2554 {
2555 2 SV *started = unblock_http3_consumer_fetch(
2556 aTHX_ context->connection_hv,
2557 "started",
2558 7,
2559 1
2560 );
2561 2 SV *failed = unblock_http3_consumer_fetch(
2562 aTHX_ context->connection_hv,
2563 "failed",
2564 6,
2565 1
2566 );
2567  
2568 2 ▶ return SvTRUE(started) && !SvTRUE(failed);
Branch: 2 branches
BranchTrueFalseTotal
return SvTRUE(started) && !SvTRUE(failed); 0 2 2
return SvTRUE(started) && !SvTRUE(failed); 0 0 0
2569 }
2570  
2571 static SV *
2572 0 unblock_http3_consumer_request(
2573 pTHX_
2574 void *opaque,
2575 SV *request
2576 )
2577 {
2578 unblock_http3_consumer_context *context =
2579 0 unblock_http3_consumer_require_context(aTHX_ opaque);
2580  
2581 0 return unblock_http3_consumer_call_scalar_2(
2582 aTHX_
2583 context->request_cv,
2584 context->connection,
2585 request
2586 );
2587 }
2588  
2589 static SV *
2590 1 unblock_http3_consumer_next_transaction(
2591 pTHX_ void *opaque
2592 )
2593 {
2594 unblock_http3_consumer_context *context =
2595 1 unblock_http3_consumer_require_context(aTHX_ opaque);
2596  
2597 1 ▶ if (unblock_http3_consumer_should_service(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_http3_consumer_should_service(aTHX_ context)) { 0 1 1
2598 0 unblock_http3_consumer_service(aTHX_ context);
2599 }
2600  
2601 1 return unblock_http3_consumer_shift_queue(
2602 aTHX_
2603 context->connection_hv,
2604 "ready_transactions",
2605 18
2606 );
2607 }
2608  
2609 static SV *
2610 1 unblock_http3_consumer_next_informational(
2611 pTHX_ void *opaque
2612 )
2613 {
2614 unblock_http3_consumer_context *context =
2615 1 unblock_http3_consumer_require_context(aTHX_ opaque);
2616  
2617 1 ▶ if (unblock_http3_consumer_should_service(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unblock_http3_consumer_should_service(aTHX_ context)) { 0 1 1
2618 0 unblock_http3_consumer_service(aTHX_ context);
2619 }
2620  
2621 1 return unblock_http3_consumer_shift_queue(
2622 aTHX_
2623 context->connection_hv,
2624 "ready_informational",
2625 19
2626 );
2627 }
2628  
2629 static HV *
2630 5 unblock_http3_consumer_transaction_hv(
2631 pTHX_ SV *transaction
2632 )
2633 {
2634 5 return unblock_http3_consumer_exact_object(
2635 aTHX_
2636 transaction,
2637 "Unblock::HTTP3::Transaction"
2638 );
2639 }
2640  
2641 static SV *
2642 1 unblock_http3_consumer_transaction_next_informational(
2643 pTHX_ SV *transaction
2644 )
2645 {
2646 1 HV *hv = unblock_http3_consumer_transaction_hv(
2647 aTHX_ transaction
2648 );
2649  
2650 1 return unblock_http3_consumer_shift_queue(
2651 aTHX_
2652 hv,
2653 "informational",
2654 13
2655 );
2656 }
2657  
2658 static SV *
2659 1 unblock_http3_consumer_transaction_request(
2660 pTHX_ SV *transaction
2661 )
2662 {
2663 1 HV *hv = unblock_http3_consumer_transaction_hv(
2664 aTHX_ transaction
2665 );
2666  
2667 1 return unblock_http3_consumer_fetch(
2668 aTHX_ hv,
2669 "request",
2670 7,
2671 1
2672 );
2673 }
2674  
2675 static SV *
2676 1 unblock_http3_consumer_transaction_response(
2677 pTHX_ SV *transaction
2678 )
2679 {
2680 1 HV *hv = unblock_http3_consumer_transaction_hv(
2681 aTHX_ transaction
2682 );
2683 1 SV *response = unblock_http3_consumer_fetch(
2684 aTHX_ hv,
2685 "response",
2686 8,
2687 1
2688 );
2689  
2690 1 ▶ return SvOK(response) ? response : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvOK(response) ? response : NULL; 1 0 1
2691 }
2692  
2693 static int64_t
2694 1 unblock_http3_consumer_transaction_stream_id(
2695 pTHX_ SV *transaction
2696 )
2697 {
2698 1 HV *hv = unblock_http3_consumer_transaction_hv(
2699 aTHX_ transaction
2700 );
2701 1 SV *stream_id = unblock_http3_consumer_fetch(
2702 aTHX_ hv,
2703 "stream_id",
2704 9,
2705 1
2706 );
2707  
2708 1 return (int64_t)SvIV(stream_id);
2709 }
2710  
2711 static uint32_t
2712 1 unblock_http3_consumer_transaction_state(
2713 pTHX_ SV *transaction
2714 )
2715 {
2716 1 HV *hv = unblock_http3_consumer_transaction_hv(
2717 aTHX_ transaction
2718 );
2719 1 SV *state_sv = unblock_http3_consumer_fetch(
2720 aTHX_ hv,
2721 "state",
2722 5,
2723 1
2724 );
2725 STRLEN len;
2726 1 const char *state = SvPV(state_sv, len);
2727  
2728 1 ▶ if (len == 6 && memcmp(state, "active", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 6 && memcmp(state, "active", 6) == 0) { 1 0 1
if (len == 6 && memcmp(state, "active", 6) == 0) { 1 0 1
2729 1 return UB_HTTP3_TX_ACTIVE;
2730 }
2731  
2732 0 ▶ if (len == 8 && memcmp(state, "complete", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 8 && memcmp(state, "complete", 8) == 0) { 0 0 0
if (len == 8 && memcmp(state, "complete", 8) == 0) { 0 0 0
2733 0 return UB_HTTP3_TX_COMPLETE;
2734 }
2735  
2736 0 ▶ if (len == 9 && memcmp(state, "cancelled", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 9 && memcmp(state, "cancelled", 9) == 0) { 0 0 0
if (len == 9 && memcmp(state, "cancelled", 9) == 0) { 0 0 0
2737 0 return UB_HTTP3_TX_CANCELLED;
2738 }
2739  
2740 0 ▶ if (len == 5 && memcmp(state, "error", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 5 && memcmp(state, "error", 5) == 0) { 0 0 0
if (len == 5 && memcmp(state, "error", 5) == 0) { 0 0 0
2741 0 return UB_HTTP3_TX_ERROR;
2742 }
2743  
2744 0 croak("Unblock::HTTP3 native ABI encountered an unknown Transaction state");
2745 return UB_HTTP3_TX_ERROR;
2746 }
2747  
2748 static void
2749 0 unblock_http3_consumer_require_transaction_owner(
2750 pTHX_
2751 unblock_http3_consumer_context *context,
2752 SV *transaction
2753 )
2754 {
2755 0 HV *hv = unblock_http3_consumer_transaction_hv(
2756 aTHX_ transaction
2757 );
2758 0 SV *owner = unblock_http3_consumer_fetch(
2759 aTHX_ hv,
2760 "connection",
2761 10,
2762 1
2763 );
2764  
2765 0 if (
2766 0 ▶ !SvOK(owner)
Branch: 1 branch
BranchTrueFalseTotal
!SvOK(owner) 0 0 0
2767 0 ▶ || !SvROK(owner)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(owner) 0 0 0
2768 0 ▶ || SvRV(owner) != SvRV(context->connection)
Branch: 1 branch
BranchTrueFalseTotal
|| SvRV(owner) != SvRV(context->connection) 0 0 0
2769 ) {
2770 0 croak("Transaction does not belong to this Unblock::HTTP3 Connection");
2771 }
2772 0 }
2773  
2774 static void
2775 0 unblock_http3_consumer_respond(
2776 pTHX_
2777 void *opaque,
2778 SV *transaction
2779 )
2780 {
2781 unblock_http3_consumer_context *context =
2782 0 unblock_http3_consumer_require_context(aTHX_ opaque);
2783 HV *transaction_hv;
2784 SV *response;
2785  
2786 0 unblock_http3_consumer_require_transaction_owner(
2787 aTHX_ context,
2788 transaction
2789 );
2790  
2791 0 transaction_hv = unblock_http3_consumer_exact_object(
2792 aTHX_ transaction,
2793 "Unblock::HTTP3::Transaction"
2794 );
2795 0 response = unblock_http3_consumer_fetch(
2796 aTHX_ transaction_hv,
2797 "response",
2798 8,
2799 1
2800 );
2801  
2802 0 ▶ if (!SvOK(response)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(response)) { 0 0 0
2803 0 croak("HTTP/3 Transaction has no response");
2804 }
2805  
2806 0 unblock_http3_consumer_call_void_2(
2807 aTHX_
2808 context->respond_cv,
2809 transaction,
2810 response
2811 );
2812 0 }
2813  
2814 static void
2815 0 unblock_http3_consumer_send_informational(
2816 pTHX_
2817 void *opaque,
2818 SV *transaction,
2819 SV *response
2820 )
2821 {
2822 unblock_http3_consumer_context *context =
2823 0 unblock_http3_consumer_require_context(aTHX_ opaque);
2824  
2825 0 unblock_http3_consumer_require_transaction_owner(
2826 aTHX_ context,
2827 transaction
2828 );
2829  
2830 0 unblock_http3_consumer_call_void_2(
2831 aTHX_
2832 context->send_informational_cv,
2833 transaction,
2834 response
2835 );
2836 0 }
2837  
2838 static int
2839 1 unblock_http3_consumer_failed(
2840 pTHX_ void *opaque
2841 )
2842 {
2843 unblock_http3_consumer_context *context =
2844 1 unblock_http3_consumer_require_context(aTHX_ opaque);
2845 1 SV *failed = unblock_http3_consumer_fetch(
2846 aTHX_ context->connection_hv,
2847 "failed",
2848 6,
2849 1
2850 );
2851  
2852 1 return SvTRUE(failed) ? 1 : 0;
2853 }
2854  
2855 static int
2856 1 unblock_http3_consumer_error_code(
2857 pTHX_
2858 void *opaque,
2859 uint64_t *code
2860 )
2861 {
2862 unblock_http3_consumer_context *context =
2863 1 unblock_http3_consumer_require_context(aTHX_ opaque);
2864 1 SV *value = unblock_http3_consumer_fetch(
2865 aTHX_ context->connection_hv,
2866 "error_code",
2867 10,
2868 1
2869 );
2870  
2871 1 ▶ if (!SvOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(value)) { 0 1 1
2872 0 return 0;
2873 }
2874  
2875 1 ▶ if (code == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (code == NULL) { 0 1 1
2876 0 croak("Unblock::HTTP3 native ABI error-code output is NULL");
2877 }
2878  
2879 1 *code = (uint64_t)SvUV(value);
2880 1 return 1;
2881 }
2882  
2883 static SV *
2884 1 unblock_http3_consumer_error(
2885 pTHX_ void *opaque
2886 )
2887 {
2888 unblock_http3_consumer_context *context =
2889 1 unblock_http3_consumer_require_context(aTHX_ opaque);
2890 1 SV *value = unblock_http3_consumer_fetch(
2891 aTHX_ context->connection_hv,
2892 "error",
2893 5,
2894 1
2895 );
2896  
2897 1 ▶ return SvOK(value) ? value : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvOK(value) ? value : NULL; 1 0 1
2898 }
2899  
2900 static void
2901 4 unblock_http3_consumer_destroy(
2902 pTHX_ void *opaque
2903 )
2904 {
2905 4 unblock_http3_consumer_context *context =
2906 (unblock_http3_consumer_context *)opaque;
2907  
2908 4 ▶ if (context == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) { 0 4 4
2909 0 return;
2910 }
2911  
2912 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 4 4
2913 4 context->interpreter != unblock_http3_consumer_interpreter(aTHX)
2914 ) {
2915 0 croak("Unblock::HTTP3 native consumer context belongs to another interpreter");
2916 }
2917  
2918 4 ▶ if (context->connection != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->connection != NULL) { 4 0 4
2919 4 SvREFCNT_dec(context->connection);
2920 4 context->connection = NULL;
2921 }
2922  
2923 4 context->connection_hv = NULL;
2924 4 context->service_cv = NULL;
2925 4 context->request_cv = NULL;
2926 4 context->respond_cv = NULL;
2927 4 context->send_informational_cv = NULL;
2928 4 context->interpreter = NULL;
2929  
2930 4 Safefree(context);
2931 }
2932  
2933 static const ub_http3_consumer_ops_v1
2934 unblock_http3_consumer_operations = {
2935 UB_HTTP3_CONSUMER_ABI_VERSION,
2936 sizeof(ub_http3_consumer_ops_v1),
2937 "Unblock::HTTP3 consumer ABI v1",
2938 &unblock_http3_consumer_create,
2939 &unblock_http3_consumer_service,
2940 &unblock_http3_consumer_request,
2941 &unblock_http3_consumer_next_transaction,
2942 &unblock_http3_consumer_next_informational,
2943 &unblock_http3_consumer_transaction_next_informational,
2944 &unblock_http3_consumer_transaction_request,
2945 &unblock_http3_consumer_transaction_response,
2946 &unblock_http3_consumer_transaction_stream_id,
2947 &unblock_http3_consumer_transaction_state,
2948 &unblock_http3_consumer_respond,
2949 &unblock_http3_consumer_send_informational,
2950 &unblock_http3_consumer_failed,
2951 &unblock_http3_consumer_error_code,
2952 &unblock_http3_consumer_error,
2953 &unblock_http3_consumer_destroy
2954 };
2955  
2956 static nghttp3_nv *
2957 4 unblock_http3_fields_from_sv(SV *fields_sv, size_t *pnvlen)
2958 {
2959 AV *fields;
2960 SSize_t last;
2961 size_t nvlen;
2962 nghttp3_nv *nva;
2963 size_t i;
2964  
2965 4 ▶ if (!SvROK(fields_sv) || SvTYPE(SvRV(fields_sv)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(fields_sv) || SvTYPE(SvRV(fields_sv)) != SVt_PVAV) { 4 0 4
if (!SvROK(fields_sv) || SvTYPE(SvRV(fields_sv)) != SVt_PVAV) { 0 4 4
2966 0 croak("HTTP fields must be an array reference");
2967 }
2968  
2969 4 fields = (AV *)SvRV(fields_sv);
2970 4 last = av_len(fields);
2971 4 ▶ nvlen = last < 0 ? 0 : (size_t)last + 1;
Branch: 1 branch
BranchTrueFalseTotal
nvlen = last < 0 ? 0 : (size_t)last + 1; 4 0 4
2972  
2973 4 ▶ if (nvlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nvlen == 0) { 0 4 4
2974 0 *pnvlen = 0;
2975 0 return NULL;
2976 }
2977  
2978 4 ▶ Newxz(nva, nvlen, nghttp3_nv);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(nva, nvlen, nghttp3_nv); 0 4 4
2979  
2980 22 ▶ for (i = 0; i < nvlen; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nvlen; ++i) { 18 4 22
2981 18 SV **fieldp = av_fetch(fields, (SSize_t)i, 0);
2982 AV *field;
2983 SV **namep;
2984 SV **valuep;
2985 STRLEN namelen;
2986 STRLEN valuelen;
2987 const char *name;
2988 const char *value;
2989  
2990 18 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 18 0 18
2991 18 fieldp == NULL ||
2992 18 ▶ !SvROK(*fieldp) ||
Branch: 1 branch
BranchTrueFalseTotal
!SvROK(*fieldp) || 18 0 18
2993 18 ▶ SvTYPE(SvRV(*fieldp)) != SVt_PVAV
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(*fieldp)) != SVt_PVAV 0 18 18
2994 ) {
2995 0 Safefree(nva);
2996 0 croak("each HTTP field must be a two-element array reference");
2997 }
2998  
2999 18 field = (AV *)SvRV(*fieldp);
3000  
3001 18 ▶ if (av_len(field) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(field) != 1) { 0 18 18
3002 0 Safefree(nva);
3003 0 croak("each HTTP field must contain exactly a name and value");
3004 }
3005  
3006 18 namep = av_fetch(field, 0, 0);
3007 18 valuep = av_fetch(field, 1, 0);
3008  
3009 18 ▶ if (namep == NULL || valuep == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (namep == NULL || valuep == NULL) { 18 0 18
if (namep == NULL || valuep == NULL) { 0 18 18
3010 0 Safefree(nva);
3011 0 croak("HTTP field name and value are required");
3012 }
3013  
3014 18 name = SvPVbyte(*namep, namelen);
3015 18 ▶ value = SvPVbyte(*valuep, valuelen);
Branch: 1 branch
BranchTrueFalseTotal
value = SvPVbyte(*valuep, valuelen); 18 0 18
3016  
3017 18 nva[i].name = (const uint8_t *)name;
3018 18 nva[i].value = (const uint8_t *)value;
3019 18 nva[i].namelen = (size_t)namelen;
3020 18 nva[i].valuelen = (size_t)valuelen;
3021 18 nva[i].flags = NGHTTP3_NV_FLAG_NONE;
3022 }
3023  
3024 4 *pnvlen = nvlen;
3025 4 return nva;
3026 }
3027  
3028 MODULE = Unblock::HTTP3 PACKAGE = Unblock::HTTP3::_Native
3029  
3030 PROTOTYPES: DISABLE
3031  
3032 BOOT:
3033 MY_CXT_INIT;
3034 22 ▶ if (!uhttp_native_init(
Branch: 1 branch
BranchTrueFalseTotal
if (!uhttp_native_init( 0 22 22
3035 aTHX_ &MY_CXT.uniform_api,
3036 UHTTP_NATIVE_ABI_VERSION
3037 ))
3038 0 croak("Uniform::HTTP native FastPath ABI mismatch");
3039  
3040 void
3041 CLONE(...)
3042 CODE:
3043 MY_CXT_CLONE;
3044 0 ▶ if (!uhttp_native_init(
Branch: 1 branch
BranchTrueFalseTotal
if (!uhttp_native_init( 0 0 0
3045 aTHX_ &MY_CXT.uniform_api,
3046 UHTTP_NATIVE_ABI_VERSION
3047 ))
3048 0 croak("Uniform::HTTP native FastPath clone ABI mismatch");
3049  
3050  
3051 UV
3052 _consumer_operations_address()
3053 CODE:
3054 2 ▶ RETVAL = PTR2UV(&unblock_http3_consumer_operations);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&unblock_http3_consumer_operations); 0 2 2
3055 OUTPUT:
3056 RETVAL
3057  
3058 UV
3059 _consumer_operations_size()
3060 CODE:
3061 3 ▶ RETVAL = (UV)sizeof(ub_http3_consumer_ops_v1);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)sizeof(ub_http3_consumer_ops_v1); 0 3 3
3062 OUTPUT:
3063 RETVAL
3064  
3065 int
3066 _consumer_context_probe(connection)
3067 SV *connection
3068 PREINIT:
3069 void *context;
3070 CODE:
3071 4 context = unblock_http3_consumer_operations.create(
3072 aTHX_ connection
3073 );
3074 3 unblock_http3_consumer_operations.destroy(
3075 aTHX_ context
3076 );
3077 3 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 3 3
3078 OUTPUT:
3079 RETVAL
3080  
3081  
3082 SV *
3083 _consumer_transaction_probe(transaction)
3084 SV *transaction
3085 PREINIT:
3086 AV *out;
3087 SV *request;
3088 SV *response;
3089 SV *informational;
3090 CODE:
3091 request =
3092 1 unblock_http3_consumer_operations.transaction_request(
3093 aTHX_ transaction
3094 );
3095 response =
3096 1 unblock_http3_consumer_operations.transaction_response(
3097 aTHX_ transaction
3098 );
3099 informational =
3100 1 unblock_http3_consumer_operations.transaction_next_informational(
3101 aTHX_ transaction
3102 );
3103  
3104 1 out = newAV();
3105 1 av_push(
3106 out,
3107 newSViv(
3108 (IV)unblock_http3_consumer_operations.transaction_stream_id(
3109 aTHX_ transaction
3110 )
3111 )
3112 );
3113 1 av_push(
3114 out,
3115 newSVuv(
3116 (UV)unblock_http3_consumer_operations.transaction_state(
3117 aTHX_ transaction
3118 )
3119 )
3120 );
3121 1 av_push(out, newSVsv(request));
3122 1 ▶ av_push(
Branch: 1 branch
BranchTrueFalseTotal
av_push( 0 1 1
3123 out,
3124 response == NULL
3125 ? newSV(0)
3126 : newSVsv(response)
3127 );
3128 1 ▶ av_push(
Branch: 1 branch
BranchTrueFalseTotal
av_push( 0 1 1
3129 out,
3130 informational == NULL
3131 ? newSV(0)
3132 : informational
3133 );
3134  
3135 1 RETVAL = newRV_noinc((SV *)out);
3136 OUTPUT:
3137 RETVAL
3138  
3139  
3140 SV *
3141 _consumer_queue_probe(connection)
3142 SV *connection
3143 PREINIT:
3144 void *context;
3145 SV *transaction;
3146 SV *informational;
3147 SV *error;
3148 uint64_t code;
3149 int has_code;
3150 AV *out;
3151 CODE:
3152 1 context = unblock_http3_consumer_operations.create(
3153 aTHX_ connection
3154 );
3155  
3156 transaction =
3157 1 unblock_http3_consumer_operations.next_transaction(
3158 aTHX_ context
3159 );
3160 informational =
3161 1 unblock_http3_consumer_operations.next_informational(
3162 aTHX_ context
3163 );
3164 has_code =
3165 1 unblock_http3_consumer_operations.error_code(
3166 aTHX_ context,
3167 &code
3168 );
3169 error =
3170 1 unblock_http3_consumer_operations.error(
3171 aTHX_ context
3172 );
3173  
3174 1 out = newAV();
3175 1 ▶ av_push(
Branch: 1 branch
BranchTrueFalseTotal
av_push( 0 1 1
3176 out,
3177 transaction == NULL
3178 ? newSV(0)
3179 : transaction
3180 );
3181 1 ▶ av_push(
Branch: 1 branch
BranchTrueFalseTotal
av_push( 0 1 1
3182 out,
3183 informational == NULL
3184 ? newSV(0)
3185 : informational
3186 );
3187 1 av_push(
3188 out,
3189 newSViv(
3190 unblock_http3_consumer_operations.failed(
3191 aTHX_ context
3192 )
3193 )
3194 );
3195 1 av_push(out, newSViv(has_code ? 1 : 0));
3196 1 ▶ av_push(
Branch: 1 branch
BranchTrueFalseTotal
av_push( 1 0 1
3197 out,
3198 has_code
3199 ? newSVuv((UV)code)
3200 : newSV(0)
3201 );
3202 1 ▶ av_push(
Branch: 1 branch
BranchTrueFalseTotal
av_push( 0 1 1
3203 out,
3204 error == NULL
3205 ? newSV(0)
3206 : newSVsv(error)
3207 );
3208  
3209 1 unblock_http3_consumer_operations.destroy(
3210 aTHX_ context
3211 );
3212  
3213 1 RETVAL = newRV_noinc((SV *)out);
3214 OUTPUT:
3215 RETVAL
3216  
3217  
3218 SV *
3219 _parse_priority(value)
3220 SV *value
3221 PREINIT:
3222 STRLEN len;
3223 const uint8_t *bytes;
3224 nghttp3_pri pri;
3225 AV *out;
3226 int rv;
3227 CODE:
3228 6 bytes = (const uint8_t *)SvPVbyte(value, len);
3229  
3230 6 pri.urgency = NGHTTP3_DEFAULT_URGENCY;
3231 6 pri.inc = 0;
3232  
3233 6 rv = nghttp3_pri_parse_priority(
3234 &pri,
3235 bytes,
3236 (size_t)len
3237 );
3238  
3239 6 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 1 5 6
3240 1 croak("invalid RFC 9218 Priority field");
3241 }
3242  
3243 5 out = newAV();
3244 5 av_push(out, newSVuv((UV)pri.urgency));
3245 5 av_push(out, newSViv(pri.inc ? 1 : 0));
3246  
3247 5 RETVAL = newRV_noinc((SV *)out);
3248 OUTPUT:
3249 RETVAL
3250  
3251 const char *
3252 _nghttp3_version()
3253 PREINIT:
3254 const nghttp3_info *info;
3255 CODE:
3256 1 info = nghttp3_version(0);
3257 1 ▶ if (info == NULL || info->version_str == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (info == NULL || info->version_str == NULL) { 1 0 1
if (info == NULL || info->version_str == NULL) { 0 1 1
3258 0 croak("could not read libnghttp3 version");
3259 }
3260 1 RETVAL = info->version_str;
3261 OUTPUT:
3262 RETVAL
3263  
3264 SV *
3265 _new_client(max_field_section_size, qpack_max_table_capacity, qpack_blocked_streams, enable_connect_protocol, h3_datagram)
3266 UV max_field_section_size
3267 UV qpack_max_table_capacity
3268 UV qpack_blocked_streams
3269 IV enable_connect_protocol
3270 IV h3_datagram
3271 CODE:
3272 23 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 23 0 23
3273 23 ▶ (uint64_t)max_field_section_size > UNBLOCK_HTTP3_MAX_VARINT ||
Branch: 1 branch
BranchTrueFalseTotal
(uint64_t)max_field_section_size > UNBLOCK_HTTP3_MAX_VARINT || 22 1 23
3274 22 ▶ (uint64_t)qpack_max_table_capacity > UNBLOCK_HTTP3_MAX_VARINT ||
Branch: 1 branch
BranchTrueFalseTotal
(uint64_t)qpack_max_table_capacity > UNBLOCK_HTTP3_MAX_VARINT || 0 22 22
3275 (uint64_t)qpack_blocked_streams > UNBLOCK_HTTP3_MAX_VARINT
3276 ) {
3277 1 croak("HTTP/3 setting exceeds varint maximum");
3278 }
3279  
3280 22 RETVAL = unblock_http3_new_conn(
3281 0,
3282 (uint64_t)max_field_section_size,
3283 (uint64_t)qpack_max_table_capacity,
3284 (uint64_t)qpack_blocked_streams,
3285 enable_connect_protocol ? 1 : 0,
3286 h3_datagram ? 1 : 0,
3287 &PL_sv_undef
3288 );
3289 OUTPUT:
3290 RETVAL
3291  
3292 SV *
3293 _new_server(max_field_section_size, qpack_max_table_capacity, qpack_blocked_streams, enable_connect_protocol, h3_datagram, origin_list)
3294 UV max_field_section_size
3295 UV qpack_max_table_capacity
3296 UV qpack_blocked_streams
3297 IV enable_connect_protocol
3298 IV h3_datagram
3299 SV *origin_list
3300 CODE:
3301 38 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 38 0 38
3302 38 ▶ (uint64_t)max_field_section_size > UNBLOCK_HTTP3_MAX_VARINT ||
Branch: 1 branch
BranchTrueFalseTotal
(uint64_t)max_field_section_size > UNBLOCK_HTTP3_MAX_VARINT || 38 0 38
3303 38 ▶ (uint64_t)qpack_max_table_capacity > UNBLOCK_HTTP3_MAX_VARINT ||
Branch: 1 branch
BranchTrueFalseTotal
(uint64_t)qpack_max_table_capacity > UNBLOCK_HTTP3_MAX_VARINT || 0 38 38
3304 (uint64_t)qpack_blocked_streams > UNBLOCK_HTTP3_MAX_VARINT
3305 ) {
3306 0 croak("HTTP/3 setting exceeds varint maximum");
3307 }
3308  
3309 38 RETVAL = unblock_http3_new_conn(
3310 1,
3311 (uint64_t)max_field_section_size,
3312 (uint64_t)qpack_max_table_capacity,
3313 (uint64_t)qpack_blocked_streams,
3314 enable_connect_protocol ? 1 : 0,
3315 h3_datagram ? 1 : 0,
3316 origin_list
3317 );
3318 OUTPUT:
3319 RETVAL
3320  
3321 MODULE = Unblock::HTTP3 PACKAGE = Unblock::HTTP3::_Native::Connection
3322  
3323 SV *
3324 header_pseudo(self, stream_id, name)
3325 SV *self
3326 IV stream_id
3327 SV *name
3328 PREINIT:
3329 unblock_http3_native_conn *native;
3330 unblock_http3_header_block *block;
3331 STRLEN namelen;
3332 const char *name_bytes;
3333 const unblock_http3_captured_field *field;
3334 CODE:
3335 166 native = unblock_http3_conn_from_sv(self);
3336 166 block = unblock_http3_header_block_find(
3337 native,
3338 (int64_t)stream_id
3339 );
3340  
3341 166 ▶ if (block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block == NULL) { 0 166 166
3342 0 croak("HTTP/3 stream has no pending header block");
3343 }
3344  
3345 166 name_bytes = SvPVbyte(name, namelen);
3346 166 field = unblock_http3_header_block_pseudo(
3347 block,
3348 name_bytes,
3349 (size_t)namelen
3350 );
3351  
3352 166 ▶ if (field == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (field == NULL) { 26 140 166
3353 26 XSRETURN_UNDEF;
3354 }
3355  
3356 140 RETVAL = newSVpvn(
3357 (const char *)field->value,
3358 (STRLEN)field->valuelen
3359 );
3360 OUTPUT:
3361 RETVAL
3362  
3363 SV *
3364 header_values(self, stream_id, name)
3365 SV *self
3366 IV stream_id
3367 SV *name
3368 PREINIT:
3369 unblock_http3_native_conn *native;
3370 unblock_http3_header_block *block;
3371 STRLEN namelen;
3372 const char *name_bytes;
3373 AV *values;
3374 size_t i;
3375 CODE:
3376 28 native = unblock_http3_conn_from_sv(self);
3377 28 block = unblock_http3_header_block_find(
3378 native,
3379 (int64_t)stream_id
3380 );
3381  
3382 28 ▶ if (block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block == NULL) { 0 28 28
3383 0 croak("HTTP/3 stream has no pending header block");
3384 }
3385  
3386 28 name_bytes = SvPVbyte(name, namelen);
3387 28 values = newAV();
3388  
3389 152 ▶ for (i = 0; i < block->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; ++i) { 124 28 152
3390 124 const unblock_http3_captured_field *field =
3391 124 &block->fields[i];
3392  
3393 124 if (
3394 124 ▶ field->namelen != 0
Branch: 1 branch
BranchTrueFalseTotal
field->namelen != 0 124 0 124
3395 124 ▶ && field->name[0] == ':'
Branch: 1 branch
BranchTrueFalseTotal
&& field->name[0] == ':' 114 10 124
3396 ) {
3397 114 continue;
3398 }
3399  
3400 10 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 1 9 10
3401 10 unblock_http3_captured_name_equal_ci(
3402 field,
3403 name_bytes,
3404 (size_t)namelen
3405 )
3406 ) {
3407 1 av_push(
3408 values,
3409 newSVpvn(
3410 (const char *)field->value,
3411 (STRLEN)field->valuelen
3412 )
3413 );
3414 }
3415 }
3416  
3417 28 RETVAL = newRV_noinc((SV *)values);
3418 OUTPUT:
3419 RETVAL
3420  
3421 int
3422 has_header(self, stream_id, name)
3423 SV *self
3424 IV stream_id
3425 SV *name
3426 PREINIT:
3427 unblock_http3_native_conn *native;
3428 unblock_http3_header_block *block;
3429 STRLEN namelen;
3430 const char *name_bytes;
3431 size_t i;
3432 CODE:
3433 26 native = unblock_http3_conn_from_sv(self);
3434 26 block = unblock_http3_header_block_find(
3435 native,
3436 (int64_t)stream_id
3437 );
3438  
3439 26 ▶ if (block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block == NULL) { 0 26 26
3440 0 croak("HTTP/3 stream has no pending header block");
3441 }
3442  
3443 26 name_bytes = SvPVbyte(name, namelen);
3444 26 RETVAL = 0;
3445  
3446 59 ▶ for (i = 0; i < block->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; ++i) { 35 24 59
3447 35 const unblock_http3_captured_field *field =
3448 35 &block->fields[i];
3449  
3450 35 if (
3451 35 ▶ field->namelen != 0
Branch: 1 branch
BranchTrueFalseTotal
field->namelen != 0 35 0 35
3452 35 ▶ && field->name[0] == ':'
Branch: 1 branch
BranchTrueFalseTotal
&& field->name[0] == ':' 26 9 35
3453 ) {
3454 26 continue;
3455 }
3456  
3457 9 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 2 7 9
3458 9 unblock_http3_captured_name_equal_ci(
3459 field,
3460 name_bytes,
3461 (size_t)namelen
3462 )
3463 ) {
3464 2 RETVAL = 1;
3465 2 break;
3466 }
3467 }
3468 OUTPUT:
3469 RETVAL
3470  
3471 SV *
3472 receive_uniform_request(self, stream_id, method, target, scheme, authority, protocol, fin)
3473 SV *self
3474 IV stream_id
3475 SV *method
3476 SV *target
3477 SV *scheme
3478 SV *authority
3479 SV *protocol
3480 IV fin
3481 PREINIT:
3482 unblock_http3_native_conn *native;
3483 unblock_http3_header_block *block;
3484 unblock_http3_header_block *removed;
3485 CODE:
3486 27 native = unblock_http3_conn_from_sv(self);
3487 27 block = unblock_http3_header_block_find(
3488 native,
3489 (int64_t)stream_id
3490 );
3491  
3492 27 ▶ if (block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block == NULL) { 0 27 27
3493 0 croak("HTTP/3 stream has no pending header block");
3494 }
3495  
3496 27 RETVAL = unblock_http3_header_block_uniform_request(
3497 aTHX_ block,
3498 method,
3499 target,
3500 scheme,
3501 authority,
3502 protocol,
3503 fin ? 1 : 0
3504 );
3505  
3506 27 removed = unblock_http3_header_block_remove(
3507 native,
3508 (int64_t)stream_id
3509 );
3510 27 unblock_http3_header_block_free(removed);
3511 OUTPUT:
3512 RETVAL
3513  
3514 SV *
3515 receive_uniform_response(self, stream_id, status, fin)
3516 SV *self
3517 IV stream_id
3518 IV status
3519 IV fin
3520 PREINIT:
3521 unblock_http3_native_conn *native;
3522 unblock_http3_header_block *block;
3523 unblock_http3_header_block *removed;
3524 CODE:
3525 26 native = unblock_http3_conn_from_sv(self);
3526 26 block = unblock_http3_header_block_find(
3527 native,
3528 (int64_t)stream_id
3529 );
3530  
3531 26 ▶ if (block == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (block == NULL) { 0 26 26
3532 0 croak("HTTP/3 stream has no pending header block");
3533 }
3534  
3535 26 RETVAL = unblock_http3_header_block_uniform_response(
3536 aTHX_ block,
3537 status,
3538 fin ? 1 : 0
3539 );
3540  
3541 26 removed = unblock_http3_header_block_remove(
3542 native,
3543 (int64_t)stream_id
3544 );
3545 26 unblock_http3_header_block_free(removed);
3546 OUTPUT:
3547 RETVAL
3548  
3549 void
3550 discard_header_block(self, stream_id)
3551 SV *self
3552 IV stream_id
3553 PREINIT:
3554 unblock_http3_native_conn *native;
3555 unblock_http3_header_block *block;
3556 CODE:
3557 2 native = unblock_http3_conn_from_sv(self);
3558 2 block = unblock_http3_header_block_remove(
3559 native,
3560 (int64_t)stream_id
3561 );
3562 2 unblock_http3_header_block_free(block);
3563  
3564 const char *
3565 role(self)
3566 SV *self
3567 PREINIT:
3568 unblock_http3_native_conn *native;
3569 CODE:
3570 3 native = unblock_http3_conn_from_sv(self);
3571 2 ▶ RETVAL = native->is_server ? "server" : "client";
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = native->is_server ? "server" : "client"; 1 1 2
3572 OUTPUT:
3573 RETVAL
3574  
3575 void
3576 set_max_client_streams_bidi(self, max_streams)
3577 SV *self
3578 UV max_streams
3579 PREINIT:
3580 unblock_http3_native_conn *native;
3581 CODE:
3582 39 native = unblock_http3_conn_from_sv(self);
3583  
3584 39 ▶ if (!native->is_server) {
Branch: 1 branch
BranchTrueFalseTotal
if (!native->is_server) { 0 39 39
3585 0 croak("client stream limit synchronization requires a server connection");
3586 }
3587  
3588 39 nghttp3_conn_set_max_client_streams_bidi(
3589 native->conn,
3590 (uint64_t)max_streams
3591 );
3592  
3593 void
3594 set_client_stream_priority(self, stream_id, value)
3595 SV *self
3596 IV stream_id
3597 SV *value
3598 PREINIT:
3599 unblock_http3_native_conn *native;
3600 STRLEN len;
3601 const uint8_t *bytes;
3602 int rv;
3603 CODE:
3604 2 native = unblock_http3_conn_from_sv(self);
3605  
3606 2 ▶ if (native->is_server) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->is_server) { 0 2 2
3607 0 croak("client stream priority update requires a client connection");
3608 }
3609  
3610 2 bytes = (const uint8_t *)SvPVbyte(value, len);
3611  
3612 2 rv = nghttp3_conn_set_client_stream_priority(
3613 native->conn,
3614 (int64_t)stream_id,
3615 bytes,
3616 (size_t)len
3617 );
3618  
3619 2 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 2 2
3620 0 unblock_http3_fail("could not update client HTTP priority", rv);
3621 }
3622  
3623 SV *
3624 get_server_stream_priority(self, stream_id)
3625 SV *self
3626 IV stream_id
3627 PREINIT:
3628 unblock_http3_native_conn *native;
3629 nghttp3_pri pri;
3630 AV *out;
3631 int rv;
3632 CODE:
3633 9 native = unblock_http3_conn_from_sv(self);
3634  
3635 9 ▶ if (!native->is_server) {
Branch: 1 branch
BranchTrueFalseTotal
if (!native->is_server) { 0 9 9
3636 0 croak("stream priority inspection requires a server connection");
3637 }
3638  
3639 9 rv = nghttp3_conn_get_stream_priority2(
3640 native->conn,
3641 &pri,
3642 (int64_t)stream_id
3643 );
3644  
3645 9 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 9 9
3646 0 unblock_http3_fail("could not inspect HTTP stream priority", rv);
3647 }
3648  
3649 9 out = newAV();
3650 9 av_push(out, newSVuv((UV)pri.urgency));
3651 9 av_push(out, newSViv(pri.inc ? 1 : 0));
3652  
3653 9 RETVAL = newRV_noinc((SV *)out);
3654 OUTPUT:
3655 RETVAL
3656  
3657 void
3658 set_server_stream_priority(self, stream_id, urgency, incremental)
3659 SV *self
3660 IV stream_id
3661 UV urgency
3662 IV incremental
3663 PREINIT:
3664 unblock_http3_native_conn *native;
3665 nghttp3_pri pri;
3666 int rv;
3667 CODE:
3668 1 native = unblock_http3_conn_from_sv(self);
3669  
3670 1 ▶ if (!native->is_server) {
Branch: 1 branch
BranchTrueFalseTotal
if (!native->is_server) { 0 1 1
3671 0 croak("server stream priority override requires a server connection");
3672 }
3673  
3674 1 ▶ if (urgency > NGHTTP3_URGENCY_LOW) {
Branch: 1 branch
BranchTrueFalseTotal
if (urgency > NGHTTP3_URGENCY_LOW) { 0 1 1
3675 0 croak("HTTP urgency must be from 0 through 7");
3676 }
3677  
3678 1 ▶ if (incremental != 0 && incremental != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (incremental != 0 && incremental != 1) { 1 0 1
if (incremental != 0 && incremental != 1) { 0 1 1
3679 0 croak("HTTP incremental priority must be 0 or 1");
3680 }
3681  
3682 1 pri.urgency = (uint32_t)urgency;
3683 1 pri.inc = incremental ? 1 : 0;
3684  
3685 1 rv = nghttp3_conn_set_server_stream_priority(
3686 native->conn,
3687 (int64_t)stream_id,
3688 &pri
3689 );
3690  
3691 1 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 1 1
3692 0 unblock_http3_fail("could not override server HTTP priority", rv);
3693 }
3694  
3695 void
3696 bind_streams(self, control_stream_id, qenc_stream_id, qdec_stream_id)
3697 SV *self
3698 IV control_stream_id
3699 IV qenc_stream_id
3700 IV qdec_stream_id
3701 PREINIT:
3702 unblock_http3_native_conn *native;
3703 int rv;
3704 CODE:
3705 27 native = unblock_http3_conn_from_sv(self);
3706  
3707 27 rv = nghttp3_conn_bind_control_stream(
3708 native->conn,
3709 (int64_t)control_stream_id
3710 );
3711 27 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 27 27
3712 0 unblock_http3_fail("could not bind HTTP/3 control stream", rv);
3713 }
3714  
3715 27 rv = nghttp3_conn_bind_qpack_streams(
3716 native->conn,
3717 (int64_t)qenc_stream_id,
3718 (int64_t)qdec_stream_id
3719 );
3720 27 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 27 27
3721 0 unblock_http3_fail("could not bind HTTP/3 QPACK streams", rv);
3722 }
3723  
3724 SV *
3725 read_stream(self, stream_id, bytes, fin, timestamp)
3726 SV *self
3727 IV stream_id
3728 SV *bytes
3729 IV fin
3730 UV timestamp
3731 PREINIT:
3732 unblock_http3_native_conn *native;
3733 STRLEN len;
3734 const char *data;
3735 nghttp3_ssize rv;
3736 AV *out;
3737 uint64_t app_error_code;
3738 CODE:
3739 270 native = unblock_http3_conn_from_sv(self);
3740 270 data = SvPVbyte(bytes, len);
3741  
3742 270 rv = nghttp3_conn_read_stream2(
3743 native->conn,
3744 (int64_t)stream_id,
3745 (const uint8_t *)data,
3746 (size_t)len,
3747 fin ? 1 : 0,
3748 (nghttp3_tstamp)timestamp
3749 );
3750  
3751 270 out = newAV();
3752  
3753 270 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 23 247 270
3754 23 native->fatal = 1;
3755 app_error_code =
3756 23 nghttp3_err_infer_quic_app_error_code((int)rv);
3757  
3758 23 av_push(out, newSV(0));
3759 23 av_push(out, newSViv((IV)rv));
3760 23 av_push(out, newSVuv((UV)app_error_code));
3761 23 av_push(out, newSVpv(nghttp3_strerror((int)rv), 0));
3762 } else {
3763 247 av_push(out, newSViv((IV)rv));
3764 }
3765  
3766 270 RETVAL = newRV_noinc((SV *)out);
3767 OUTPUT:
3768 RETVAL
3769  
3770 SV *
3771 next_write(self)
3772 SV *self
3773 PREINIT:
3774 unblock_http3_native_conn *native;
3775 nghttp3_vec vec[16];
3776 nghttp3_ssize nvec;
3777 int64_t stream_id;
3778 int fin;
3779 SV *payload;
3780 AV *out;
3781 nghttp3_ssize i;
3782 CODE:
3783 798 native = unblock_http3_conn_from_sv(self);
3784 798 stream_id = -1;
3785 798 fin = 0;
3786  
3787 798 nvec = nghttp3_conn_writev_stream(
3788 native->conn,
3789 &stream_id,
3790 &fin,
3791 vec,
3792 16
3793 );
3794  
3795 798 ▶ if (nvec < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nvec < 0) { 0 798 798
3796 0 unblock_http3_fail("could not obtain HTTP/3 output", (int)nvec);
3797 }
3798  
3799 798 ▶ if (nvec == 0 && stream_id == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (nvec == 0 && stream_id == -1) { 578 220 798
if (nvec == 0 && stream_id == -1) { 567 11 578
3800 567 XSRETURN_UNDEF;
3801 }
3802  
3803 231 payload = newSVpvn("", 0);
3804  
3805 497 ▶ for (i = 0; i < nvec; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nvec; ++i) { 266 231 497
3806 266 sv_catpvn(
3807 payload,
3808 (const char *)vec[i].base,
3809 (STRLEN)vec[i].len
3810 );
3811 }
3812  
3813 231 out = newAV();
3814 231 av_push(out, newSViv((IV)stream_id));
3815 231 av_push(out, payload);
3816 231 av_push(out, newSViv(fin ? 1 : 0));
3817  
3818 231 RETVAL = newRV_noinc((SV *)out);
3819 OUTPUT:
3820 RETVAL
3821  
3822 void
3823 add_write_offset(self, stream_id, amount)
3824 SV *self
3825 IV stream_id
3826 UV amount
3827 PREINIT:
3828 unblock_http3_native_conn *native;
3829 int rv;
3830 CODE:
3831 231 native = unblock_http3_conn_from_sv(self);
3832  
3833 231 rv = nghttp3_conn_add_write_offset(
3834 native->conn,
3835 (int64_t)stream_id,
3836 (size_t)amount
3837 );
3838  
3839 231 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 231 231
3840 0 unblock_http3_fail("could not advance HTTP/3 write offset", rv);
3841 }
3842  
3843 void
3844 update_ack_offset(self, stream_id, offset)
3845 SV *self
3846 IV stream_id
3847 UV offset
3848 PREINIT:
3849 unblock_http3_native_conn *native;
3850 int rv;
3851 CODE:
3852 352 native = unblock_http3_conn_from_sv(self);
3853  
3854 352 rv = nghttp3_conn_update_ack_offset(
3855 native->conn,
3856 (int64_t)stream_id,
3857 (uint64_t)offset
3858 );
3859  
3860 352 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 352 352
3861 0 unblock_http3_fail("could not update HTTP/3 acknowledgement offset", rv);
3862 }
3863  
3864 void
3865 submit_shutdown_notice(self)
3866 SV *self
3867 PREINIT:
3868 unblock_http3_native_conn *native;
3869 int rv;
3870 CODE:
3871 1 native = unblock_http3_conn_from_sv(self);
3872  
3873 1 rv = nghttp3_conn_submit_shutdown_notice(native->conn);
3874  
3875 1 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 1 1
3876 0 unblock_http3_fail("could not submit HTTP/3 shutdown notice", rv);
3877 }
3878  
3879 void
3880 begin_shutdown(self)
3881 SV *self
3882 PREINIT:
3883 unblock_http3_native_conn *native;
3884 int rv;
3885 CODE:
3886 1 native = unblock_http3_conn_from_sv(self);
3887  
3888 1 rv = nghttp3_conn_shutdown(native->conn);
3889  
3890 1 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 1 1
3891 0 unblock_http3_fail("could not begin HTTP/3 shutdown", rv);
3892 }
3893  
3894 int
3895 is_drained(self)
3896 SV *self
3897 PREINIT:
3898 unblock_http3_native_conn *native;
3899 CODE:
3900 1 native = unblock_http3_conn_from_sv(self);
3901 1 ▶ RETVAL = nghttp3_conn_is_drained2(native->conn) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = nghttp3_conn_is_drained2(native->conn) ? 1 : 0; 0 1 1
3902 OUTPUT:
3903 RETVAL
3904  
3905 void
3906 shutdown_stream_read(self, stream_id)
3907 SV *self
3908 IV stream_id
3909 PREINIT:
3910 unblock_http3_native_conn *native;
3911 int rv;
3912 CODE:
3913 6 native = unblock_http3_conn_from_sv(self);
3914  
3915 6 rv = nghttp3_conn_shutdown_stream_read(
3916 native->conn,
3917 (int64_t)stream_id
3918 );
3919  
3920 6 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 6 6
3921 0 unblock_http3_fail("could not shut down HTTP/3 stream read side", rv);
3922 }
3923  
3924 void
3925 shutdown_stream_write(self, stream_id)
3926 SV *self
3927 IV stream_id
3928 PREINIT:
3929 unblock_http3_native_conn *native;
3930 CODE:
3931 7 native = unblock_http3_conn_from_sv(self);
3932  
3933 7 nghttp3_conn_shutdown_stream_write(
3934 native->conn,
3935 (int64_t)stream_id
3936 );
3937  
3938 void
3939 close_stream(self, stream_id, rx_error = &PL_sv_undef, tx_error = &PL_sv_undef)
3940 SV *self
3941 IV stream_id
3942 SV *rx_error
3943 SV *tx_error
3944 PREINIT:
3945 unblock_http3_native_conn *native;
3946 uint32_t flags;
3947 uint64_t rx_code;
3948 uint64_t tx_code;
3949 int rv;
3950 CODE:
3951 47 native = unblock_http3_conn_from_sv(self);
3952 47 flags = 0;
3953 47 rx_code = 0;
3954 47 tx_code = 0;
3955  
3956 47 ▶ if (SvOK(rx_error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rx_error)) { 3 44 47
3957 3 flags |= NGHTTP3_STREAM_CLOSE_FLAG_RX_APP_ERROR_CODE_SET;
3958 3 rx_code = (uint64_t)SvUV(rx_error);
3959 }
3960  
3961 47 ▶ if (SvOK(tx_error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(tx_error)) { 1 46 47
3962 1 flags |= NGHTTP3_STREAM_CLOSE_FLAG_TX_APP_ERROR_CODE_SET;
3963 1 tx_code = (uint64_t)SvUV(tx_error);
3964 }
3965  
3966 47 rv = nghttp3_conn_close_stream2(
3967 native->conn,
3968 flags,
3969 (int64_t)stream_id,
3970 rx_code,
3971 tx_code
3972 );
3973  
3974 47 ▶ if (rv != 0 && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) { 0 47 47
if (rv != 0 && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) { 0 0 0
3975 0 unblock_http3_fail("could not close HTTP/3 stream", rv);
3976 }
3977  
3978 47 unblock_http3_body_remove(
3979 native,
3980 (int64_t)stream_id
3981 );
3982  
3983  
3984 UV
3985 uniform_request_field_section_size(self, message)
3986 SV *self
3987 SV *message
3988 PREINIT:
3989 unblock_http3_native_conn *native;
3990 uhttp_native_view view;
3991 size_t size;
3992 CODE:
3993 29 native = unblock_http3_conn_from_sv(self);
3994 (void)native;
3995 29 unblock_http3_uniform_view(
3996 aTHX_ message,
3997 UHTTP_KIND_REQUEST,
3998 &view
3999 );
4000 29 unblock_http3_uniform_validate_section(
4001 aTHX_ &view.headers,
4002 0
4003 );
4004 29 size = unblock_http3_uniform_request_size(
4005 aTHX_ &view
4006 );
4007 29 ▶ RETVAL = (UV)size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)size; 26 3 29
4008 OUTPUT:
4009 RETVAL
4010  
4011 UV
4012 uniform_response_field_section_size(self, message)
4013 SV *self
4014 SV *message
4015 PREINIT:
4016 unblock_http3_native_conn *native;
4017 uhttp_native_view view;
4018 size_t size;
4019 CODE:
4020 26 native = unblock_http3_conn_from_sv(self);
4021 (void)native;
4022 26 unblock_http3_uniform_view(
4023 aTHX_ message,
4024 UHTTP_KIND_RESPONSE,
4025 &view
4026 );
4027 26 unblock_http3_uniform_validate_section(
4028 aTHX_ &view.headers,
4029 1
4030 );
4031 26 size = unblock_http3_uniform_response_size(
4032 aTHX_ &view
4033 );
4034 26 ▶ RETVAL = (UV)size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)size; 23 3 26
4035 OUTPUT:
4036 RETVAL
4037  
4038 UV
4039 uniform_trailer_field_section_size(self, message)
4040 SV *self
4041 SV *message
4042 PREINIT:
4043 unblock_http3_native_conn *native;
4044 uhttp_native_view view;
4045 size_t size;
4046 CODE:
4047 52 native = unblock_http3_conn_from_sv(self);
4048 (void)native;
4049  
4050 52 unblock_http3_uniform_any_view(
4051 aTHX_ message,
4052 &view
4053 );
4054  
4055 52 unblock_http3_uniform_validate_section(
4056 aTHX_ &view.trailers,
4057 2
4058 );
4059 52 size = unblock_http3_uniform_section_size(
4060 aTHX_ &view.trailers
4061 );
4062 52 ▶ RETVAL = (UV)size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)size; 47 5 52
4063 OUTPUT:
4064 RETVAL
4065  
4066 void
4067 submit_uniform_request(self, stream_id, message, streaming = 0)
4068 SV *self
4069 IV stream_id
4070 SV *message
4071 IV streaming
4072 PREINIT:
4073 unblock_http3_native_conn *native;
4074 uhttp_native_view view;
4075 unblock_http3_uniform_fields headers;
4076 unblock_http3_uniform_fields trailers;
4077 unblock_http3_body *body_ctx;
4078 const nghttp3_data_reader *reader;
4079 Size_t trailer_count;
4080 int rv;
4081 CODE:
4082 28 native = unblock_http3_conn_from_sv(self);
4083 28 unblock_http3_uniform_view(
4084 aTHX_ message,
4085 UHTTP_KIND_REQUEST,
4086 &view
4087 );
4088 28 unblock_http3_uniform_request_fields(
4089 aTHX_ &view,
4090 &headers
4091 );
4092 28 unblock_http3_uniform_trailer_fields(
4093 aTHX_ &view,
4094 &trailers
4095 );
4096 28 trailer_count = uhttp_native_field_count(
4097 aTHX_ &view.trailers
4098 );
4099 28 body_ctx = NULL;
4100 28 reader = NULL;
4101  
4102 28 if (
4103 28 ▶ (view.flags & UHTTP_HAS_BUFFERED_BODY)
Branch: 1 branch
BranchTrueFalseTotal
(view.flags & UHTTP_HAS_BUFFERED_BODY) 25 3 28
4104 25 ▶ || streaming
Branch: 1 branch
BranchTrueFalseTotal
|| streaming 12 13 25
4105 12 ▶ || trailer_count != 0
Branch: 1 branch
BranchTrueFalseTotal
|| trailer_count != 0 0 12 12
4106 ) {
4107 16 body_ctx = unblock_http3_body_create(
4108 native,
4109 (int64_t)stream_id,
4110 streaming ? 1 : 0
4111 );
4112  
4113 16 ▶ if (view.flags & UHTTP_HAS_BUFFERED_BODY) {
Branch: 1 branch
BranchTrueFalseTotal
if (view.flags & UHTTP_HAS_BUFFERED_BODY) { 3 13 16
4114 3 unblock_http3_body_append(
4115 native,
4116 body_ctx,
4117 view.body
4118 );
4119 }
4120  
4121 16 ▶ if (!streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (!streaming) { 3 13 16
4122 3 body_ctx->eof = 1;
4123 }
4124  
4125 16 reader = &unblock_http3_body_reader;
4126 }
4127  
4128 28 rv = nghttp3_conn_submit_request(
4129 native->conn,
4130 (int64_t)stream_id,
4131 28 headers.nva,
4132 headers.nvlen,
4133 reader,
4134 body_ctx
4135 );
4136  
4137 28 unblock_http3_uniform_fields_free(&headers);
4138  
4139 28 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 28 28
4140 0 unblock_http3_uniform_fields_free(&trailers);
4141 0 ▶ if (body_ctx != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_ctx != NULL) { 0 0 0
4142 0 unblock_http3_body_remove(
4143 native,
4144 (int64_t)stream_id
4145 );
4146 }
4147 0 unblock_http3_fail("could not submit HTTP/3 request", rv);
4148 }
4149  
4150 28 ▶ if (trailers.nvlen != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (trailers.nvlen != 0) { 2 26 28
4151 2 rv = nghttp3_conn_submit_trailers(
4152 native->conn,
4153 (int64_t)stream_id,
4154 2 trailers.nva,
4155 trailers.nvlen
4156 );
4157  
4158 2 unblock_http3_uniform_fields_free(&trailers);
4159  
4160 2 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 2 2
4161 0 unblock_http3_fail(
4162 "could not submit HTTP/3 request trailers",
4163 rv
4164 );
4165 }
4166 } else {
4167 26 unblock_http3_uniform_fields_free(&trailers);
4168 }
4169  
4170 void
4171 submit_uniform_info(self, stream_id, message)
4172 SV *self
4173 IV stream_id
4174 SV *message
4175 PREINIT:
4176 unblock_http3_native_conn *native;
4177 uhttp_native_view view;
4178 unblock_http3_uniform_fields fields;
4179 char status_buffer[4];
4180 int rv;
4181 CODE:
4182 2 native = unblock_http3_conn_from_sv(self);
4183 2 unblock_http3_uniform_view(
4184 aTHX_ message,
4185 UHTTP_KIND_RESPONSE,
4186 &view
4187 );
4188 2 unblock_http3_uniform_response_fields(
4189 aTHX_ &view,
4190 &fields,
4191 status_buffer
4192 );
4193  
4194 2 rv = nghttp3_conn_submit_info(
4195 native->conn,
4196 (int64_t)stream_id,
4197 2 fields.nva,
4198 fields.nvlen
4199 );
4200  
4201 2 unblock_http3_uniform_fields_free(&fields);
4202  
4203 2 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 2 2
4204 0 unblock_http3_fail(
4205 "could not submit HTTP/3 informational response",
4206 rv
4207 );
4208 }
4209  
4210 void
4211 submit_uniform_response(self, stream_id, message, streaming = 0)
4212 SV *self
4213 IV stream_id
4214 SV *message
4215 IV streaming
4216 PREINIT:
4217 unblock_http3_native_conn *native;
4218 uhttp_native_view view;
4219 unblock_http3_uniform_fields headers;
4220 unblock_http3_uniform_fields trailers;
4221 unblock_http3_body *body_ctx;
4222 const nghttp3_data_reader *reader;
4223 Size_t trailer_count;
4224 char status_buffer[4];
4225 int rv;
4226 CODE:
4227 24 native = unblock_http3_conn_from_sv(self);
4228 24 unblock_http3_uniform_view(
4229 aTHX_ message,
4230 UHTTP_KIND_RESPONSE,
4231 &view
4232 );
4233 24 unblock_http3_uniform_response_fields(
4234 aTHX_ &view,
4235 &headers,
4236 status_buffer
4237 );
4238 24 unblock_http3_uniform_trailer_fields(
4239 aTHX_ &view,
4240 &trailers
4241 );
4242 24 trailer_count = uhttp_native_field_count(
4243 aTHX_ &view.trailers
4244 );
4245 24 body_ctx = NULL;
4246 24 reader = NULL;
4247  
4248 24 if (
4249 24 ▶ (view.flags & UHTTP_HAS_BUFFERED_BODY)
Branch: 1 branch
BranchTrueFalseTotal
(view.flags & UHTTP_HAS_BUFFERED_BODY) 18 6 24
4250 18 ▶ || streaming
Branch: 1 branch
BranchTrueFalseTotal
|| streaming 10 8 18
4251 10 ▶ || trailer_count != 0
Branch: 1 branch
BranchTrueFalseTotal
|| trailer_count != 0 0 10 10
4252 ) {
4253 14 body_ctx = unblock_http3_body_create(
4254 native,
4255 (int64_t)stream_id,
4256 streaming ? 1 : 0
4257 );
4258  
4259 14 ▶ if (view.flags & UHTTP_HAS_BUFFERED_BODY) {
Branch: 1 branch
BranchTrueFalseTotal
if (view.flags & UHTTP_HAS_BUFFERED_BODY) { 6 8 14
4260 6 unblock_http3_body_append(
4261 native,
4262 body_ctx,
4263 view.body
4264 );
4265 }
4266  
4267 14 ▶ if (!streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (!streaming) { 6 8 14
4268 6 body_ctx->eof = 1;
4269 }
4270  
4271 14 reader = &unblock_http3_body_reader;
4272  
4273 14 rv = nghttp3_conn_set_stream_user_data(
4274 native->conn,
4275 (int64_t)stream_id,
4276 body_ctx
4277 );
4278  
4279 14 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 14 14
4280 0 unblock_http3_uniform_fields_free(&headers);
4281 0 unblock_http3_uniform_fields_free(&trailers);
4282 0 unblock_http3_body_remove(
4283 native,
4284 (int64_t)stream_id
4285 );
4286 0 unblock_http3_fail(
4287 "could not attach HTTP/3 response body",
4288 rv
4289 );
4290 }
4291 }
4292  
4293 24 rv = nghttp3_conn_submit_response(
4294 native->conn,
4295 (int64_t)stream_id,
4296 24 headers.nva,
4297 headers.nvlen,
4298 reader
4299 );
4300  
4301 24 unblock_http3_uniform_fields_free(&headers);
4302  
4303 24 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 24 24
4304 0 unblock_http3_uniform_fields_free(&trailers);
4305 0 ▶ if (body_ctx != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_ctx != NULL) { 0 0 0
4306 0 unblock_http3_body_remove(
4307 native,
4308 (int64_t)stream_id
4309 );
4310 }
4311 0 unblock_http3_fail("could not submit HTTP/3 response", rv);
4312 }
4313  
4314 24 ▶ if (trailers.nvlen != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (trailers.nvlen != 0) { 2 22 24
4315 2 rv = nghttp3_conn_submit_trailers(
4316 native->conn,
4317 (int64_t)stream_id,
4318 2 trailers.nva,
4319 trailers.nvlen
4320 );
4321  
4322 2 unblock_http3_uniform_fields_free(&trailers);
4323  
4324 2 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 2 2
4325 0 unblock_http3_fail(
4326 "could not submit HTTP/3 response trailers",
4327 rv
4328 );
4329 }
4330 } else {
4331 22 unblock_http3_uniform_fields_free(&trailers);
4332 }
4333  
4334 void
4335 submit_request(self, stream_id, fields, body = &PL_sv_undef, streaming = 0)
4336 SV *self
4337 IV stream_id
4338 SV *fields
4339 SV *body
4340 IV streaming
4341 PREINIT:
4342 unblock_http3_native_conn *native;
4343 unblock_http3_body *body_ctx;
4344 const nghttp3_data_reader *reader;
4345 nghttp3_nv *nva;
4346 size_t nvlen;
4347 int rv;
4348 CODE:
4349 4 native = unblock_http3_conn_from_sv(self);
4350 4 nva = unblock_http3_fields_from_sv(fields, &nvlen);
4351 4 body_ctx = NULL;
4352 4 reader = NULL;
4353  
4354 4 ▶ if (SvOK(body) || streaming) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(body) || streaming) { 4 0 4
if (SvOK(body) || streaming) { 2 2 4
4355 2 body_ctx = unblock_http3_body_create(
4356 native,
4357 (int64_t)stream_id,
4358 streaming ? 1 : 0
4359 );
4360  
4361 2 ▶ if (SvOK(body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(body)) { 0 2 2
4362 0 unblock_http3_body_append(native, body_ctx, body);
4363 0 body_ctx->eof = 1;
4364 }
4365  
4366 2 reader = &unblock_http3_body_reader;
4367 }
4368  
4369 4 rv = nghttp3_conn_submit_request(
4370 native->conn,
4371 (int64_t)stream_id,
4372 nva,
4373 nvlen,
4374 reader,
4375 body_ctx
4376 );
4377  
4378 4 ▶ if (nva != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (nva != NULL) { 4 0 4
4379 4 Safefree(nva);
4380 }
4381  
4382 4 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 4 4
4383 0 unblock_http3_fail("could not submit HTTP/3 request", rv);
4384 }
4385  
4386 void
4387 submit_info(self, stream_id, fields)
4388 SV *self
4389 IV stream_id
4390 SV *fields
4391 PREINIT:
4392 unblock_http3_native_conn *native;
4393 nghttp3_nv *nva;
4394 size_t nvlen;
4395 int rv;
4396 CODE:
4397 0 native = unblock_http3_conn_from_sv(self);
4398 0 nva = unblock_http3_fields_from_sv(fields, &nvlen);
4399  
4400 0 rv = nghttp3_conn_submit_info(
4401 native->conn,
4402 (int64_t)stream_id,
4403 nva,
4404 nvlen
4405 );
4406  
4407 0 ▶ if (nva != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (nva != NULL) { 0 0 0
4408 0 Safefree(nva);
4409 }
4410  
4411 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
4412 0 unblock_http3_fail(
4413 "could not submit HTTP/3 informational response",
4414 rv
4415 );
4416 }
4417  
4418 void
4419 submit_response(self, stream_id, fields, body = &PL_sv_undef, streaming = 0)
4420 SV *self
4421 IV stream_id
4422 SV *fields
4423 SV *body
4424 IV streaming
4425 PREINIT:
4426 unblock_http3_native_conn *native;
4427 unblock_http3_body *body_ctx;
4428 const nghttp3_data_reader *reader;
4429 nghttp3_nv *nva;
4430 size_t nvlen;
4431 int rv;
4432 CODE:
4433 0 native = unblock_http3_conn_from_sv(self);
4434 0 nva = unblock_http3_fields_from_sv(fields, &nvlen);
4435 0 body_ctx = NULL;
4436 0 reader = NULL;
4437  
4438 0 ▶ if (SvOK(body) || streaming) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(body) || streaming) { 0 0 0
if (SvOK(body) || streaming) { 0 0 0
4439 0 body_ctx = unblock_http3_body_create(
4440 native,
4441 (int64_t)stream_id,
4442 streaming ? 1 : 0
4443 );
4444  
4445 0 ▶ if (SvOK(body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(body)) { 0 0 0
4446 0 unblock_http3_body_append(native, body_ctx, body);
4447 0 body_ctx->eof = 1;
4448 }
4449  
4450 0 reader = &unblock_http3_body_reader;
4451  
4452 0 rv = nghttp3_conn_set_stream_user_data(
4453 native->conn,
4454 (int64_t)stream_id,
4455 body_ctx
4456 );
4457  
4458 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
4459 0 ▶ if (nva != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (nva != NULL) { 0 0 0
4460 0 Safefree(nva);
4461 }
4462 0 unblock_http3_fail(
4463 "could not attach HTTP/3 response body",
4464 rv
4465 );
4466 }
4467 }
4468  
4469 0 rv = nghttp3_conn_submit_response(
4470 native->conn,
4471 (int64_t)stream_id,
4472 nva,
4473 nvlen,
4474 reader
4475 );
4476  
4477 0 ▶ if (nva != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (nva != NULL) { 0 0 0
4478 0 Safefree(nva);
4479 }
4480  
4481 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
4482 0 unblock_http3_fail("could not submit HTTP/3 response", rv);
4483 }
4484  
4485 UV
4486 append_body(self, stream_id, bytes, final)
4487 SV *self
4488 IV stream_id
4489 SV *bytes
4490 IV final
4491 PREINIT:
4492 unblock_http3_native_conn *native;
4493 unblock_http3_body *body;
4494 size_t appended;
4495 int rv;
4496 CODE:
4497 38 native = unblock_http3_conn_from_sv(self);
4498 38 body = unblock_http3_body_find(native, (int64_t)stream_id);
4499  
4500 38 ▶ if (body == NULL || !body->streaming) {
Branch: 2 branches
BranchTrueFalseTotal
if (body == NULL || !body->streaming) { 38 0 38
if (body == NULL || !body->streaming) { 0 38 38
4501 0 croak("HTTP/3 stream does not have an incremental body");
4502 }
4503  
4504 38 ▶ if (body->eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (body->eof) { 0 38 38
4505 0 croak("HTTP/3 incremental body is already complete");
4506 }
4507  
4508 38 appended = unblock_http3_body_append(native, body, bytes);
4509  
4510 38 ▶ if (final) {
Branch: 1 branch
BranchTrueFalseTotal
if (final) { 18 20 38
4511 18 body->eof = 1;
4512 }
4513  
4514 38 rv = nghttp3_conn_resume_stream(
4515 native->conn,
4516 (int64_t)stream_id
4517 );
4518  
4519 38 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 38 38
4520 0 unblock_http3_fail("could not resume HTTP/3 body stream", rv);
4521 }
4522  
4523 38 ▶ RETVAL = (UV)appended;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)appended; 33 5 38
4524 OUTPUT:
4525 RETVAL
4526  
4527 void
4528 discard_body(self, stream_id)
4529 SV *self
4530 IV stream_id
4531 PREINIT:
4532 unblock_http3_native_conn *native;
4533 unblock_http3_body *body;
4534 CODE:
4535 9 native = unblock_http3_conn_from_sv(self);
4536 9 body = unblock_http3_body_find(native, (int64_t)stream_id);
4537 9 unblock_http3_body_discard(native, body);
4538  
4539 UV
4540 streaming_retained_bytes(self)
4541 SV *self
4542 PREINIT:
4543 unblock_http3_native_conn *native;
4544 CODE:
4545 389 native = unblock_http3_conn_from_sv(self);
4546 389 ▶ RETVAL = (UV)native->streaming_retained_bytes;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)native->streaming_retained_bytes; 380 9 389
4547 OUTPUT:
4548 RETVAL
4549  
4550 void
4551 submit_trailers(self, stream_id, fields)
4552 SV *self
4553 IV stream_id
4554 SV *fields
4555 PREINIT:
4556 unblock_http3_native_conn *native;
4557 nghttp3_nv *nva;
4558 size_t nvlen;
4559 int rv;
4560 CODE:
4561 0 native = unblock_http3_conn_from_sv(self);
4562 0 nva = unblock_http3_fields_from_sv(fields, &nvlen);
4563  
4564 0 rv = nghttp3_conn_submit_trailers(
4565 native->conn,
4566 (int64_t)stream_id,
4567 nva,
4568 nvlen
4569 );
4570  
4571 0 ▶ if (nva != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (nva != NULL) { 0 0 0
4572 0 Safefree(nva);
4573 }
4574  
4575 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
4576 0 unblock_http3_fail("could not submit HTTP/3 trailers", rv);
4577 }
4578  
4579 SV *
4580 next_event(self)
4581 SV *self
4582 PREINIT:
4583 unblock_http3_native_conn *native;
4584 SV *event;
4585 CODE:
4586 1298 native = unblock_http3_conn_from_sv(self);
4587  
4588 1298 ▶ if (av_len(native->events) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(native->events) < 0) { 1072 226 1298
4589 1072 XSRETURN_UNDEF;
4590 }
4591  
4592 226 event = av_shift(native->events);
4593  
4594 226 ▶ if (event == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (event == NULL) { 0 226 226
4595 0 XSRETURN_UNDEF;
4596 }
4597  
4598 226 RETVAL = event;
4599 OUTPUT:
4600 RETVAL
4601  
4602 void
4603 DESTROY(self)
4604 SV *self
4605 PREINIT:
4606 unblock_http3_native_conn *native;
4607 CODE:
4608 60 ▶ if (!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) { 0 60 60
4609 0 XSRETURN_EMPTY;
4610 }
4611  
4612 60 native = INT2PTR(
4613 unblock_http3_native_conn *,
4614 SvIV(SvRV(self))
4615 );
4616  
4617 60 ▶ if (native != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (native != NULL) { 60 0 60
4618 60 ▶ if (native->conn != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->conn != NULL) { 60 0 60
4619 60 nghttp3_conn_del(native->conn);
4620 60 native->conn = NULL;
4621 }
4622  
4623 60 ▶ if (native->events != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->events != NULL) { 60 0 60
4624 60 SvREFCNT_dec((SV *)native->events);
4625 60 native->events = NULL;
4626 }
4627  
4628 60 ▶ if (native->origin_list_data != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (native->origin_list_data != NULL) { 1 59 60
4629 1 Safefree(native->origin_list_data);
4630 1 native->origin_list_data = NULL;
4631 }
4632  
4633 60 unblock_http3_body_free_all(native);
4634 60 unblock_http3_header_block_free_all(native);
4635 60 Safefree(native);
4636 60 sv_setiv(SvRV(self), 0);
4637 }
« blib/lib/Unblock/HTTP3/NativeABI.pm ↑ Summary blib/lib/Unblock/HTTP3.pm »