nghttp2.xs

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