xs/writer.xs

filter: Statementstmt 86.4% 64 / 74 Branchbran 63.7% 51 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.6% 115 / 154 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_workerhook_perl.h ↑ Summary Hyperman.c »
1 MODULE = Hyperman PACKAGE = Hyperman::Writer
2  
3 # The psgi.streaming writer, handed out by the responder: ->write($chunk),
4 # ->close.
5 #
6 # Payload shapes, discriminated on length because a writer is a blessed AV
7 # and not three classes:
8 #
9 # [fd, id, serial, "h1"] HTTP/1.1: chunked and keep-alive when the
10 # request allows, else EOF-delimited. The
11 # serial names the response on that
12 # connection, so a writer kept past it
13 # cannot end the one that followed
14 # [fd, id] the pre-0.57 HTTP/1.1 shape, kept so a
15 # writer built by hand still works
16 # [fd, gen, stream_id, "h2"] HTTP/2, buffered until close
17 # [udp_fd, qid, stream_id, "h3"] HTTP/3, the same shape over QUIC
18 # [handle, serial, "stream"] an ABI v6 stream handle (hm_stream.h),
19 # the transport-neutral one: the same three
20 # calls write a body over HTTP/1.1 or over
21 # an h2 stream, and the branch is inside
22 # the handle rather than out here.
23 #
24 # The four-element forms are told apart by the TAG at index 3, not by length.
25 # Length was enough while h2 was the only one; h3 has the same arity, so a
26 # length test would route every h3 writer into hm_h2_writer_write and write
27 # an HTTP/3 body into an nghttp2 session. Existing h2 writers already carry
28 # their tag, so keying on it changes no behaviour.
29 #
30 # The serial is carried for diagnostics only. What proves a handle is live is
31 # the registry, which every entry point consults before dereferencing it, so
32 # ->write after the connection died is a return value and not a crash.
33  
34 # Returns 1 when the bytes were taken and the connection has room, 0 when
35 # they were taken but the connection is holding more than it should (the
36 # producer pauses until a write answers 1 again), and -1 when nothing was
37 # written because the writer is stale: closed, aborted, or outliving its
38 # request. Before 0.62 it returned nothing, so a Perl producer could not
39 # see FULL at all and a slow reader grew the buffer without bound.
40 IV
41 write(self, data)
42 SV *self
43 SV *data
44 CODE:
45 {
46 53 AV *w = (AV *)SvRV(self);
47 53 SSize_t n = av_len(w);
48 53 RETVAL = -1;
49 53 ▶ if (n == 2) { /* stream-handle form */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) { /* stream-handle form */ 10 43 53
50 10 SV **h = av_fetch(w, 0, 0);
51 STRLEN l;
52 10 const char *p = SvPV(data, l);
53 10 ▶ if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 1 10
54 9 int rc = hm_stream_write_h(aTHX_ INT2PTR(void *, SvIV(*h)), p, l);
55 9 RETVAL = rc == HM_ABI_STREAM_OK ? 1
56 9 ▶ : rc == HM_ABI_STREAM_FULL ? 0 : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
: rc == HM_ABI_STREAM_FULL ? 0 : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 8 9
: rc == HM_ABI_STREAM_FULL ? 0 : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
57 }
58 43 ▶ } else if (n >= 3) { /* h2 or h3: told apart by the tag */
Branch: 1 branch
BranchTrueFalseTotal
} else if (n >= 3) { /* h2 or h3: told apart by the tag */ 40 3 43
59 40 SV **f = av_fetch(w, 0, 0);
60 40 SV **g = av_fetch(w, 1, 0);
61 40 SV **s = av_fetch(w, 2, 0);
62 40 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 40 0 40
# uncoverable branch false class:ignore_covered_err 40 0 40
# uncoverable branch false class:ignore_covered_err 40 0 40
63 40 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 40 0 40
64 40 # uncoverable condition right count:2 class:ignore_covered_err
65 0 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 0 0 0
66 0 if (f && g && s) {
67 0 if (hm_writer_tag_is(aTHX_ w, "h1", 2))
68 RETVAL = hm_stream_write(aTHX_ (int)SvIV(*f), SvUV(*g), SvUV(*s), data);
69 0 # uncoverable branch true class:ignore_covered_err
70 0 # uncoverable condition left count:1 class:ignore_covered_err
71 else if (hm_writer_tag_is(aTHX_ w, "h3", 2))
72 RETVAL = hm_h3_writer_write(aTHX_ (int)SvIV(*f), SvUV(*g), /* # uncoverable statement class:ignore_covered_err */
73 3 (int64_t)SvIV(*s), data); /* # uncoverable statement class:ignore_covered_err */
74 3 else
75 3 ▶ RETVAL = hm_h2_writer_write(aTHX_ (int)SvIV(*f), SvUV(*g),
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = hm_h2_writer_write(aTHX_ (int)SvIV(*f), SvUV(*g), 3 0 3
RETVAL = hm_h2_writer_write(aTHX_ (int)SvIV(*f), SvUV(*g), 3 0 3
76 3 (int32_t)SvIV(*s), data);
77 }
78 } else {
79 SV **f = av_fetch(w, 0, 0);
80 SV **i = av_fetch(w, 1, 0);
81 # uncoverable branch false class:ignore_covered_err
82 # uncoverable condition right count:1 class:ignore_covered_err
83 # uncoverable condition right count:2 class:ignore_covered_err
84 if (f && i)
85 RETVAL = hm_stream_write(aTHX_ (int)SvIV(*f), SvUV(*i), 0, data);
86 }
87 }
88 OUTPUT:
89 RETVAL
90  
91 # A writer dropped without ->close still ends its body, the promise a lexical
92 # filehandle makes. The stream-handle form owns a C handle to close. The
93 # HTTP/1.1 pair names a connection parked on this writer: without the close
94 # it would stay awaiting, with keep-alive off, for ever - the sweep skips a
95 # parked connection and the drain never closes one. The h2 and h3 shapes
96 # buffer until close and are left alone: ending those here would submit a
97 # response the application may not have finished.
98 void
99 DESTROY(self)
100 SV *self
101 CODE:
102 {
103 33 AV *w = (AV *)SvRV(self);
104 33 SSize_t n = av_len(w);
105 33 ▶ if (n == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) { 6 27 33
106 6 SV **h = av_fetch(w, 0, 0);
107 6 ▶ if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 0 6
if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 5 6
108 1 hm_stream_close_h(aTHX_ INT2PTR(void *, SvIV(*h)));
109 1 sv_setiv(*h, 0);
110 }
111 27 ▶ } else if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (n == 1) { 3 24 27
112 3 SV **f = av_fetch(w, 0, 0);
113 3 SV **i = av_fetch(w, 1, 0);
114 3 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 3 0 3
# uncoverable condition right count:1 class:ignore_covered_err 3 0 3
# uncoverable condition right count:1 class:ignore_covered_err 2 1 3
115 2 # uncoverable condition right count:2 class:ignore_covered_err
116 24 ▶ if (f && i && hm_cur_loop)
Branch: 2 branches
BranchTrueFalseTotal
if (f && i && hm_cur_loop) 24 0 24
if (f && i && hm_cur_loop) 24 0 24
117 24 hm_stream_close(aTHX_ (int)SvIV(*f), SvUV(*i), 0);
118 24 } else if (n >= 3 && hm_writer_tag_is(aTHX_ w, "h1", 2)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
119 24 SV **f = av_fetch(w, 0, 0);
120 24 ▶ SV **g = av_fetch(w, 1, 0);
Branch: 4 branches
BranchTrueFalseTotal
SV **g = av_fetch(w, 1, 0); 24 0 24
SV **g = av_fetch(w, 1, 0); 24 0 24
SV **g = av_fetch(w, 1, 0); 24 0 24
SV **g = av_fetch(w, 1, 0); 23 1 24
121 23 SV **s = av_fetch(w, 2, 0);
122 # uncoverable condition right count:1 class:ignore_covered_err
123 # uncoverable condition right count:2 class:ignore_covered_err
124 # uncoverable condition right count:3 class:ignore_covered_err
125 if (f && g && s && hm_cur_loop)
126 hm_stream_close(aTHX_ (int)SvIV(*f), SvUV(*g), SvUV(*s));
127 }
128 }
129  
130 void
131 close(self)
132 SV *self
133 CODE:
134 {
135 24 AV *w = (AV *)SvRV(self);
136 24 SSize_t n = av_len(w);
137 24 ▶ if (n == 2) { /* stream-handle form */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) { /* stream-handle form */ 3 21 24
138 3 SV **h = av_fetch(w, 0, 0);
139 3 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 3 0 3
# uncoverable branch false class:ignore_covered_err 3 0 3
140 3 # uncoverable condition right count:1 class:ignore_covered_err
141 # uncoverable condition right count:2 class:ignore_covered_err
142 if (h && SvIV(*h)) {
143 3 hm_stream_close_h(aTHX_ INT2PTR(void *, SvIV(*h)));
144 /* the handle is freed; blank the slot so a second ->close
145 21 ▶ * cannot hand the registry a stale pointer to compare */
Branch: 1 branch
BranchTrueFalseTotal
* cannot hand the registry a stale pointer to compare */ 20 1 21
146 20 sv_setiv(*h, 0);
147 20 }
148 20 } else if (n >= 3) { /* h2 or h3: told apart by the tag */
149 20 ▶ SV **f = av_fetch(w, 0, 0);
Branch: 3 branches
BranchTrueFalseTotal
SV **f = av_fetch(w, 0, 0); 20 0 20
SV **f = av_fetch(w, 0, 0); 20 0 20
SV **f = av_fetch(w, 0, 0); 20 0 20
150 20 ▶ SV **g = av_fetch(w, 1, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **g = av_fetch(w, 1, 0); 20 0 20
151 20 SV **s = av_fetch(w, 2, 0);
152 0 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 0 0 0
153 0 # uncoverable condition right count:1 class:ignore_covered_err
154 0 # uncoverable condition right count:2 class:ignore_covered_err
155 # uncoverable condition right count:3 class:ignore_covered_err
156 0 if (f && g && s) {
157 0 if (hm_writer_tag_is(aTHX_ w, "h1", 2))
158 hm_stream_close(aTHX_ (int)SvIV(*f), SvUV(*g), SvUV(*s));
159 # uncoverable branch true class:ignore_covered_err
160 1 # uncoverable condition left count:1 class:ignore_covered_err
161 1 else if (hm_writer_tag_is(aTHX_ w, "h3", 2))
162 1 ▶ hm_h3_writer_close(aTHX_ (int)SvIV(*f), SvUV(*g), /* # uncoverable statement class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
hm_h3_writer_close(aTHX_ (int)SvIV(*f), SvUV(*g), /* # uncoverable statement class:ignore_covered_err */ 1 0 1
hm_h3_writer_close(aTHX_ (int)SvIV(*f), SvUV(*g), /* # uncoverable statement class:ignore_covered_err */ 1 0 1
163 1 (int64_t)SvIV(*s)); /* # uncoverable statement class:ignore_covered_err */
164 else
165 hm_h2_writer_close(aTHX_ (int)SvIV(*f), SvUV(*g),
166 (int32_t)SvIV(*s));
167 }
168 } else {
169 SV **f = av_fetch(w, 0, 0);
170 SV **i = av_fetch(w, 1, 0);
171 # uncoverable branch false class:ignore_covered_err
172 # uncoverable condition right count:1 class:ignore_covered_err
173 # uncoverable condition right count:2 class:ignore_covered_err
174 if (f && i)
175 hm_stream_close(aTHX_ (int)SvIV(*f), SvUV(*i), 0);
176 }
177 }
178  
179 # The other ending, for a producer that failed part way through a body. Only
180 # the stream-handle form has one: the older two shapes have no verb for it,
181 # and inventing a graceful close here would be exactly the lie this exists to
182 # avoid, so they are left alone and the caller is told nothing happened.
183 IV
184 abort(self)
185 SV *self
186 CODE:
187 {
188 5 AV *w = (AV *)SvRV(self);
189 5 RETVAL = HM_ABI_STREAM_STALE;
190 5 ▶ if (av_len(w) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(w) == 2) { 3 2 5
191 3 SV **h = av_fetch(w, 0, 0);
192 3 ▶ if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 2 3
193 1 RETVAL = hm_stream_abort_h(aTHX_ INT2PTR(void *, SvIV(*h)));
194 1 sv_setiv(*h, 0); /* freed; a later ->close is a no-op */
195 }
196 }
197 }
198 OUTPUT:
199 RETVAL
« include/hyperman/hm_workerhook_perl.h ↑ Summary Hyperman.c »