nghttp2.xs

filter: Statementstmt 81.9% 598 / 730 Branchbran 58.0% 359 / 618 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.9% 957 / 1348 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Net/HTTP2/nghttp2/Session.pm ↑ Summary blib/lib/Net/HTTP2/nghttp2.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <nghttp2/nghttp2.h>
7 #include <string.h>
8  
9 /*
10 * Net::HTTP2::nghttp2 - Perl XS bindings for nghttp2
11 *
12 * This module provides server-side HTTP/2 support via nghttp2.
13 */
14  
15 /* Per-stream data provider state for streaming responses */
16 typedef struct {
17 SV *callback; /* Perl callback to produce data */
18 SV *user_data; /* User data for callback */
19 int32_t stream_id; /* Stream ID */
20 int eof; /* End of data flag */
21 int no_end_stream; /* Suppress END_STREAM at EOF */
22 int deferred; /* Currently deferred */
23 int released; /* Stream closed; awaiting free, do not touch Perl data */
24 } nghttp2_perl_data_provider;
25  
26 /* Session wrapper structure */
27 typedef struct {
28 nghttp2_session *session;
29 SV *user_data;
30 SV *cb_on_begin_headers;
31 SV *cb_on_header;
32 SV *cb_on_frame_recv;
33 SV *cb_on_data_chunk_recv;
34 SV *cb_on_stream_close;
35 SV *cb_on_frame_send;
36 SV *cb_on_frame_not_send;
37 SV *cb_on_invalid_frame_recv;
38 SV *cb_on_error;
39 SV *cb_send;
40 SV *cb_data_source_read;
41 /* Output buffer for mem_send */
42 char *send_buf;
43 size_t send_buf_len;
44 size_t send_buf_cap;
45 /* Data providers for active streams (simple linear array) */
46 nghttp2_perl_data_provider **data_providers;
47 int data_providers_count;
48 int data_providers_cap;
49 /* Nonzero while nghttp2 is running callbacks for this session */
50 int in_session_call;
51 /* Providers unlinked during a session call, freed when it returns */
52 nghttp2_perl_data_provider **pending_free;
53 int pending_free_count;
54 int pending_free_cap;
55 } nghttp2_perl_session;
56  
57 /* Every method reaches nghttp2 through ps->session. DESTROY clears that pointer
58 before it releases anything that can run Perl, so a Perl destructor that calls
59 back into the session being torn down is refused here rather than handed a
60 deleted nghttp2_session. */
61 #define SESSION_ALIVE_OR_CROAK(ps) STMT_START { \
62 if (!(ps) || !(ps)->session) { \
63 croak("Net::HTTP2::nghttp2::Session: session has been destroyed"); \
64 } \
65 } STMT_END
66  
67 /* Forward declarations */
68 static ssize_t perl_send_callback(nghttp2_session *session,
69 const uint8_t *data, size_t length,
70 int flags, void *user_data);
71 static int perl_on_begin_headers_callback(nghttp2_session *session,
72 const nghttp2_frame *frame,
73 void *user_data);
74 static int perl_on_header_callback(nghttp2_session *session,
75 const nghttp2_frame *frame,
76 const uint8_t *name, size_t namelen,
77 const uint8_t *value, size_t valuelen,
78 uint8_t flags, void *user_data);
79 static int perl_on_frame_recv_callback(nghttp2_session *session,
80 const nghttp2_frame *frame,
81 void *user_data);
82 static int perl_on_data_chunk_recv_callback(nghttp2_session *session,
83 uint8_t flags, int32_t stream_id,
84 const uint8_t *data, size_t len,
85 void *user_data);
86 static int perl_on_stream_close_callback(nghttp2_session *session,
87 int32_t stream_id,
88 uint32_t error_code,
89 void *user_data);
90 static int perl_on_frame_send_callback(nghttp2_session *session,
91 const nghttp2_frame *frame,
92 void *user_data);
93 static int perl_on_frame_not_send_callback(nghttp2_session *session,
94 const nghttp2_frame *frame,
95 int lib_error_code,
96 void *user_data);
97 static int perl_on_invalid_frame_recv_callback(nghttp2_session *session,
98 const nghttp2_frame *frame,
99 int lib_error_code,
100 void *user_data);
101 static int perl_error_callback(nghttp2_session *session, int lib_error_code,
102 const char *msg, size_t len, void *user_data);
103  
104 /* Session callback plumbing shared by the server and client constructors */
105 256 static void extract_perl_callbacks(pTHX_ nghttp2_perl_session *ps,
106 HV *callbacks_hv) {
107 SV **svp;
108  
109 256 ▶ if (!callbacks_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!callbacks_hv) { 0 256 256
110 0 return;
111 }
112  
113 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_begin_headers", 16, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_begin_headers", 16, 0))) { 247 9 256
114 247 ps->cb_on_begin_headers = newSVsv(*svp);
115 }
116 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_header", 9, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_header", 9, 0))) { 248 8 256
117 248 ps->cb_on_header = newSVsv(*svp);
118 }
119 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_frame_recv", 13, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_frame_recv", 13, 0))) { 256 0 256
120 256 ps->cb_on_frame_recv = newSVsv(*svp);
121 }
122 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_data_chunk_recv", 18, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_data_chunk_recv", 18, 0))) { 95 161 256
123 95 ps->cb_on_data_chunk_recv = newSVsv(*svp);
124 }
125 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_stream_close", 15, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_stream_close", 15, 0))) { 111 145 256
126 111 ps->cb_on_stream_close = newSVsv(*svp);
127 }
128 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_frame_send", 13, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_frame_send", 13, 0))) { 26 230 256
129 26 ps->cb_on_frame_send = newSVsv(*svp);
130 }
131 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_frame_not_send", 17, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_frame_not_send", 17, 0))) { 6 250 256
132 6 ps->cb_on_frame_not_send = newSVsv(*svp);
133 }
134 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_invalid_frame_recv", 21, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_invalid_frame_recv", 21, 0))) { 1 255 256
135 1 ps->cb_on_invalid_frame_recv = newSVsv(*svp);
136 }
137 256 ▶ if ((svp = hv_fetch(callbacks_hv, "on_error", 8, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(callbacks_hv, "on_error", 8, 0))) { 1 255 256
138 1 ps->cb_on_error = newSVsv(*svp);
139 }
140 }
141  
142 256 static void release_perl_callbacks(pTHX_ nghttp2_perl_session *ps) {
143 256 ▶ if (ps->user_data) SvREFCNT_dec(ps->user_data);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->user_data) SvREFCNT_dec(ps->user_data); 0 256 256
144 256 ▶ if (ps->cb_on_begin_headers) SvREFCNT_dec(ps->cb_on_begin_headers);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_begin_headers) SvREFCNT_dec(ps->cb_on_begin_headers); 247 9 256
145 256 ▶ if (ps->cb_on_header) SvREFCNT_dec(ps->cb_on_header);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_header) SvREFCNT_dec(ps->cb_on_header); 248 8 256
146 256 ▶ if (ps->cb_on_frame_recv) SvREFCNT_dec(ps->cb_on_frame_recv);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_frame_recv) SvREFCNT_dec(ps->cb_on_frame_recv); 256 0 256
147 256 ▶ if (ps->cb_on_data_chunk_recv) SvREFCNT_dec(ps->cb_on_data_chunk_recv);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_data_chunk_recv) SvREFCNT_dec(ps->cb_on_data_chunk_recv); 95 161 256
148 256 ▶ if (ps->cb_on_stream_close) SvREFCNT_dec(ps->cb_on_stream_close);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_stream_close) SvREFCNT_dec(ps->cb_on_stream_close); 111 145 256
149 256 ▶ if (ps->cb_on_frame_send) SvREFCNT_dec(ps->cb_on_frame_send);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_frame_send) SvREFCNT_dec(ps->cb_on_frame_send); 26 230 256
150 256 ▶ if (ps->cb_on_frame_not_send) SvREFCNT_dec(ps->cb_on_frame_not_send);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_frame_not_send) SvREFCNT_dec(ps->cb_on_frame_not_send); 6 250 256
151 256 ▶ if (ps->cb_on_invalid_frame_recv) SvREFCNT_dec(ps->cb_on_invalid_frame_recv);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_invalid_frame_recv) SvREFCNT_dec(ps->cb_on_invalid_frame_recv); 1 255 256
152 256 ▶ if (ps->cb_on_error) SvREFCNT_dec(ps->cb_on_error);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cb_on_error) SvREFCNT_dec(ps->cb_on_error); 1 255 256
153 256 }
154  
155 256 static void register_nghttp2_callbacks(nghttp2_session_callbacks *callbacks) {
156 256 nghttp2_session_callbacks_set_send_callback(callbacks, perl_send_callback);
157 256 nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks, perl_on_begin_headers_callback);
158 256 nghttp2_session_callbacks_set_on_header_callback(callbacks, perl_on_header_callback);
159 256 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, perl_on_frame_recv_callback);
160 256 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, perl_on_data_chunk_recv_callback);
161 256 nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, perl_on_stream_close_callback);
162 256 nghttp2_session_callbacks_set_on_frame_send_callback(callbacks, perl_on_frame_send_callback);
163 256 nghttp2_session_callbacks_set_on_frame_not_send_callback(callbacks, perl_on_frame_not_send_callback);
164 256 nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(callbacks, perl_on_invalid_frame_recv_callback);
165 256 nghttp2_session_callbacks_set_error_callback2(callbacks, perl_error_callback);
166 256 }
167  
168 /* Data provider helper functions */
169 4580 static nghttp2_perl_data_provider *find_data_provider(nghttp2_perl_session *ps, int32_t stream_id) {
170 int i;
171 9032 ▶ for (i = 0; i < ps->data_providers_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ps->data_providers_count; i++) { 4465 4567 9032
172 4465 ▶ if (ps->data_providers[i] && ps->data_providers[i]->stream_id == stream_id) {
Branch: 2 branches
BranchTrueFalseTotal
if (ps->data_providers[i] && ps->data_providers[i]->stream_id == stream_id) { 15 4450 4465
if (ps->data_providers[i] && ps->data_providers[i]->stream_id == stream_id) { 13 2 15
173 13 return ps->data_providers[i];
174 }
175 }
176 4567 return NULL;
177 }
178  
179 /* One provider per stream id. A second one would leave nghttp2 holding two data
180 sources for the same stream while stream close reclaims only the first, and
181 the loser would sit in the array keyed to a stream that can never free it.
182 Submit paths check this before handing the descriptor to nghttp2; the check
183 inside add_data_provider stands as the invariant, and reaching it there would
184 mean stranding a provider nghttp2 has already taken. */
185 4569 static void assert_stream_has_no_provider(pTHX_ nghttp2_perl_session *ps,
186 int32_t stream_id) {
187 4569 ▶ if (find_data_provider(ps, stream_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_data_provider(ps, stream_id)) { 2 4567 4569
188 2 croak("Net::HTTP2::nghttp2::Session: stream %d already has a data provider",
189 (int)stream_id);
190 }
191 4567 }
192  
193 2287 static void add_data_provider(pTHX_ nghttp2_perl_session *ps, nghttp2_perl_data_provider *dp) {
194 int i;
195 2287 assert_stream_has_no_provider(aTHX_ ps, dp->stream_id);
196 /* Find empty slot */
197 2288 ▶ for (i = 0; i < ps->data_providers_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ps->data_providers_count; i++) { 2225 63 2288
198 2225 ▶ if (!ps->data_providers[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->data_providers[i]) { 2224 1 2225
199 2224 ps->data_providers[i] = dp;
200 2224 return;
201 }
202 }
203 /* Grow array if needed */
204 63 ▶ if (ps->data_providers_count >= ps->data_providers_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->data_providers_count >= ps->data_providers_cap) { 62 1 63
205 62 ▶ int new_cap = ps->data_providers_cap ? ps->data_providers_cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int new_cap = ps->data_providers_cap ? ps->data_providers_cap * 2 : 8; 0 62 62
206 62 ps->data_providers = (nghttp2_perl_data_provider **)realloc(
207 62 ps->data_providers, new_cap * sizeof(nghttp2_perl_data_provider *));
208 62 memset(ps->data_providers + ps->data_providers_cap, 0,
209 62 (new_cap - ps->data_providers_cap) * sizeof(nghttp2_perl_data_provider *));
210 62 ps->data_providers_cap = new_cap;
211 }
212 63 ps->data_providers[ps->data_providers_count++] = dp;
213 }
214  
215 2289 static void free_data_provider(pTHX_ nghttp2_perl_data_provider *dp) {
216 2289 ▶ if (dp->callback) SvREFCNT_dec(dp->callback);
Branch: 1 branch
BranchTrueFalseTotal
if (dp->callback) SvREFCNT_dec(dp->callback); 2280 9 2289
217 2289 ▶ if (dp->user_data) SvREFCNT_dec(dp->user_data);
Branch: 1 branch
BranchTrueFalseTotal
if (dp->user_data) SvREFCNT_dec(dp->user_data); 2213 76 2289
218 2289 Safefree(dp);
219 2289 }
220  
221 /* Hold a released provider until the running session call returns. nghttp2
222 still owns the outbound item whose data source points at it, and the header
223 promises nothing about that pointer's lifetime past on_stream_close. */
224 2275 static void defer_free_data_provider(pTHX_ nghttp2_perl_session *ps,
225 nghttp2_perl_data_provider *dp) {
226 2275 ▶ if (ps->pending_free_count >= ps->pending_free_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->pending_free_count >= ps->pending_free_cap) { 2274 1 2275
227 2274 ▶ int new_cap = ps->pending_free_cap ? ps->pending_free_cap * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
int new_cap = ps->pending_free_cap ? ps->pending_free_cap * 2 : 4; 0 2274 2274
228 nghttp2_perl_data_provider **grown =
229 2274 (nghttp2_perl_data_provider **)realloc(
230 2274 ps->pending_free, new_cap * sizeof(nghttp2_perl_data_provider *));
231 2274 ▶ if (!grown) {
Branch: 1 branch
BranchTrueFalseTotal
if (!grown) { 0 2274 2274
232 /* Nowhere to park it; freeing now beats losing the list. */
233 0 free_data_provider(aTHX_ dp);
234 0 return;
235 }
236 2274 ps->pending_free = grown;
237 2274 ps->pending_free_cap = new_cap;
238 }
239 2275 ps->pending_free[ps->pending_free_count++] = dp;
240 }
241  
242 /* Freeing a provider drops the last reference to its Perl callback_data, so a
243 DESTROY runs here and may re-enter the session and drain again. Take the list
244 off the session first: the nested drain then sees an empty list instead of
245 freeing these entries a second time, and an append from the nested call
246 cannot realloc the array this loop is walking. */
247 28247 static void drain_pending_free(pTHX_ nghttp2_perl_session *ps) {
248 28247 nghttp2_perl_data_provider **list = ps->pending_free;
249 28247 int count = ps->pending_free_count;
250 int i;
251  
252 28247 ps->pending_free = NULL;
253 28247 ps->pending_free_count = 0;
254 28247 ps->pending_free_cap = 0;
255  
256 30522 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 2275 28247 30522
257 2275 free_data_provider(aTHX_ list[i]);
258 }
259 28247 ▶ if (list) free(list);
Branch: 1 branch
BranchTrueFalseTotal
if (list) free(list); 2274 25973 28247
260 28247 }
261  
262 6565 static void remove_data_provider(nghttp2_perl_session *ps, int32_t stream_id) {
263 dTHX;
264 int i;
265 8565 ▶ for (i = 0; i < ps->data_providers_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ps->data_providers_count; i++) { 4275 4290 8565
266 4275 ▶ if (ps->data_providers[i] && ps->data_providers[i]->stream_id == stream_id) {
Branch: 2 branches
BranchTrueFalseTotal
if (ps->data_providers[i] && ps->data_providers[i]->stream_id == stream_id) { 2275 2000 4275
if (ps->data_providers[i] && ps->data_providers[i]->stream_id == stream_id) { 2275 0 2275
267 2275 nghttp2_perl_data_provider *dp = ps->data_providers[i];
268 2275 ps->data_providers[i] = NULL;
269 2275 dp->released = 1;
270 2275 ▶ if (ps->in_session_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->in_session_call) { 2275 0 2275
271 2275 defer_free_data_provider(aTHX_ ps, dp);
272 } else {
273 0 free_data_provider(aTHX_ dp);
274 }
275 2275 return;
276 }
277 }
278 }
279  
280 4608 static nghttp2_nv *perl_headers_to_nva(pTHX_ AV *headers_av,
281 size_t *nvlen_out) {
282 4608 I32 last_index = av_len(headers_av);
283 4608 ▶ size_t nvlen = last_index < 0 ? 0 : (size_t)last_index + 1;
Branch: 1 branch
BranchTrueFalseTotal
size_t nvlen = last_index < 0 ? 0 : (size_t)last_index + 1; 4606 2 4608
284 4608 nghttp2_nv *nva = NULL;
285 I32 i;
286  
287 4608 *nvlen_out = nvlen;
288 4608 ▶ if (nvlen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nvlen == 0) { 2 4606 4608
289 2 return NULL;
290 }
291  
292 4606 ▶ Newxz(nva, nvlen, nghttp2_nv);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(nva, nvlen, nghttp2_nv); 0 4606 4606
293  
294 16222 ▶ for (i = 0; i < (I32)nvlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (I32)nvlen; i++) { 11616 4606 16222
295 11616 SV **pair = av_fetch(headers_av, i, 0);
296 11616 ▶ if (pair && SvROK(*pair) && SvTYPE(SvRV(*pair)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (pair && SvROK(*pair) && SvTYPE(SvRV(*pair)) == SVt_PVAV) { 11616 0 11616
if (pair && SvROK(*pair) && SvTYPE(SvRV(*pair)) == SVt_PVAV) { 11616 0 11616
if (pair && SvROK(*pair) && SvTYPE(SvRV(*pair)) == SVt_PVAV) { 11616 0 11616
297 11616 AV *pair_av = (AV *)SvRV(*pair);
298 11616 SV **name_sv = av_fetch(pair_av, 0, 0);
299 11616 SV **value_sv = av_fetch(pair_av, 1, 0);
300  
301 11616 ▶ if (name_sv && value_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (name_sv && value_sv) { 11616 0 11616
if (name_sv && value_sv) { 11616 0 11616
302 STRLEN name_len;
303 STRLEN value_len;
304 11616 nva[i].name = (uint8_t *)SvPVbyte(*name_sv, name_len);
305 11616 nva[i].namelen = name_len;
306 11616 ▶ nva[i].value = (uint8_t *)SvPVbyte(*value_sv, value_len);
Branch: 1 branch
BranchTrueFalseTotal
nva[i].value = (uint8_t *)SvPVbyte(*value_sv, value_len); 9335 2281 11616
307 11616 nva[i].valuelen = value_len;
308 11616 nva[i].flags = NGHTTP2_NV_FLAG_NONE;
309 }
310 }
311 }
312  
313 4606 return nva;
314 }
315  
316 /* Data provider read callback - called by nghttp2 when it wants response body data */
317 302 static ssize_t perl_data_source_read_callback(
318 nghttp2_session *session,
319 int32_t stream_id,
320 uint8_t *buf,
321 size_t length,
322 uint32_t *data_flags,
323 nghttp2_data_source *source,
324 void *user_data)
325 {
326 dTHX;
327 302 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
328 302 nghttp2_perl_data_provider *dp = (nghttp2_perl_data_provider *)source->ptr;
329 302 dSP;
330 int count;
331 302 ssize_t ret = 0;
332  
333 302 ▶ if (!dp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dp) { 0 302 302
334 0 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
335 0 return 0;
336 }
337  
338 /* The stream closed while nghttp2 still held this data source. The Perl
339 callback and body are spoken for, so end the body without touching them. */
340 302 ▶ if (dp->released) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp->released) { 0 302 302
341 0 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
342 0 return 0;
343 }
344  
345 /* Special case: if callback is NULL but user_data contains body, use it directly */
346 302 ▶ if (!dp->callback && dp->user_data && SvOK(dp->user_data)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!dp->callback && dp->user_data && SvOK(dp->user_data)) { 14 288 302
if (!dp->callback && dp->user_data && SvOK(dp->user_data)) { 12 2 14
if (!dp->callback && dp->user_data && SvOK(dp->user_data)) { 12 0 12
347 STRLEN full_len;
348 12 const char *body_ptr = SvPVbyte(dp->user_data, full_len);
349 12 STRLEN send_len = (full_len > length) ? length : full_len;
350  
351 12 ▶ if (send_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (send_len > 0) { 11 1 12
352 11 memcpy(buf, body_ptr, send_len);
353 }
354  
355 12 ▶ if (send_len >= full_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (send_len >= full_len) { 11 1 12
356 /* All data consumed */
357 11 SvREFCNT_dec(dp->user_data);
358 11 dp->user_data = NULL;
359 11 ▶ if (dp->eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp->eof) { 9 2 11
360 9 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
361 9 ▶ if (dp->no_end_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp->no_end_stream) { 2 7 9
362 2 *data_flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
363 }
364 } else {
365 2 dp->no_end_stream = 0;
366 }
367 } else {
368 /* Partial send - keep remainder for next call */
369 1 SV *remaining = newSVpvn(body_ptr + send_len, full_len - send_len);
370 1 SvREFCNT_dec(dp->user_data);
371 1 dp->user_data = remaining;
372 }
373  
374 12 return (ssize_t)send_len;
375 }
376  
377 /* No callback and no pending data */
378 290 ▶ if (!dp->callback || !SvOK(dp->callback)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!dp->callback || !SvOK(dp->callback)) { 288 2 290
if (!dp->callback || !SvOK(dp->callback)) { 0 288 288
379 2 ▶ if (dp->eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp->eof) { 0 2 2
380 0 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
381 0 ▶ if (dp->no_end_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp->no_end_stream) { 0 0 0
382 0 *data_flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
383 }
384 0 return 0;
385 }
386 /* No EOF requested - defer until more data arrives via submit_data */
387 2 dp->deferred = 1;
388 2 return NGHTTP2_ERR_DEFERRED;
389 }
390  
391 288 ENTER;
392 288 SAVETMPS;
393 288 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 288 288
394  
395 /* Call: $callback->($stream_id, $max_length[, $user_data]) */
396 /* Returns: ($data, $eof[, $no_end_stream]) or undef/empty list to defer */
397 288 ▶ XPUSHs(sv_2mortal(newSViv(stream_id)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(stream_id))); 0 288 288
398 288 ▶ XPUSHs(sv_2mortal(newSVuv(length)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv(length))); 0 288 288
399 288 ▶ if (dp->user_data && SvOK(dp->user_data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (dp->user_data && SvOK(dp->user_data)) { 210 78 288
if (dp->user_data && SvOK(dp->user_data)) { 210 0 210
400 210 ▶ XPUSHs(dp->user_data);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dp->user_data); 0 210 210
401 }
402  
403 288 PUTBACK;
404 288 count = call_sv(dp->callback, G_ARRAY | G_EVAL);
405 288 SPAGAIN;
406  
407 288 ▶ if (dp->released) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp->released) { 0 288 288
408 /* The callback closed its own stream. The provider is awaiting free,
409 so discard the body rather than record state on it. */
410 0 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
411 0 ret = 0;
412 288 ▶ } else if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvTRUE(ERRSV)) { 288 0 288
} else if (SvTRUE(ERRSV)) { 1 287 288
413 /* Callback threw an exception */
414 2 ▶ warn("nghttp2 data provider callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("nghttp2 data provider callback error: %s", SvPV_nolen(ERRSV)); 1 0 1
415 1 ret = NGHTTP2_ERR_CALLBACK_FAILURE;
416 287 ▶ } else if (count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (count == 0) { 1 286 287
417 /* No return value = defer */
418 1 dp->deferred = 1;
419 1 ret = NGHTTP2_ERR_DEFERRED;
420 } else {
421 286 SV **return_values = SP - count + 1;
422 286 SV *data_sv = return_values[0];
423 286 ▶ SV *eof_sv = count >= 2 ? return_values[1] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *eof_sv = count >= 2 ? return_values[1] : NULL; 273 13 286
424 286 ▶ SV *no_end_stream_sv = count >= 3 ? return_values[2] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *no_end_stream_sv = count >= 3 ? return_values[2] : NULL; 10 276 286
425  
426 286 ▶ if (!SvOK(data_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(data_sv)) { 12 274 286
427 /* undef = defer */
428 12 dp->deferred = 1;
429 12 ret = NGHTTP2_ERR_DEFERRED;
430 } else {
431 STRLEN data_len;
432 274 const char *data_ptr = SvPVbyte(data_sv, data_len);
433  
434 /* Copy data to buffer */
435 274 ▶ if (data_len > length) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_len > length) { 2 272 274
436 2 data_len = length; /* Truncate if too much */
437 }
438 274 ▶ if (data_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_len > 0) { 273 1 274
439 273 memcpy(buf, data_ptr, data_len);
440 }
441 274 ret = (ssize_t)data_len;
442  
443 /* Check EOF flag */
444 274 ▶ if (eof_sv && SvTRUE(eof_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (eof_sv && SvTRUE(eof_sv)) { 273 1 274
if (eof_sv && SvTRUE(eof_sv)) { 251 22 273
445 251 *data_flags |= NGHTTP2_DATA_FLAG_EOF;
446 251 dp->eof = 1;
447 251 ▶ if (no_end_stream_sv && SvTRUE(no_end_stream_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (no_end_stream_sv && SvTRUE(no_end_stream_sv)) { 9 242 251
if (no_end_stream_sv && SvTRUE(no_end_stream_sv)) { 9 0 9
448 9 *data_flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
449 }
450 }
451  
452 /* If returned empty string with no eof, also defer */
453 274 ▶ if (data_len == 0 && !dp->eof) {
Branch: 2 branches
BranchTrueFalseTotal
if (data_len == 0 && !dp->eof) { 1 273 274
if (data_len == 0 && !dp->eof) { 0 1 1
454 0 dp->deferred = 1;
455 0 ret = NGHTTP2_ERR_DEFERRED;
456 }
457 }
458 }
459  
460 288 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 286 2 288
461 286 SP -= count;
462 }
463 288 PUTBACK;
464 288 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 288 0 288
465 288 LEAVE;
466  
467 288 return ret;
468 }
469  
470 /* Helper to call Perl callbacks */
471 24712 static int call_perl_callback(pTHX_ SV *callback, AV *args) {
472 24712 dSP;
473 int count;
474 24712 int ret = 0;
475  
476 24712 ▶ if (!callback || !SvOK(callback)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!callback || !SvOK(callback)) { 24712 0 24712
if (!callback || !SvOK(callback)) { 0 24712 24712
477 0 return 0;
478 }
479  
480 24712 ENTER;
481 24712 SAVETMPS;
482 24712 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 24712 24712
483  
484 24712 ▶ if (args) {
Branch: 1 branch
BranchTrueFalseTotal
if (args) { 24712 0 24712
485 int i;
486 24712 int len = av_len(args) + 1;
487 90985 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 66273 24712 90985
488 66273 SV **elem = av_fetch(args, i, 0);
489 66273 ▶ if (elem) {
Branch: 1 branch
BranchTrueFalseTotal
if (elem) { 66273 0 66273
490 66273 ▶ XPUSHs(*elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*elem); 0 66273 66273
491 }
492 }
493 }
494  
495 24712 PUTBACK;
496 24712 count = call_sv(callback, G_SCALAR | G_EVAL);
497 24712 SPAGAIN;
498  
499 24712 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 24712 0 24712
if (SvTRUE(ERRSV)) { 2 24710 24712
500 /* Callback threw an exception */
501 4 ▶ warn("nghttp2 callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("nghttp2 callback error: %s", SvPV_nolen(ERRSV)); 2 0 2
502 0 ret = NGHTTP2_ERR_CALLBACK_FAILURE;
503 24710 ▶ } else if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (count > 0) { 24710 0 24710
504 24710 SV *result = POPs;
505 24710 ▶ if (SvIOK(result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(result)) { 24710 0 24710
506 24710 ret = SvIV(result);
507 }
508 }
509  
510 24710 PUTBACK;
511 24710 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 24710 0 24710
512 24710 LEAVE;
513  
514 24710 return ret;
515 }
516  
517 /* Send callback - buffers data for mem_send */
518 5488 static ssize_t perl_send_callback(nghttp2_session *session,
519 const uint8_t *data, size_t length,
520 int flags, void *user_data) {
521 5488 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
522  
523 /* Grow buffer if needed */
524 5488 ▶ if (ps->send_buf_len + length > ps->send_buf_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->send_buf_len + length > ps->send_buf_cap) { 2 5486 5488
525 2 size_t new_cap = ps->send_buf_cap * 2;
526 2 ▶ if (new_cap < ps->send_buf_len + length) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_cap < ps->send_buf_len + length) { 0 2 2
527 0 new_cap = ps->send_buf_len + length + 16384;
528 }
529 2 ps->send_buf = (char *)realloc(ps->send_buf, new_cap);
530 2 ▶ if (!ps->send_buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps->send_buf) { 0 2 2
531 0 return NGHTTP2_ERR_NOMEM;
532 }
533 2 ps->send_buf_cap = new_cap;
534 }
535  
536 5488 memcpy(ps->send_buf + ps->send_buf_len, data, length);
537 5488 ps->send_buf_len += length;
538  
539 5488 return (ssize_t)length;
540 }
541  
542 /* Begin headers callback */
543 2642 static int perl_on_begin_headers_callback(nghttp2_session *session,
544 const nghttp2_frame *frame,
545 void *user_data) {
546 dTHX;
547 2642 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
548 AV *args;
549 int ret;
550  
551 2642 ▶ if (!ps->cb_on_begin_headers || !SvOK(ps->cb_on_begin_headers)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_begin_headers || !SvOK(ps->cb_on_begin_headers)) { 2642 0 2642
if (!ps->cb_on_begin_headers || !SvOK(ps->cb_on_begin_headers)) { 0 2642 2642
552 0 return 0;
553 }
554  
555 2642 args = newAV();
556 2642 av_push(args, newSViv(frame->hd.stream_id));
557 2642 av_push(args, newSViv(frame->hd.type));
558 2642 av_push(args, newSViv(frame->hd.flags));
559  
560 2642 ret = call_perl_callback(aTHX_ ps->cb_on_begin_headers, args);
561  
562 2642 SvREFCNT_dec((SV *)args);
563 2642 return ret;
564 }
565  
566 /* Header callback */
567 9719 static int perl_on_header_callback(nghttp2_session *session,
568 const nghttp2_frame *frame,
569 const uint8_t *name, size_t namelen,
570 const uint8_t *value, size_t valuelen,
571 uint8_t flags, void *user_data) {
572 dTHX;
573 9719 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
574 AV *args;
575 int ret;
576  
577 9719 ▶ if (!ps->cb_on_header || !SvOK(ps->cb_on_header)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_header || !SvOK(ps->cb_on_header)) { 9719 0 9719
if (!ps->cb_on_header || !SvOK(ps->cb_on_header)) { 0 9719 9719
578 0 return 0;
579 }
580  
581 9719 args = newAV();
582 9719 av_push(args, newSViv(frame->hd.stream_id));
583 9719 av_push(args, newSVpvn((const char *)name, namelen));
584 9719 av_push(args, newSVpvn((const char *)value, valuelen));
585 9719 av_push(args, newSViv(flags));
586  
587 9719 ret = call_perl_callback(aTHX_ ps->cb_on_header, args);
588  
589 9719 SvREFCNT_dec((SV *)args);
590 9719 return ret;
591 }
592  
593 /* Build the frame info hash the frame callbacks deliver to Perl */
594 7532 static HV *perl_frame_to_hv(pTHX_ const nghttp2_frame *frame) {
595 7532 HV *frame_hv = newHV();
596  
597 7532 hv_store(frame_hv, "stream_id", 9, newSViv(frame->hd.stream_id), 0);
598 7532 hv_store(frame_hv, "type", 4, newSViv(frame->hd.type), 0);
599 7532 hv_store(frame_hv, "flags", 5, newSViv(frame->hd.flags), 0);
600 7532 hv_store(frame_hv, "length", 6, newSViv(frame->hd.length), 0);
601 7532 ▶ if (frame->hd.type == NGHTTP2_HEADERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->hd.type == NGHTTP2_HEADERS) { 4682 2850 7532
602 4682 hv_store(frame_hv, "headers_category", 16,
603 newSViv(frame->headers.cat), 0);
604 }
605  
606 7532 return frame_hv;
607 }
608  
609 /* Frame receive callback */
610 5359 static int perl_on_frame_recv_callback(nghttp2_session *session,
611 const nghttp2_frame *frame,
612 void *user_data) {
613 dTHX;
614 5359 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
615 AV *args;
616 int ret;
617  
618 5359 ▶ if (!ps->cb_on_frame_recv || !SvOK(ps->cb_on_frame_recv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_frame_recv || !SvOK(ps->cb_on_frame_recv)) { 5359 0 5359
if (!ps->cb_on_frame_recv || !SvOK(ps->cb_on_frame_recv)) { 0 5359 5359
619 0 return 0;
620 }
621  
622 5359 args = newAV();
623 5359 av_push(args, newRV_noinc((SV *)perl_frame_to_hv(aTHX_ frame)));
624  
625 5359 ret = call_perl_callback(aTHX_ ps->cb_on_frame_recv, args);
626  
627 5358 SvREFCNT_dec((SV *)args);
628 5358 return ret;
629 }
630  
631 /* Frame send callback - the frame has been serialized into the send buffer */
632 5401 static int perl_on_frame_send_callback(nghttp2_session *session,
633 const nghttp2_frame *frame,
634 void *user_data) {
635 dTHX;
636 5401 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
637 AV *args;
638 int ret;
639  
640 5401 ▶ if (!ps->cb_on_frame_send || !SvOK(ps->cb_on_frame_send)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_frame_send || !SvOK(ps->cb_on_frame_send)) { 168 5233 5401
if (!ps->cb_on_frame_send || !SvOK(ps->cb_on_frame_send)) { 0 168 168
641 5233 return 0;
642 }
643  
644 168 args = newAV();
645 168 av_push(args, newRV_noinc((SV *)perl_frame_to_hv(aTHX_ frame)));
646  
647 168 ret = call_perl_callback(aTHX_ ps->cb_on_frame_send, args);
648  
649 167 SvREFCNT_dec((SV *)args);
650 167 return ret;
651 }
652  
653 /* Frame not-send callback - the queued frame was discarded before the wire */
654 2027 static int perl_on_frame_not_send_callback(nghttp2_session *session,
655 const nghttp2_frame *frame,
656 int lib_error_code,
657 void *user_data) {
658 dTHX;
659 2027 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
660 AV *args;
661 int ret;
662  
663 /* A response or trailer HEADERS discarded for a stream nghttp2 no longer
664 has is the end of that stream's story: on_stream_close already ran, so
665 nothing else will release the provider the submit registered, and
666 nothing will ever read it. A HEADERS refused while the stream is still
667 there is a different thing -- the stream is live and its provider is
668 still feeding the response half -- so the release keys on the stream
669 being gone, not on the frame being discarded.
670  
671 nghttp2.h, on the existence test used here:
672  
673 Returns 1 if remote peer half closed the given stream |stream_id|.
674 Returns 0 if it did not. Returns -1 if no such stream exists.
675  
676 nghttp2_session_get_stream_user_data cannot answer this question: it
677 returns NULL both for a missing stream and for a live stream that has no
678 user data, which on a server is every stream the peer opened.
679  
680 remove_data_provider defers the free while a session call is in
681 progress, which it always is here. */
682 4031 if (frame->hd.type == NGHTTP2_HEADERS &&
683 2004 nghttp2_session_get_stream_remote_close(session, frame->hd.stream_id) < 0) {
684 2001 remove_data_provider(ps, frame->hd.stream_id);
685 }
686  
687 2027 ▶ if (!ps->cb_on_frame_not_send || !SvOK(ps->cb_on_frame_not_send)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_frame_not_send || !SvOK(ps->cb_on_frame_not_send)) { 2004 23 2027
if (!ps->cb_on_frame_not_send || !SvOK(ps->cb_on_frame_not_send)) { 0 2004 2004
688 23 return 0;
689 }
690  
691 2004 args = newAV();
692 2004 av_push(args, newRV_noinc((SV *)perl_frame_to_hv(aTHX_ frame)));
693 2004 av_push(args, newSViv(lib_error_code));
694  
695 2004 ret = call_perl_callback(aTHX_ ps->cb_on_frame_not_send, args);
696  
697 2004 SvREFCNT_dec((SV *)args);
698 2004 return ret;
699 }
700  
701 /* Invalid frame receive callback - nghttp2 rejected a peer frame */
702 21 static int perl_on_invalid_frame_recv_callback(nghttp2_session *session,
703 const nghttp2_frame *frame,
704 int lib_error_code,
705 void *user_data) {
706 dTHX;
707 21 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
708 AV *args;
709 int ret;
710  
711 21 ▶ if (!ps->cb_on_invalid_frame_recv || !SvOK(ps->cb_on_invalid_frame_recv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_invalid_frame_recv || !SvOK(ps->cb_on_invalid_frame_recv)) { 1 20 21
if (!ps->cb_on_invalid_frame_recv || !SvOK(ps->cb_on_invalid_frame_recv)) { 0 1 1
712 20 return 0;
713 }
714  
715 1 args = newAV();
716 1 av_push(args, newRV_noinc((SV *)perl_frame_to_hv(aTHX_ frame)));
717 1 av_push(args, newSViv(lib_error_code));
718  
719 1 ret = call_perl_callback(aTHX_ ps->cb_on_invalid_frame_recv, args);
720  
721 1 SvREFCNT_dec((SV *)args);
722 1 return ret;
723 }
724  
725 /* Error callback - nghttp2's human-readable diagnostics */
726 6 static int perl_error_callback(nghttp2_session *session, int lib_error_code,
727 const char *msg, size_t len, void *user_data) {
728 dTHX;
729 6 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
730 AV *args;
731 int ret;
732  
733 6 ▶ if (!ps->cb_on_error || !SvOK(ps->cb_on_error)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_error || !SvOK(ps->cb_on_error)) { 1 5 6
if (!ps->cb_on_error || !SvOK(ps->cb_on_error)) { 0 1 1
734 5 return 0;
735 }
736  
737 1 args = newAV();
738 1 av_push(args, newSViv(lib_error_code));
739 1 ▶ av_push(args, newSVpvn(msg ? msg : "", msg ? len : 0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(args, newSVpvn(msg ? msg : "", msg ? len : 0)); 1 0 1
av_push(args, newSVpvn(msg ? msg : "", msg ? len : 0)); 1 0 1
740  
741 1 ret = call_perl_callback(aTHX_ ps->cb_on_error, args);
742  
743 1 SvREFCNT_dec((SV *)args);
744 1 return ret;
745 }
746  
747 /* Data chunk receive callback */
748 297 static int perl_on_data_chunk_recv_callback(nghttp2_session *session,
749 uint8_t flags, int32_t stream_id,
750 const uint8_t *data, size_t len,
751 void *user_data) {
752 dTHX;
753 297 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
754 AV *args;
755 int ret;
756  
757 297 ▶ if (!ps->cb_on_data_chunk_recv || !SvOK(ps->cb_on_data_chunk_recv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_data_chunk_recv || !SvOK(ps->cb_on_data_chunk_recv)) { 296 1 297
if (!ps->cb_on_data_chunk_recv || !SvOK(ps->cb_on_data_chunk_recv)) { 0 296 296
758 1 return 0;
759 }
760  
761 296 args = newAV();
762 296 av_push(args, newSViv(stream_id));
763 296 av_push(args, newSVpvn((const char *)data, len));
764 296 av_push(args, newSViv(flags));
765  
766 296 ret = call_perl_callback(aTHX_ ps->cb_on_data_chunk_recv, args);
767  
768 296 SvREFCNT_dec((SV *)args);
769 296 return ret;
770 }
771  
772 /* Stream close callback */
773 4564 static int perl_on_stream_close_callback(nghttp2_session *session,
774 int32_t stream_id,
775 uint32_t error_code,
776 void *user_data) {
777 dTHX;
778 4564 nghttp2_perl_session *ps = (nghttp2_perl_session *)user_data;
779 AV *args;
780 4564 int ret = 0;
781  
782 /* Clean up any data provider for this stream */
783 4564 remove_data_provider(ps, stream_id);
784  
785 4564 ▶ if (!ps->cb_on_stream_close || !SvOK(ps->cb_on_stream_close)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->cb_on_stream_close || !SvOK(ps->cb_on_stream_close)) { 4522 42 4564
if (!ps->cb_on_stream_close || !SvOK(ps->cb_on_stream_close)) { 0 4522 4522
786 42 return 0;
787 }
788  
789 4522 args = newAV();
790 4522 av_push(args, newSViv(stream_id));
791 4522 av_push(args, newSVuv(error_code));
792  
793 4522 ret = call_perl_callback(aTHX_ ps->cb_on_stream_close, args);
794  
795 4522 SvREFCNT_dec((SV *)args);
796 4522 return ret;
797 }
798  
799 MODULE = Net::HTTP2::nghttp2 PACKAGE = Net::HTTP2::nghttp2
800  
801 PROTOTYPES: DISABLE
802  
803 # Check if nghttp2 is available
804 int
805 _check_nghttp2_available()
806 CODE:
807 30 nghttp2_info *info = nghttp2_version(0);
808 30 ▶ RETVAL = info ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = info ? 1 : 0; 0 30 30
809 OUTPUT:
810 RETVAL
811  
812 # Get nghttp2 version string
813 const char *
814 version_string()
815 CODE:
816 2 nghttp2_info *info = nghttp2_version(0);
817 2 ▶ RETVAL = info ? info->version_str : "unknown";
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = info ? info->version_str : "unknown"; 2 0 2
818 OUTPUT:
819 RETVAL
820  
821 # Get nghttp2 version number
822 int
823 version_num()
824 CODE:
825 3 nghttp2_info *info = nghttp2_version(0);
826 3 ▶ RETVAL = info ? info->version_num : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = info ? info->version_num : 0; 3 0 3
RETVAL = info ? info->version_num : 0; 0 3 3
827 OUTPUT:
828 RETVAL
829  
830 # Constants
831 int
832 NGHTTP2_ERR_WOULDBLOCK()
833 CODE:
834 0 ▶ RETVAL = NGHTTP2_ERR_WOULDBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_ERR_WOULDBLOCK; 0 0 0
835 OUTPUT:
836 RETVAL
837  
838 int
839 NGHTTP2_ERR_CALLBACK_FAILURE()
840 CODE:
841 0 ▶ RETVAL = NGHTTP2_ERR_CALLBACK_FAILURE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_ERR_CALLBACK_FAILURE; 0 0 0
842 OUTPUT:
843 RETVAL
844  
845 int
846 NGHTTP2_ERR_DEFERRED()
847 CODE:
848 0 ▶ RETVAL = NGHTTP2_ERR_DEFERRED;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_ERR_DEFERRED; 0 0 0
849 OUTPUT:
850 RETVAL
851  
852 int
853 NGHTTP2_ERR_STREAM_CLOSING()
854 CODE:
855 3 ▶ RETVAL = NGHTTP2_ERR_STREAM_CLOSING;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_ERR_STREAM_CLOSING; 0 3 3
856 OUTPUT:
857 RETVAL
858  
859 int
860 NGHTTP2_ERR_PROTO()
861 CODE:
862 2 ▶ RETVAL = NGHTTP2_ERR_PROTO;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_ERR_PROTO; 0 2 2
863 OUTPUT:
864 RETVAL
865  
866 int
867 NGHTTP2_ERR_HTTP_HEADER()
868 CODE:
869 2 ▶ RETVAL = NGHTTP2_ERR_HTTP_HEADER;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_ERR_HTTP_HEADER; 0 2 2
870 OUTPUT:
871 RETVAL
872  
873 int
874 NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE()
875 CODE:
876 2 ▶ RETVAL = NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; 0 2 2
877 OUTPUT:
878 RETVAL
879  
880 int
881 NGHTTP2_FLAG_NONE()
882 CODE:
883 0 ▶ RETVAL = NGHTTP2_FLAG_NONE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_FLAG_NONE; 0 0 0
884 OUTPUT:
885 RETVAL
886  
887 int
888 NGHTTP2_FLAG_END_STREAM()
889 CODE:
890 1 ▶ RETVAL = NGHTTP2_FLAG_END_STREAM;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_FLAG_END_STREAM; 0 1 1
891 OUTPUT:
892 RETVAL
893  
894 int
895 NGHTTP2_FLAG_END_HEADERS()
896 CODE:
897 0 ▶ RETVAL = NGHTTP2_FLAG_END_HEADERS;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_FLAG_END_HEADERS; 0 0 0
898 OUTPUT:
899 RETVAL
900  
901 int
902 NGHTTP2_FLAG_ACK()
903 CODE:
904 0 ▶ RETVAL = NGHTTP2_FLAG_ACK;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_FLAG_ACK; 0 0 0
905 OUTPUT:
906 RETVAL
907  
908 int
909 NGHTTP2_FLAG_PADDED()
910 CODE:
911 0 ▶ RETVAL = NGHTTP2_FLAG_PADDED;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_FLAG_PADDED; 0 0 0
912 OUTPUT:
913 RETVAL
914  
915 int
916 NGHTTP2_FLAG_PRIORITY()
917 CODE:
918 0 ▶ RETVAL = NGHTTP2_FLAG_PRIORITY;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_FLAG_PRIORITY; 0 0 0
919 OUTPUT:
920 RETVAL
921  
922 int
923 NGHTTP2_NO_ERROR()
924 CODE:
925 37 ▶ RETVAL = NGHTTP2_NO_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_NO_ERROR; 3 34 37
926 OUTPUT:
927 RETVAL
928  
929 int
930 NGHTTP2_PROTOCOL_ERROR()
931 CODE:
932 0 ▶ RETVAL = NGHTTP2_PROTOCOL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_PROTOCOL_ERROR; 0 0 0
933 OUTPUT:
934 RETVAL
935  
936 int
937 NGHTTP2_INTERNAL_ERROR()
938 CODE:
939 1 ▶ RETVAL = NGHTTP2_INTERNAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_INTERNAL_ERROR; 0 1 1
940 OUTPUT:
941 RETVAL
942  
943 int
944 NGHTTP2_FLOW_CONTROL_ERROR()
945 CODE:
946 0 ▶ RETVAL = NGHTTP2_FLOW_CONTROL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_FLOW_CONTROL_ERROR; 0 0 0
947 OUTPUT:
948 RETVAL
949  
950 int
951 NGHTTP2_SETTINGS_TIMEOUT()
952 CODE:
953 0 ▶ RETVAL = NGHTTP2_SETTINGS_TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_SETTINGS_TIMEOUT; 0 0 0
954 OUTPUT:
955 RETVAL
956  
957 int
958 NGHTTP2_STREAM_CLOSED()
959 CODE:
960 0 ▶ RETVAL = NGHTTP2_STREAM_CLOSED;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_STREAM_CLOSED; 0 0 0
961 OUTPUT:
962 RETVAL
963  
964 int
965 NGHTTP2_FRAME_SIZE_ERROR()
966 CODE:
967 0 ▶ RETVAL = NGHTTP2_FRAME_SIZE_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_FRAME_SIZE_ERROR; 0 0 0
968 OUTPUT:
969 RETVAL
970  
971 int
972 NGHTTP2_REFUSED_STREAM()
973 CODE:
974 0 ▶ RETVAL = NGHTTP2_REFUSED_STREAM;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_REFUSED_STREAM; 0 0 0
975 OUTPUT:
976 RETVAL
977  
978 int
979 NGHTTP2_CANCEL()
980 CODE:
981 2007 ▶ RETVAL = NGHTTP2_CANCEL;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_CANCEL; 1999 8 2007
982 OUTPUT:
983 RETVAL
984  
985 int
986 NGHTTP2_COMPRESSION_ERROR()
987 CODE:
988 0 ▶ RETVAL = NGHTTP2_COMPRESSION_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_COMPRESSION_ERROR; 0 0 0
989 OUTPUT:
990 RETVAL
991  
992 int
993 NGHTTP2_CONNECT_ERROR()
994 CODE:
995 0 ▶ RETVAL = NGHTTP2_CONNECT_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_CONNECT_ERROR; 0 0 0
996 OUTPUT:
997 RETVAL
998  
999 int
1000 NGHTTP2_ENHANCE_YOUR_CALM()
1001 CODE:
1002 2 ▶ RETVAL = NGHTTP2_ENHANCE_YOUR_CALM;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_ENHANCE_YOUR_CALM; 0 2 2
1003 OUTPUT:
1004 RETVAL
1005  
1006 int
1007 NGHTTP2_INADEQUATE_SECURITY()
1008 CODE:
1009 0 ▶ RETVAL = NGHTTP2_INADEQUATE_SECURITY;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_INADEQUATE_SECURITY; 0 0 0
1010 OUTPUT:
1011 RETVAL
1012  
1013 int
1014 NGHTTP2_HTTP_1_1_REQUIRED()
1015 CODE:
1016 0 ▶ RETVAL = NGHTTP2_HTTP_1_1_REQUIRED;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_HTTP_1_1_REQUIRED; 0 0 0
1017 OUTPUT:
1018 RETVAL
1019  
1020 int
1021 NGHTTP2_HCAT_REQUEST()
1022 CODE:
1023 0 ▶ RETVAL = NGHTTP2_HCAT_REQUEST;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_HCAT_REQUEST; 0 0 0
1024 OUTPUT:
1025 RETVAL
1026  
1027 int
1028 NGHTTP2_HCAT_RESPONSE()
1029 CODE:
1030 3 ▶ RETVAL = NGHTTP2_HCAT_RESPONSE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_HCAT_RESPONSE; 0 3 3
1031 OUTPUT:
1032 RETVAL
1033  
1034 int
1035 NGHTTP2_HCAT_PUSH_RESPONSE()
1036 CODE:
1037 0 ▶ RETVAL = NGHTTP2_HCAT_PUSH_RESPONSE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_HCAT_PUSH_RESPONSE; 0 0 0
1038 OUTPUT:
1039 RETVAL
1040  
1041 int
1042 NGHTTP2_HCAT_HEADERS()
1043 CODE:
1044 11 ▶ RETVAL = NGHTTP2_HCAT_HEADERS;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_HCAT_HEADERS; 2 9 11
1045 OUTPUT:
1046 RETVAL
1047  
1048 int
1049 NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS()
1050 CODE:
1051 0 ▶ RETVAL = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; 0 0 0
1052 OUTPUT:
1053 RETVAL
1054  
1055 int
1056 NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE()
1057 CODE:
1058 0 ▶ RETVAL = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; 0 0 0
1059 OUTPUT:
1060 RETVAL
1061  
1062 int
1063 NGHTTP2_SETTINGS_MAX_FRAME_SIZE()
1064 CODE:
1065 0 ▶ RETVAL = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_SETTINGS_MAX_FRAME_SIZE; 0 0 0
1066 OUTPUT:
1067 RETVAL
1068  
1069 int
1070 NGHTTP2_SETTINGS_ENABLE_PUSH()
1071 CODE:
1072 0 ▶ RETVAL = NGHTTP2_SETTINGS_ENABLE_PUSH;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_SETTINGS_ENABLE_PUSH; 0 0 0
1073 OUTPUT:
1074 RETVAL
1075  
1076 int
1077 NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL()
1078 CODE:
1079 1 ▶ RETVAL = NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL; 0 1 1
1080 OUTPUT:
1081 RETVAL
1082  
1083 int
1084 NGHTTP2_DATA_FLAG_NONE()
1085 CODE:
1086 0 ▶ RETVAL = NGHTTP2_DATA_FLAG_NONE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_DATA_FLAG_NONE; 0 0 0
1087 OUTPUT:
1088 RETVAL
1089  
1090 int
1091 NGHTTP2_DATA_FLAG_EOF()
1092 CODE:
1093 0 ▶ RETVAL = NGHTTP2_DATA_FLAG_EOF;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_DATA_FLAG_EOF; 0 0 0
1094 OUTPUT:
1095 RETVAL
1096  
1097 int
1098 NGHTTP2_DATA_FLAG_NO_END_STREAM()
1099 CODE:
1100 0 ▶ RETVAL = NGHTTP2_DATA_FLAG_NO_END_STREAM;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_DATA_FLAG_NO_END_STREAM; 0 0 0
1101 OUTPUT:
1102 RETVAL
1103  
1104 int
1105 NGHTTP2_DATA_FLAG_NO_COPY()
1106 CODE:
1107 0 ▶ RETVAL = NGHTTP2_DATA_FLAG_NO_COPY;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_DATA_FLAG_NO_COPY; 0 0 0
1108 OUTPUT:
1109 RETVAL
1110  
1111 # Frame types
1112 int
1113 NGHTTP2_DATA()
1114 CODE:
1115 0 ▶ RETVAL = NGHTTP2_DATA;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_DATA; 0 0 0
1116 OUTPUT:
1117 RETVAL
1118  
1119 int
1120 NGHTTP2_HEADERS()
1121 CODE:
1122 0 ▶ RETVAL = NGHTTP2_HEADERS;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_HEADERS; 0 0 0
1123 OUTPUT:
1124 RETVAL
1125  
1126 int
1127 NGHTTP2_SETTINGS()
1128 CODE:
1129 0 ▶ RETVAL = NGHTTP2_SETTINGS;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_SETTINGS; 0 0 0
1130 OUTPUT:
1131 RETVAL
1132  
1133 int
1134 NGHTTP2_PUSH_PROMISE()
1135 CODE:
1136 0 ▶ RETVAL = NGHTTP2_PUSH_PROMISE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_PUSH_PROMISE; 0 0 0
1137 OUTPUT:
1138 RETVAL
1139  
1140 int
1141 NGHTTP2_GOAWAY()
1142 CODE:
1143 0 ▶ RETVAL = NGHTTP2_GOAWAY;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = NGHTTP2_GOAWAY; 0 0 0
1144 OUTPUT:
1145 RETVAL
1146  
1147  
1148 MODULE = Net::HTTP2::nghttp2 PACKAGE = Net::HTTP2::nghttp2::Session
1149  
1150 # Create new server session
1151 SV *
1152 _new_server_xs(class, callbacks_hv, user_data, ...)
1153 char *class
1154 HV *callbacks_hv
1155 SV *user_data
1156 PREINIT:
1157 nghttp2_perl_session *ps;
1158 nghttp2_session_callbacks *callbacks;
1159 168 nghttp2_option *option = NULL;
1160 int rv;
1161 SV **svp;
1162 168 HV *options_hv = NULL;
1163 CODE:
1164 /* Check for optional options hash (4th argument) */
1165 168 ▶ if (items > 3 && SvROK(ST(3)) && SvTYPE(SvRV(ST(3))) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (items > 3 && SvROK(ST(3)) && SvTYPE(SvRV(ST(3))) == SVt_PVHV) { 3 165 168
if (items > 3 && SvROK(ST(3)) && SvTYPE(SvRV(ST(3))) == SVt_PVHV) { 3 0 3
if (items > 3 && SvROK(ST(3)) && SvTYPE(SvRV(ST(3))) == SVt_PVHV) { 3 0 3
1166 3 options_hv = (HV *)SvRV(ST(3));
1167 }
1168  
1169 /* Allocate our wrapper structure */
1170 168 Newxz(ps, 1, nghttp2_perl_session);
1171  
1172 /* Initialize send buffer */
1173 168 ps->send_buf_cap = 16384;
1174 168 ps->send_buf = (char *)malloc(ps->send_buf_cap);
1175 168 ps->send_buf_len = 0;
1176  
1177 /* Store user data */
1178 168 ▶ if (SvOK(user_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(user_data)) { 0 168 168
1179 0 ps->user_data = newSVsv(user_data);
1180 }
1181  
1182 /* Extract callbacks from hash */
1183 168 extract_perl_callbacks(aTHX_ ps, callbacks_hv);
1184  
1185 /* Create nghttp2 callbacks */
1186 168 rv = nghttp2_session_callbacks_new(&callbacks);
1187 168 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 168 168
1188 0 release_perl_callbacks(aTHX_ ps);
1189 0 free(ps->send_buf);
1190 0 Safefree(ps);
1191 0 croak("nghttp2_session_callbacks_new failed: %s", nghttp2_strerror(rv));
1192 }
1193 168 register_nghttp2_callbacks(callbacks);
1194  
1195 /* Create session — use new2 with options if provided */
1196 168 ▶ if (options_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (options_hv) { 3 165 168
1197 3 rv = nghttp2_option_new(&option);
1198 3 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 3 3
1199 0 nghttp2_session_callbacks_del(callbacks);
1200 0 release_perl_callbacks(aTHX_ ps);
1201 0 free(ps->send_buf);
1202 0 Safefree(ps);
1203 0 croak("nghttp2_option_new failed: %s", nghttp2_strerror(rv));
1204 }
1205  
1206 3 ▶ if ((svp = hv_fetch(options_hv, "max_send_header_block_length", 28, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(options_hv, "max_send_header_block_length", 28, 0))) { 1 2 3
1207 1 nghttp2_option_set_max_send_header_block_length(option, SvUV(*svp));
1208 }
1209  
1210 3 ▶ if ((svp = hv_fetch(options_hv, "stream_reset_burst", 18, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(options_hv, "stream_reset_burst", 18, 0))) { 2 1 3
1211 2 SV **rate_svp = hv_fetch(options_hv, "stream_reset_rate", 17, 0);
1212 2 ▶ if (rate_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (rate_svp) { 2 0 2
1213 2 nghttp2_option_set_stream_reset_rate_limit(
1214 2 option, (uint64_t)SvUV(*svp), (uint64_t)SvUV(*rate_svp));
1215 }
1216 }
1217  
1218 3 rv = nghttp2_session_server_new2(&ps->session, callbacks, ps, option);
1219 3 nghttp2_option_del(option);
1220 } else {
1221 165 rv = nghttp2_session_server_new(&ps->session, callbacks, ps);
1222 }
1223 168 nghttp2_session_callbacks_del(callbacks);
1224  
1225 168 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 168 168
1226 0 release_perl_callbacks(aTHX_ ps);
1227 0 free(ps->send_buf);
1228 0 Safefree(ps);
1229 0 croak("nghttp2_session_server_new failed: %s", nghttp2_strerror(rv));
1230 }
1231  
1232 /* Bless and return */
1233 168 RETVAL = sv_newmortal();
1234 168 sv_setref_pv(RETVAL, class, (void *)ps);
1235 168 SvREFCNT_inc(RETVAL);
1236 OUTPUT:
1237 RETVAL
1238  
1239 # Destructor
1240 void
1241 DESTROY(self)
1242 SV *self
1243 PREINIT:
1244 nghttp2_perl_session *ps;
1245 CODE:
1246 256 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1247 256 ▶ if (ps) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps) { 256 0 256
1248 int i;
1249 256 ▶ if (ps->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->session) { 256 0 256
1250 256 nghttp2_session_del(ps->session);
1251 /* Releasing the callbacks and the providers below runs Perl
1252 destructors, which may call back in. From here on every
1253 method croaks instead of following this pointer. */
1254 256 ps->session = NULL;
1255 }
1256 256 release_perl_callbacks(aTHX_ ps);
1257 256 ▶ if (ps->send_buf) free(ps->send_buf);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->send_buf) free(ps->send_buf); 256 0 256
1258 /* Clean up data providers, released ones included */
1259 256 drain_pending_free(aTHX_ ps);
1260 256 ▶ if (ps->pending_free) free(ps->pending_free);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->pending_free) free(ps->pending_free); 0 256 256
1261 319 ▶ for (i = 0; i < ps->data_providers_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ps->data_providers_count; i++) { 63 256 319
1262 63 ▶ if (ps->data_providers[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->data_providers[i]) { 12 51 63
1263 12 free_data_provider(aTHX_ ps->data_providers[i]);
1264 }
1265 }
1266 256 ▶ if (ps->data_providers) free(ps->data_providers);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->data_providers) free(ps->data_providers); 62 194 256
1267 256 Safefree(ps);
1268 }
1269  
1270 # Feed incoming data to session
1271 int
1272 mem_recv(self, data)
1273 SV *self
1274 SV *data
1275 PREINIT:
1276 nghttp2_perl_session *ps;
1277 STRLEN len;
1278 const char *buf;
1279 ssize_t rv;
1280 CODE:
1281 4971 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1282 4971 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 4971 0 4971
SESSION_ALIVE_OR_CROAK(ps); 0 4971 4971
1283 4971 ▶ if (ps->in_session_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->in_session_call) { 1 4970 4971
1284 1 croak("mem_recv called from inside a session callback");
1285 }
1286 4970 buf = SvPVbyte(data, len);
1287  
1288 /* A callback error is reported with warn(), outside the eval that traps
1289 the callback, so a $SIG{__WARN__} that throws unwinds past the clear.
1290 The save stack restores the flag on every exit, C or Perl. */
1291 4970 ENTER;
1292 4970 SAVEINT(ps->in_session_call);
1293 4970 ps->in_session_call = 1;
1294 4970 rv = nghttp2_session_mem_recv(ps->session, (const uint8_t *)buf, len);
1295 4969 LEAVE;
1296 4969 drain_pending_free(aTHX_ ps);
1297  
1298 4969 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 1 4968 4969
1299 1 croak("nghttp2_session_mem_recv failed: %s", nghttp2_strerror((int)rv));
1300 }
1301 4968 ▶ RETVAL = (int)rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)rv; 4807 161 4968
1302 OUTPUT:
1303 RETVAL
1304  
1305 # Get data to send
1306 SV *
1307 mem_send(self)
1308 SV *self
1309 PREINIT:
1310 nghttp2_perl_session *ps;
1311 int rv;
1312 CODE:
1313 23029 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1314 23029 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 23029 0 23029
SESSION_ALIVE_OR_CROAK(ps); 0 23029 23029
1315 23029 ▶ if (ps->in_session_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->in_session_call) { 6 23023 23029
1316 6 croak("mem_send called from inside a session callback");
1317 }
1318  
1319 /* Clear send buffer */
1320 23023 ps->send_buf_len = 0;
1321  
1322 /* Trigger send callback to fill buffer */
1323 /* See mem_recv: the flag must survive a Perl-level unwind out of the
1324 session call. */
1325 23023 ENTER;
1326 23023 SAVEINT(ps->in_session_call);
1327 23023 ps->in_session_call = 1;
1328 23023 rv = nghttp2_session_send(ps->session);
1329 23022 LEAVE;
1330 23022 drain_pending_free(aTHX_ ps);
1331  
1332 23022 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 1 23021 23022
1333 1 croak("nghttp2_session_send failed: %s", nghttp2_strerror(rv));
1334 }
1335  
1336 /* Return buffered data */
1337 23021 ▶ if (ps->send_buf_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->send_buf_len > 0) { 5009 18012 23021
1338 5009 RETVAL = newSVpvn(ps->send_buf, ps->send_buf_len);
1339 } else {
1340 18012 RETVAL = newSVpvn("", 0);
1341 }
1342 OUTPUT:
1343 RETVAL
1344  
1345 # Check if session wants to read
1346 int
1347 want_read(self)
1348 SV *self
1349 PREINIT:
1350 nghttp2_perl_session *ps;
1351 CODE:
1352 3 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1353 3 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 3 0 3
SESSION_ALIVE_OR_CROAK(ps); 0 3 3
1354 3 RETVAL = nghttp2_session_want_read(ps->session);
1355 OUTPUT:
1356 RETVAL
1357  
1358 # Check if session wants to write
1359 int
1360 want_write(self)
1361 SV *self
1362 PREINIT:
1363 nghttp2_perl_session *ps;
1364 CODE:
1365 3 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1366 3 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 3 0 3
SESSION_ALIVE_OR_CROAK(ps); 0 3 3
1367 3 RETVAL = nghttp2_session_want_write(ps->session);
1368 OUTPUT:
1369 RETVAL
1370  
1371 # Submit SETTINGS frame
1372 int
1373 submit_settings(self, settings_hv)
1374 SV *self
1375 HV *settings_hv
1376 PREINIT:
1377 nghttp2_perl_session *ps;
1378 nghttp2_settings_entry iv[16];
1379 258 ▶ int niv = 0;
Branch: 1 branch
BranchTrueFalseTotal
int niv = 0; 258 0 258
1380 SV **svp;
1381 int rv;
1382 CODE:
1383 258 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1384 258 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 258 0 258
SESSION_ALIVE_OR_CROAK(ps); 0 258 258
1385  
1386 258 ▶ if ((svp = hv_fetch(settings_hv, "max_concurrent_streams", 22, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(settings_hv, "max_concurrent_streams", 22, 0))) { 223 35 258
1387 223 iv[niv].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
1388 223 iv[niv].value = SvUV(*svp);
1389 223 niv++;
1390 }
1391 258 ▶ if ((svp = hv_fetch(settings_hv, "initial_window_size", 19, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(settings_hv, "initial_window_size", 19, 0))) { 244 14 258
1392 244 iv[niv].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
1393 244 iv[niv].value = SvUV(*svp);
1394 244 niv++;
1395 }
1396 258 ▶ if ((svp = hv_fetch(settings_hv, "max_frame_size", 14, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(settings_hv, "max_frame_size", 14, 0))) { 0 258 258
1397 0 iv[niv].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
1398 0 iv[niv].value = SvUV(*svp);
1399 0 niv++;
1400 }
1401 258 ▶ if ((svp = hv_fetch(settings_hv, "enable_push", 11, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(settings_hv, "enable_push", 11, 0))) { 0 258 258
1402 0 iv[niv].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
1403 0 iv[niv].value = SvTRUE(*svp) ? 1 : 0;
1404 0 niv++;
1405 }
1406 258 ▶ if ((svp = hv_fetch(settings_hv, "enable_connect_protocol", 23, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(settings_hv, "enable_connect_protocol", 23, 0))) { 10 248 258
1407 10 iv[niv].settings_id = NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL;
1408 10 iv[niv].value = SvTRUE(*svp) ? 1 : 0;
1409 10 niv++;
1410 }
1411 258 ▶ if ((svp = hv_fetch(settings_hv, "header_table_size", 17, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(settings_hv, "header_table_size", 17, 0))) { 1 257 258
1412 1 iv[niv].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
1413 1 iv[niv].value = SvUV(*svp);
1414 1 niv++;
1415 }
1416 258 ▶ if ((svp = hv_fetch(settings_hv, "max_header_list_size", 20, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((svp = hv_fetch(settings_hv, "max_header_list_size", 20, 0))) { 1 257 258
1417 1 iv[niv].settings_id = NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE;
1418 1 iv[niv].value = SvUV(*svp);
1419 1 niv++;
1420 }
1421  
1422 258 rv = nghttp2_submit_settings(ps->session, NGHTTP2_FLAG_NONE, iv, niv);
1423 258 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 258 258
1424 0 croak("nghttp2_submit_settings failed: %s", nghttp2_strerror(rv));
1425 }
1426 258 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 229 29 258
1427 OUTPUT:
1428 RETVAL
1429  
1430 # Submit response without body
1431 int
1432 _submit_response_no_body(self, stream_id, headers_av)
1433 SV *self
1434 int stream_id
1435 AV *headers_av
1436 PREINIT:
1437 nghttp2_perl_session *ps;
1438 nghttp2_nv *nva;
1439 size_t nvlen;
1440 int rv;
1441 CODE:
1442 0 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1443 0 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 0 0 0
SESSION_ALIVE_OR_CROAK(ps); 0 0 0
1444  
1445 0 nva = perl_headers_to_nva(aTHX_ headers_av, &nvlen);
1446  
1447 0 rv = nghttp2_submit_response(ps->session, stream_id, nva, nvlen, NULL);
1448  
1449 0 ▶ if (nva) Safefree(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) Safefree(nva); 0 0 0
1450  
1451 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
1452 0 croak("nghttp2_submit_response failed: %s", nghttp2_strerror(rv));
1453 }
1454 0 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 0 0
1455 OUTPUT:
1456 RETVAL
1457  
1458 int
1459 _submit_trailer_xs(self, stream_id, headers_av)
1460 SV *self
1461 int stream_id
1462 AV *headers_av
1463 PREINIT:
1464 nghttp2_perl_session *ps;
1465 nghttp2_nv *nva;
1466 size_t nvlen;
1467 int rv;
1468 CODE:
1469 10 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1470 10 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 10 0 10
SESSION_ALIVE_OR_CROAK(ps); 0 10 10
1471 10 nva = perl_headers_to_nva(aTHX_ headers_av, &nvlen);
1472  
1473 10 rv = nghttp2_submit_trailer(ps->session, stream_id, nva, nvlen);
1474  
1475 10 ▶ if (nva) {
Branch: 1 branch
BranchTrueFalseTotal
if (nva) { 8 2 10
1476 8 Safefree(nva);
1477 }
1478  
1479 10 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 1 9 10
1480 1 croak("nghttp2_submit_trailer failed: %s", nghttp2_strerror(rv));
1481 }
1482 9 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 4 5 9
1483 OUTPUT:
1484 RETVAL
1485  
1486 # Submit a HEADERS block on a stream: a 1xx informational response, or
1487 # headers that end the stream when end_stream is true.
1488 int
1489 _submit_headers_xs(self, stream_id, headers_av, end_stream)
1490 SV *self
1491 int stream_id
1492 AV *headers_av
1493 int end_stream
1494 PREINIT:
1495 nghttp2_perl_session *ps;
1496 nghttp2_nv *nva;
1497 size_t nvlen;
1498 int rv;
1499 CODE:
1500 3 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1501 3 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 3 0 3
SESSION_ALIVE_OR_CROAK(ps); 0 3 3
1502 3 nva = perl_headers_to_nva(aTHX_ headers_av, &nvlen);
1503  
1504 3 rv = nghttp2_submit_headers(ps->session,
1505 end_stream ? NGHTTP2_FLAG_END_STREAM : NGHTTP2_FLAG_NONE,
1506 stream_id, NULL, nva, nvlen, NULL);
1507  
1508 3 ▶ if (nva) {
Branch: 1 branch
BranchTrueFalseTotal
if (nva) { 3 0 3
1509 3 Safefree(nva);
1510 }
1511  
1512 3 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 1 2 3
1513 1 croak("nghttp2_submit_headers failed: %s", nghttp2_strerror(rv));
1514 }
1515 2 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 1 1 2
1516 OUTPUT:
1517 RETVAL
1518  
1519 # Resume data on a stream (after NGHTTP2_ERR_DEFERRED)
1520 int
1521 resume_data(self, stream_id)
1522 SV *self
1523 int stream_id
1524 PREINIT:
1525 nghttp2_perl_session *ps;
1526 int rv;
1527 CODE:
1528 2 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1529 2 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 2 0 2
SESSION_ALIVE_OR_CROAK(ps); 0 2 2
1530 2 rv = nghttp2_session_resume_data(ps->session, stream_id);
1531 2 ▶ if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) { 0 2 2
if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) { 0 0 0
1532 0 croak("nghttp2_session_resume_data failed: %s", nghttp2_strerror(rv));
1533 }
1534 2 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 1 1 2
1535 OUTPUT:
1536 RETVAL
1537  
1538 # Get stream user data
1539 SV *
1540 get_stream_user_data(self, stream_id)
1541 SV *self
1542 int stream_id
1543 PREINIT:
1544 nghttp2_perl_session *ps;
1545 void *data;
1546 CODE:
1547 0 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1548 0 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 0 0 0
SESSION_ALIVE_OR_CROAK(ps); 0 0 0
1549 0 data = nghttp2_session_get_stream_user_data(ps->session, stream_id);
1550 0 ▶ if (data) {
Branch: 1 branch
BranchTrueFalseTotal
if (data) { 0 0 0
1551 0 RETVAL = newSVsv((SV *)data);
1552 } else {
1553 0 RETVAL = &PL_sv_undef;
1554 }
1555 OUTPUT:
1556 RETVAL
1557  
1558 # Set stream user data
1559 int
1560 set_stream_user_data(self, stream_id, data)
1561 SV *self
1562 int stream_id
1563 SV *data
1564 PREINIT:
1565 nghttp2_perl_session *ps;
1566 int rv;
1567 CODE:
1568 0 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1569 0 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 0 0 0
SESSION_ALIVE_OR_CROAK(ps); 0 0 0
1570 /* Note: caller must ensure data SV survives */
1571 0 rv = nghttp2_session_set_stream_user_data(ps->session, stream_id,
1572 0 ▶ SvOK(data) ? newSVsv(data) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SvOK(data) ? newSVsv(data) : NULL); 0 0 0
1573 0 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 0 0
1574 OUTPUT:
1575 RETVAL
1576  
1577 # Query whether the remote peer half closed a stream.
1578 # Returns 1 or 0, or undef when no such stream exists.
1579 SV *
1580 get_stream_remote_close(self, stream_id)
1581 SV *self
1582 int stream_id
1583 PREINIT:
1584 nghttp2_perl_session *ps;
1585 int rv;
1586 CODE:
1587 8 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1588 8 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 8 0 8
SESSION_ALIVE_OR_CROAK(ps); 0 8 8
1589 8 rv = nghttp2_session_get_stream_remote_close(ps->session, stream_id);
1590 8 ▶ RETVAL = rv < 0 ? &PL_sv_undef : newSViv(rv);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv < 0 ? &PL_sv_undef : newSViv(rv); 4 4 8
1591 OUTPUT:
1592 RETVAL
1593  
1594 # Query whether the local peer half closed a stream.
1595 # Returns 1 or 0, or undef when no such stream exists.
1596 SV *
1597 get_stream_local_close(self, stream_id)
1598 SV *self
1599 int stream_id
1600 PREINIT:
1601 nghttp2_perl_session *ps;
1602 int rv;
1603 CODE:
1604 6 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1605 6 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 6 0 6
SESSION_ALIVE_OR_CROAK(ps); 0 6 6
1606 6 rv = nghttp2_session_get_stream_local_close(ps->session, stream_id);
1607 6 ▶ RETVAL = rv < 0 ? &PL_sv_undef : newSViv(rv);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv < 0 ? &PL_sv_undef : newSViv(rv); 4 2 6
1608 OUTPUT:
1609 RETVAL
1610  
1611 # Terminate session with GOAWAY
1612 int
1613 terminate_session(self, error_code)
1614 SV *self
1615 int error_code
1616 PREINIT:
1617 nghttp2_perl_session *ps;
1618 int rv;
1619 CODE:
1620 1 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1621 1 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 1 0 1
SESSION_ALIVE_OR_CROAK(ps); 0 1 1
1622 1 rv = nghttp2_session_terminate_session(ps->session, error_code);
1623 1 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 1 1
1624 OUTPUT:
1625 RETVAL
1626  
1627 # Submit response with streaming data callback
1628 # Callback receives ($stream_id, $max_length[, $user_data]); user data is optional.
1629 # It returns:
1630 # ($data, $eof_flag) - send data; EOF ends the stream
1631 # ($data, $eof_flag, $no_end_stream) - EOF without DATA END_STREAM for trailers
1632 # defer: return undef or an empty list; resume or submit data later
1633 int
1634 _submit_response_streaming(self, stream_id, headers_av, data_callback, cb_user_data)
1635 SV *self
1636 int stream_id
1637 AV *headers_av
1638 SV *data_callback
1639 SV *cb_user_data
1640 PREINIT:
1641 nghttp2_perl_session *ps;
1642 nghttp2_nv *nva;
1643 size_t nvlen;
1644 nghttp2_data_provider data_prd;
1645 nghttp2_perl_data_provider *dp;
1646 int rv;
1647 CODE:
1648 2282 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1649 2282 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 2282 0 2282
SESSION_ALIVE_OR_CROAK(ps); 0 2282 2282
1650 2282 assert_stream_has_no_provider(aTHX_ ps, stream_id);
1651  
1652 2280 nva = perl_headers_to_nva(aTHX_ headers_av, &nvlen);
1653  
1654 /* Create data provider state */
1655 2280 Newxz(dp, 1, nghttp2_perl_data_provider);
1656 2280 dp->stream_id = stream_id;
1657 2280 dp->callback = newSVsv(data_callback);
1658 2280 ▶ if (SvOK(cb_user_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(cb_user_data)) { 2213 67 2280
1659 2213 dp->user_data = newSVsv(cb_user_data);
1660 }
1661 2280 dp->eof = 0;
1662 2280 dp->deferred = 0;
1663  
1664 /* Set up nghttp2 data provider */
1665 2280 data_prd.source.ptr = dp;
1666 2280 data_prd.read_callback = perl_data_source_read_callback;
1667  
1668 2280 rv = nghttp2_submit_response(ps->session, stream_id, nva, nvlen, &data_prd);
1669  
1670 2280 ▶ if (nva) Safefree(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) Safefree(nva); 2280 0 2280
1671  
1672 2280 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 2 2278 2280
1673 /* nghttp2 copies the descriptor only when the submit succeeds, so a
1674 failure leaves the provider ours. No session call is running here,
1675 so there is nothing to hold it past. */
1676 2 free_data_provider(aTHX_ dp);
1677 2 croak("nghttp2_submit_response failed: %s", nghttp2_strerror(rv));
1678 }
1679  
1680 /* The session owns the provider from here on. */
1681 2278 add_data_provider(aTHX_ ps, dp);
1682 2278 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 2259 19 2278
1683 OUTPUT:
1684 RETVAL
1685  
1686 # Queue data to send on an existing stream.
1687 # The stream must already have a data provider (from submit_request or
1688 # submit_response with a streaming body callback). This sets the data
1689 # provider's user_data to the given data and eof flag, clears the deferred
1690 # state, and calls nghttp2_session_resume_data so the next mem_send will
1691 # invoke the read callback which returns this data.
1692 int
1693 submit_data(self, stream_id, data_sv, eof, no_end_stream = 0)
1694 SV *self
1695 int stream_id
1696 SV *data_sv
1697 int eof
1698 int no_end_stream
1699 PREINIT:
1700 nghttp2_perl_session *ps;
1701 nghttp2_perl_data_provider *dp;
1702 int rv;
1703 CODE:
1704 9 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1705 9 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 9 0 9
SESSION_ALIVE_OR_CROAK(ps); 0 9 9
1706  
1707 9 dp = find_data_provider(ps, stream_id);
1708 9 ▶ if (!dp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dp) { 0 9 9
1709 0 croak("submit_data: no data provider for stream %d "
1710 "(submit_request or submit_response with body callback first)",
1711 stream_id);
1712 }
1713  
1714 /* Replace the callback with NULL (one-shot static body mode) and
1715 store the data in user_data for the read callback to pick up */
1716 9 ▶ if (dp->callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp->callback) { 7 2 9
1717 7 SvREFCNT_dec(dp->callback);
1718 7 dp->callback = NULL;
1719 }
1720 9 ▶ if (dp->user_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp->user_data) { 0 9 9
1721 0 SvREFCNT_dec(dp->user_data);
1722 }
1723 9 ▶ dp->user_data = SvOK(data_sv) ? newSVsv(data_sv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
dp->user_data = SvOK(data_sv) ? newSVsv(data_sv) : NULL; 9 0 9
1724 9 dp->eof = eof ? 1 : 0;
1725 9 ▶ dp->no_end_stream = (eof && no_end_stream) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
dp->no_end_stream = (eof && no_end_stream) ? 1 : 0; 7 2 9
dp->no_end_stream = (eof && no_end_stream) ? 1 : 0; 2 5 7
1726 9 dp->deferred = 0;
1727  
1728 /* Resume the stream so nghttp2 calls the read callback */
1729 9 rv = nghttp2_session_resume_data(ps->session, stream_id);
1730 9 ▶ if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) { 0 9 9
if (rv != 0 && rv != NGHTTP2_ERR_INVALID_ARGUMENT) { 0 0 0
1731 0 croak("submit_data: resume failed: %s", nghttp2_strerror(rv));
1732 }
1733 9 ▶ RETVAL = 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 0; 0 9 9
1734 OUTPUT:
1735 RETVAL
1736  
1737 # Check if stream is deferred (waiting for data)
1738 int
1739 is_stream_deferred(self, stream_id)
1740 SV *self
1741 int stream_id
1742 PREINIT:
1743 nghttp2_perl_session *ps;
1744 nghttp2_perl_data_provider *dp;
1745 CODE:
1746 0 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1747 0 dp = find_data_provider(ps, stream_id);
1748 0 ▶ RETVAL = dp ? dp->deferred : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = dp ? dp->deferred : 0; 0 0 0
RETVAL = dp ? dp->deferred : 0; 0 0 0
1749 OUTPUT:
1750 RETVAL
1751  
1752 # Clear deferred flag for a stream (internal use after resume_data)
1753 void
1754 _clear_deferred(self, stream_id)
1755 SV *self
1756 int stream_id
1757 PREINIT:
1758 nghttp2_perl_session *ps;
1759 nghttp2_perl_data_provider *dp;
1760 CODE:
1761 2 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1762 2 dp = find_data_provider(ps, stream_id);
1763 2 ▶ if (dp) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp) { 2 0 2
1764 2 dp->deferred = 0;
1765 }
1766  
1767 # Create new client session
1768 SV *
1769 _new_client_xs(class, callbacks_hv, user_data)
1770 char *class
1771 HV *callbacks_hv
1772 SV *user_data
1773 PREINIT:
1774 nghttp2_perl_session *ps;
1775 nghttp2_session_callbacks *callbacks;
1776 int rv;
1777 CODE:
1778 /* Allocate our wrapper structure */
1779 88 Newxz(ps, 1, nghttp2_perl_session);
1780  
1781 /* Initialize send buffer */
1782 88 ps->send_buf_cap = 16384;
1783 88 ps->send_buf = (char *)malloc(ps->send_buf_cap);
1784 88 ps->send_buf_len = 0;
1785  
1786 /* Store user data */
1787 88 ▶ if (SvOK(user_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(user_data)) { 0 88 88
1788 0 ps->user_data = newSVsv(user_data);
1789 }
1790  
1791 /* Extract callbacks from hash */
1792 88 extract_perl_callbacks(aTHX_ ps, callbacks_hv);
1793  
1794 /* Create nghttp2 callbacks */
1795 88 rv = nghttp2_session_callbacks_new(&callbacks);
1796 88 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 88 88
1797 0 release_perl_callbacks(aTHX_ ps);
1798 0 free(ps->send_buf);
1799 0 Safefree(ps);
1800 0 croak("nghttp2_session_callbacks_new failed: %s", nghttp2_strerror(rv));
1801 }
1802 88 register_nghttp2_callbacks(callbacks);
1803  
1804 /* Create CLIENT session (difference from server) */
1805 88 rv = nghttp2_session_client_new(&ps->session, callbacks, ps);
1806 88 nghttp2_session_callbacks_del(callbacks);
1807  
1808 88 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 88 88
1809 0 release_perl_callbacks(aTHX_ ps);
1810 0 free(ps->send_buf);
1811 0 Safefree(ps);
1812 0 croak("nghttp2_session_client_new failed: %s", nghttp2_strerror(rv));
1813 }
1814  
1815 /* Bless and return */
1816 88 RETVAL = sv_newmortal();
1817 88 sv_setref_pv(RETVAL, class, (void *)ps);
1818 88 SvREFCNT_inc(RETVAL);
1819 OUTPUT:
1820 RETVAL
1821  
1822 # Submit request (client-side)
1823 # Returns stream ID on success
1824 int
1825 _submit_request_xs(self, headers_av, body_sv)
1826 SV *self
1827 AV *headers_av
1828 SV *body_sv
1829 PREINIT:
1830 nghttp2_perl_session *ps;
1831 nghttp2_nv *nva;
1832 size_t nvlen;
1833 nghttp2_data_provider data_prd;
1834 2315 nghttp2_data_provider *data_prd_ptr = NULL;
1835 2315 nghttp2_perl_data_provider *dp = NULL;
1836 int32_t stream_id;
1837 2315 ▶ STRLEN body_len = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN body_len = 0; 2315 0 2315
1838 CODE:
1839 2315 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1840 2315 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 2315 0 2315
SESSION_ALIVE_OR_CROAK(ps); 0 2315 2315
1841  
1842 2315 nva = perl_headers_to_nva(aTHX_ headers_av, &nvlen);
1843  
1844 /* Check if we have a body to send */
1845 2315 ▶ if (SvOK(body_sv) && SvROK(body_sv) && SvTYPE(SvRV(body_sv)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvOK(body_sv) && SvROK(body_sv) && SvTYPE(SvRV(body_sv)) == SVt_PVCV) { 9 2306 2315
if (SvOK(body_sv) && SvROK(body_sv) && SvTYPE(SvRV(body_sv)) == SVt_PVCV) { 7 2 9
if (SvOK(body_sv) && SvROK(body_sv) && SvTYPE(SvRV(body_sv)) == SVt_PVCV) { 7 0 7
1846 /* CODE ref body: streaming callback data provider */
1847 7 Newxz(dp, 1, nghttp2_perl_data_provider);
1848 7 dp->stream_id = 0; /* Will be set after submit */
1849 7 dp->eof = 0;
1850 7 dp->deferred = 0;
1851 7 dp->callback = newSVsv(body_sv);
1852  
1853 7 data_prd.source.ptr = dp;
1854 7 data_prd.read_callback = perl_data_source_read_callback;
1855 7 data_prd_ptr = &data_prd;
1856 }
1857 2308 ▶ else if (SvOK(body_sv) && SvPOK(body_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvOK(body_sv) && SvPOK(body_sv)) { 2 2306 2308
else if (SvOK(body_sv) && SvPOK(body_sv)) { 2 0 2
1858 2 const char *body_ptr = SvPVbyte(body_sv, body_len);
1859 2 ▶ if (body_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_len > 0) { 2 0 2
1860 /* Static string body: one-shot data provider */
1861 2 Newxz(dp, 1, nghttp2_perl_data_provider);
1862 2 dp->stream_id = 0; /* Will be set after submit */
1863 2 dp->eof = 1; /* Static bodies always want EOF after sending */
1864 2 dp->deferred = 0;
1865 2 dp->user_data = newSVsv(body_sv);
1866 2 dp->callback = NULL; /* Use user_data as body */
1867  
1868 2 data_prd.source.ptr = dp;
1869 2 data_prd.read_callback = perl_data_source_read_callback;
1870 2 data_prd_ptr = &data_prd;
1871 }
1872 }
1873  
1874 2315 stream_id = nghttp2_submit_request(ps->session, NULL, nva, nvlen, data_prd_ptr, NULL);
1875  
1876 2315 ▶ if (nva) Safefree(nva);
Branch: 1 branch
BranchTrueFalseTotal
if (nva) Safefree(nva); 2315 0 2315
1877  
1878 2315 ▶ if (stream_id < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stream_id < 0) { 0 2315 2315
1879 /* The session never took the provider; see _submit_response_streaming. */
1880 0 ▶ if (dp) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp) { 0 0 0
1881 0 free_data_provider(aTHX_ dp);
1882 }
1883 0 croak("nghttp2_submit_request failed: %s", nghttp2_strerror(stream_id));
1884 }
1885  
1886 /* The session owns the provider from here on. */
1887 2315 ▶ if (dp) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp) { 9 2306 2315
1888 9 dp->stream_id = stream_id;
1889 9 add_data_provider(aTHX_ ps, dp);
1890 }
1891  
1892 2315 ▶ RETVAL = stream_id;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = stream_id; 2297 18 2315
1893 OUTPUT:
1894 RETVAL
1895  
1896 # Submit RST_STREAM (reset a stream)
1897 int
1898 submit_rst_stream(self, stream_id, error_code)
1899 SV *self
1900 int stream_id
1901 unsigned int error_code
1902 PREINIT:
1903 nghttp2_perl_session *ps;
1904 int rv;
1905 CODE:
1906 2028 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1907 2028 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 2028 0 2028
SESSION_ALIVE_OR_CROAK(ps); 0 2028 2028
1908 2028 rv = nghttp2_submit_rst_stream(ps->session, NGHTTP2_FLAG_NONE, stream_id, error_code);
1909 2028 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 2028 2028
1910 0 croak("nghttp2_submit_rst_stream failed: %s", nghttp2_strerror(rv));
1911 }
1912 2028 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 1999 29 2028
1913 OUTPUT:
1914 RETVAL
1915  
1916 # Submit GOAWAY frame
1917 int
1918 _submit_goaway_xs(self, last_stream_id, error_code, opaque_data)
1919 SV *self
1920 int last_stream_id
1921 unsigned int error_code
1922 SV *opaque_data
1923 PREINIT:
1924 nghttp2_perl_session *ps;
1925 5 STRLEN len = 0;
1926 5 ▶ const uint8_t *data = NULL;
Branch: 1 branch
BranchTrueFalseTotal
const uint8_t *data = NULL; 5 0 5
1927 int rv;
1928 CODE:
1929 5 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1930 5 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 5 0 5
SESSION_ALIVE_OR_CROAK(ps); 0 5 5
1931  
1932 5 ▶ if (SvOK(opaque_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(opaque_data)) { 1 4 5
1933 1 data = (const uint8_t *)SvPVbyte(opaque_data, len);
1934 }
1935  
1936 5 rv = nghttp2_submit_goaway(ps->session, NGHTTP2_FLAG_NONE,
1937 last_stream_id, error_code, data, len);
1938 5 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 1 4 5
1939 1 croak("nghttp2_submit_goaway failed: %s", nghttp2_strerror(rv));
1940 }
1941 4 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 3 1 4
1942 OUTPUT:
1943 RETVAL
1944  
1945 # Submit PING frame
1946 int
1947 submit_ping(self, ack, opaque_data)
1948 SV *self
1949 int ack
1950 SV *opaque_data
1951 PREINIT:
1952 nghttp2_perl_session *ps;
1953 STRLEN len;
1954 const uint8_t *data;
1955 uint8_t flags;
1956 int rv;
1957 CODE:
1958 1 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1959 1 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 1 0 1
SESSION_ALIVE_OR_CROAK(ps); 1 0 1
1960 0 flags = ack ? NGHTTP2_FLAG_ACK : NGHTTP2_FLAG_NONE;
1961  
1962 0 ▶ if (SvOK(opaque_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(opaque_data)) { 0 0 0
1963 0 data = (const uint8_t *)SvPVbyte(opaque_data, len);
1964 0 ▶ if (len != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (len != 8) { 0 0 0
1965 0 croak("PING opaque_data must be exactly 8 bytes");
1966 }
1967 } else {
1968 0 data = NULL;
1969 }
1970  
1971 0 rv = nghttp2_submit_ping(ps->session, flags, data);
1972 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
1973 0 croak("nghttp2_submit_ping failed: %s", nghttp2_strerror(rv));
1974 }
1975 0 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 0 0
1976 OUTPUT:
1977 RETVAL
1978  
1979 # Submit WINDOW_UPDATE frame
1980 int
1981 submit_window_update(self, stream_id, window_size_increment)
1982 SV *self
1983 int stream_id
1984 int window_size_increment
1985 PREINIT:
1986 nghttp2_perl_session *ps;
1987 int rv;
1988 CODE:
1989 0 ps = (nghttp2_perl_session *)SvIV(SvRV(self));
1990 0 ▶ SESSION_ALIVE_OR_CROAK(ps);
Branch: 2 branches
BranchTrueFalseTotal
SESSION_ALIVE_OR_CROAK(ps); 0 0 0
SESSION_ALIVE_OR_CROAK(ps); 0 0 0
1991 0 rv = nghttp2_submit_window_update(ps->session, NGHTTP2_FLAG_NONE, stream_id, window_size_increment);
1992 0 ▶ if (rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != 0) { 0 0 0
1993 0 croak("nghttp2_submit_window_update failed: %s", nghttp2_strerror(rv));
1994 }
1995 0 ▶ RETVAL = rv;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rv; 0 0 0
1996 OUTPUT:
1997 RETVAL
« blib/lib/Net/HTTP2/nghttp2/Session.pm ↑ Summary blib/lib/Net/HTTP2/nghttp2.pm »