nghttp2.xs

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