nghttp2.xs

filter: Statementstmt 75.9% 1248 / 1644 Branchbran 54.7% 677 / 1236 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.8% 1925 / 2880 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Unblock/HTTP2/NativeABI.pm ↑ Summary h2_receive_guard.h »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "uniform_http_fastpath.h"
6 #include "unblock_http2_native_abi.h"
7  
8 #include <nghttp2/nghttp2.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "h2_receive_guard.h"
13  
14 typedef struct unblock_h2_provider unblock_h2_provider;
15 typedef struct unblock_h2_header_block unblock_h2_header_block;
16  
17 typedef struct {
18 char *name;
19 size_t namelen;
20 char *value;
21 size_t valuelen;
22 } unblock_h2_header_field;
23  
24 struct unblock_h2_header_block {
25 unblock_h2_header_block *next;
26 int32_t stream_id;
27 int category;
28 uint8_t flags;
29 size_t list_size;
30 size_t count;
31 size_t capacity;
32 unblock_h2_header_field *fields;
33 const char *error;
34 uint32_t error_code;
35 };
36  
37 struct unblock_h2_provider {
38 unblock_h2_provider *next;
39 unblock_h2_provider *pending_next;
40 SV *callback;
41 int32_t stream_id;
42 int deferred;
43 int released;
44 };
45  
46 typedef struct {
47 nghttp2_session *session;
48 ub_h2_guard guard;
49 uhttp_native_api uniform_api;
50 SV *cb_begin_headers;
51 SV *cb_frame_recv;
52 SV *cb_data_chunk_recv;
53 SV *cb_stream_close;
54 SV *cb_invalid_frame;
55 SV *cb_error;
56 SV *callback_error;
57 unblock_h2_provider *providers;
58 unblock_h2_provider *pending_free;
59 unblock_h2_header_block *header_blocks;
60 size_t max_header_list_size;
61 int server;
62 int in_session_call;
63 } unblock_h2_session;
64  
65  
66 typedef struct {
67 SV *engine;
68 SV *session_object;
69 unblock_h2_session *session;
70 CV *after_session_cv;
71 CV *finish_close_cv;
72 } ub_http2_native_context;
73  
74 static unblock_h2_session *
75 67193 session_from_sv(pTHX_ SV *self)
76 {
77 unblock_h2_session *ps;
78  
79 67193 ▶ if (!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) { 0 67193 67193
80 0 croak("Unblock::HTTP2::_nghttp2::Session: invalid session object");
81 }
82  
83 67193 ps = INT2PTR(unblock_h2_session *, SvIV(SvRV(self)));
84 67193 ▶ if (!ps || !ps->session) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps || !ps->session) { 67193 0 67193
if (!ps || !ps->session) { 0 67193 67193
85 0 croak("Unblock::HTTP2::_nghttp2::Session: session has been destroyed");
86 }
87  
88 67193 return ps;
89 }
90  
91 static void
92 0 set_callback_error(pTHX_ unblock_h2_session *ps, const char *prefix)
93 {
94 SV *error;
95  
96 0 ▶ if (ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->callback_error) { 0 0 0
97 0 return;
98 }
99  
100 0 ▶ error = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
error = ERRSV; 0 0 0
101 0 ▶ if (error && SvTRUE(error)) {
Branch: 2 branches
BranchTrueFalseTotal
if (error && SvTRUE(error)) { 0 0 0
if (error && SvTRUE(error)) { 0 0 0
102 0 ps->callback_error = newSVpvf("%s: %s", prefix, SvPV_nolen(error));
103 }
104 else {
105 0 ps->callback_error = newSVpv(prefix, 0);
106 }
107 }
108  
109 static void
110 63405 clear_callback_error(pTHX_ unblock_h2_session *ps)
111 {
112 63405 ▶ if (ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->callback_error) { 0 63405 63405
113 0 SvREFCNT_dec(ps->callback_error);
114 0 ps->callback_error = NULL;
115 }
116 63405 }
117  
118 static void
119 27741 croak_callback_error(pTHX_ unblock_h2_session *ps)
120 {
121 SV *error;
122  
123 27741 ▶ if (!ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->callback_error) { 27741 0 27741
124 27741 return;
125 }
126  
127 0 error = ps->callback_error;
128 0 ps->callback_error = NULL;
129 0 sv_2mortal(error);
130 0 croak("%s", SvPV_nolen(error));
131 }
132  
133 static SV *
134 1386 callback_from_hash(pTHX_ HV *callbacks, const char *name, I32 name_len)
135 {
136 SV **svp;
137  
138 1386 ▶ if (!callbacks) {
Branch: 1 branch
BranchTrueFalseTotal
if (!callbacks) { 0 1386 1386
139 0 return NULL;
140 }
141  
142 1386 svp = hv_fetch(callbacks, name, name_len, 0);
143 1386 ▶ if (!svp || !SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !SvOK(*svp)) { 1155 231 1386
if (!svp || !SvOK(*svp)) { 0 1155 1155
144 231 return NULL;
145 }
146  
147 1155 ▶ if (!SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVCV) { 1155 0 1155
if (!SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVCV) { 0 1155 1155
148 0 croak("callback %s must be a coderef", name);
149 }
150  
151 1155 return newSVsv(*svp);
152 }
153  
154 static void
155 231 load_callbacks(pTHX_ unblock_h2_session *ps, HV *callbacks)
156 {
157 231 ps->cb_begin_headers = callback_from_hash(aTHX_ callbacks, "on_begin_headers", 16);
158 231 ps->cb_frame_recv = callback_from_hash(aTHX_ callbacks, "on_frame_recv", 13);
159 231 ps->cb_data_chunk_recv = callback_from_hash(aTHX_ callbacks, "on_data_chunk_recv", 18);
160 231 ps->cb_stream_close = callback_from_hash(aTHX_ callbacks, "on_stream_close", 15);
161 231 ps->cb_invalid_frame = callback_from_hash(aTHX_ callbacks, "on_invalid_frame", 16);
162 231 ps->cb_error = callback_from_hash(aTHX_ callbacks, "on_error", 8);
163 231 }
164  
165 static void
166 231 release_callbacks(pTHX_ unblock_h2_session *ps)
167 {
168 231 ▶ if (ps->cb_begin_headers) SvREFCNT_dec(ps->cb_begin_headers);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_begin_headers) SvREFCNT_dec(ps->cb_begin_headers); 231 0 231
169 231 ▶ if (ps->cb_frame_recv) SvREFCNT_dec(ps->cb_frame_recv);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_frame_recv) SvREFCNT_dec(ps->cb_frame_recv); 231 0 231
170 231 ▶ if (ps->cb_data_chunk_recv) SvREFCNT_dec(ps->cb_data_chunk_recv);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_data_chunk_recv) SvREFCNT_dec(ps->cb_data_chunk_recv); 231 0 231
171 231 ▶ if (ps->cb_stream_close) SvREFCNT_dec(ps->cb_stream_close);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_stream_close) SvREFCNT_dec(ps->cb_stream_close); 231 0 231
172 231 ▶ if (ps->cb_invalid_frame) SvREFCNT_dec(ps->cb_invalid_frame);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_invalid_frame) SvREFCNT_dec(ps->cb_invalid_frame); 231 0 231
173 231 ▶ if (ps->cb_error) SvREFCNT_dec(ps->cb_error);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_error) SvREFCNT_dec(ps->cb_error); 0 231 231
174 231 ▶ if (ps->callback_error) SvREFCNT_dec(ps->callback_error);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->callback_error) SvREFCNT_dec(ps->callback_error); 0 231 231
175  
176 231 ps->cb_begin_headers = NULL;
177 231 ps->cb_frame_recv = NULL;
178 231 ps->cb_data_chunk_recv = NULL;
179 231 ps->cb_stream_close = NULL;
180 231 ps->cb_invalid_frame = NULL;
181 231 ps->cb_error = NULL;
182 231 ps->callback_error = NULL;
183 231 }
184  
185 static unblock_h2_provider *
186 14476 find_provider(unblock_h2_session *ps, int32_t stream_id)
187 {
188 14476 unblock_h2_provider *provider = ps->providers;
189  
190 14511 ▶ while (provider) {
Branch: 1 branch
BranchTrueFalseTotal
while (provider) { 60 14451 14511
191 60 ▶ if (provider->stream_id == stream_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (provider->stream_id == stream_id) { 25 35 60
192 25 return provider;
193 }
194 35 provider = provider->next;
195 }
196  
197 14451 return NULL;
198 }
199  
200 static void
201 7236 free_provider(pTHX_ unblock_h2_provider *provider)
202 {
203 7236 ▶ if (provider->callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (provider->callback) { 7236 0 7236
204 7236 SvREFCNT_dec(provider->callback);
205 }
206 7236 free(provider);
207 7236 }
208  
209 static void
210 63636 drain_pending_free(pTHX_ unblock_h2_session *ps)
211 {
212 63636 unblock_h2_provider *provider = ps->pending_free;
213  
214 63636 ps->pending_free = NULL;
215 70862 ▶ while (provider) {
Branch: 1 branch
BranchTrueFalseTotal
while (provider) { 7226 63636 70862
216 7226 unblock_h2_provider *next = provider->pending_next;
217 7226 free_provider(aTHX_ provider);
218 7226 provider = next;
219 }
220 63636 }
221  
222 static void
223 7333 remove_provider(pTHX_ unblock_h2_session *ps, int32_t stream_id)
224 {
225 7333 unblock_h2_provider **link = &ps->providers;
226  
227 7354 ▶ while (*link) {
Branch: 1 branch
BranchTrueFalseTotal
while (*link) { 7247 107 7354
228 7247 unblock_h2_provider *provider = *link;
229 7247 ▶ if (provider->stream_id == stream_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (provider->stream_id == stream_id) { 7226 21 7247
230 7226 *link = provider->next;
231 7226 provider->released = 1;
232  
233 7226 ▶ if (ps->in_session_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->in_session_call) { 7226 0 7226
234 7226 provider->pending_next = ps->pending_free;
235 7226 ps->pending_free = provider;
236 }
237 else {
238 0 free_provider(aTHX_ provider);
239 }
240 7226 return;
241 }
242 21 link = &provider->next;
243 }
244 }
245  
246 static void
247 7236 add_provider(pTHX_ unblock_h2_session *ps, unblock_h2_provider *provider)
248 {
249 7236 ▶ if (find_provider(ps, provider->stream_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_provider(ps, provider->stream_id)) { 0 7236 7236
250 0 croak("stream %d already has a data provider", (int)provider->stream_id);
251 }
252  
253 7236 provider->next = ps->providers;
254 7236 ps->providers = provider;
255 7236 }
256  
257 static void
258 22678 free_header_block(pTHX_ unblock_h2_header_block *block)
259 {
260 size_t i;
261  
262 22678 ▶ if (!block) {
Branch: 1 branch
BranchTrueFalseTotal
if (!block) { 15304 7374 22678
263 15304 return;
264 }
265  
266 36726 ▶ for (i = 0; i < block->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; i++) { 29352 7374 36726
267 29352 free(block->fields[i].name);
268 29352 free(block->fields[i].value);
269 }
270 7374 free(block->fields);
271 7374 free(block);
272 }
273  
274 static unblock_h2_header_block *
275 29353 find_header_block(unblock_h2_session *ps, int32_t stream_id)
276 {
277 29353 unblock_h2_header_block *block = ps->header_blocks;
278  
279 29353 ▶ while (block) {
Branch: 1 branch
BranchTrueFalseTotal
while (block) { 29353 0 29353
280 29353 ▶ if (block->stream_id == stream_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->stream_id == stream_id) { 29353 0 29353
281 29353 return block;
282 }
283 0 block = block->next;
284 }
285 0 return NULL;
286 }
287  
288 static unblock_h2_header_block *
289 22073 take_header_block(unblock_h2_session *ps, int32_t stream_id)
290 {
291 22073 unblock_h2_header_block **link = &ps->header_blocks;
292  
293 22073 ▶ while (*link) {
Branch: 1 branch
BranchTrueFalseTotal
while (*link) { 7366 14707 22073
294 7366 unblock_h2_header_block *block = *link;
295 7366 ▶ if (block->stream_id == stream_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->stream_id == stream_id) { 7366 0 7366
296 7366 *link = block->next;
297 7366 block->next = NULL;
298 7366 return block;
299 }
300 0 link = &block->next;
301 }
302 14707 return NULL;
303 }
304  
305 static void
306 14707 remove_header_block(pTHX_ unblock_h2_session *ps, int32_t stream_id)
307 {
308 14707 free_header_block(aTHX_ take_header_block(ps, stream_id));
309 14707 }
310  
311 static void
312 231 free_header_blocks(pTHX_ unblock_h2_session *ps)
313 {
314 231 unblock_h2_header_block *block = ps->header_blocks;
315  
316 231 ps->header_blocks = NULL;
317 239 ▶ while (block) {
Branch: 1 branch
BranchTrueFalseTotal
while (block) { 8 231 239
318 8 unblock_h2_header_block *next = block->next;
319 8 free_header_block(aTHX_ block);
320 8 block = next;
321 }
322 231 }
323  
324 static int
325 7374 start_header_block(pTHX_ unblock_h2_session *ps, const nghttp2_frame *frame)
326 {
327 unblock_h2_header_block *block;
328  
329 7374 remove_header_block(aTHX_ ps, frame->hd.stream_id);
330  
331 7374 block = (unblock_h2_header_block *)calloc(1, sizeof(*block));
332 7374 ▶ if (!block) {
Branch: 1 branch
BranchTrueFalseTotal
if (!block) { 0 7374 7374
333 0 return 0;
334 }
335  
336 7374 block->stream_id = frame->hd.stream_id;
337 7374 block->category = frame->headers.cat;
338 7374 block->flags = frame->hd.flags;
339 7374 block->next = ps->header_blocks;
340 7374 ps->header_blocks = block;
341 7374 return 1;
342 }
343  
344 static int
345 29353 append_header_field(pTHX_ unblock_h2_session *ps,
346 unblock_h2_header_block *block,
347 const uint8_t *name, size_t namelen,
348 const uint8_t *value, size_t valuelen)
349 {
350 unblock_h2_header_field *fields;
351 unblock_h2_header_field *field;
352 size_t new_size;
353 size_t capacity;
354  
355 29353 ▶ if (!block || block->error) {
Branch: 2 branches
BranchTrueFalseTotal
if (!block || block->error) { 29353 0 29353
if (!block || block->error) { 0 29353 29353
356 0 return 1;
357 }
358  
359 29353 ▶ if (namelen > (size_t)-1 - valuelen - 32
Branch: 1 branch
BranchTrueFalseTotal
if (namelen > (size_t)-1 - valuelen - 32 29353 0 29353
360 29353 ▶ || block->list_size > (size_t)-1 - namelen - valuelen - 32) {
Branch: 1 branch
BranchTrueFalseTotal
|| block->list_size > (size_t)-1 - namelen - valuelen - 32) { 0 29353 29353
361 0 block->error = "HTTP/2 header list size overflow";
362 0 block->error_code = NGHTTP2_ENHANCE_YOUR_CALM;
363 0 return 1;
364 }
365  
366 29353 new_size = block->list_size + namelen + valuelen + 32;
367 29353 ▶ if (new_size > ps->max_header_list_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_size > ps->max_header_list_size) { 1 29352 29353
368 2 block->error = ps->server
369 ? "HTTP/2 request header list exceeds configured limit"
370 1 ▶ : "HTTP/2 response header list exceeds configured limit";
Branch: 1 branch
BranchTrueFalseTotal
: "HTTP/2 response header list exceeds configured limit"; 1 0 1
371 1 block->error_code = NGHTTP2_ENHANCE_YOUR_CALM;
372 1 return 1;
373 }
374  
375 29352 ▶ if (block->count == block->capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->count == block->capacity) { 7374 21978 29352
376 7374 ▶ capacity = block->capacity ? block->capacity * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
capacity = block->capacity ? block->capacity * 2 : 8; 0 7374 7374
377 7374 ▶ if (capacity < block->capacity
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < block->capacity 7374 0 7374
378 7374 ▶ || capacity > (size_t)-1 / sizeof(*fields)) {
Branch: 1 branch
BranchTrueFalseTotal
|| capacity > (size_t)-1 / sizeof(*fields)) { 0 7374 7374
379 0 return 0;
380 }
381 7374 fields = (unblock_h2_header_field *)realloc(
382 7374 block->fields, capacity * sizeof(*fields));
383 7374 ▶ if (!fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fields) { 0 7374 7374
384 0 return 0;
385 }
386 7374 memset(fields + block->capacity, 0,
387 7374 (capacity - block->capacity) * sizeof(*fields));
388 7374 block->fields = fields;
389 7374 block->capacity = capacity;
390 }
391  
392 29352 field = &block->fields[block->count];
393 29352 ▶ field->name = (char *)malloc(namelen ? namelen : 1);
Branch: 1 branch
BranchTrueFalseTotal
field->name = (char *)malloc(namelen ? namelen : 1); 29352 0 29352
394 29352 ▶ field->value = (char *)malloc(valuelen ? valuelen : 1);
Branch: 1 branch
BranchTrueFalseTotal
field->value = (char *)malloc(valuelen ? valuelen : 1); 29352 0 29352
395 29352 ▶ if (!field->name || !field->value) {
Branch: 2 branches
BranchTrueFalseTotal
if (!field->name || !field->value) { 29352 0 29352
if (!field->name || !field->value) { 0 29352 29352
396 0 free(field->name);
397 0 free(field->value);
398 0 field->name = NULL;
399 0 field->value = NULL;
400 0 return 0;
401 }
402  
403 29352 ▶ if (namelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (namelen) { 29352 0 29352
404 29352 memcpy(field->name, name, namelen);
405 }
406 29352 ▶ if (valuelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (valuelen) { 29352 0 29352
407 29352 memcpy(field->value, value, valuelen);
408 }
409 29352 field->namelen = namelen;
410 29352 field->valuelen = valuelen;
411 29352 block->count++;
412 29352 block->list_size = new_size;
413 29352 return 1;
414 }
415  
416 static nghttp2_nv *
417 5 headers_to_nva(pTHX_ AV *headers, size_t *count_out)
418 {
419 5 I32 last = av_len(headers);
420 5 ▶ size_t count = last < 0 ? 0 : (size_t)last + 1;
Branch: 1 branch
BranchTrueFalseTotal
size_t count = last < 0 ? 0 : (size_t)last + 1; 5 0 5
421 nghttp2_nv *nva;
422 I32 i;
423  
424 5 *count_out = count;
425 5 ▶ if (count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == 0) { 0 5 5
426 0 return NULL;
427 }
428  
429 12 ▶ for (i = 0; i <= last; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= last; i++) { 7 5 12
430 7 SV **pair_sv = av_fetch(headers, i, 0);
431 AV *pair;
432 SV **name_sv;
433 SV **value_sv;
434  
435 7 ▶ if (!pair_sv || !SvROK(*pair_sv) || SvTYPE(SvRV(*pair_sv)) != SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!pair_sv || !SvROK(*pair_sv) || SvTYPE(SvRV(*pair_sv)) != SVt_PVAV) { 7 0 7
if (!pair_sv || !SvROK(*pair_sv) || SvTYPE(SvRV(*pair_sv)) != SVt_PVAV) { 7 0 7
if (!pair_sv || !SvROK(*pair_sv) || SvTYPE(SvRV(*pair_sv)) != SVt_PVAV) { 0 7 7
436 0 croak("header %ld must be a two-element array reference", (long)i);
437 }
438  
439 7 pair = (AV *)SvRV(*pair_sv);
440 7 ▶ if (av_len(pair) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(pair) != 1) { 0 7 7
441 0 croak("header %ld must be a two-element array reference", (long)i);
442 }
443  
444 7 name_sv = av_fetch(pair, 0, 0);
445 7 value_sv = av_fetch(pair, 1, 0);
446 7 ▶ if (!name_sv || !value_sv || !SvOK(*name_sv) || !SvOK(*value_sv)
Branch: 4 branches
BranchTrueFalseTotal
if (!name_sv || !value_sv || !SvOK(*name_sv) || !SvOK(*value_sv) 7 0 7
if (!name_sv || !value_sv || !SvOK(*name_sv) || !SvOK(*value_sv) 7 0 7
if (!name_sv || !value_sv || !SvOK(*name_sv) || !SvOK(*value_sv) 7 0 7
if (!name_sv || !value_sv || !SvOK(*name_sv) || !SvOK(*value_sv) 7 0 7
447 7 ▶ || SvROK(*name_sv) || SvROK(*value_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
|| SvROK(*name_sv) || SvROK(*value_sv)) { 7 0 7
|| SvROK(*name_sv) || SvROK(*value_sv)) { 0 7 7
448 0 croak("header %ld name and value must be defined scalars", (long)i);
449 }
450 }
451  
452 5 nva = (nghttp2_nv *)calloc(count, sizeof(*nva));
453 5 ▶ if (!nva) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nva) { 0 5 5
454 0 croak("unable to allocate HTTP/2 header block");
455 }
456  
457 12 ▶ for (i = 0; i <= last; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= last; i++) { 7 5 12
458 7 SV **pair_sv = av_fetch(headers, i, 0);
459 7 AV *pair = (AV *)SvRV(*pair_sv);
460 7 SV **name_sv = av_fetch(pair, 0, 0);
461 7 SV **value_sv = av_fetch(pair, 1, 0);
462 STRLEN name_len;
463 STRLEN value_len;
464  
465 7 nva[i].name = (uint8_t *)SvPVbyte(*name_sv, name_len);
466 7 nva[i].namelen = (size_t)name_len;
467 7 ▶ nva[i].value = (uint8_t *)SvPVbyte(*value_sv, value_len);
Branch: 1 branch
BranchTrueFalseTotal
nva[i].value = (uint8_t *)SvPVbyte(*value_sv, value_len); 7 0 7
468 7 nva[i].valuelen = (size_t)value_len;
469 7 nva[i].flags = NGHTTP2_NV_FLAG_NONE;
470 }
471  
472 5 return nva;
473 }
474  
475  
476 static void
477 7278 validate_uniform_native_view(pTHX_ unblock_h2_session *ps, SV *message,
478 U32 expected_kind, uhttp_native_view *view)
479 {
480 STRLEN version_len;
481 const char *version;
482  
483 7278 ▶ if (!uhttp_native_inspect(aTHX_ &ps->uniform_api, message, view)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!uhttp_native_inspect(aTHX_ &ps->uniform_api, message, view)) { 0 7278 7278
484 0 croak("Uniform::HTTP native path requires an exact canonical message");
485 }
486 7278 ▶ if (view->kind != expected_kind) {
Branch: 1 branch
BranchTrueFalseTotal
if (view->kind != expected_kind) { 0 7278 7278
487 0 croak("Uniform::HTTP native path has the wrong message kind");
488 }
489  
490 7278 ▶ if (SvOK(view->version)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(view->version)) { 0 7278 7278
491 0 version = SvPVbyte(view->version, version_len);
492 0 ▶ if (version_len != 1 || version[0] != '2') {
Branch: 2 branches
BranchTrueFalseTotal
if (version_len != 1 || version[0] != '2') { 0 0 0
if (version_len != 1 || version[0] != '2') { 0 0 0
493 0 croak("explicit HTTP version must be 2");
494 }
495 }
496 7278 }
497  
498 static int
499 7509 ascii_equal_ci(const char *value, size_t value_len, const char *literal)
500 {
501 7509 size_t literal_len = strlen(literal);
502 size_t i;
503  
504 7509 ▶ if (value_len != literal_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (value_len != literal_len) { 7502 7 7509
505 7502 return 0;
506 }
507  
508 28 ▶ for (i = 0; i < value_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < value_len; i++) { 25 3 28
509 25 unsigned char a = (unsigned char)value[i];
510 25 unsigned char b = (unsigned char)literal[i];
511  
512 25 ▶ if (a >= 'A' && a <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (a >= 'A' && a <= 'Z') { 25 0 25
if (a >= 'A' && a <= 'Z') { 23 2 25
513 23 a = (unsigned char)(a + ('a' - 'A'));
514 }
515 25 ▶ if (b >= 'A' && b <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (b >= 'A' && b <= 'Z') { 25 0 25
if (b >= 'A' && b <= 'Z') { 21 4 25
516 21 b = (unsigned char)(b + ('a' - 'A'));
517 }
518 25 ▶ if (a != b) {
Branch: 1 branch
BranchTrueFalseTotal
if (a != b) { 4 21 25
519 4 return 0;
520 }
521 }
522  
523 3 return 1;
524 }
525  
526 static int
527 22424 http_token_char(unsigned char ch)
528 {
529 22424 ▶ if ((ch >= '0' && ch <= '9')
Branch: 2 branches
BranchTrueFalseTotal
if ((ch >= '0' && ch <= '9') 22366 58 22424
if ((ch >= '0' && ch <= '9') 22366 0 22366
530 22424 ▶ || (ch >= 'A' && ch <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
|| (ch >= 'A' && ch <= 'Z') 22366 58 22424
|| (ch >= 'A' && ch <= 'Z') 398 21968 22366
531 456 ▶ || (ch >= 'a' && ch <= 'z')) {
Branch: 2 branches
BranchTrueFalseTotal
|| (ch >= 'a' && ch <= 'z')) { 398 58 456
|| (ch >= 'a' && ch <= 'z')) { 398 0 398
532 22366 return 1;
533 }
534  
535 58 ▶ switch (ch) {
Branch: 1 branch
BranchTrueFalseTotal
switch (ch) { 58 0 58
536 58 case '!': case '#': case '$': case '%': case '&': case '\'':
537 case '*': case '+': case '-': case '.': case '^': case '_':
538 case 0x60: case '|': case '~':
539 58 return 1;
540 0 default:
541 0 return 0;
542 }
543 }
544  
545 static void
546 14 validate_h2_normal_field(pTHX_ SV *name_sv, SV *value_sv, Size_t index)
547 {
548 STRLEN name_len;
549 STRLEN value_len;
550 const char *name;
551 const char *value;
552 size_t i;
553  
554 14 ▶ if (!name_sv || !value_sv || !SvOK(name_sv) || !SvOK(value_sv)
Branch: 4 branches
BranchTrueFalseTotal
if (!name_sv || !value_sv || !SvOK(name_sv) || !SvOK(value_sv) 14 0 14
if (!name_sv || !value_sv || !SvOK(name_sv) || !SvOK(value_sv) 14 0 14
if (!name_sv || !value_sv || !SvOK(name_sv) || !SvOK(value_sv) 14 0 14
if (!name_sv || !value_sv || !SvOK(name_sv) || !SvOK(value_sv) 14 0 14
555 14 ▶ || SvROK(name_sv) || SvROK(value_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
|| SvROK(name_sv) || SvROK(value_sv)) { 14 0 14
|| SvROK(name_sv) || SvROK(value_sv)) { 0 14 14
556 0 croak("header %lu name and value must be defined scalars",
557 (unsigned long)index);
558 }
559  
560 14 ▶ name = SvPVbyte(name_sv, name_len);
Branch: 1 branch
BranchTrueFalseTotal
name = SvPVbyte(name_sv, name_len); 14 0 14
561 14 value = SvPVbyte(value_sv, value_len);
562  
563 14 ▶ if (name_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_len == 0) { 0 14 14
564 0 croak("header %lu name must not be empty", (unsigned long)index);
565 }
566  
567 157 ▶ for (i = 0; i < (size_t)name_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (size_t)name_len; i++) { 143 14 157
568 143 ▶ if (!http_token_char((unsigned char)name[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!http_token_char((unsigned char)name[i])) { 0 143 143
569 0 croak("header %lu name must be an HTTP token",
570 (unsigned long)index);
571 }
572 }
573  
574 451 ▶ for (i = 0; i < (size_t)value_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (size_t)value_len; i++) { 437 14 451
575 437 unsigned char ch = (unsigned char)value[i];
576 437 ▶ if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) {
Branch: 4 branches
BranchTrueFalseTotal
if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) { 437 0 437
if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) { 437 0 437
if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) { 437 0 437
if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) { 0 437 437
577 0 croak("header %lu value contains a prohibited control byte",
578 (unsigned long)index);
579 }
580 }
581  
582 14 ▶ if (ascii_equal_ci(name, (size_t)name_len, "connection")
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(name, (size_t)name_len, "connection") 14 0 14
583 14 ▶ || ascii_equal_ci(name, (size_t)name_len, "keep-alive")
Branch: 1 branch
BranchTrueFalseTotal
|| ascii_equal_ci(name, (size_t)name_len, "keep-alive") 14 0 14
584 14 ▶ || ascii_equal_ci(name, (size_t)name_len, "proxy-connection")
Branch: 1 branch
BranchTrueFalseTotal
|| ascii_equal_ci(name, (size_t)name_len, "proxy-connection") 14 0 14
585 14 ▶ || ascii_equal_ci(name, (size_t)name_len, "transfer-encoding")
Branch: 1 branch
BranchTrueFalseTotal
|| ascii_equal_ci(name, (size_t)name_len, "transfer-encoding") 14 0 14
586 14 ▶ || ascii_equal_ci(name, (size_t)name_len, "upgrade")) {
Branch: 1 branch
BranchTrueFalseTotal
|| ascii_equal_ci(name, (size_t)name_len, "upgrade")) { 0 14 14
587 0 croak("HTTP/2 forbids connection-specific header '%.*s'",
588 (int)name_len, name);
589 }
590  
591 14 ▶ if (ascii_equal_ci(name, (size_t)name_len, "te")
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(name, (size_t)name_len, "te") 0 14 14
592 0 ▶ && !ascii_equal_ci(value, (size_t)value_len, "trailers")) {
Branch: 1 branch
BranchTrueFalseTotal
&& !ascii_equal_ci(value, (size_t)value_len, "trailers")) { 0 0 0
593 0 croak("HTTP/2 TE is limited to trailers");
594 }
595 14 }
596  
597 static size_t
598 7278 measure_native_headers(pTHX_ const uhttp_native_section *headers,
599 size_t *name_bytes_out)
600 {
601 7278 Size_t count = uhttp_native_field_count(aTHX_ headers);
602 Size_t i;
603 7278 size_t name_bytes = 0;
604  
605 7292 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 14 7278 7292
606 SV *name_sv;
607 SV *value_sv;
608 STRLEN name_len;
609  
610 14 ▶ if (!uhttp_native_field_at(aTHX_ headers, i, &name_sv, &value_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!uhttp_native_field_at(aTHX_ headers, i, &name_sv, &value_sv)) { 0 14 14
611 0 croak("Uniform::HTTP native header index is out of range");
612 }
613 14 validate_h2_normal_field(aTHX_ name_sv, value_sv, i);
614 14 (void)SvPVbyte(name_sv, name_len);
615 14 name_bytes += (size_t)name_len;
616 }
617  
618 7278 *name_bytes_out = name_bytes;
619 7278 return (size_t)count;
620 }
621  
622 static void
623 233 set_nv_from_sv(pTHX_ nghttp2_nv *nv, const char *name, size_t name_len,
624 SV *value_sv)
625 {
626 STRLEN value_len;
627 233 const char *value = SvPVbyte(value_sv, value_len);
628  
629 233 nv->name = (uint8_t *)name;
630 233 nv->namelen = name_len;
631 233 nv->value = (uint8_t *)value;
632 233 nv->valuelen = (size_t)value_len;
633 233 nv->flags = NGHTTP2_NV_FLAG_NONE;
634 233 }
635  
636 static void
637 7278 append_native_headers(pTHX_ nghttp2_nv *nva, size_t start,
638 const uhttp_native_section *headers, char *name_buffer)
639 {
640 7278 Size_t count = uhttp_native_field_count(aTHX_ headers);
641 Size_t i;
642 7278 size_t out = start;
643 7278 char *cursor = name_buffer;
644  
645 7292 ▶ for (i = 0; i < count; i++, out++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++, out++) { 14 7278 7292
646 SV *name_sv;
647 SV *value_sv;
648 STRLEN name_len;
649 STRLEN value_len;
650 const char *name;
651 const char *value;
652 size_t j;
653  
654 14 ▶ if (!uhttp_native_field_at(aTHX_ headers, i, &name_sv, &value_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!uhttp_native_field_at(aTHX_ headers, i, &name_sv, &value_sv)) { 0 14 14
655 0 croak("Uniform::HTTP native header index is out of range");
656 }
657  
658 14 name = SvPVbyte(name_sv, name_len);
659 14 ▶ value = SvPVbyte(value_sv, value_len);
Branch: 1 branch
BranchTrueFalseTotal
value = SvPVbyte(value_sv, value_len); 14 0 14
660  
661 157 ▶ for (j = 0; j < (size_t)name_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < (size_t)name_len; j++) { 143 14 157
662 143 unsigned char ch = (unsigned char)name[j];
663 159 ▶ cursor[j] = (char)((ch >= 'A' && ch <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
cursor[j] = (char)((ch >= 'A' && ch <= 'Z') 128 15 143
cursor[j] = (char)((ch >= 'A' && ch <= 'Z') 16 112 128
664 16 ? ch + ('a' - 'A') : ch);
665 }
666  
667 14 nva[out].name = (uint8_t *)cursor;
668 14 nva[out].namelen = (size_t)name_len;
669 14 nva[out].value = (uint8_t *)value;
670 14 nva[out].valuelen = (size_t)value_len;
671 14 nva[out].flags = NGHTTP2_NV_FLAG_NONE;
672 14 cursor += name_len;
673 }
674 7278 }
675  
676 static nghttp2_nv *
677 58 uniform_request_to_nva(pTHX_ unblock_h2_session *ps, SV *message,
678 size_t *count_out)
679 {
680 uhttp_native_view view;
681 SV *method_sv;
682 SV *target_sv;
683 SV *scheme_sv;
684 SV *authority_sv;
685 SV *protocol_sv;
686 STRLEN method_len;
687 STRLEN target_len;
688 58 STRLEN scheme_len = 0;
689 58 STRLEN authority_len = 0;
690 58 STRLEN protocol_len = 0;
691 const char *method;
692 const char *target;
693 58 const char *scheme = NULL;
694 58 const char *authority = NULL;
695 58 const char *protocol = NULL;
696 size_t normal_count;
697 size_t normal_name_bytes;
698 size_t pseudo_count;
699 size_t count;
700 nghttp2_nv *nva;
701 char *name_buffer;
702 58 size_t out = 0;
703  
704 58 validate_uniform_native_view(
705 aTHX_ ps, message, UHTTP_KIND_REQUEST, &view);
706  
707 58 method_sv = view.method;
708 58 target_sv = view.target;
709 58 scheme_sv = view.scheme;
710 58 authority_sv = view.authority;
711 58 protocol_sv = view.protocol;
712  
713 58 ▶ method = SvPVbyte(method_sv, method_len);
Branch: 1 branch
BranchTrueFalseTotal
method = SvPVbyte(method_sv, method_len); 58 0 58
714 58 target = SvPVbyte(target_sv, target_len);
715 58 ▶ if (SvOK(authority_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(authority_sv)) { 58 0 58
716 58 authority = SvPVbyte(authority_sv, authority_len);
717 }
718 58 ▶ if (SvOK(scheme_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(scheme_sv)) { 58 0 58
719 58 scheme = SvPVbyte(scheme_sv, scheme_len);
720 }
721 58 ▶ if (SvOK(protocol_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(protocol_sv)) { 1 57 58
722 1 protocol = SvPVbyte(protocol_sv, protocol_len);
723 }
724  
725 58 ▶ if (ascii_equal_ci(method, (size_t)method_len, "CONNECT")) {
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(method, (size_t)method_len, "CONNECT")) { 1 57 58
726 1 ▶ if (!authority || authority_len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!authority || authority_len == 0) { 1 0 1
if (!authority || authority_len == 0) { 0 1 1
727 0 croak("request_headers(): CONNECT requires authority");
728 }
729 1 ▶ if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) { 1 0 1
730 1 ▶ if (protocol_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol_len == 0) { 0 1 1
731 0 croak("request_headers(): extended CONNECT requires nonempty protocol");
732 }
733 1 ▶ if (!scheme || scheme_len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!scheme || scheme_len == 0) { 1 0 1
if (!scheme || scheme_len == 0) { 0 1 1
734 0 croak("request_headers(): extended CONNECT requires scheme");
735 }
736 1 ▶ if (target_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (target_len == 0) { 0 1 1
737 0 croak("request_headers(): extended CONNECT requires a path target");
738 }
739 1 pseudo_count = 5;
740 }
741 else {
742 0 ▶ if (scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if (scheme) { 0 0 0
743 0 croak("request_headers(): ordinary CONNECT must not have scheme");
744 }
745 0 ▶ if (target_len != authority_len
Branch: 1 branch
BranchTrueFalseTotal
if (target_len != authority_len 0 0 0
746 0 ▶ || memcmp(target, authority, (size_t)target_len) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
|| memcmp(target, authority, (size_t)target_len) != 0) { 0 0 0
747 0 croak("request_headers(): ordinary CONNECT target must equal authority");
748 }
749 0 pseudo_count = 2;
750 }
751 }
752 else {
753 57 ▶ if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) { 0 57 57
754 0 croak("request_headers(): protocol metadata requires CONNECT");
755 }
756 57 ▶ if (target_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (target_len == 0) { 0 57 57
757 0 croak("request_headers(): HTTP/2 Request requires nonempty path target");
758 }
759 57 ▶ if (!scheme || scheme_len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!scheme || scheme_len == 0) { 57 0 57
if (!scheme || scheme_len == 0) { 0 57 57
760 0 croak("request_headers(): HTTP/2 Request requires scheme");
761 }
762 57 ▶ if (!authority || authority_len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!authority || authority_len == 0) { 57 0 57
if (!authority || authority_len == 0) { 0 57 57
763 0 croak("request_headers(): HTTP/2 Request requires authority");
764 }
765 57 pseudo_count = 4;
766 }
767  
768 58 normal_count = measure_native_headers(
769 aTHX_ &view.headers, &normal_name_bytes);
770 58 count = pseudo_count + normal_count;
771 58 nva = (nghttp2_nv *)calloc(1, count * sizeof(*nva) + normal_name_bytes);
772 58 ▶ if (!nva) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nva) { 0 58 58
773 0 croak("unable to allocate HTTP/2 Uniform native header block");
774 }
775 58 name_buffer = (char *)(nva + count);
776  
777 58 set_nv_from_sv(aTHX_ &nva[out++], ":method", 7, method_sv);
778 58 ▶ if (ascii_equal_ci(method, (size_t)method_len, "CONNECT")) {
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(method, (size_t)method_len, "CONNECT")) { 1 57 58
779 1 ▶ if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) { 1 0 1
780 1 set_nv_from_sv(aTHX_ &nva[out++], ":protocol", 9, protocol_sv);
781 1 set_nv_from_sv(aTHX_ &nva[out++], ":scheme", 7, scheme_sv);
782 1 set_nv_from_sv(aTHX_ &nva[out++], ":authority", 10, authority_sv);
783 1 set_nv_from_sv(aTHX_ &nva[out++], ":path", 5, target_sv);
784 }
785 else {
786 0 set_nv_from_sv(aTHX_ &nva[out++], ":authority", 10, authority_sv);
787 }
788 }
789 else {
790 57 set_nv_from_sv(aTHX_ &nva[out++], ":scheme", 7, scheme_sv);
791 57 set_nv_from_sv(aTHX_ &nva[out++], ":authority", 10, authority_sv);
792 57 set_nv_from_sv(aTHX_ &nva[out++], ":path", 5, target_sv);
793 }
794  
795 58 append_native_headers(aTHX_ nva, out, &view.headers, name_buffer);
796 58 *count_out = count;
797 58 return nva;
798 }
799  
800 static nghttp2_nv *
801 7220 uniform_response_to_nva(pTHX_ unblock_h2_session *ps, SV *message,
802 size_t *count_out)
803 {
804 uhttp_native_view view;
805 IV status_value;
806 size_t normal_count;
807 size_t normal_name_bytes;
808 size_t count;
809 nghttp2_nv *nva;
810 char *status_buffer;
811 char *name_buffer;
812  
813 7220 validate_uniform_native_view(
814 aTHX_ ps, message, UHTTP_KIND_RESPONSE, &view);
815  
816 7220 status_value = SvIV(view.status);
817 7220 ▶ if (status_value < 100 || status_value > 599) {
Branch: 2 branches
BranchTrueFalseTotal
if (status_value < 100 || status_value > 599) { 7220 0 7220
if (status_value < 100 || status_value > 599) { 0 7220 7220
818 0 croak("response_headers(): status must be an integer from 100 through 599");
819 }
820  
821 7220 normal_count = measure_native_headers(
822 aTHX_ &view.headers, &normal_name_bytes);
823 7220 count = 1 + normal_count;
824 7220 nva = (nghttp2_nv *)calloc(
825 7220 1, count * sizeof(*nva) + 3 + normal_name_bytes);
826 7220 ▶ if (!nva) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nva) { 0 7220 7220
827 0 croak("unable to allocate HTTP/2 Uniform native header block");
828 }
829  
830 7220 status_buffer = (char *)(nva + count);
831 7220 status_buffer[0] = (char)('0' + (status_value / 100) % 10);
832 7220 status_buffer[1] = (char)('0' + (status_value / 10) % 10);
833 7220 status_buffer[2] = (char)('0' + status_value % 10);
834 7220 name_buffer = status_buffer + 3;
835  
836 7220 nva[0].name = (uint8_t *)":status";
837 7220 nva[0].namelen = 7;
838 7220 nva[0].value = (uint8_t *)status_buffer;
839 7220 nva[0].valuelen = 3;
840 7220 nva[0].flags = NGHTTP2_NV_FLAG_NONE;
841  
842 7220 append_native_headers(aTHX_ nva, 1, &view.headers, name_buffer);
843 7220 *count_out = count;
844 7220 return nva;
845 }
846  
847 static int
848 73360 bytes_equal(const char *value, size_t value_len, const char *literal)
849 {
850 73360 size_t literal_len = strlen(literal);
851 return value_len == literal_len
852 73360 ▶ && memcmp(value, literal, literal_len) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(value, literal, literal_len) == 0; 36600 36760 73360
&& memcmp(value, literal, literal_len) == 0; 29283 7317 36600
853 }
854  
855 static int
856 7310 span_is_token(const char *value, size_t len)
857 {
858 size_t i;
859  
860 7310 ▶ if (!len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!len) { 0 7310 7310
861 0 return 0;
862 }
863 29271 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 21961 7310 29271
864 21961 ▶ if (!http_token_char((unsigned char)value[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!http_token_char((unsigned char)value[i])) { 0 21961 21961
865 0 return 0;
866 }
867 }
868 7310 return 1;
869 }
870  
871 static const char *
872 36 validate_received_normal_field(const unblock_h2_header_field *field)
873 {
874 size_t i;
875  
876 36 ▶ if (!field->namelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (!field->namelen) { 0 36 36
877 0 return "HTTP/2 field name must not be empty";
878 }
879  
880 356 ▶ for (i = 0; i < field->namelen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < field->namelen; i++) { 320 36 356
881 320 unsigned char ch = (unsigned char)field->name[i];
882 320 ▶ if (ch >= 'A' && ch <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'A' && ch <= 'Z') { 277 43 320
if (ch >= 'A' && ch <= 'Z') { 0 277 277
883 0 return "HTTP/2 field names must be lowercase";
884 }
885 320 ▶ if (!http_token_char(ch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!http_token_char(ch)) { 0 320 320
886 0 return "HTTP/2 field name must be an HTTP token";
887 }
888 }
889  
890 242 ▶ for (i = 0; i < field->valuelen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < field->valuelen; i++) { 206 36 242
891 206 unsigned char ch = (unsigned char)field->value[i];
892 206 ▶ if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) {
Branch: 4 branches
BranchTrueFalseTotal
if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) { 206 0 206
if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) { 206 0 206
if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) { 206 0 206
if (ch <= 0x08 || (ch >= 0x0a && ch <= 0x1f) || ch == 0x7f) { 0 206 206
893 0 return "HTTP/2 field value contains a prohibited control byte";
894 }
895 }
896  
897 36 ▶ if (bytes_equal(field->name, field->namelen, "connection")
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_equal(field->name, field->namelen, "connection") 36 0 36
898 36 ▶ || bytes_equal(field->name, field->namelen, "keep-alive")
Branch: 1 branch
BranchTrueFalseTotal
|| bytes_equal(field->name, field->namelen, "keep-alive") 36 0 36
899 36 ▶ || bytes_equal(field->name, field->namelen, "proxy-connection")
Branch: 1 branch
BranchTrueFalseTotal
|| bytes_equal(field->name, field->namelen, "proxy-connection") 36 0 36
900 36 ▶ || bytes_equal(field->name, field->namelen, "transfer-encoding")
Branch: 1 branch
BranchTrueFalseTotal
|| bytes_equal(field->name, field->namelen, "transfer-encoding") 36 0 36
901 36 ▶ || bytes_equal(field->name, field->namelen, "upgrade")) {
Branch: 1 branch
BranchTrueFalseTotal
|| bytes_equal(field->name, field->namelen, "upgrade")) { 0 36 36
902 0 return "HTTP/2 forbids connection-specific field";
903 }
904  
905 36 ▶ if (bytes_equal(field->name, field->namelen, "te")
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_equal(field->name, field->namelen, "te") 0 36 36
906 0 ▶ && !ascii_equal_ci(field->value, field->valuelen, "trailers")) {
Branch: 1 branch
BranchTrueFalseTotal
&& !ascii_equal_ci(field->value, field->valuelen, "trailers")) { 0 0 0
907 0 return "HTTP/2 TE is limited to trailers";
908 }
909  
910 36 return NULL;
911 }
912  
913 static const char *
914 7309 validate_request_target(const char *value, size_t len)
915 {
916 size_t i;
917  
918 7309 ▶ if (!len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!len) { 0 7309 7309
919 0 return "request_from_headers(): target must not be empty";
920 }
921 14893 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 7584 7309 14893
922 7584 unsigned char ch = (unsigned char)value[i];
923 7584 ▶ if (ch <= 0x20 || ch == 0x7f) {
Branch: 2 branches
BranchTrueFalseTotal
if (ch <= 0x20 || ch == 0x7f) { 7584 0 7584
if (ch <= 0x20 || ch == 0x7f) { 0 7584 7584
924 0 return "request_from_headers(): target contains spaces or control bytes";
925 }
926 }
927 7309 return NULL;
928 }
929  
930 static const char *
931 7309 validate_request_scheme(const char *value, size_t len)
932 {
933 size_t i;
934  
935 7309 ▶ if (!len
Branch: 1 branch
BranchTrueFalseTotal
if (!len 7309 0 7309
936 7309 ▶ || !((value[0] >= 'A' && value[0] <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
|| !((value[0] >= 'A' && value[0] <= 'Z') 7309 0 7309
|| !((value[0] >= 'A' && value[0] <= 'Z') 7309 0 7309
937 7309 ▶ || (value[0] >= 'a' && value[0] <= 'z'))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (value[0] >= 'a' && value[0] <= 'z'))) { 7309 0 7309
|| (value[0] >= 'a' && value[0] <= 'z'))) { 0 7309 7309
938 0 return "request_from_headers(): scheme must be a valid URI scheme";
939 }
940  
941 29280 ▶ for (i = 1; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; i++) { 21971 7309 29280
942 21971 unsigned char ch = (unsigned char)value[i];
943 21971 ▶ if (!((ch >= 'A' && ch <= 'Z')
Branch: 3 branches
BranchTrueFalseTotal
if (!((ch >= 'A' && ch <= 'Z') 21971 0 21971
if (!((ch >= 'A' && ch <= 'Z') 21971 0 21971
if (!((ch >= 'A' && ch <= 'Z') 0 0 0
944 21971 ▶ || (ch >= 'a' && ch <= 'z')
Branch: 2 branches
BranchTrueFalseTotal
|| (ch >= 'a' && ch <= 'z') 21971 0 21971
|| (ch >= 'a' && ch <= 'z') 0 21971 21971
945 0 ▶ || (ch >= '0' && ch <= '9')
Branch: 2 branches
BranchTrueFalseTotal
|| (ch >= '0' && ch <= '9') 0 0 0
|| (ch >= '0' && ch <= '9') 0 0 0
946 0 ▶ || ch == '+' || ch == '-' || ch == '.')) {
Branch: 2 branches
BranchTrueFalseTotal
|| ch == '+' || ch == '-' || ch == '.')) { 0 0 0
|| ch == '+' || ch == '-' || ch == '.')) { 0 0 0
947 0 return "request_from_headers(): scheme must be a valid URI scheme";
948 }
949 }
950 7309 return NULL;
951 }
952  
953 static const char *
954 7309 validate_request_authority(const char *value, size_t len)
955 {
956 size_t i;
957  
958 7309 ▶ if (!len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!len) { 0 7309 7309
959 0 return "request_from_headers(): authority must not be empty";
960 }
961  
962 94913 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 87604 7309 94913
963 87604 unsigned char ch = (unsigned char)value[i];
964 87604 ▶ if (ch <= 0x20 || ch == 0x7f
Branch: 2 branches
BranchTrueFalseTotal
if (ch <= 0x20 || ch == 0x7f 87604 0 87604
if (ch <= 0x20 || ch == 0x7f 87604 0 87604
965 87604 ▶ || ch == '/' || ch == '?' || ch == '#') {
Branch: 3 branches
BranchTrueFalseTotal
|| ch == '/' || ch == '?' || ch == '#') { 87604 0 87604
|| ch == '/' || ch == '?' || ch == '#') { 87604 0 87604
|| ch == '/' || ch == '?' || ch == '#') { 0 87604 87604
966 0 return "request_from_headers(): authority contains a prohibited delimiter or control byte";
967 }
968 }
969 7309 return NULL;
970 }
971  
972 static void
973 43845 set_native_span(uhttp_native_bytes *span, const char *data, size_t len)
974 {
975 43845 span->data = data;
976 43845 span->len = (STRLEN)len;
977 43845 }
978  
979 static U32
980 7352 received_native_flags(U32 kind, int complete)
981 {
982 7352 U32 flags = UHTTP_HEADERS_LOSSLESS | UHTTP_TRAILERS_LOSSLESS;
983  
984 7352 ▶ if (kind == UHTTP_KIND_REQUEST) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind == UHTTP_KIND_REQUEST) { 7309 43 7352
985 7309 flags |= UHTTP_TARGET_EXACT;
986 }
987  
988 7352 ▶ if (complete) {
Branch: 1 branch
BranchTrueFalseTotal
if (complete) { 7214 138 7352
989 7214 flags |= UHTTP_COMPLETE | UHTTP_HAS_BUFFERED_BODY;
990 }
991 else {
992 138 flags |= UHTTP_MUTABLE | UHTTP_BODY_MUTABLE
993 | UHTTP_TRAILERS_MUTABLE;
994 }
995  
996 7352 return flags;
997 }
998  
999 static SV *
1000 7309 request_from_native_headers(pTHX_ unblock_h2_session *ps,
1001 const unblock_h2_header_block *block,
1002 const char **error_out)
1003 {
1004 7309 const unblock_h2_header_field *method = NULL;
1005 7309 const unblock_h2_header_field *scheme = NULL;
1006 7309 const unblock_h2_header_field *authority = NULL;
1007 7309 const unblock_h2_header_field *path = NULL;
1008 7309 const unblock_h2_header_field *protocol = NULL;
1009 7309 uhttp_native_field *normal = NULL;
1010 7309 size_t normal_count = 0;
1011 size_t i;
1012 7309 int saw_normal = 0;
1013 7309 const char *error = NULL;
1014 uhttp_native_input input;
1015 SV *object;
1016  
1017 7309 ▶ if (block->count) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->count) { 7309 0 7309
1018 7309 normal = (uhttp_native_field *)calloc(block->count, sizeof(*normal));
1019 7309 ▶ if (!normal) {
Branch: 1 branch
BranchTrueFalseTotal
if (!normal) { 0 7309 7309
1020 0 croak("unable to allocate HTTP/2 native request fields");
1021 }
1022 }
1023  
1024 36560 ▶ for (i = 0; i < block->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; i++) { 29251 7309 36560
1025 29251 const unblock_h2_header_field *field = &block->fields[i];
1026  
1027 29251 ▶ if (field->namelen && field->name[0] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (field->namelen && field->name[0] == ':') { 29251 0 29251
if (field->namelen && field->name[0] == ':') { 29237 14 29251
1028 29237 ▶ if (saw_normal) {
Branch: 1 branch
BranchTrueFalseTotal
if (saw_normal) { 0 29237 29237
1029 0 error = "request_from_headers(): pseudo-header follows a regular field";
1030 0 break;
1031 }
1032  
1033 #define UHTTP_H2_PSEUDO(literal, slot) \
1034 if (bytes_equal(field->name, field->namelen, literal)) { \
1035 if (slot) { \
1036 error = "request_from_headers(): duplicate pseudo-header"; \
1037 break; \
1038 } \
1039 slot = field; \
1040 continue; \
1041 }
1042 29237 ▶ UHTTP_H2_PSEUDO(":method", method)
Branch: 2 branches
BranchTrueFalseTotal
UHTTP_H2_PSEUDO(":method", method) 7309 21928 29237
UHTTP_H2_PSEUDO(":method", method) 0 7309 7309
1043 21928 ▶ UHTTP_H2_PSEUDO(":scheme", scheme)
Branch: 2 branches
BranchTrueFalseTotal
UHTTP_H2_PSEUDO(":scheme", scheme) 7309 14619 21928
UHTTP_H2_PSEUDO(":scheme", scheme) 0 7309 7309
1044 14619 ▶ UHTTP_H2_PSEUDO(":authority", authority)
Branch: 2 branches
BranchTrueFalseTotal
UHTTP_H2_PSEUDO(":authority", authority) 7309 7310 14619
UHTTP_H2_PSEUDO(":authority", authority) 0 7309 7309
1045 7310 ▶ UHTTP_H2_PSEUDO(":path", path)
Branch: 2 branches
BranchTrueFalseTotal
UHTTP_H2_PSEUDO(":path", path) 7309 1 7310
UHTTP_H2_PSEUDO(":path", path) 0 7309 7309
1046 1 ▶ UHTTP_H2_PSEUDO(":protocol", protocol)
Branch: 2 branches
BranchTrueFalseTotal
UHTTP_H2_PSEUDO(":protocol", protocol) 1 0 1
UHTTP_H2_PSEUDO(":protocol", protocol) 0 1 1
1047 #undef UHTTP_H2_PSEUDO
1048 0 error = "request_from_headers(): unsupported pseudo-header";
1049 0 break;
1050 }
1051  
1052 14 saw_normal = 1;
1053 14 error = validate_received_normal_field(field);
1054 14 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 14 14
1055 0 break;
1056 }
1057  
1058 14 set_native_span(&normal[normal_count].name,
1059 14 field->name, field->namelen);
1060 14 set_native_span(&normal[normal_count].value,
1061 14 field->value, field->valuelen);
1062 14 normal_count++;
1063 }
1064  
1065 7309 ▶ if (!error && (!method || !method->valuelen)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!error && (!method || !method->valuelen)) { 7309 0 7309
if (!error && (!method || !method->valuelen)) { 7309 0 7309
if (!error && (!method || !method->valuelen)) { 0 7309 7309
1066 0 error = "request_from_headers(): missing :method";
1067 }
1068 7309 ▶ if (!error && !span_is_token(method->value, method->valuelen)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && !span_is_token(method->value, method->valuelen)) { 7309 0 7309
if (!error && !span_is_token(method->value, method->valuelen)) { 0 7309 7309
1069 0 error = "request_from_headers(): method must be an HTTP token";
1070 }
1071  
1072 7309 ▶ if (!error && ascii_equal_ci(method->value, method->valuelen, "CONNECT")) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && ascii_equal_ci(method->value, method->valuelen, "CONNECT")) { 7309 0 7309
if (!error && ascii_equal_ci(method->value, method->valuelen, "CONNECT")) { 1 7308 7309
1073 1 ▶ if (!authority || !authority->valuelen) {
Branch: 2 branches
BranchTrueFalseTotal
if (!authority || !authority->valuelen) { 1 0 1
if (!authority || !authority->valuelen) { 0 1 1
1074 0 error = "request_from_headers(): CONNECT requires :authority";
1075 }
1076 1 ▶ else if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
else if (protocol) { 1 0 1
1077 1 ▶ if (!protocol->valuelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (!protocol->valuelen) { 0 1 1
1078 0 error = "request_from_headers(): extended CONNECT requires nonempty :protocol";
1079 }
1080 1 ▶ else if (!scheme || !scheme->valuelen) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!scheme || !scheme->valuelen) { 1 0 1
else if (!scheme || !scheme->valuelen) { 0 1 1
1081 0 error = "request_from_headers(): extended CONNECT requires :scheme";
1082 }
1083 1 ▶ else if (!path || !path->valuelen) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!path || !path->valuelen) { 1 0 1
else if (!path || !path->valuelen) { 0 1 1
1084 0 error = "request_from_headers(): extended CONNECT requires :path";
1085 }
1086 }
1087 0 ▶ else if (scheme || path) {
Branch: 2 branches
BranchTrueFalseTotal
else if (scheme || path) { 0 0 0
else if (scheme || path) { 0 0 0
1088 0 error = "request_from_headers(): ordinary CONNECT must omit :scheme and :path";
1089 }
1090 }
1091 7308 ▶ else if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!error) { 7308 0 7308
1092 7308 ▶ if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) { 0 7308 7308
1093 0 error = "request_from_headers(): :protocol requires CONNECT";
1094 }
1095 7308 ▶ else if (!scheme || !scheme->valuelen) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!scheme || !scheme->valuelen) { 7308 0 7308
else if (!scheme || !scheme->valuelen) { 0 7308 7308
1096 0 error = "request_from_headers(): missing :scheme";
1097 }
1098 7308 ▶ else if (!path || !path->valuelen) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!path || !path->valuelen) { 7308 0 7308
else if (!path || !path->valuelen) { 0 7308 7308
1099 0 error = "request_from_headers(): missing :path";
1100 }
1101 7308 ▶ else if (!authority || !authority->valuelen) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!authority || !authority->valuelen) { 7308 0 7308
else if (!authority || !authority->valuelen) { 0 7308 7308
1102 0 error = "request_from_headers(): missing :authority";
1103 }
1104 }
1105  
1106 7309 ▶ if (!error && protocol
Branch: 2 branches
BranchTrueFalseTotal
if (!error && protocol 7309 0 7309
if (!error && protocol 1 7308 7309
1107 1 ▶ && !span_is_token(protocol->value, protocol->valuelen)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !span_is_token(protocol->value, protocol->valuelen)) { 0 1 1
1108 0 error = "request_from_headers(): protocol must be an HTTP token";
1109 }
1110 7309 ▶ if (!error && scheme) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && scheme) { 7309 0 7309
if (!error && scheme) { 7309 0 7309
1111 7309 error = validate_request_scheme(scheme->value, scheme->valuelen);
1112 }
1113 7309 ▶ if (!error && authority) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && authority) { 7309 0 7309
if (!error && authority) { 7309 0 7309
1114 7309 error = validate_request_authority(
1115 7309 authority->value, authority->valuelen);
1116 }
1117  
1118 7309 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 7309 0 7309
1119 const char *target;
1120 size_t target_len;
1121  
1122 7309 ▶ if (path) {
Branch: 1 branch
BranchTrueFalseTotal
if (path) { 7309 0 7309
1123 7309 target = path->value;
1124 7309 target_len = path->valuelen;
1125 }
1126 else {
1127 0 target = authority->value;
1128 0 target_len = authority->valuelen;
1129 }
1130  
1131 7309 error = validate_request_target(target, target_len);
1132 7309 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 7309 0 7309
1133 7309 uhttp_native_input_init(&input, UHTTP_KIND_REQUEST);
1134 14618 input.flags = received_native_flags(
1135 UHTTP_KIND_REQUEST,
1136 7309 (block->flags & NGHTTP2_FLAG_END_STREAM) ? 1 : 0);
1137 7309 ▶ if (input.flags & UHTTP_COMPLETE) {
Branch: 1 branch
BranchTrueFalseTotal
if (input.flags & UHTTP_COMPLETE) { 7209 100 7309
1138 7209 set_native_span(&input.body, "", 0);
1139 }
1140 7309 set_native_span(&input.version, "2", 1);
1141 7309 set_native_span(&input.method, method->value, method->valuelen);
1142 7309 set_native_span(&input.target, target, target_len);
1143 7309 ▶ if (scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if (scheme) { 7309 0 7309
1144 7309 set_native_span(
1145 7309 &input.scheme, scheme->value, scheme->valuelen);
1146 }
1147 7309 ▶ if (authority) {
Branch: 1 branch
BranchTrueFalseTotal
if (authority) { 7309 0 7309
1148 7309 set_native_span(
1149 7309 &input.authority, authority->value, authority->valuelen);
1150 }
1151 7309 ▶ if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) { 1 7308 7309
1152 1 set_native_span(
1153 1 &input.protocol, protocol->value, protocol->valuelen);
1154 }
1155 7309 input.headers = normal;
1156 7309 input.header_count = normal_count;
1157 7309 object = uhttp_native_from_validated(
1158 7309 aTHX_ &ps->uniform_api, &input, UHTTP_NATIVE_TRUSTED);
1159 7309 free(normal);
1160 7309 *error_out = NULL;
1161 7309 return object;
1162 }
1163 }
1164  
1165 0 free(normal);
1166 0 *error_out = error;
1167 0 return NULL;
1168 }
1169  
1170 static SV *
1171 43 response_from_native_headers(pTHX_ unblock_h2_session *ps,
1172 const unblock_h2_header_block *block,
1173 const char **error_out)
1174 {
1175 43 const unblock_h2_header_field *status = NULL;
1176 43 uhttp_native_field *normal = NULL;
1177 43 size_t normal_count = 0;
1178 size_t i;
1179 43 int saw_normal = 0;
1180 43 IV status_value = 0;
1181 43 const char *error = NULL;
1182 uhttp_native_input input;
1183 SV *object;
1184  
1185 43 ▶ if (block->count) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->count) { 43 0 43
1186 43 normal = (uhttp_native_field *)calloc(block->count, sizeof(*normal));
1187 43 ▶ if (!normal) {
Branch: 1 branch
BranchTrueFalseTotal
if (!normal) { 0 43 43
1188 0 croak("unable to allocate HTTP/2 native response fields");
1189 }
1190 }
1191  
1192 93 ▶ for (i = 0; i < block->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; i++) { 50 43 93
1193 50 const unblock_h2_header_field *field = &block->fields[i];
1194  
1195 50 ▶ if (field->namelen && field->name[0] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (field->namelen && field->name[0] == ':') { 50 0 50
if (field->namelen && field->name[0] == ':') { 43 7 50
1196 size_t j;
1197  
1198 43 ▶ if (saw_normal) {
Branch: 1 branch
BranchTrueFalseTotal
if (saw_normal) { 0 43 43
1199 0 error = "response_from_headers(): pseudo-header follows a regular field";
1200 0 break;
1201 }
1202 43 ▶ if (!bytes_equal(field->name, field->namelen, ":status")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes_equal(field->name, field->namelen, ":status")) { 0 43 43
1203 0 error = "response_from_headers(): unsupported pseudo-header";
1204 0 break;
1205 }
1206 43 ▶ if (status) {
Branch: 1 branch
BranchTrueFalseTotal
if (status) { 0 43 43
1207 0 error = "response_from_headers(): duplicate pseudo-header";
1208 0 break;
1209 }
1210  
1211 43 status = field;
1212 172 ▶ for (j = 0; j < field->valuelen; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < field->valuelen; j++) { 129 43 172
1213 129 unsigned char ch = (unsigned char)field->value[j];
1214 129 ▶ if (ch < '0' || ch > '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (ch < '0' || ch > '9') { 129 0 129
if (ch < '0' || ch > '9') { 0 129 129
1215 0 error = "response_from_headers(): status must be an integer from 100 through 599";
1216 0 break;
1217 }
1218 129 ▶ if (status_value > 599) {
Branch: 1 branch
BranchTrueFalseTotal
if (status_value > 599) { 0 129 129
1219 0 error = "response_from_headers(): status must be an integer from 100 through 599";
1220 0 break;
1221 }
1222 129 status_value = status_value * 10 + (ch - '0');
1223 }
1224 43 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 43 43
1225 0 break;
1226 }
1227 43 continue;
1228 }
1229  
1230 7 saw_normal = 1;
1231 7 error = validate_received_normal_field(field);
1232 7 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 7 7
1233 0 break;
1234 }
1235  
1236 7 set_native_span(&normal[normal_count].name,
1237 7 field->name, field->namelen);
1238 7 set_native_span(&normal[normal_count].value,
1239 7 field->value, field->valuelen);
1240 7 normal_count++;
1241 }
1242  
1243 43 ▶ if (!error && (!status || !status->valuelen
Branch: 3 branches
BranchTrueFalseTotal
if (!error && (!status || !status->valuelen 43 0 43
if (!error && (!status || !status->valuelen 43 0 43
if (!error && (!status || !status->valuelen 43 0 43
1244 43 ▶ || status_value < 100 || status_value > 599)) {
Branch: 2 branches
BranchTrueFalseTotal
|| status_value < 100 || status_value > 599)) { 43 0 43
|| status_value < 100 || status_value > 599)) { 0 43 43
1245 0 error = "response_from_headers(): status must be an integer from 100 through 599";
1246 }
1247  
1248 43 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 43 0 43
1249 43 ▶ int complete = (status_value >= 100 && status_value < 200)
Branch: 1 branch
BranchTrueFalseTotal
int complete = (status_value >= 100 && status_value < 200) 40 3 43
1250 ? 1
1251 86 ▶ : ((block->flags & NGHTTP2_FLAG_END_STREAM) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
: ((block->flags & NGHTTP2_FLAG_END_STREAM) ? 1 : 0); 43 0 43
1252  
1253 43 uhttp_native_input_init(&input, UHTTP_KIND_RESPONSE);
1254 43 input.flags = received_native_flags(UHTTP_KIND_RESPONSE, complete);
1255 43 ▶ if (complete) {
Branch: 1 branch
BranchTrueFalseTotal
if (complete) { 5 38 43
1256 5 set_native_span(&input.body, "", 0);
1257 }
1258 43 set_native_span(&input.version, "2", 1);
1259 43 input.status = status_value;
1260 43 input.headers = normal;
1261 43 input.header_count = normal_count;
1262 43 object = uhttp_native_from_validated(
1263 43 aTHX_ &ps->uniform_api, &input, UHTTP_NATIVE_TRUSTED);
1264 43 free(normal);
1265 43 *error_out = NULL;
1266 43 return object;
1267 }
1268  
1269 0 free(normal);
1270 0 *error_out = error;
1271 0 return NULL;
1272 }
1273  
1274 static AV *
1275 13 trailers_from_native_headers(pTHX_ const unblock_h2_header_block *block,
1276 const char **error_out)
1277 {
1278 13 AV *headers = newAV();
1279 size_t i;
1280  
1281 28 ▶ for (i = 0; i < block->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; i++) { 15 13 28
1282 15 const unblock_h2_header_field *field = &block->fields[i];
1283 const char *error;
1284 AV *pair;
1285  
1286 15 ▶ if (field->namelen && field->name[0] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (field->namelen && field->name[0] == ':') { 15 0 15
if (field->namelen && field->name[0] == ':') { 0 15 15
1287 0 SvREFCNT_dec((SV *)headers);
1288 0 *error_out = "apply_trailers(): unsupported pseudo-header";
1289 0 return NULL;
1290 }
1291  
1292 15 error = validate_received_normal_field(field);
1293 15 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 15 15
1294 0 SvREFCNT_dec((SV *)headers);
1295 0 *error_out = error;
1296 0 return NULL;
1297 }
1298  
1299 15 pair = newAV();
1300 15 av_push(pair, newSVpvn(field->name, field->namelen));
1301 15 av_push(pair, newSVpvn(field->value, field->valuelen));
1302 15 av_push(headers, newRV_noinc((SV *)pair));
1303 }
1304  
1305 13 *error_out = NULL;
1306 13 return headers;
1307 }
1308  
1309 static int
1310 5 header_block_has_name(const unblock_h2_header_block *block,
1311 const char *name)
1312 {
1313 size_t i;
1314  
1315 8 ▶ for (i = 0; i < block->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < block->count; i++) { 6 2 8
1316 6 ▶ if (bytes_equal(
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_equal( 3 3 6
1317 6 block->fields[i].name,
1318 6 block->fields[i].namelen,
1319 name)) {
1320 3 return 1;
1321 }
1322 }
1323 2 return 0;
1324 }
1325  
1326 static void
1327 7366 attach_native_header_result(pTHX_ unblock_h2_session *ps,
1328 const nghttp2_frame *frame, HV *hv,
1329 unblock_h2_header_block *block)
1330 {
1331 7366 const char *error = NULL;
1332 7366 SV *message = NULL;
1333 7366 AV *headers = NULL;
1334  
1335 7366 ▶ if (!block) {
Branch: 1 branch
BranchTrueFalseTotal
if (!block) { 0 7366 7366
1336 0 hv_store(hv, "header_error", 12,
1337 newSVpv("HTTP/2 header block state is missing", 0), 0);
1338 0 hv_store(hv, "header_error_code", 17,
1339 newSVuv(NGHTTP2_PROTOCOL_ERROR), 0);
1340 1 return;
1341 }
1342  
1343 7366 ▶ if (block->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (block->error) { 1 7365 7366
1344 1 hv_store(hv, "header_error", 12,
1345 newSVpv(block->error, 0), 0);
1346 1 hv_store(hv, "header_error_code", 17,
1347 newSVuv(block->error_code), 0);
1348 1 return;
1349 }
1350  
1351 7365 switch (block->category) {
1352 7309 case NGHTTP2_HCAT_REQUEST:
1353 7309 ▶ if (!ps->server) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->server) { 0 7309 7309
1354 0 error = "unexpected HTTP/2 request header category";
1355 0 break;
1356 }
1357 7309 message = request_from_native_headers(
1358 aTHX_ ps, block, &error);
1359 7309 break;
1360  
1361 40 case NGHTTP2_HCAT_RESPONSE:
1362 40 ▶ if (ps->server) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->server) { 0 40 40
1363 0 error = "unexpected HTTP/2 response header category";
1364 0 break;
1365 }
1366 40 message = response_from_native_headers(
1367 aTHX_ ps, block, &error);
1368 40 break;
1369  
1370 16 case NGHTTP2_HCAT_HEADERS:
1371 16 ▶ if (!ps->server && header_block_has_name(block, ":status")) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->server && header_block_has_name(block, ":status")) { 5 11 16
if (!ps->server && header_block_has_name(block, ":status")) { 3 2 5
1372 3 message = response_from_native_headers(
1373 aTHX_ ps, block, &error);
1374 }
1375 else {
1376 13 headers = trailers_from_native_headers(
1377 aTHX_ block, &error);
1378 }
1379 16 break;
1380  
1381 0 default:
1382 0 error = "unsupported HTTP/2 header category";
1383 0 break;
1384 }
1385  
1386 7365 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 7365 7365
1387 0 hv_store(hv, "header_error", 12, newSVpv(error, 0), 0);
1388 0 hv_store(hv, "header_error_code", 17,
1389 newSVuv(NGHTTP2_PROTOCOL_ERROR), 0);
1390 0 return;
1391 }
1392  
1393 7365 ▶ if (message) {
Branch: 1 branch
BranchTrueFalseTotal
if (message) { 7352 13 7365
1394 7352 hv_store(hv, "uniform_message", 15, message, 0);
1395 }
1396 13 ▶ else if (headers) {
Branch: 1 branch
BranchTrueFalseTotal
else if (headers) { 13 0 13
1397 13 hv_store(hv, "native_headers", 14,
1398 newRV_noinc((SV *)headers), 0);
1399 }
1400 }
1401  
1402 static int
1403 22765 call_scalar_callback(pTHX_ unblock_h2_session *ps, SV *callback, AV *args)
1404 {
1405 22765 dSP;
1406 int count;
1407 22765 int result = 0;
1408 I32 i;
1409 I32 len;
1410  
1411 22765 ▶ if (!callback || !SvOK(callback)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!callback || !SvOK(callback)) { 22765 0 22765
if (!callback || !SvOK(callback)) { 0 22765 22765
1412 0 return 0;
1413 }
1414  
1415 22765 ENTER;
1416 22765 SAVETMPS;
1417 22765 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 22765 22765
1418  
1419 22765 ▶ len = args ? av_len(args) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
len = args ? av_len(args) + 1 : 0; 22765 0 22765
1420 67798 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 45033 22765 67798
1421 45033 SV **arg = av_fetch(args, i, 0);
1422 45033 ▶ if (arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg) { 45033 0 45033
1423 45033 ▶ XPUSHs(*arg);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*arg); 0 45033 45033
1424 }
1425 }
1426  
1427 22765 PUTBACK;
1428 22765 count = call_sv(callback, G_SCALAR | G_EVAL);
1429 22765 SPAGAIN;
1430  
1431 22765 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 22765 0 22765
if (SvTRUE(ERRSV)) { 0 22765 22765
1432 0 set_callback_error(aTHX_ ps, "HTTP/2 callback failed");
1433 0 result = NGHTTP2_ERR_CALLBACK_FAILURE;
1434 }
1435 22765 ▶ else if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) { 22765 0 22765
1436 22765 SV *return_value = POPs;
1437 22765 ▶ if (SvOK(return_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(return_value)) { 22765 0 22765
1438 22765 result = (int)SvIV(return_value);
1439 }
1440 }
1441  
1442 22765 PUTBACK;
1443 22765 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 22765 0 22765
1444 22765 LEAVE;
1445 22765 return result;
1446 }
1447  
1448 static HV *
1449 7966 frame_to_hv(pTHX_ const nghttp2_frame *frame)
1450 {
1451 7966 HV *hv = newHV();
1452 AV *settings_av;
1453 AV *entry_av;
1454 size_t i;
1455  
1456 7966 hv_store(hv, "stream_id", 9, newSViv(frame->hd.stream_id), 0);
1457 7966 hv_store(hv, "type", 4, newSViv(frame->hd.type), 0);
1458 7966 hv_store(hv, "flags", 5, newSViv(frame->hd.flags), 0);
1459 7966 hv_store(hv, "length", 6, newSVuv((UV)frame->hd.length), 0);
1460  
1461 7966 ▶ if (frame->hd.type == NGHTTP2_HEADERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->hd.type == NGHTTP2_HEADERS) { 7366 600 7966
1462 7366 hv_store(hv, "headers_category", 16,
1463 newSViv(frame->headers.cat), 0);
1464 }
1465 600 ▶ else if (frame->hd.type == NGHTTP2_SETTINGS
Branch: 1 branch
BranchTrueFalseTotal
else if (frame->hd.type == NGHTTP2_SETTINGS 437 163 600
1466 437 ▶ && !(frame->hd.flags & NGHTTP2_FLAG_ACK)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !(frame->hd.flags & NGHTTP2_FLAG_ACK)) { 223 214 437
1467 223 settings_av = newAV();
1468 624 ▶ for (i = 0; i < frame->settings.niv; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < frame->settings.niv; i++) { 401 223 624
1469 401 entry_av = newAV();
1470 401 av_push(entry_av,
1471 newSViv((IV)frame->settings.iv[i].settings_id));
1472 401 av_push(entry_av,
1473 newSVuv((UV)frame->settings.iv[i].value));
1474 401 av_push(settings_av, newRV_noinc((SV *)entry_av));
1475 }
1476 223 hv_store(hv, "settings", 8,
1477 newRV_noinc((SV *)settings_av), 0);
1478 }
1479 377 ▶ else if (frame->hd.type == NGHTTP2_PING) {
Branch: 1 branch
BranchTrueFalseTotal
else if (frame->hd.type == NGHTTP2_PING) { 17 360 377
1480 17 hv_store(hv, "opaque_data", 11,
1481 newSVpvn((const char *)frame->ping.opaque_data, 8), 0);
1482 }
1483 360 ▶ else if (frame->hd.type == NGHTTP2_PRIORITY_UPDATE
Branch: 1 branch
BranchTrueFalseTotal
else if (frame->hd.type == NGHTTP2_PRIORITY_UPDATE 1 359 360
1484 2 ▶ && frame->ext.payload) {
Branch: 1 branch
BranchTrueFalseTotal
&& frame->ext.payload) { 1 0 1
1485 1 nghttp2_ext_priority_update *priority_update
1486 = (nghttp2_ext_priority_update *)frame->ext.payload;
1487 1 hv_store(hv, "prioritized_stream_id", 21,
1488 newSViv(priority_update->stream_id), 0);
1489 1 ▶ hv_store(hv, "priority_field_value", 20,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "priority_field_value", 20, 1 0 1
1490 newSVpvn(
1491 priority_update->field_value
1492 ? (const char *)priority_update->field_value
1493 : "",
1494 priority_update->field_value_len
1495 ), 0);
1496 }
1497 359 ▶ else if (frame->hd.type == NGHTTP2_GOAWAY) {
Branch: 1 branch
BranchTrueFalseTotal
else if (frame->hd.type == NGHTTP2_GOAWAY) { 5 354 359
1498 5 hv_store(hv, "last_stream_id", 14,
1499 newSViv(frame->goaway.last_stream_id), 0);
1500 5 hv_store(hv, "error_code", 10,
1501 newSVuv((UV)frame->goaway.error_code), 0);
1502 5 ▶ hv_store(hv, "debug_data", 10,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "debug_data", 10, 3 2 5
1503 newSVpvn(
1504 frame->goaway.opaque_data
1505 ? (const char *)frame->goaway.opaque_data
1506 : "",
1507 frame->goaway.opaque_data_len
1508 ), 0);
1509 }
1510  
1511 7966 return hv;
1512 }
1513  
1514 static ssize_t
1515 7259 provider_read_callback(
1516 nghttp2_session *session,
1517 int32_t stream_id,
1518 uint8_t *buf,
1519 size_t length,
1520 uint32_t *data_flags,
1521 nghttp2_data_source *source,
1522 void *user_data)
1523 {
1524 dTHX;
1525 7259 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1526 7259 unblock_h2_provider *provider = (unblock_h2_provider *)source->ptr;
1527 7259 dSP;
1528 int count;
1529 7259 ssize_t result = 0;
1530  
1531 7259 ▶ if (!provider || provider->released) {
Branch: 2 branches
BranchTrueFalseTotal
if (!provider || provider->released) { 7259 0 7259
if (!provider || provider->released) { 0 7259 7259
1532 0 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
1533 0 return 0;
1534 }
1535  
1536 7259 ENTER;
1537 7259 SAVETMPS;
1538 7259 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7259 7259
1539 7259 ▶ XPUSHs(sv_2mortal(newSViv(stream_id)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(stream_id))); 0 7259 7259
1540 7259 ▶ XPUSHs(sv_2mortal(newSVuv((UV)length)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv((UV)length))); 0 7259 7259
1541 7259 PUTBACK;
1542  
1543 7259 count = call_sv(provider->callback, G_ARRAY | G_EVAL);
1544 7259 SPAGAIN;
1545  
1546 7259 ▶ if (provider->released) {
Branch: 1 branch
BranchTrueFalseTotal
if (provider->released) { 0 7259 7259
1547 0 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
1548 0 result = 0;
1549 }
1550 7259 ▶ else if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvTRUE(ERRSV)) { 7259 0 7259
else if (SvTRUE(ERRSV)) { 0 7259 7259
1551 0 set_callback_error(aTHX_ ps, "HTTP/2 data provider failed");
1552 0 result = NGHTTP2_ERR_CALLBACK_FAILURE;
1553 }
1554 7259 ▶ else if (count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count == 0) { 8 7251 7259
1555 8 provider->deferred = 1;
1556 8 result = NGHTTP2_ERR_DEFERRED;
1557 }
1558 else {
1559 7251 SV **values = SP - count + 1;
1560 7251 SV *data_sv = values[0];
1561 7251 ▶ SV *eof_sv = count >= 2 ? values[1] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *eof_sv = count >= 2 ? values[1] : NULL; 7251 0 7251
1562 7251 ▶ SV *no_end_stream_sv = count >= 3 ? values[2] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *no_end_stream_sv = count >= 3 ? values[2] : NULL; 5 7246 7251
1563  
1564 7251 ▶ if (!SvOK(data_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(data_sv)) { 0 7251 7251
1565 0 provider->deferred = 1;
1566 0 result = NGHTTP2_ERR_DEFERRED;
1567 }
1568 else {
1569 STRLEN data_len;
1570 7251 const char *data = SvPVbyte(data_sv, data_len);
1571  
1572 7251 ▶ if ((size_t)data_len > length) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)data_len > length) { 0 7251 7251
1573 0 ▶ if (!ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->callback_error) { 0 0 0
1574 0 ps->callback_error = newSVpvf(
1575 "HTTP/2 data provider returned %lu bytes with a %lu byte limit",
1576 (unsigned long)data_len,
1577 (unsigned long)length
1578 );
1579 }
1580 0 result = NGHTTP2_ERR_CALLBACK_FAILURE;
1581 }
1582 else {
1583 7251 ▶ if (data_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_len) { 7250 1 7251
1584 7250 memcpy(buf, data, data_len);
1585 }
1586 7251 result = (ssize_t)data_len;
1587 7251 provider->deferred = 0;
1588  
1589 7251 ▶ if (eof_sv && SvTRUE(eof_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (eof_sv && SvTRUE(eof_sv)) { 7251 0 7251
if (eof_sv && SvTRUE(eof_sv)) { 7225 26 7251
1590 7225 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
1591 7225 ▶ if (no_end_stream_sv && SvTRUE(no_end_stream_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (no_end_stream_sv && SvTRUE(no_end_stream_sv)) { 5 7220 7225
if (no_end_stream_sv && SvTRUE(no_end_stream_sv)) { 5 0 5
1592 5 *data_flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
1593 }
1594 }
1595 26 ▶ else if (data_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (data_len == 0) { 0 26 26
1596 0 provider->deferred = 1;
1597 0 result = NGHTTP2_ERR_DEFERRED;
1598 }
1599 }
1600 }
1601 }
1602  
1603 7259 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 7251 8 7259
1604 7251 SP -= count;
1605 }
1606 7259 PUTBACK;
1607 7259 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 7259 0 7259
1608 7259 LEAVE;
1609 7259 return result;
1610 }
1611  
1612 static int
1613 7374 on_begin_headers_callback(nghttp2_session *session,
1614 const nghttp2_frame *frame,
1615 void *user_data)
1616 {
1617 dTHX;
1618 7374 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1619 AV *args;
1620 7374 int result = 0;
1621  
1622 7374 ▶ if (ps->server && frame->hd.type == NGHTTP2_HEADERS
Branch: 2 branches
BranchTrueFalseTotal
if (ps->server && frame->hd.type == NGHTTP2_HEADERS 7329 45 7374
if (ps->server && frame->hd.type == NGHTTP2_HEADERS 7329 0 7329
1623 7329 ▶ && frame->headers.cat == NGHTTP2_HCAT_REQUEST
Branch: 1 branch
BranchTrueFalseTotal
&& frame->headers.cat == NGHTTP2_HCAT_REQUEST 7318 11 7329
1624 7318 ▶ && frame->hd.stream_id > ps->guard.last_peer_id) {
Branch: 1 branch
BranchTrueFalseTotal
&& frame->hd.stream_id > ps->guard.last_peer_id) { 7318 0 7318
1625 7318 ▶ int32_t first = ps->guard.last_peer_id ? ps->guard.last_peer_id + 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
int32_t first = ps->guard.last_peer_id ? ps->guard.last_peer_id + 2 : 1; 7182 136 7318
1626 7318 result = ub_h2_remember(&ps->guard, session, first,
1627 7318 frame->hd.stream_id - 2, UB_H2_SKIPPED);
1628 7318 ▶ if (result != 0) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (result != 0) return result; 0 7318 7318
1629 7318 ps->guard.last_peer_id = frame->hd.stream_id;
1630 }
1631  
1632 7374 ▶ if (ps->cb_begin_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_begin_headers) { 7374 0 7374
1633 7374 args = newAV();
1634 7374 av_push(args, newSViv(frame->hd.stream_id));
1635 7374 av_push(args, newSViv(frame->hd.type));
1636 7374 av_push(args, newSViv(frame->hd.flags));
1637 7374 result = call_scalar_callback(
1638 aTHX_ ps, ps->cb_begin_headers, args);
1639 7374 SvREFCNT_dec((SV *)args);
1640 7374 ▶ if (result != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result != 0) { 0 7374 7374
1641 0 return result;
1642 }
1643 }
1644  
1645 7374 ▶ if (frame->hd.type == NGHTTP2_HEADERS
Branch: 1 branch
BranchTrueFalseTotal
if (frame->hd.type == NGHTTP2_HEADERS 7374 0 7374
1646 7374 ▶ && !start_header_block(aTHX_ ps, frame)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !start_header_block(aTHX_ ps, frame)) { 0 7374 7374
1647 0 ▶ if (!ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->callback_error) { 0 0 0
1648 0 ps->callback_error = newSVpv(
1649 "unable to allocate HTTP/2 receive header block", 0);
1650 }
1651 0 return NGHTTP2_ERR_CALLBACK_FAILURE;
1652 }
1653  
1654 7374 return 0;
1655 }
1656  
1657 static int
1658 29353 on_header_callback(nghttp2_session *session,
1659 const nghttp2_frame *frame,
1660 const uint8_t *name,
1661 size_t namelen,
1662 const uint8_t *value,
1663 size_t valuelen,
1664 uint8_t flags,
1665 void *user_data)
1666 {
1667 dTHX;
1668 29353 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1669 unblock_h2_header_block *block;
1670  
1671 29353 block = find_header_block(ps, frame->hd.stream_id);
1672 29353 ▶ if (!block) {
Branch: 1 branch
BranchTrueFalseTotal
if (!block) { 0 29353 29353
1673 0 ▶ if (!ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->callback_error) { 0 0 0
1674 0 ps->callback_error = newSVpv(
1675 "HTTP/2 receive header block state is missing", 0);
1676 }
1677 0 return NGHTTP2_ERR_CALLBACK_FAILURE;
1678 }
1679  
1680 29353 ▶ if (!append_header_field(
Branch: 1 branch
BranchTrueFalseTotal
if (!append_header_field( 0 29353 29353
1681 aTHX_ ps, block, name, namelen, value, valuelen)) {
1682 0 ▶ if (!ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->callback_error) { 0 0 0
1683 0 ps->callback_error = newSVpv(
1684 "unable to allocate HTTP/2 receive header field", 0);
1685 }
1686 0 return NGHTTP2_ERR_CALLBACK_FAILURE;
1687 }
1688  
1689 29353 return 0;
1690 }
1691  
1692 static int
1693 7963 on_frame_recv_callback(nghttp2_session *session,
1694 const nghttp2_frame *frame,
1695 void *user_data)
1696 {
1697 dTHX;
1698 7963 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1699 7963 unblock_h2_header_block *block = NULL;
1700 HV *frame_hv;
1701 AV *args;
1702 int result;
1703  
1704 7963 ▶ if (frame->hd.type == NGHTTP2_SETTINGS
Branch: 1 branch
BranchTrueFalseTotal
if (frame->hd.type == NGHTTP2_SETTINGS 437 7526 7963
1705 437 ▶ && !(frame->hd.flags & NGHTTP2_FLAG_ACK)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !(frame->hd.flags & NGHTTP2_FLAG_ACK)) { 223 214 437
1706 223 ps->guard.ready = 1;
1707 }
1708 7963 ▶ if (frame->hd.type == NGHTTP2_RST_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->hd.type == NGHTTP2_RST_STREAM) { 33 7930 7963
1709 33 result = ub_h2_peer_reset(&ps->guard, session, frame->hd.stream_id);
1710 33 ▶ if (result != 0) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (result != 0) return result; 0 33 33
1711 }
1712  
1713 7963 ▶ if (frame->hd.type == NGHTTP2_HEADERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->hd.type == NGHTTP2_HEADERS) { 7366 597 7963
1714 7366 block = take_header_block(ps, frame->hd.stream_id);
1715 }
1716  
1717 7963 ▶ if (!ps->cb_frame_recv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->cb_frame_recv) { 0 7963 7963
1718 0 free_header_block(aTHX_ block);
1719 0 return 0;
1720 }
1721  
1722 7963 frame_hv = frame_to_hv(aTHX_ frame);
1723 7963 ▶ if (frame->hd.type == NGHTTP2_HEADERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->hd.type == NGHTTP2_HEADERS) { 7366 597 7963
1724 7366 attach_native_header_result(
1725 aTHX_ ps, frame, frame_hv, block);
1726 }
1727 7963 free_header_block(aTHX_ block);
1728  
1729 7963 args = newAV();
1730 7963 av_push(args, newRV_noinc((SV *)frame_hv));
1731 7963 result = call_scalar_callback(aTHX_ ps, ps->cb_frame_recv, args);
1732 7963 SvREFCNT_dec((SV *)args);
1733 7963 return result;
1734 }
1735  
1736 static int
1737 92 on_data_chunk_recv_callback(nghttp2_session *session,
1738 uint8_t flags,
1739 int32_t stream_id,
1740 const uint8_t *data,
1741 size_t len,
1742 void *user_data)
1743 {
1744 dTHX;
1745 92 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1746 AV *args;
1747 int consume_rv;
1748 int result;
1749  
1750 92 consume_rv = nghttp2_session_consume_connection(session, len);
1751 92 ▶ if (consume_rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (consume_rv != 0) { 0 92 92
1752 0 ▶ if (!ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->callback_error) { 0 0 0
1753 0 ps->callback_error = newSVpvf(
1754 "nghttp2_session_consume_connection failed (%d): %s",
1755 consume_rv, nghttp2_strerror(consume_rv)
1756 );
1757 }
1758 0 return NGHTTP2_ERR_CALLBACK_FAILURE;
1759 }
1760  
1761 92 ▶ if (!ps->cb_data_chunk_recv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->cb_data_chunk_recv) { 0 92 92
1762 0 return 0;
1763 }
1764  
1765 92 args = newAV();
1766 92 av_push(args, newSViv(stream_id));
1767 92 av_push(args, newSVpvn((const char *)data, len));
1768 92 av_push(args, newSViv(flags));
1769 92 result = call_scalar_callback(aTHX_ ps, ps->cb_data_chunk_recv, args);
1770 92 SvREFCNT_dec((SV *)args);
1771 92 return result;
1772 }
1773  
1774 static int
1775 7333 on_stream_close_callback(nghttp2_session *session,
1776 int32_t stream_id,
1777 uint32_t error_code,
1778 void *user_data)
1779 {
1780 dTHX;
1781 7333 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1782 AV *args;
1783 7333 int result = 0;
1784  
1785 /* Retain the distinction between peer closure and a local reset: bytes
1786 * already in flight after our RST_STREAM are not peer protocol errors. */
1787 7333 ▶ if (stream_id & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (stream_id & 1) { 7333 0 7333
1788 14641 ▶ unsigned char kind = ps->guard.current_rst == stream_id
Branch: 1 branch
BranchTrueFalseTotal
unsigned char kind = ps->guard.current_rst == stream_id 7308 25 7333
1789 7308 ▶ || nghttp2_session_get_stream_remote_close(session, stream_id) == 1
Branch: 1 branch
BranchTrueFalseTotal
|| nghttp2_session_get_stream_remote_close(session, stream_id) == 1 7252 56 7308
1790 ? UB_H2_CLOSED : UB_H2_LOCAL_RESET;
1791 7333 result = ub_h2_remember(&ps->guard, session, stream_id, stream_id, kind);
1792 7333 ▶ if (result != 0) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (result != 0) return result; 0 7333 7333
1793 }
1794 7333 remove_provider(aTHX_ ps, stream_id);
1795 7333 remove_header_block(aTHX_ ps, stream_id);
1796  
1797 7333 ▶ if (!ps->cb_stream_close) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->cb_stream_close) { 0 7333 7333
1798 0 return 0;
1799 }
1800  
1801 7333 args = newAV();
1802 7333 av_push(args, newSViv(stream_id));
1803 7333 av_push(args, newSVuv((UV)error_code));
1804 7333 result = call_scalar_callback(aTHX_ ps, ps->cb_stream_close, args);
1805 7333 SvREFCNT_dec((SV *)args);
1806 7333 return result;
1807 }
1808  
1809 static int
1810 8 on_frame_not_send_callback(nghttp2_session *session,
1811 const nghttp2_frame *frame,
1812 int lib_error_code,
1813 void *user_data)
1814 {
1815 dTHX;
1816 8 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1817  
1818 8 ▶ if (frame->hd.type == NGHTTP2_HEADERS
Branch: 1 branch
BranchTrueFalseTotal
if (frame->hd.type == NGHTTP2_HEADERS 8 0 8
1819 8 ▶ && nghttp2_session_get_stream_remote_close(session, frame->hd.stream_id) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& nghttp2_session_get_stream_remote_close(session, frame->hd.stream_id) < 0) { 0 8 8
1820 0 remove_provider(aTHX_ ps, frame->hd.stream_id);
1821 }
1822  
1823 8 return 0;
1824 }
1825  
1826 static int
1827 3 on_invalid_frame_recv_callback(nghttp2_session *session,
1828 const nghttp2_frame *frame,
1829 int lib_error_code,
1830 void *user_data)
1831 {
1832 dTHX;
1833 3 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1834 AV *args;
1835 int result;
1836  
1837 3 ▶ if (!ps->cb_invalid_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->cb_invalid_frame) { 0 3 3
1838 0 return 0;
1839 }
1840  
1841 3 args = newAV();
1842 3 av_push(args, newRV_noinc((SV *)frame_to_hv(aTHX_ frame)));
1843 3 av_push(args, newSViv(lib_error_code));
1844 3 result = call_scalar_callback(aTHX_ ps, ps->cb_invalid_frame, args);
1845 3 SvREFCNT_dec((SV *)args);
1846 3 return result;
1847 }
1848  
1849 static int
1850 0 error_callback(nghttp2_session *session,
1851 int lib_error_code,
1852 const char *msg,
1853 size_t len,
1854 void *user_data)
1855 {
1856 dTHX;
1857 0 unblock_h2_session *ps = (unblock_h2_session *)user_data;
1858 AV *args;
1859 int result;
1860  
1861 0 ▶ if (!ps->cb_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->cb_error) { 0 0 0
1862 0 return 0;
1863 }
1864  
1865 0 args = newAV();
1866 0 av_push(args, newSViv(lib_error_code));
1867 0 ▶ av_push(args, newSVpvn(msg ? msg : "", msg ? len : 0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(args, newSVpvn(msg ? msg : "", msg ? len : 0)); 0 0 0
av_push(args, newSVpvn(msg ? msg : "", msg ? len : 0)); 0 0 0
1868 0 result = call_scalar_callback(aTHX_ ps, ps->cb_error, args);
1869 0 SvREFCNT_dec((SV *)args);
1870 0 return result;
1871 }
1872  
1873 static int
1874 231 configure_callbacks(pTHX_ nghttp2_session_callbacks **callbacks_out)
1875 {
1876 nghttp2_session_callbacks *callbacks;
1877 231 int rv = nghttp2_session_callbacks_new(&callbacks);
1878  
1879 231 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 231 231
1880 0 return rv;
1881 }
1882  
1883 231 nghttp2_session_callbacks_set_on_begin_headers_callback(
1884 callbacks, on_begin_headers_callback);
1885 231 nghttp2_session_callbacks_set_on_header_callback(
1886 callbacks, on_header_callback);
1887 231 nghttp2_session_callbacks_set_on_frame_recv_callback(
1888 callbacks, on_frame_recv_callback);
1889 231 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
1890 callbacks, on_data_chunk_recv_callback);
1891 231 nghttp2_session_callbacks_set_on_stream_close_callback(
1892 callbacks, on_stream_close_callback);
1893 231 nghttp2_session_callbacks_set_on_frame_not_send_callback(
1894 callbacks, on_frame_not_send_callback);
1895 231 nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(
1896 callbacks, on_invalid_frame_recv_callback);
1897 231 nghttp2_session_callbacks_set_error_callback2(callbacks, error_callback);
1898  
1899 231 *callbacks_out = callbacks;
1900 231 return 0;
1901 }
1902  
1903 static unblock_h2_session *
1904 231 new_session(pTHX_ HV *callbacks_hv, int server, size_t max_header_list_size)
1905 {
1906 unblock_h2_session *ps;
1907 uhttp_native_api uniform_api;
1908 231 nghttp2_session_callbacks *callbacks = NULL;
1909 231 nghttp2_option *option = NULL;
1910 int rv;
1911  
1912 231 ▶ if (!uhttp_native_init(aTHX_ &uniform_api, UHTTP_NATIVE_ABI_VERSION)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!uhttp_native_init(aTHX_ &uniform_api, UHTTP_NATIVE_ABI_VERSION)) { 0 231 231
1913 0 croak("Uniform::HTTP 0.06 native ABI is unavailable or incompatible");
1914 }
1915  
1916 231 ps = (unblock_h2_session *)calloc(1, sizeof(*ps));
1917 231 ▶ if (!ps) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) { 0 231 231
1918 0 croak("unable to allocate HTTP/2 session");
1919 }
1920 231 ps->uniform_api = uniform_api;
1921 231 ps->server = server ? 1 : 0;
1922 231 ▶ ps->guard.preface_left = server ? 24 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ps->guard.preface_left = server ? 24 : 0; 173 58 231
1923 231 ps->max_header_list_size = max_header_list_size;
1924  
1925 231 load_callbacks(aTHX_ ps, callbacks_hv);
1926 231 rv = configure_callbacks(aTHX_ &callbacks);
1927 231 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 231 231
1928 0 free_header_blocks(aTHX_ ps);
1929 0 release_callbacks(aTHX_ ps);
1930 0 free(ps->guard.history);
1931 0 free(ps);
1932 0 croak("nghttp2_session_callbacks_new failed (%d): %s",
1933 rv, nghttp2_strerror(rv));
1934 }
1935  
1936 231 rv = nghttp2_option_new(&option);
1937 231 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 231 231
1938 0 nghttp2_session_callbacks_del(callbacks);
1939 0 release_callbacks(aTHX_ ps);
1940 0 free(ps->guard.history);
1941 0 free(ps);
1942 0 croak("nghttp2_option_new failed (%d): %s",
1943 rv, nghttp2_strerror(rv));
1944 }
1945  
1946 231 nghttp2_option_set_no_auto_window_update(option, 1);
1947  
1948 231 ▶ if (server) {
Branch: 1 branch
BranchTrueFalseTotal
if (server) { 173 58 231
1949 173 nghttp2_option_set_builtin_recv_extension_type(
1950 option, NGHTTP2_PRIORITY_UPDATE);
1951 173 rv = nghttp2_session_server_new2(
1952 &ps->session, callbacks, ps, option);
1953 }
1954 else {
1955 58 rv = nghttp2_session_client_new2(
1956 &ps->session, callbacks, ps, option);
1957 }
1958  
1959 231 nghttp2_option_del(option);
1960 231 nghttp2_session_callbacks_del(callbacks);
1961  
1962 231 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 231 231
1963 0 release_callbacks(aTHX_ ps);
1964 0 free(ps->guard.history);
1965 0 free(ps);
1966 0 croak("nghttp2 session creation failed (%d): %s",
1967 rv, nghttp2_strerror(rv));
1968 }
1969  
1970 231 return ps;
1971 }
1972  
1973 static HV *
1974 178521 ub_http2_native_engine_hv(pTHX_ ub_http2_native_context *context)
1975 {
1976 178521 ▶ if (!context || !context->engine || !SvROK(context->engine)
Branch: 3 branches
BranchTrueFalseTotal
if (!context || !context->engine || !SvROK(context->engine) 178521 0 178521
if (!context || !context->engine || !SvROK(context->engine) 178521 0 178521
if (!context || !context->engine || !SvROK(context->engine) 178521 0 178521
1977 178521 ▶ || SvTYPE(SvRV(context->engine)) != SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(context->engine)) != SVt_PVHV) { 0 178521 178521
1978 0 croak("invalid Unblock::HTTP2 native transport context");
1979 }
1980  
1981 178521 return (HV *)SvRV(context->engine);
1982 }
1983  
1984 static int
1985 59524 ub_http2_native_engine_closed(pTHX_ ub_http2_native_context *context)
1986 {
1987 59524 HV *hv = ub_http2_native_engine_hv(aTHX_ context);
1988 59524 SV **value = hv_fetch(hv, "closed", 6, 0);
1989  
1990 59524 ▶ return value && SvOK(*value) && SvTRUE(*value) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return value && SvOK(*value) && SvTRUE(*value) ? 1 : 0; 59524 0 59524
return value && SvOK(*value) && SvTRUE(*value) ? 1 : 0; 59524 0 59524
return value && SvOK(*value) && SvTRUE(*value) ? 1 : 0; 0 59524 59524
1991 }
1992  
1993 static int
1994 29750 ub_http2_native_engine_in_call(pTHX_ ub_http2_native_context *context)
1995 {
1996 29750 HV *hv = ub_http2_native_engine_hv(aTHX_ context);
1997 29750 SV **value = hv_fetch(hv, "in_session_call", 15, 0);
1998  
1999 29750 ▶ return value && SvOK(*value) && SvTRUE(*value) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return value && SvOK(*value) && SvTRUE(*value) ? 1 : 0; 29750 0 29750
return value && SvOK(*value) && SvTRUE(*value) ? 1 : 0; 29750 0 29750
return value && SvOK(*value) && SvTRUE(*value) ? 1 : 0; 0 29750 29750
2000 }
2001  
2002 static void
2003 59498 ub_http2_native_set_engine_in_call(
2004 pTHX_ ub_http2_native_context *context, int value)
2005 {
2006 59498 HV *hv = ub_http2_native_engine_hv(aTHX_ context);
2007 59498 SV **slot = hv_fetch(hv, "in_session_call", 15, 0);
2008  
2009 59498 ▶ if (slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot) { 59498 0 59498
2010 59498 sv_setiv(*slot, value ? 1 : 0);
2011 }
2012 else {
2013 0 hv_store(hv, "in_session_call", 15, newSViv(value ? 1 : 0), 0);
2014 }
2015 59498 }
2016  
2017 static CV *
2018 29750 ub_http2_native_engine_method(
2019 pTHX_
2020 ub_http2_native_context *context,
2021 CV **slot,
2022 const char *name)
2023 {
2024 GV *gv;
2025 CV *cv;
2026  
2027 29750 ▶ if (*slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slot) { 29680 70 29750
2028 29680 return *slot;
2029 }
2030  
2031 70 ▶ if (!context || !context->engine || !SvROK(context->engine)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!context || !context->engine || !SvROK(context->engine)) { 70 0 70
if (!context || !context->engine || !SvROK(context->engine)) { 70 0 70
if (!context || !context->engine || !SvROK(context->engine)) { 0 70 70
2032 0 croak("invalid Unblock::HTTP2 native transport engine");
2033 }
2034  
2035 70 gv = gv_fetchmethod_autoload(
2036 SvSTASH(SvRV(context->engine)), name, 0);
2037 70 ▶ if (!gv || !(cv = GvCV(gv))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!gv || !(cv = GvCV(gv))) { 70 0 70
if (!gv || !(cv = GvCV(gv))) { 0 70 70
2038 0 croak("Unblock::HTTP2 native transport method %s is unavailable",
2039 name);
2040 }
2041  
2042 70 *slot = (CV *)SvREFCNT_inc((SV *)cv);
2043 70 return *slot;
2044 }
2045  
2046 static void
2047 29750 ub_http2_native_call_engine(
2048 pTHX_
2049 ub_http2_native_context *context,
2050 CV **slot,
2051 const char *name,
2052 SV *arg)
2053 {
2054 29750 CV *cv = ub_http2_native_engine_method(
2055 aTHX_ context, slot, name);
2056 29750 SV *error = NULL;
2057 29750 dSP;
2058  
2059 29750 ENTER;
2060 29750 SAVETMPS;
2061 29750 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 29750 0 29750
2062 29750 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 29750 29750
2063 29750 ▶ XPUSHs(context->engine);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(context->engine); 0 29750 29750
2064 29750 ▶ if (arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg) { 1 29749 29750
2065 1 ▶ XPUSHs(arg);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(arg); 0 1 1
2066 }
2067 29750 PUTBACK;
2068 29750 call_sv((SV *)cv, G_DISCARD | G_EVAL);
2069 29750 SPAGAIN;
2070 29750 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 29750 0 29750
if (SvTRUE(ERRSV)) { 0 29750 29750
2071 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
2072 }
2073 29750 PUTBACK;
2074 29750 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 29750 29750
2075 29750 LEAVE;
2076  
2077 29750 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 29750 29750
2078 0 sv_2mortal(error);
2079 0 croak("%s", SvPV_nolen(error));
2080 }
2081 29750 }
2082  
2083 static void
2084 1 ub_http2_native_finish_close(
2085 pTHX_ ub_http2_native_context *context, SV *error)
2086 {
2087 1 ▶ if (!ub_http2_native_engine_closed(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ub_http2_native_engine_closed(aTHX_ context)) { 1 0 1
2088 1 ub_http2_native_call_engine(
2089 aTHX_
2090 context,
2091 &context->finish_close_cv,
2092 "_finish_close",
2093 error
2094 );
2095 }
2096 1 }
2097  
2098 static int
2099 29749 ub_http2_native_after_session(
2100 pTHX_ ub_http2_native_context *context)
2101 {
2102 29749 HV *hv = ub_http2_native_engine_hv(aTHX_ context);
2103 29749 SV **pending = hv_fetch(hv, "close_pending", 13, 0);
2104  
2105 29749 ▶ if (pending && SvOK(*pending)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pending && SvOK(*pending)) { 29749 0 29749
if (pending && SvOK(*pending)) { 0 29749 29749
2106 0 SV *error = newSVsv(*pending);
2107 0 hv_delete(hv, "close_pending", 13, G_DISCARD);
2108 0 ub_http2_native_finish_close(aTHX_ context, error);
2109 0 SvREFCNT_dec(error);
2110 }
2111 else {
2112 29749 ub_http2_native_call_engine(
2113 aTHX_
2114 context,
2115 &context->after_session_cv,
2116 "_after_session_call",
2117 NULL
2118 );
2119 }
2120  
2121 29749 return ub_http2_native_engine_closed(aTHX_ context) ? 1 : 0;
2122 }
2123  
2124 static SV *
2125 29749 ub_http2_native_take_callback_error(
2126 pTHX_ unblock_h2_session *ps)
2127 {
2128 SV *error;
2129  
2130 29749 ▶ if (!ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->callback_error) { 29749 0 29749
2131 29749 return NULL;
2132 }
2133  
2134 0 error = newSVsv(ps->callback_error);
2135 0 clear_callback_error(aTHX_ ps);
2136 0 return error;
2137 }
2138  
2139 static void
2140 0 ub_http2_native_croak_error(
2141 pTHX_ ub_http2_native_context *context, SV *error)
2142 {
2143 const char *message;
2144  
2145 0 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 0 0 0
2146 0 error = newSVpv("Unblock::HTTP2 native transport failure", 0);
2147 }
2148  
2149 0 ub_http2_native_finish_close(aTHX_ context, error);
2150 0 message = SvPV_nolen(error);
2151 0 sv_2mortal(error);
2152 0 croak("%s", message);
2153 }
2154  
2155 static void *
2156 69 ub_http2_native_create(pTHX_ SV *engine)
2157 {
2158 ub_http2_native_context *context;
2159 HV *engine_hv;
2160 SV **session_value;
2161  
2162 69 ▶ if (!engine || !SvROK(engine)
Branch: 2 branches
BranchTrueFalseTotal
if (!engine || !SvROK(engine) 69 0 69
if (!engine || !SvROK(engine) 69 0 69
2163 69 ▶ || !sv_derived_from(engine, "Unblock::HTTP2::_Connection")
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(engine, "Unblock::HTTP2::_Connection") 69 0 69
2164 69 ▶ || SvTYPE(SvRV(engine)) != SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(engine)) != SVt_PVHV) { 0 69 69
2165 0 return NULL;
2166 }
2167  
2168 69 engine_hv = (HV *)SvRV(engine);
2169 69 session_value = hv_fetch(engine_hv, "session", 7, 0);
2170 69 ▶ if (!session_value || !SvOK(*session_value) || !SvROK(*session_value)
Branch: 3 branches
BranchTrueFalseTotal
if (!session_value || !SvOK(*session_value) || !SvROK(*session_value) 69 0 69
if (!session_value || !SvOK(*session_value) || !SvROK(*session_value) 69 0 69
if (!session_value || !SvOK(*session_value) || !SvROK(*session_value) 69 0 69
2171 69 ▶ || !sv_derived_from(
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from( 0 69 69
2172 *session_value, "Unblock::HTTP2::_nghttp2::Session")) {
2173 0 return NULL;
2174 }
2175  
2176 69 Newxz(context, 1, ub_http2_native_context);
2177 69 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 69 69
2178 0 return NULL;
2179 }
2180  
2181 69 context->engine = SvREFCNT_inc(engine);
2182 69 context->session_object = SvREFCNT_inc(*session_value);
2183 69 context->session = session_from_sv(aTHX_ context->session_object);
2184 69 return context;
2185 }
2186  
2187 static int
2188 27978 ub_http2_native_input(
2189 pTHX_
2190 void *opaque,
2191 const char *data,
2192 size_t length,
2193 size_t *consumed)
2194 {
2195 27978 ub_http2_native_context *context =
2196 (ub_http2_native_context *)opaque;
2197 unblock_h2_session *ps;
2198 27978 ssize_t rv = 0;
2199 27978 SV *error = NULL;
2200 int jump_status;
2201 int old_session_call;
2202 dJMPENV;
2203  
2204 27978 ▶ if (!context || !context->session || !consumed) {
Branch: 3 branches
BranchTrueFalseTotal
if (!context || !context->session || !consumed) { 27978 0 27978
if (!context || !context->session || !consumed) { 27978 0 27978
if (!context || !context->session || !consumed) { 0 27978 27978
2205 0 croak("invalid Unblock::HTTP2 native input context");
2206 }
2207 27978 ▶ if (!data && length) {
Branch: 2 branches
BranchTrueFalseTotal
if (!data && length) { 0 27978 27978
if (!data && length) { 0 0 0
2208 0 croak("native input data is NULL with a nonzero length");
2209 }
2210  
2211 27978 *consumed = 0;
2212 27978 ▶ if (ub_http2_native_engine_closed(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ub_http2_native_engine_closed(aTHX_ context)) { 0 27978 27978
2213 0 return UB_HTTP2_INPUT_CLOSED;
2214 }
2215 27978 ▶ if (ub_http2_native_engine_in_call(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ub_http2_native_engine_in_call(aTHX_ context)) { 0 27978 27978
2216 0 croak("native input cannot be called from an HTTP/2 callback");
2217 }
2218 27978 ▶ if (!length) {
Branch: 1 branch
BranchTrueFalseTotal
if (!length) { 0 27978 27978
2219 0 return UB_HTTP2_INPUT_OK;
2220 }
2221  
2222 27978 ps = context->session;
2223 27978 clear_callback_error(aTHX_ ps);
2224 27978 old_session_call = ps->in_session_call;
2225 27978 ub_http2_native_set_engine_in_call(aTHX_ context, 1);
2226 27978 ps->in_session_call = 1;
2227  
2228 27978 JMPENV_PUSH(jump_status);
2229 27978 ▶ if (jump_status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (jump_status == 0) { 27978 0 27978
2230 27978 rv = ub_h2_receive(&ps->guard,
2231 ps->session, (const uint8_t *)data, length);
2232 27978 JMPENV_POP;
2233 }
2234 else {
2235 0 JMPENV_POP;
2236 0 ps->in_session_call = old_session_call;
2237 0 ub_http2_native_set_engine_in_call(aTHX_ context, 0);
2238 0 drain_pending_free(aTHX_ ps);
2239 0 ▶ JMPENV_JUMP(jump_status);
Branch: 2 branches
BranchTrueFalseTotal
JMPENV_JUMP(jump_status); 0 0 0
JMPENV_JUMP(jump_status); 0 0 0
2240 }
2241  
2242 27978 ps->in_session_call = old_session_call;
2243 27978 ub_http2_native_set_engine_in_call(aTHX_ context, 0);
2244 27978 drain_pending_free(aTHX_ ps);
2245  
2246 27978 error = ub_http2_native_take_callback_error(aTHX_ ps);
2247 27978 ▶ if (!error && rv < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && rv < 0) { 27978 0 27978
if (!error && rv < 0) { 0 27978 27978
2248 0 error = newSVpvf(
2249 "nghttp2_session_mem_recv failed (%ld): %s",
2250 (long)rv, nghttp2_strerror((int)rv));
2251 }
2252 27978 ▶ if (!error && (size_t)rv != length) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && (size_t)rv != length) { 27978 0 27978
if (!error && (size_t)rv != length) { 0 27978 27978
2253 0 error = newSVpv(
2254 "native input: nghttp2 did not consume complete input", 0);
2255 }
2256 27978 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 27978 27978
2257 0 ub_http2_native_croak_error(aTHX_ context, error);
2258 }
2259  
2260 27978 *consumed = (size_t)rv;
2261 27978 return ub_http2_native_after_session(aTHX_ context)
2262 ? UB_HTTP2_INPUT_CLOSED
2263 27978 ▶ : UB_HTTP2_INPUT_OK;
Branch: 1 branch
BranchTrueFalseTotal
: UB_HTTP2_INPUT_OK; 0 27978 27978
2264 }
2265  
2266 static int
2267 1771 ub_http2_native_output(
2268 pTHX_
2269 void *opaque,
2270 ub_http2_output_sink_v1 sink,
2271 void *sink_context,
2272 size_t *produced)
2273 {
2274 1771 ub_http2_native_context *context =
2275 (ub_http2_native_context *)opaque;
2276 unblock_h2_session *ps;
2277 1771 const uint8_t *data = NULL;
2278 1771 ssize_t rv = 0;
2279 1771 size_t total = 0;
2280 1771 SV *error = NULL;
2281 1771 int sink_result = UB_HTTP2_OUTPUT_CONTINUE;
2282 int jump_status;
2283 int old_session_call;
2284 dJMPENV;
2285  
2286 1771 ▶ if (!context || !context->session || !produced) {
Branch: 3 branches
BranchTrueFalseTotal
if (!context || !context->session || !produced) { 1771 0 1771
if (!context || !context->session || !produced) { 1771 0 1771
if (!context || !context->session || !produced) { 0 1771 1771
2287 0 croak("invalid Unblock::HTTP2 native output context");
2288 }
2289  
2290 1771 *produced = 0;
2291 1771 ▶ if (ub_http2_native_engine_closed(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ub_http2_native_engine_closed(aTHX_ context)) { 0 1771 1771
2292 0 return UB_HTTP2_OUTPUT_CLOSED;
2293 }
2294 1771 ▶ if (ub_http2_native_engine_in_call(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ub_http2_native_engine_in_call(aTHX_ context)) { 0 1771 1771
2295 0 croak("native output cannot be called from an HTTP/2 callback");
2296 }
2297  
2298 1771 ps = context->session;
2299 1771 ▶ if (!nghttp2_session_want_write(ps->session)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nghttp2_session_want_write(ps->session)) { 0 1771 1771
2300 0 return UB_HTTP2_OUTPUT_OK;
2301 }
2302 1771 ▶ if (!sink) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sink) { 0 1771 1771
2303 0 croak("native output requires a sink while output is pending");
2304 }
2305  
2306 1771 clear_callback_error(aTHX_ ps);
2307 1771 old_session_call = ps->in_session_call;
2308 1771 ub_http2_native_set_engine_in_call(aTHX_ context, 1);
2309 1771 ps->in_session_call = 1;
2310  
2311 1771 JMPENV_PUSH(jump_status);
2312 1771 ▶ if (jump_status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (jump_status == 0) { 1771 0 1771
2313 for (;;) {
2314 5258 rv = nghttp2_session_mem_send(ps->session, &data);
2315 5258 ▶ if (rv <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv <= 0) { 1770 3488 5258
2316 1770 break;
2317 }
2318  
2319 3488 sink_result = sink(
2320 aTHX_
2321 sink_context,
2322 (const char *)data,
2323 (size_t)rv
2324 );
2325 3488 total += (size_t)rv;
2326  
2327 3488 ▶ if (sink_result == UB_HTTP2_OUTPUT_PAUSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (sink_result == UB_HTTP2_OUTPUT_PAUSE) { 1 3487 3488
2328 1 rv = 0;
2329 1 break;
2330 }
2331 3487 ▶ if (sink_result != UB_HTTP2_OUTPUT_CONTINUE) {
Branch: 1 branch
BranchTrueFalseTotal
if (sink_result != UB_HTTP2_OUTPUT_CONTINUE) { 0 3487 3487
2332 0 error = newSVpv(
2333 "native output sink reported a fatal failure", 0);
2334 0 rv = 0;
2335 0 break;
2336 }
2337 }
2338 1771 JMPENV_POP;
2339 }
2340 else {
2341 0 JMPENV_POP;
2342 0 ps->in_session_call = old_session_call;
2343 0 ub_http2_native_set_engine_in_call(aTHX_ context, 0);
2344 0 drain_pending_free(aTHX_ ps);
2345 0 ▶ JMPENV_JUMP(jump_status);
Branch: 2 branches
BranchTrueFalseTotal
JMPENV_JUMP(jump_status); 0 0 0
JMPENV_JUMP(jump_status); 0 0 0
2346 }
2347  
2348 1771 ps->in_session_call = old_session_call;
2349 1771 ub_http2_native_set_engine_in_call(aTHX_ context, 0);
2350 1771 drain_pending_free(aTHX_ ps);
2351  
2352 1771 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 1771 0 1771
2353 1771 error = ub_http2_native_take_callback_error(aTHX_ ps);
2354 }
2355 else {
2356 0 clear_callback_error(aTHX_ ps);
2357 }
2358  
2359 1771 ▶ if (!error && rv < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && rv < 0) { 1771 0 1771
if (!error && rv < 0) { 0 1771 1771
2360 0 error = newSVpvf(
2361 "nghttp2_session_mem_send failed (%ld): %s",
2362 (long)rv, nghttp2_strerror((int)rv));
2363 }
2364  
2365 1771 *produced = total;
2366 1771 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 1771 1771
2367 0 ub_http2_native_croak_error(aTHX_ context, error);
2368 }
2369  
2370 1771 return ub_http2_native_after_session(aTHX_ context)
2371 ? UB_HTTP2_OUTPUT_CLOSED
2372 1771 ▶ : UB_HTTP2_OUTPUT_OK;
Branch: 1 branch
BranchTrueFalseTotal
: UB_HTTP2_OUTPUT_OK; 0 1771 1771
2373 }
2374  
2375 static int
2376 2 ub_http2_native_want_read(pTHX_ void *opaque)
2377 {
2378 2 ub_http2_native_context *context =
2379 (ub_http2_native_context *)opaque;
2380  
2381 2 ▶ if (!context || !context->session) {
Branch: 2 branches
BranchTrueFalseTotal
if (!context || !context->session) { 2 0 2
if (!context || !context->session) { 0 2 2
2382 0 croak("invalid Unblock::HTTP2 native transport context");
2383 }
2384 2 ▶ if (ub_http2_native_engine_closed(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ub_http2_native_engine_closed(aTHX_ context)) { 0 2 2
2385 0 return 0;
2386 }
2387  
2388 2 return nghttp2_session_want_read(context->session->session) ? 1 : 0;
2389 }
2390  
2391 static int
2392 22 ub_http2_native_want_write(pTHX_ void *opaque)
2393 {
2394 22 ub_http2_native_context *context =
2395 (ub_http2_native_context *)opaque;
2396  
2397 22 ▶ if (!context || !context->session) {
Branch: 2 branches
BranchTrueFalseTotal
if (!context || !context->session) { 22 0 22
if (!context || !context->session) { 0 22 22
2398 0 croak("invalid Unblock::HTTP2 native transport context");
2399 }
2400 22 ▶ if (ub_http2_native_engine_closed(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ub_http2_native_engine_closed(aTHX_ context)) { 0 22 22
2401 0 return 0;
2402 }
2403  
2404 22 return nghttp2_session_want_write(context->session->session) ? 1 : 0;
2405 }
2406  
2407 static int
2408 1 ub_http2_native_eof(pTHX_ void *opaque)
2409 {
2410 1 ub_http2_native_context *context =
2411 (ub_http2_native_context *)opaque;
2412 SV *error;
2413  
2414 1 ▶ if (!context || !context->session) {
Branch: 2 branches
BranchTrueFalseTotal
if (!context || !context->session) { 1 0 1
if (!context || !context->session) { 0 1 1
2415 0 croak("invalid Unblock::HTTP2 native transport context");
2416 }
2417 1 ▶ if (ub_http2_native_engine_closed(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ub_http2_native_engine_closed(aTHX_ context)) { 0 1 1
2418 0 return UB_HTTP2_INPUT_CLOSED;
2419 }
2420 1 ▶ if (ub_http2_native_engine_in_call(aTHX_ context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ub_http2_native_engine_in_call(aTHX_ context)) { 0 1 1
2421 0 croak("native EOF cannot be called from an HTTP/2 callback");
2422 }
2423  
2424 1 error = newSVpv("HTTP/2 transport reached EOF", 0);
2425 1 ub_http2_native_finish_close(aTHX_ context, error);
2426 1 SvREFCNT_dec(error);
2427 1 return UB_HTTP2_INPUT_CLOSED;
2428 }
2429  
2430 static void
2431 69 ub_http2_native_destroy(pTHX_ void *opaque)
2432 {
2433 69 ub_http2_native_context *context =
2434 (ub_http2_native_context *)opaque;
2435  
2436 PERL_UNUSED_CONTEXT;
2437 69 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 69 69
2438 0 return;
2439 }
2440  
2441 69 ▶ if (context->after_session_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->after_session_cv) { 69 0 69
2442 69 SvREFCNT_dec((SV *)context->after_session_cv);
2443 }
2444 69 ▶ if (context->finish_close_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->finish_close_cv) { 1 68 69
2445 1 SvREFCNT_dec((SV *)context->finish_close_cv);
2446 }
2447 69 ▶ if (context->session_object) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->session_object) { 69 0 69
2448 69 SvREFCNT_dec(context->session_object);
2449 }
2450 69 ▶ if (context->engine) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->engine) { 69 0 69
2451 69 SvREFCNT_dec(context->engine);
2452 }
2453 69 Safefree(context);
2454 }
2455  
2456 static const ub_http2_native_ops_v1 ub_http2_native_ops = {
2457 UB_HTTP2_NATIVE_ABI_VERSION,
2458 sizeof(ub_http2_native_ops_v1),
2459 "Unblock::HTTP2 native transport",
2460 ub_http2_native_create,
2461 ub_http2_native_input,
2462 ub_http2_native_eof,
2463 ub_http2_native_destroy,
2464 ub_http2_native_output,
2465 ub_http2_native_want_read,
2466 ub_http2_native_want_write
2467 };
2468  
2469 typedef struct {
2470 SV *buffer;
2471 int pause_after_first;
2472 size_t chunks;
2473 } ub_http2_test_sink_context;
2474  
2475 static int
2476 3483 ub_http2_test_output_sink(
2477 pTHX_
2478 void *opaque,
2479 const char *data,
2480 size_t length)
2481 {
2482 3483 ub_http2_test_sink_context *sink =
2483 (ub_http2_test_sink_context *)opaque;
2484  
2485 3483 ▶ if (!sink || !sink->buffer) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sink || !sink->buffer) { 3483 0 3483
if (!sink || !sink->buffer) { 0 3483 3483
2486 0 return UB_HTTP2_OUTPUT_ERROR;
2487 }
2488  
2489 3483 sv_catpvn(sink->buffer, data, (STRLEN)length);
2490 3483 sink->chunks++;
2491 3484 return sink->pause_after_first && sink->chunks >= 1
2492 ? UB_HTTP2_OUTPUT_PAUSE
2493 3483 ▶ : UB_HTTP2_OUTPUT_CONTINUE;
Branch: 2 branches
BranchTrueFalseTotal
: UB_HTTP2_OUTPUT_CONTINUE; 1 3482 3483
: UB_HTTP2_OUTPUT_CONTINUE; 1 0 1
2494 }
2495  
2496 typedef struct {
2497 ub_http2_native_context *destination;
2498 size_t moved;
2499 int input_status;
2500 } ub_http2_test_bridge_context;
2501  
2502 static int
2503 5 ub_http2_test_bridge_sink(
2504 pTHX_
2505 void *opaque,
2506 const char *data,
2507 size_t length)
2508 {
2509 5 ub_http2_test_bridge_context *bridge =
2510 (ub_http2_test_bridge_context *)opaque;
2511 5 size_t consumed = 0;
2512  
2513 5 ▶ if (!bridge || !bridge->destination) {
Branch: 2 branches
BranchTrueFalseTotal
if (!bridge || !bridge->destination) { 5 0 5
if (!bridge || !bridge->destination) { 0 5 5
2514 0 return UB_HTTP2_OUTPUT_ERROR;
2515 }
2516  
2517 10 bridge->input_status = ub_http2_native_input(
2518 aTHX_
2519 5 bridge->destination,
2520 data,
2521 length,
2522 &consumed
2523 );
2524 5 ▶ if (consumed != length) {
Branch: 1 branch
BranchTrueFalseTotal
if (consumed != length) { 0 5 5
2525 0 return UB_HTTP2_OUTPUT_ERROR;
2526 }
2527  
2528 5 bridge->moved += consumed;
2529 5 return UB_HTTP2_OUTPUT_CONTINUE;
2530 }
2531  
2532  
2533 MODULE = Unblock::HTTP2 PACKAGE = Unblock::HTTP2::_nghttp2
2534  
2535 PROTOTYPES: DISABLE
2536  
2537 int
2538 _available()
2539 CODE:
2540 231 ▶ RETVAL = nghttp2_version(0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = nghttp2_version(0) ? 1 : 0; 203 28 231
2541 OUTPUT:
2542 RETVAL
2543  
2544 UV
2545 _native_transport_operations_address()
2546 CODE:
2547 1 ▶ RETVAL = PTR2UV(&ub_http2_native_ops);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&ub_http2_native_ops); 0 1 1
2548 OUTPUT:
2549 RETVAL
2550  
2551 UV
2552 _native_transport_operations_size()
2553 CODE:
2554 1 ▶ RETVAL = (UV)sizeof(ub_http2_native_ops_v1);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)sizeof(ub_http2_native_ops_v1); 0 1 1
2555 OUTPUT:
2556 RETVAL
2557  
2558 const char *
2559 version_string()
2560 CODE:
2561 0 nghttp2_info *info = nghttp2_version(0);
2562 0 ▶ RETVAL = info ? info->version_str : "unknown";
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = info ? info->version_str : "unknown"; 0 0 0
2563 OUTPUT:
2564 RETVAL
2565  
2566 MODULE = Unblock::HTTP2 PACKAGE = Unblock::HTTP2::_nghttp2::Session
2567  
2568 SV *
2569 _new_client_xs(class, callbacks_hv, max_header_list_size)
2570 char *class
2571 HV *callbacks_hv
2572 UV max_header_list_size
2573 PREINIT:
2574 unblock_h2_session *ps;
2575 CODE:
2576 58 ▶ if (max_header_list_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (max_header_list_size == 0) { 0 58 58
2577 0 croak("max_header_list_size must be positive");
2578 }
2579 58 ps = new_session(
2580 aTHX_ callbacks_hv, 0, (size_t)max_header_list_size);
2581 58 RETVAL = newSV(0);
2582 58 sv_setref_pv(RETVAL, class, (void *)ps);
2583 OUTPUT:
2584 RETVAL
2585  
2586 SV *
2587 _new_server_xs(class, callbacks_hv, max_header_list_size)
2588 char *class
2589 HV *callbacks_hv
2590 UV max_header_list_size
2591 PREINIT:
2592 unblock_h2_session *ps;
2593 CODE:
2594 173 ▶ if (max_header_list_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (max_header_list_size == 0) { 0 173 173
2595 0 croak("max_header_list_size must be positive");
2596 }
2597 173 ps = new_session(
2598 aTHX_ callbacks_hv, 1, (size_t)max_header_list_size);
2599 173 RETVAL = newSV(0);
2600 173 sv_setref_pv(RETVAL, class, (void *)ps);
2601 OUTPUT:
2602 RETVAL
2603  
2604 void
2605 DESTROY(self)
2606 SV *self
2607 PREINIT:
2608 unblock_h2_session *ps;
2609 unblock_h2_provider *provider;
2610 CODE:
2611 231 ▶ if (!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) { 0 231 231
2612 0 XSRETURN_EMPTY;
2613 }
2614 231 ps = INT2PTR(unblock_h2_session *, SvIV(SvRV(self)));
2615 231 ▶ if (!ps) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) { 0 231 231
2616 0 XSRETURN_EMPTY;
2617 }
2618  
2619 231 ▶ if (ps->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->session) { 231 0 231
2620 231 nghttp2_session_del(ps->session);
2621 231 ps->session = NULL;
2622 }
2623  
2624 231 drain_pending_free(aTHX_ ps);
2625 231 provider = ps->providers;
2626 231 ps->providers = NULL;
2627 241 ▶ while (provider) {
Branch: 1 branch
BranchTrueFalseTotal
while (provider) { 10 231 241
2628 10 unblock_h2_provider *next = provider->next;
2629 10 free_provider(aTHX_ provider);
2630 10 provider = next;
2631 }
2632 231 free_header_blocks(aTHX_ ps);
2633 231 release_callbacks(aTHX_ ps);
2634 231 free(ps->guard.history);
2635 231 free(ps);
2636 231 sv_setiv(SvRV(self), 0);
2637  
2638 IV
2639 mem_recv(self, data)
2640 SV *self
2641 SV *data
2642 PREINIT:
2643 unblock_h2_session *ps;
2644 STRLEN len;
2645 const char *bytes;
2646 ssize_t rv;
2647 CODE:
2648 27741 ps = session_from_sv(aTHX_ self);
2649 27741 ▶ if (ps->in_session_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->in_session_call) { 0 27741 27741
2650 0 croak("mem_recv called from inside an HTTP/2 callback");
2651 }
2652 27741 bytes = SvPVbyte(data, len);
2653 27741 clear_callback_error(aTHX_ ps);
2654  
2655 27741 ENTER;
2656 27741 SAVEINT(ps->in_session_call);
2657 27741 ps->in_session_call = 1;
2658 27741 rv = ub_h2_receive(&ps->guard,
2659 ps->session, (const uint8_t *)bytes, (size_t)len);
2660 27741 LEAVE;
2661 27741 drain_pending_free(aTHX_ ps);
2662 27741 croak_callback_error(aTHX_ ps);
2663  
2664 27741 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 1 27740 27741
2665 1 croak("nghttp2_session_mem_recv failed (%ld): %s",
2666 (long)rv, nghttp2_strerror((int)rv));
2667 }
2668 27740 ▶ RETVAL = (IV)rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)rv; 27714 26 27740
2669 OUTPUT:
2670 RETVAL
2671  
2672 SV *
2673 mem_send(self)
2674 SV *self
2675 PREINIT:
2676 unblock_h2_session *ps;
2677 5915 const uint8_t *data = NULL;
2678 5915 ssize_t rv = 0;
2679 SV *output;
2680 CODE:
2681 5915 ps = session_from_sv(aTHX_ self);
2682 5915 ▶ if (ps->in_session_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->in_session_call) { 0 5915 5915
2683 0 croak("mem_send called from inside an HTTP/2 callback");
2684 }
2685 5915 clear_callback_error(aTHX_ ps);
2686 5915 output = newSVpvn("", 0);
2687  
2688 5915 ENTER;
2689 5915 SAVEINT(ps->in_session_call);
2690 5915 ps->in_session_call = 1;
2691 for (;;) {
2692 17656 rv = nghttp2_session_mem_send(ps->session, &data);
2693 17656 ▶ if (rv <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv <= 0) { 5915 11741 17656
2694 5915 break;
2695 }
2696 11741 sv_catpvn(output, (const char *)data, (STRLEN)rv);
2697 }
2698 5915 LEAVE;
2699 5915 drain_pending_free(aTHX_ ps);
2700  
2701 5915 ▶ if (ps->callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->callback_error) { 0 5915 5915
2702 0 SvREFCNT_dec(output);
2703 0 croak_callback_error(aTHX_ ps);
2704 }
2705  
2706 5915 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 0 5915 5915
2707 0 SvREFCNT_dec(output);
2708 0 croak("nghttp2_session_mem_send failed (%ld): %s",
2709 (long)rv, nghttp2_strerror((int)rv));
2710 }
2711 5915 RETVAL = output;
2712 OUTPUT:
2713 RETVAL
2714  
2715 int
2716 want_read(self)
2717 SV *self
2718 PREINIT:
2719 unblock_h2_session *ps;
2720 CODE:
2721 32 ps = session_from_sv(aTHX_ self);
2722 32 RETVAL = nghttp2_session_want_read(ps->session);
2723 OUTPUT:
2724 RETVAL
2725  
2726 int
2727 want_write(self)
2728 SV *self
2729 PREINIT:
2730 unblock_h2_session *ps;
2731 CODE:
2732 21466 ps = session_from_sv(aTHX_ self);
2733 21466 RETVAL = nghttp2_session_want_write(ps->session);
2734 OUTPUT:
2735 RETVAL
2736  
2737 int
2738 submit_settings(self, settings_hv)
2739 SV *self
2740 HV *settings_hv
2741 PREINIT:
2742 unblock_h2_session *ps;
2743 nghttp2_settings_entry entries[9];
2744 236 ▶ size_t count = 0;
Branch: 1 branch
BranchTrueFalseTotal
size_t count = 0; 236 0 236
2745 SV **value;
2746 int rv;
2747 CODE:
2748 236 ps = session_from_sv(aTHX_ self);
2749  
2750 236 ▶ if ((value = hv_fetch(settings_hv, "header_table_size", 17, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value = hv_fetch(settings_hv, "header_table_size", 17, 0))) { 3 233 236
2751 3 entries[count].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
2752 3 entries[count++].value = (uint32_t)SvUV(*value);
2753 }
2754 236 ▶ if ((value = hv_fetch(settings_hv, "enable_push", 11, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value = hv_fetch(settings_hv, "enable_push", 11, 0))) { 58 178 236
2755 58 entries[count].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
2756 58 entries[count++].value = SvTRUE(*value) ? 1 : 0;
2757 }
2758 236 ▶ if ((value = hv_fetch(settings_hv, "max_concurrent_streams", 22, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value = hv_fetch(settings_hv, "max_concurrent_streams", 22, 0))) { 232 4 236
2759 232 entries[count].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
2760 232 entries[count++].value = (uint32_t)SvUV(*value);
2761 }
2762 236 ▶ if ((value = hv_fetch(settings_hv, "initial_window_size", 19, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value = hv_fetch(settings_hv, "initial_window_size", 19, 0))) { 6 230 236
2763 6 entries[count].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
2764 6 entries[count++].value = (uint32_t)SvUV(*value);
2765 }
2766 236 ▶ if ((value = hv_fetch(settings_hv, "max_frame_size", 14, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value = hv_fetch(settings_hv, "max_frame_size", 14, 0))) { 0 236 236
2767 0 entries[count].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
2768 0 entries[count++].value = (uint32_t)SvUV(*value);
2769 }
2770 236 ▶ if ((value = hv_fetch(settings_hv, "max_header_list_size", 20, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value = hv_fetch(settings_hv, "max_header_list_size", 20, 0))) { 231 5 236
2771 231 entries[count].settings_id = NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE;
2772 231 entries[count++].value = (uint32_t)SvUV(*value);
2773 }
2774 236 ▶ if ((value = hv_fetch(settings_hv, "enable_connect_protocol", 23, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value = hv_fetch(settings_hv, "enable_connect_protocol", 23, 0))) { 173 63 236
2775 173 entries[count].settings_id = NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL;
2776 173 entries[count++].value = SvTRUE(*value) ? 1 : 0;
2777 }
2778 236 ▶ if ((value = hv_fetch(settings_hv, "no_rfc7540_priorities", 21, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((value = hv_fetch(settings_hv, "no_rfc7540_priorities", 21, 0))) { 231 5 236
2779 231 entries[count].settings_id = NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES;
2780 231 entries[count++].value = SvTRUE(*value) ? 1 : 0;
2781 }
2782  
2783 236 rv = nghttp2_submit_settings(
2784 ps->session, NGHTTP2_FLAG_NONE, entries, count);
2785 236 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 236 236
2786 0 croak("nghttp2_submit_settings failed (%d): %s",
2787 rv, nghttp2_strerror(rv));
2788 }
2789 236 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 208 28 236
2790 OUTPUT:
2791 RETVAL
2792  
2793 UV
2794 remote_setting(self, setting_id)
2795 SV *self
2796 int setting_id
2797 PREINIT:
2798 unblock_h2_session *ps;
2799 CODE:
2800 4272 ps = session_from_sv(aTHX_ self);
2801 4272 ▶ RETVAL = (UV)nghttp2_session_get_remote_settings(
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)nghttp2_session_get_remote_settings( 4244 28 4272
2802 ps->session, (nghttp2_settings_id)setting_id);
2803 OUTPUT:
2804 RETVAL
2805  
2806 int
2807 consume_stream(self, stream_id, size)
2808 SV *self
2809 int stream_id
2810 UV size
2811 PREINIT:
2812 unblock_h2_session *ps;
2813 int rv;
2814 CODE:
2815 87 ps = session_from_sv(aTHX_ self);
2816 87 rv = nghttp2_session_consume_stream(
2817 ps->session, stream_id, (size_t)size);
2818 87 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 87 87
2819 0 croak("nghttp2_session_consume_stream failed (%d): %s",
2820 rv, nghttp2_strerror(rv));
2821 }
2822 87 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 70 17 87
2823 OUTPUT:
2824 RETVAL
2825  
2826 int
2827 get_stream_remote_close(self, stream_id)
2828 SV *self
2829 int stream_id
2830 PREINIT:
2831 unblock_h2_session *ps;
2832 int rv;
2833 CODE:
2834 0 ps = session_from_sv(aTHX_ self);
2835 0 rv = nghttp2_session_get_stream_remote_close(ps->session, stream_id);
2836 0 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 0 0
2837 OUTPUT:
2838 RETVAL
2839  
2840 int
2841 get_stream_local_close(self, stream_id)
2842 SV *self
2843 int stream_id
2844 PREINIT:
2845 unblock_h2_session *ps;
2846 int rv;
2847 CODE:
2848 0 ps = session_from_sv(aTHX_ self);
2849 0 rv = nghttp2_session_get_stream_local_close(ps->session, stream_id);
2850 0 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 0 0
2851 OUTPUT:
2852 RETVAL
2853  
2854 int
2855 _submit_request_uniform_native(self, message, provider_sv)
2856 SV *self
2857 SV *message
2858 SV *provider_sv
2859 PREINIT:
2860 unblock_h2_session *ps;
2861 nghttp2_nv *nva;
2862 size_t nvlen;
2863 nghttp2_data_provider data_provider;
2864 58 nghttp2_data_provider *data_provider_ptr = NULL;
2865 58 ▶ unblock_h2_provider *provider = NULL;
Branch: 1 branch
BranchTrueFalseTotal
unblock_h2_provider *provider = NULL; 58 0 58
2866 int32_t stream_id;
2867 CODE:
2868 58 ps = session_from_sv(aTHX_ self);
2869 58 nva = uniform_request_to_nva(aTHX_ ps, message, &nvlen);
2870  
2871 58 ▶ if (SvOK(provider_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(provider_sv)) { 21 37 58
2872 21 ▶ if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) { 21 0 21
if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) { 0 21 21
2873 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
2874 0 croak("request data provider must be a coderef");
2875 }
2876 21 provider = (unblock_h2_provider *)calloc(1, sizeof(*provider));
2877 21 ▶ if (!provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (!provider) { 0 21 21
2878 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
2879 0 croak("unable to allocate HTTP/2 data provider");
2880 }
2881 21 provider->callback = newSVsv(provider_sv);
2882 21 data_provider.source.ptr = provider;
2883 21 data_provider.read_callback = provider_read_callback;
2884 21 data_provider_ptr = &data_provider;
2885 }
2886  
2887 58 stream_id = nghttp2_submit_request(
2888 ps->session, NULL, nva, nvlen, data_provider_ptr, NULL);
2889 58 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 58 0 58
2890  
2891 58 ▶ if (stream_id < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stream_id < 0) { 0 58 58
2892 0 ▶ if (provider) free_provider(aTHX_ provider);
Branch: 1 branch
BranchTrueFalseTotal
if (provider) free_provider(aTHX_ provider); 0 0 0
2893 0 croak("nghttp2_submit_request failed (%d): %s",
2894 (int)stream_id, nghttp2_strerror((int)stream_id));
2895 }
2896  
2897 58 ▶ if (provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (provider) { 21 37 58
2898 21 provider->stream_id = stream_id;
2899 21 add_provider(aTHX_ ps, provider);
2900 }
2901 58 ▶ RETVAL = stream_id;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = stream_id; 34 24 58
2902 OUTPUT:
2903 RETVAL
2904  
2905 int
2906 _submit_request_native(self, headers_av, provider_sv)
2907 SV *self
2908 AV *headers_av
2909 SV *provider_sv
2910 PREINIT:
2911 unblock_h2_session *ps;
2912 nghttp2_nv *nva;
2913 size_t nvlen;
2914 nghttp2_data_provider data_provider;
2915 0 nghttp2_data_provider *data_provider_ptr = NULL;
2916 0 ▶ unblock_h2_provider *provider = NULL;
Branch: 1 branch
BranchTrueFalseTotal
unblock_h2_provider *provider = NULL; 0 0 0
2917 int32_t stream_id;
2918 CODE:
2919 0 ps = session_from_sv(aTHX_ self);
2920 0 nva = headers_to_nva(aTHX_ headers_av, &nvlen);
2921  
2922 0 ▶ if (SvOK(provider_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(provider_sv)) { 0 0 0
2923 0 ▶ if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) { 0 0 0
if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) { 0 0 0
2924 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
2925 0 croak("request data provider must be a coderef");
2926 }
2927 0 provider = (unblock_h2_provider *)calloc(1, sizeof(*provider));
2928 0 ▶ if (!provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (!provider) { 0 0 0
2929 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
2930 0 croak("unable to allocate HTTP/2 data provider");
2931 }
2932 0 provider->callback = newSVsv(provider_sv);
2933 0 data_provider.source.ptr = provider;
2934 0 data_provider.read_callback = provider_read_callback;
2935 0 data_provider_ptr = &data_provider;
2936 }
2937  
2938 0 stream_id = nghttp2_submit_request(
2939 ps->session, NULL, nva, nvlen, data_provider_ptr, NULL);
2940 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
2941  
2942 0 ▶ if (stream_id < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stream_id < 0) { 0 0 0
2943 0 ▶ if (provider) free_provider(aTHX_ provider);
Branch: 1 branch
BranchTrueFalseTotal
if (provider) free_provider(aTHX_ provider); 0 0 0
2944 0 croak("nghttp2_submit_request failed (%d): %s",
2945 (int)stream_id, nghttp2_strerror((int)stream_id));
2946 }
2947  
2948 0 ▶ if (provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (provider) { 0 0 0
2949 0 provider->stream_id = stream_id;
2950 0 add_provider(aTHX_ ps, provider);
2951 }
2952 0 ▶ RETVAL = stream_id;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = stream_id; 0 0 0
2953 OUTPUT:
2954 RETVAL
2955  
2956 int
2957 _submit_response_uniform_no_body_native(self, stream_id, message)
2958 SV *self
2959 int stream_id
2960 SV *message
2961 PREINIT:
2962 unblock_h2_session *ps;
2963 nghttp2_nv *nva;
2964 size_t nvlen;
2965 int rv;
2966 CODE:
2967 2 ps = session_from_sv(aTHX_ self);
2968 2 nva = uniform_response_to_nva(aTHX_ ps, message, &nvlen);
2969 2 rv = nghttp2_submit_response(ps->session, stream_id, nva, nvlen, NULL);
2970 2 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 2 0 2
2971 2 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 2 2
2972 0 croak("nghttp2_submit_response failed (%d): %s",
2973 rv, nghttp2_strerror(rv));
2974 }
2975 2 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 2 2
2976 OUTPUT:
2977 RETVAL
2978  
2979 int
2980 _submit_response_uniform_streaming_native(self, stream_id, message, provider_sv)
2981 SV *self
2982 int stream_id
2983 SV *message
2984 SV *provider_sv
2985 PREINIT:
2986 unblock_h2_session *ps;
2987 nghttp2_nv *nva;
2988 size_t nvlen;
2989 nghttp2_data_provider data_provider;
2990 unblock_h2_provider *provider;
2991 int rv;
2992 CODE:
2993 7215 ps = session_from_sv(aTHX_ self);
2994 7215 ▶ if (find_provider(ps, stream_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_provider(ps, stream_id)) { 0 7215 7215
2995 0 croak("stream %d already has a data provider", stream_id);
2996 }
2997 7215 ▶ if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) { 7215 0 7215
if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) { 0 7215 7215
2998 0 croak("response data provider must be a coderef");
2999 }
3000  
3001 7215 nva = uniform_response_to_nva(aTHX_ ps, message, &nvlen);
3002 7215 provider = (unblock_h2_provider *)calloc(1, sizeof(*provider));
3003 7215 ▶ if (!provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (!provider) { 0 7215 7215
3004 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
3005 0 croak("unable to allocate HTTP/2 data provider");
3006 }
3007 7215 provider->stream_id = stream_id;
3008 7215 provider->callback = newSVsv(provider_sv);
3009 7215 data_provider.source.ptr = provider;
3010 7215 data_provider.read_callback = provider_read_callback;
3011  
3012 7215 rv = nghttp2_submit_response(
3013 ps->session, stream_id, nva, nvlen, &data_provider);
3014 7215 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 7215 0 7215
3015 7215 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 7215 7215
3016 0 free_provider(aTHX_ provider);
3017 0 croak("nghttp2_submit_response failed (%d): %s",
3018 rv, nghttp2_strerror(rv));
3019 }
3020  
3021 7215 add_provider(aTHX_ ps, provider);
3022 7215 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 7197 18 7215
3023 OUTPUT:
3024 RETVAL
3025  
3026 int
3027 _submit_response_no_body_native(self, stream_id, headers_av)
3028 SV *self
3029 int stream_id
3030 AV *headers_av
3031 PREINIT:
3032 unblock_h2_session *ps;
3033 nghttp2_nv *nva;
3034 size_t nvlen;
3035 int rv;
3036 CODE:
3037 0 ps = session_from_sv(aTHX_ self);
3038 0 nva = headers_to_nva(aTHX_ headers_av, &nvlen);
3039 0 rv = nghttp2_submit_response(ps->session, stream_id, nva, nvlen, NULL);
3040 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
3041 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
3042 0 croak("nghttp2_submit_response failed (%d): %s",
3043 rv, nghttp2_strerror(rv));
3044 }
3045 0 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 0 0
3046 OUTPUT:
3047 RETVAL
3048  
3049 int
3050 _submit_response_streaming_native(self, stream_id, headers_av, provider_sv)
3051 SV *self
3052 int stream_id
3053 AV *headers_av
3054 SV *provider_sv
3055 PREINIT:
3056 unblock_h2_session *ps;
3057 nghttp2_nv *nva;
3058 size_t nvlen;
3059 nghttp2_data_provider data_provider;
3060 unblock_h2_provider *provider;
3061 int rv;
3062 CODE:
3063 0 ps = session_from_sv(aTHX_ self);
3064 0 ▶ if (find_provider(ps, stream_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_provider(ps, stream_id)) { 0 0 0
3065 0 croak("stream %d already has a data provider", stream_id);
3066 }
3067 0 ▶ if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) { 0 0 0
if (!SvROK(provider_sv) || SvTYPE(SvRV(provider_sv)) != SVt_PVCV) { 0 0 0
3068 0 croak("response data provider must be a coderef");
3069 }
3070  
3071 0 nva = headers_to_nva(aTHX_ headers_av, &nvlen);
3072 0 provider = (unblock_h2_provider *)calloc(1, sizeof(*provider));
3073 0 ▶ if (!provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (!provider) { 0 0 0
3074 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
3075 0 croak("unable to allocate HTTP/2 data provider");
3076 }
3077 0 provider->stream_id = stream_id;
3078 0 provider->callback = newSVsv(provider_sv);
3079 0 data_provider.source.ptr = provider;
3080 0 data_provider.read_callback = provider_read_callback;
3081  
3082 0 rv = nghttp2_submit_response(
3083 ps->session, stream_id, nva, nvlen, &data_provider);
3084 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
3085 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
3086 0 free_provider(aTHX_ provider);
3087 0 croak("nghttp2_submit_response failed (%d): %s",
3088 rv, nghttp2_strerror(rv));
3089 }
3090  
3091 0 add_provider(aTHX_ ps, provider);
3092 0 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 0 0
3093 OUTPUT:
3094 RETVAL
3095  
3096 int
3097 _submit_response_headers_uniform_native(self, stream_id, message, end_stream)
3098 SV *self
3099 int stream_id
3100 SV *message
3101 int end_stream
3102 PREINIT:
3103 unblock_h2_session *ps;
3104 nghttp2_nv *nva;
3105 size_t nvlen;
3106 int32_t rv;
3107 uint8_t flags;
3108 CODE:
3109 3 ps = session_from_sv(aTHX_ self);
3110 3 nva = uniform_response_to_nva(aTHX_ ps, message, &nvlen);
3111 3 flags = end_stream ? NGHTTP2_FLAG_END_STREAM : NGHTTP2_FLAG_NONE;
3112 3 rv = nghttp2_submit_headers(
3113 ps->session, flags, stream_id, NULL, nva, nvlen, NULL);
3114 3 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 3 0 3
3115 3 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 0 3 3
3116 0 croak("nghttp2_submit_headers failed (%d): %s",
3117 (int)rv, nghttp2_strerror((int)rv));
3118 }
3119 3 ▶ RETVAL = (int)rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)rv; 1 2 3
3120 OUTPUT:
3121 RETVAL
3122  
3123 int
3124 _submit_headers_native(self, stream_id, headers_av, end_stream)
3125 SV *self
3126 int stream_id
3127 AV *headers_av
3128 int end_stream
3129 PREINIT:
3130 unblock_h2_session *ps;
3131 nghttp2_nv *nva;
3132 size_t nvlen;
3133 int32_t rv;
3134 uint8_t flags;
3135 CODE:
3136 0 ps = session_from_sv(aTHX_ self);
3137 0 nva = headers_to_nva(aTHX_ headers_av, &nvlen);
3138 0 flags = end_stream ? NGHTTP2_FLAG_END_STREAM : NGHTTP2_FLAG_NONE;
3139 0 rv = nghttp2_submit_headers(
3140 ps->session, flags, stream_id, NULL, nva, nvlen, NULL);
3141 0 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 0 0 0
3142 0 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 0 0 0
3143 0 croak("nghttp2_submit_headers failed (%d): %s",
3144 (int)rv, nghttp2_strerror((int)rv));
3145 }
3146 0 ▶ RETVAL = (int)rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)rv; 0 0 0
3147 OUTPUT:
3148 RETVAL
3149  
3150 int
3151 _submit_trailer_native(self, stream_id, headers_av)
3152 SV *self
3153 int stream_id
3154 AV *headers_av
3155 PREINIT:
3156 unblock_h2_session *ps;
3157 nghttp2_nv *nva;
3158 size_t nvlen;
3159 int rv;
3160 CODE:
3161 5 ps = session_from_sv(aTHX_ self);
3162 5 nva = headers_to_nva(aTHX_ headers_av, &nvlen);
3163 5 rv = nghttp2_submit_trailer(ps->session, stream_id, nva, nvlen);
3164 5 ▶ if (nva) free(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) free(nva); 5 0 5
3165 5 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 5 5
3166 0 croak("nghttp2_submit_trailer failed (%d): %s",
3167 rv, nghttp2_strerror(rv));
3168 }
3169 5 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 2 3 5
3170 OUTPUT:
3171 RETVAL
3172  
3173 int
3174 submit_rst_stream(self, stream_id, error_code)
3175 SV *self
3176 int stream_id
3177 unsigned int error_code
3178 PREINIT:
3179 unblock_h2_session *ps;
3180 int rv;
3181 CODE:
3182 49 ps = session_from_sv(aTHX_ self);
3183 49 rv = nghttp2_submit_rst_stream(
3184 ps->session, NGHTTP2_FLAG_NONE, stream_id, error_code);
3185 49 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 49 49
3186 0 croak("nghttp2_submit_rst_stream failed (%d): %s",
3187 rv, nghttp2_strerror(rv));
3188 }
3189 49 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 43 6 49
3190 OUTPUT:
3191 RETVAL
3192  
3193 int
3194 _submit_priority_update_native(self, stream_id, field_value)
3195 SV *self
3196 int stream_id
3197 SV *field_value
3198 PREINIT:
3199 unblock_h2_session *ps;
3200 1 ▶ STRLEN len = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN len = 0; 1 0 1
3201 const uint8_t *value;
3202 int rv;
3203 CODE:
3204 1 ps = session_from_sv(aTHX_ self);
3205 1 value = (const uint8_t *)SvPVbyte(field_value, len);
3206 1 ▶ if (len > 16380) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 16380) { 0 1 1
3207 0 croak("PRIORITY_UPDATE field value exceeds 16380 bytes");
3208 }
3209 1 rv = nghttp2_submit_priority_update(
3210 ps->session, NGHTTP2_FLAG_NONE, stream_id, value, (size_t)len);
3211 1 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 1 1
3212 0 croak("nghttp2_submit_priority_update failed (%d): %s",
3213 rv, nghttp2_strerror(rv));
3214 }
3215 1 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 1 1
3216 OUTPUT:
3217 RETVAL
3218  
3219 int
3220 _submit_ping_native(self, opaque_data)
3221 SV *self
3222 SV *opaque_data
3223 PREINIT:
3224 unblock_h2_session *ps;
3225 6 ▶ STRLEN len = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN len = 0; 6 0 6
3226 const uint8_t *data;
3227 int rv;
3228 CODE:
3229 6 ps = session_from_sv(aTHX_ self);
3230 6 data = (const uint8_t *)SvPVbyte(opaque_data, len);
3231 6 ▶ if (len != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (len != 8) { 0 6 6
3232 0 croak("PING opaque data must be exactly 8 bytes");
3233 }
3234 6 rv = nghttp2_submit_ping(
3235 ps->session, NGHTTP2_FLAG_NONE, data);
3236 6 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 6 6
3237 0 croak("nghttp2_submit_ping failed (%d): %s",
3238 rv, nghttp2_strerror(rv));
3239 }
3240 6 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 3 3 6
3241 OUTPUT:
3242 RETVAL
3243  
3244 int
3245 _submit_goaway_native(self, last_stream_id, error_code, debug_data)
3246 SV *self
3247 int last_stream_id
3248 unsigned int error_code
3249 SV *debug_data
3250 PREINIT:
3251 unblock_h2_session *ps;
3252 6 STRLEN len = 0;
3253 6 ▶ const uint8_t *data = NULL;
Branch: 1 branch
BranchTrueFalseTotal
const uint8_t *data = NULL; 6 0 6
3254 int rv;
3255 CODE:
3256 6 ps = session_from_sv(aTHX_ self);
3257 6 ▶ if (SvOK(debug_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(debug_data)) { 6 0 6
3258 6 data = (const uint8_t *)SvPVbyte(debug_data, len);
3259 }
3260 6 rv = nghttp2_submit_goaway(
3261 ps->session, NGHTTP2_FLAG_NONE, last_stream_id,
3262 error_code, data, (size_t)len);
3263 6 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 6 6
3264 0 croak("nghttp2_submit_goaway failed (%d): %s",
3265 rv, nghttp2_strerror(rv));
3266 }
3267 6 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 3 3 6
3268 OUTPUT:
3269 RETVAL
3270  
3271 int
3272 resume_data(self, stream_id)
3273 SV *self
3274 int stream_id
3275 PREINIT:
3276 unblock_h2_session *ps;
3277 int rv;
3278 CODE:
3279 5 ps = session_from_sv(aTHX_ self);
3280 5 rv = nghttp2_session_resume_data(ps->session, stream_id);
3281 5 ▶ if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) { 0 5 5
if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) { 0 0 0
3282 0 croak("nghttp2_session_resume_data failed (%d): %s",
3283 rv, nghttp2_strerror(rv));
3284 }
3285 5 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 3 2 5
3286 OUTPUT:
3287 RETVAL
3288  
3289 int
3290 is_stream_deferred(self, stream_id)
3291 SV *self
3292 int stream_id
3293 PREINIT:
3294 unblock_h2_session *ps;
3295 unblock_h2_provider *provider;
3296 CODE:
3297 20 ps = session_from_sv(aTHX_ self);
3298 20 provider = find_provider(ps, stream_id);
3299 20 ▶ RETVAL = provider ? provider->deferred : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = provider ? provider->deferred : 0; 20 0 20
RETVAL = provider ? provider->deferred : 0; 11 9 20
3300 OUTPUT:
3301 RETVAL
3302  
3303 void
3304 _clear_deferred(self, stream_id)
3305 SV *self
3306 int stream_id
3307 PREINIT:
3308 unblock_h2_session *ps;
3309 unblock_h2_provider *provider;
3310 CODE:
3311 5 ps = session_from_sv(aTHX_ self);
3312 5 provider = find_provider(ps, stream_id);
3313 5 ▶ if (provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (provider) { 5 0 5
3314 5 provider->deferred = 0;
3315 }
3316  
3317  
3318 MODULE = Unblock::HTTP2 PACKAGE = Unblock::HTTP2::_nghttp2::NativeDriver
3319  
3320 SV *
3321 new(CLASS, engine)
3322 const char *CLASS
3323 SV *engine
3324 PREINIT:
3325 ub_http2_native_context *context;
3326 SV *inner;
3327 SV *object;
3328 CODE:
3329 context = (ub_http2_native_context *)
3330 69 ub_http2_native_create(aTHX_ engine);
3331 69 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 69 69
3332 0 croak("engine does not support Unblock::HTTP2 native transport");
3333 }
3334 69 inner = newSViv(PTR2IV(context));
3335 69 object = newRV_noinc(inner);
3336 69 sv_bless(object, gv_stashpv(CLASS, GV_ADD));
3337 69 RETVAL = object;
3338 OUTPUT:
3339 RETVAL
3340  
3341 void
3342 feed(self, buffer)
3343 SV *self
3344 SV *buffer
3345 PREINIT:
3346 SV *inner;
3347 ub_http2_native_context *context;
3348 STRLEN buffer_len;
3349 const char *data;
3350 27973 size_t consumed = 0;
3351 int status;
3352 PPCODE:
3353 27973 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 27973 0 27973
3354 27973 ▶ || !sv_derived_from(
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from( 0 27973 27973
3355 self, "Unblock::HTTP2::_nghttp2::NativeDriver")) {
3356 0 croak("not an Unblock::HTTP2 native transport driver");
3357 }
3358 27973 inner = SvRV(self);
3359 27973 context = INT2PTR(
3360 ub_http2_native_context *, SvIV(inner));
3361 27973 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 27973 27973
3362 0 croak("native transport driver has already been released");
3363 }
3364  
3365 27973 data = SvPVbyte(buffer, buffer_len);
3366 27973 status = ub_http2_native_input(
3367 aTHX_
3368 context,
3369 data,
3370 (size_t)buffer_len,
3371 &consumed
3372 );
3373  
3374 27973 ▶ XPUSHs(sv_2mortal(newSViv(status)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(status))); 0 27973 27973
3375 27973 ▶ XPUSHs(sv_2mortal(newSVuv((UV)consumed)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv((UV)consumed))); 0 27973 27973
3376  
3377 void
3378 drain(self, pause_after_first = 0)
3379 SV *self
3380 int pause_after_first
3381 PREINIT:
3382 SV *inner;
3383 ub_http2_native_context *context;
3384 SV *output;
3385 ub_http2_test_sink_context sink;
3386 1767 ▶ size_t produced = 0;
Branch: 1 branch
BranchTrueFalseTotal
size_t produced = 0; 1766 1 1767
3387 int status;
3388 PPCODE:
3389 1767 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 1767 0 1767
3390 1767 ▶ || !sv_derived_from(
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from( 0 1767 1767
3391 self, "Unblock::HTTP2::_nghttp2::NativeDriver")) {
3392 0 croak("not an Unblock::HTTP2 native transport driver");
3393 }
3394 1767 inner = SvRV(self);
3395 1767 context = INT2PTR(
3396 ub_http2_native_context *, SvIV(inner));
3397 1767 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 1767 1767
3398 0 croak("native transport driver has already been released");
3399 }
3400  
3401 1767 output = newSVpvn("", 0);
3402 1767 sink.buffer = output;
3403 1767 sink.pause_after_first = pause_after_first ? 1 : 0;
3404 1767 sink.chunks = 0;
3405  
3406 1767 status = ub_http2_native_output(
3407 aTHX_
3408 context,
3409 ub_http2_test_output_sink,
3410 &sink,
3411 &produced
3412 );
3413  
3414 1767 ▶ XPUSHs(sv_2mortal(newSViv(status)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(status))); 0 1767 1767
3415 1767 ▶ XPUSHs(sv_2mortal(output));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(output)); 0 1767 1767
3416 1767 ▶ XPUSHs(sv_2mortal(newSVuv((UV)produced)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv((UV)produced))); 0 1767 1767
3417  
3418 void
3419 transfer_to(self, destination)
3420 SV *self
3421 SV *destination
3422 PREINIT:
3423 SV *inner;
3424 SV *destination_inner;
3425 ub_http2_native_context *context;
3426 ub_http2_native_context *destination_context;
3427 ub_http2_test_bridge_context bridge;
3428 4 size_t produced = 0;
3429 int output_status;
3430 PPCODE:
3431 4 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 4 0 4
3432 4 ▶ || !sv_derived_from(
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from( 4 0 4
3433 self, "Unblock::HTTP2::_nghttp2::NativeDriver")
3434 4 ▶ || !SvROK(destination)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(destination) 4 0 4
3435 4 ▶ || !sv_derived_from(
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from( 0 4 4
3436 destination, "Unblock::HTTP2::_nghttp2::NativeDriver")) {
3437 0 croak("transfer_to requires two HTTP/2 native transport drivers");
3438 }
3439  
3440 4 inner = SvRV(self);
3441 4 destination_inner = SvRV(destination);
3442 4 context = INT2PTR(
3443 ub_http2_native_context *, SvIV(inner));
3444 4 destination_context = INT2PTR(
3445 ub_http2_native_context *, SvIV(destination_inner));
3446 4 ▶ if (!context || !destination_context) {
Branch: 2 branches
BranchTrueFalseTotal
if (!context || !destination_context) { 4 0 4
if (!context || !destination_context) { 0 4 4
3447 0 croak("native transport driver has already been released");
3448 }
3449  
3450 4 bridge.destination = destination_context;
3451 4 bridge.moved = 0;
3452 4 bridge.input_status = UB_HTTP2_INPUT_OK;
3453  
3454 4 output_status = ub_http2_native_output(
3455 aTHX_
3456 context,
3457 ub_http2_test_bridge_sink,
3458 &bridge,
3459 &produced
3460 );
3461 4 ▶ if (produced != bridge.moved) {
Branch: 1 branch
BranchTrueFalseTotal
if (produced != bridge.moved) { 0 4 4
3462 0 croak("native transport bridge byte count mismatch");
3463 }
3464  
3465 4 ▶ XPUSHs(sv_2mortal(newSViv(output_status)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(output_status))); 0 4 4
3466 4 ▶ XPUSHs(sv_2mortal(newSViv(bridge.input_status)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(bridge.input_status))); 0 4 4
3467 4 ▶ XPUSHs(sv_2mortal(newSVuv((UV)bridge.moved)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv((UV)bridge.moved))); 0 4 4
3468  
3469 int
3470 want_read(self)
3471 SV *self
3472 PREINIT:
3473 SV *inner;
3474 ub_http2_native_context *context;
3475 CODE:
3476 2 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 2 0 2
3477 2 ▶ || !sv_derived_from(
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from( 0 2 2
3478 self, "Unblock::HTTP2::_nghttp2::NativeDriver")) {
3479 0 croak("not an Unblock::HTTP2 native transport driver");
3480 }
3481 2 inner = SvRV(self);
3482 2 context = INT2PTR(
3483 ub_http2_native_context *, SvIV(inner));
3484 2 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 2 2
3485 0 croak("native transport driver has already been released");
3486 }
3487 2 RETVAL = ub_http2_native_want_read(aTHX_ context);
3488 OUTPUT:
3489 RETVAL
3490  
3491 int
3492 want_write(self)
3493 SV *self
3494 PREINIT:
3495 SV *inner;
3496 ub_http2_native_context *context;
3497 CODE:
3498 22 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 22 0 22
3499 22 ▶ || !sv_derived_from(
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from( 0 22 22
3500 self, "Unblock::HTTP2::_nghttp2::NativeDriver")) {
3501 0 croak("not an Unblock::HTTP2 native transport driver");
3502 }
3503 22 inner = SvRV(self);
3504 22 context = INT2PTR(
3505 ub_http2_native_context *, SvIV(inner));
3506 22 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 22 22
3507 0 croak("native transport driver has already been released");
3508 }
3509 22 RETVAL = ub_http2_native_want_write(aTHX_ context);
3510 OUTPUT:
3511 RETVAL
3512  
3513 int
3514 eof(self)
3515 SV *self
3516 PREINIT:
3517 SV *inner;
3518 ub_http2_native_context *context;
3519 CODE:
3520 1 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 1 0 1
3521 1 ▶ || !sv_derived_from(
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from( 0 1 1
3522 self, "Unblock::HTTP2::_nghttp2::NativeDriver")) {
3523 0 croak("not an Unblock::HTTP2 native transport driver");
3524 }
3525 1 inner = SvRV(self);
3526 1 context = INT2PTR(
3527 ub_http2_native_context *, SvIV(inner));
3528 1 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 1 1
3529 0 croak("native transport driver has already been released");
3530 }
3531 1 RETVAL = ub_http2_native_eof(aTHX_ context);
3532 OUTPUT:
3533 RETVAL
3534  
3535 void
3536 DESTROY(self)
3537 SV *self
3538 PREINIT:
3539 SV *inner;
3540 ub_http2_native_context *context;
3541 CODE:
3542 69 ▶ if (!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) { 0 69 69
3543 0 XSRETURN_EMPTY;
3544 }
3545 69 inner = SvRV(self);
3546 69 context = INT2PTR(
3547 ub_http2_native_context *, SvIV(inner));
3548 69 ▶ if (!context) {
Branch: 1 branch
BranchTrueFalseTotal
if (!context) { 0 69 69
3549 0 XSRETURN_EMPTY;
3550 }
3551 69 ub_http2_native_destroy(aTHX_ context);
3552 69 sv_setiv(inner, 0);
« blib/lib/Unblock/HTTP2/NativeABI.pm ↑ Summary h2_receive_guard.h »