nghttp2.xs

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