nghttp2.xs

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