xs/writer.xs

filter: Statementstmt 93.9% 77 / 82 Branchbran 73.3% 66 / 90 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.1% 143 / 172 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_sa_bus_perl.h ↑ Summary include/hyperman/hm_future.h »
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 56 AV *w = (AV *)SvRV(self);
104 56 SSize_t n = av_len(w);
105 56 ▶ if (n == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) { 8 48 56
106 8 SV **h = av_fetch(w, 0, 0);
107 8 ▶ 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 */ 8 0 8
if (h && SvIV(*h)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 7 8
108 1 hm_stream_close_h(aTHX_ INT2PTR(void *, SvIV(*h)));
109 1 sv_setiv(*h, 0);
110 }
111 48 ▶ } else if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (n == 1) { 3 45 48
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 45 ▶ if (f && i && hm_cur_loop)
Branch: 2 branches
BranchTrueFalseTotal
if (f && i && hm_cur_loop) 45 0 45
if (f && i && hm_cur_loop) 25 20 45
117 25 hm_stream_close(aTHX_ (int)SvIV(*f), SvUV(*i), 0);
118 25 } else if (n >= 3 && hm_writer_tag_is(aTHX_ w, "h1", 2)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
119 25 SV **f = av_fetch(w, 0, 0);
120 25 ▶ SV **g = av_fetch(w, 1, 0);
Branch: 4 branches
BranchTrueFalseTotal
SV **g = av_fetch(w, 1, 0); 25 0 25
SV **g = av_fetch(w, 1, 0); 25 0 25
SV **g = av_fetch(w, 1, 0); 25 0 25
SV **g = av_fetch(w, 1, 0); 23 2 25
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 # ->close ends the body. ->close(\@trailers) ends it and sends the listed
131 # fields after it. The list is judged first, whatever the writer's shape
132 # (hm_trailers.h), so a bad list croaks before anything is sent and the
133 # body is still open for a corrected close. A good list then goes where the
134 # transport can put it:
135 #
136 # h2, and a stream handle over h2 a trailing HEADERS frame
137 # h1, and a stream handle over h1 after the last chunk, if the body is
138 # chunked and the request's TE asked,
139 # and otherwise nowhere
140 # h3 nowhere yet: close croaks
141 void
142 close(self, given = NULL)
143 SV *self
144 SV *given
145 CODE:
146 {
147 47 AV *w = (AV *)SvRV(self);
148 47 SSize_t n = av_len(w);
149 SV *err;
150 47 AV *trailers = hm_trailers_build(aTHX_ given, &err);
151 47 ▶ if (err) croak("Hyperman::Writer::close: %" SVf, SVfARG(err));
Branch: 1 branch
BranchTrueFalseTotal
if (err) croak("Hyperman::Writer::close: %" SVf, SVfARG(err)); 13 34 47
152 34 ▶ if (trailers && n >= 3 && hm_writer_tag_is(aTHX_ w, "h3", 2)) {
Branch: 3 branches
BranchTrueFalseTotal
if (trailers && n >= 3 && hm_writer_tag_is(aTHX_ w, "h3", 2)) { 5 29 34
if (trailers && n >= 3 && hm_writer_tag_is(aTHX_ w, "h3", 2)) { 3 2 5
if (trailers && n >= 3 && hm_writer_tag_is(aTHX_ w, "h3", 2)) { 1 2 3
153 1 SvREFCNT_dec((SV *)trailers);
154 1 croak("Hyperman::Writer::close: trailers are not implemented "
155 "over HTTP/3");
156 }
157 33 ▶ if (n == 2) { /* stream-handle form */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) { /* stream-handle form */ 5 28 33
158 5 SV **h = av_fetch(w, 0, 0);
159 5 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 5 0 5
# uncoverable branch false class:ignore_covered_err 4 1 5
160 4 # uncoverable condition right count:1 class:ignore_covered_err
161 4 # uncoverable condition right count:2 class:ignore_covered_err
162 if (h && SvIV(*h)) {
163 hm_stream_finish_h(aTHX_ INT2PTR(void *, SvIV(*h)), trailers);
164 4 trailers = NULL;
165 /* the handle is freed; blank the slot so a second ->close
166 28 ▶ * cannot hand the registry a stale pointer to compare */
Branch: 1 branch
BranchTrueFalseTotal
* cannot hand the registry a stale pointer to compare */ 27 1 28
167 27 sv_setiv(*h, 0);
168 27 }
169 27 } else if (n >= 3) { /* h1, h2 or h3: told apart by the tag */
170 27 ▶ SV **f = av_fetch(w, 0, 0);
Branch: 3 branches
BranchTrueFalseTotal
SV **f = av_fetch(w, 0, 0); 27 0 27
SV **f = av_fetch(w, 0, 0); 27 0 27
SV **f = av_fetch(w, 0, 0); 27 0 27
171 27 ▶ SV **g = av_fetch(w, 1, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **g = av_fetch(w, 1, 0); 21 6 27
172 21 SV **s = av_fetch(w, 2, 0);
173 6 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 2 4 6
174 2 # uncoverable condition right count:1 class:ignore_covered_err
175 2 # uncoverable condition right count:2 class:ignore_covered_err
176 # uncoverable condition right count:3 class:ignore_covered_err
177 4 if (f && g && s) {
178 4 if (hm_writer_tag_is(aTHX_ w, "h1", 2))
179 4 hm_stream_finish(aTHX_ (int)SvIV(*f), SvUV(*g), SvUV(*s), trailers);
180 # uncoverable branch true class:ignore_covered_err
181 # uncoverable condition left count:1 class:ignore_covered_err
182 else if (hm_writer_tag_is(aTHX_ w, "h3", 2))
183 1 hm_h3_writer_close(aTHX_ (int)SvIV(*f), SvUV(*g), /* # uncoverable statement class:ignore_covered_err */
184 1 (int64_t)SvIV(*s)); /* # uncoverable statement class:ignore_covered_err */
185 1 ▶ else {
Branch: 2 branches
BranchTrueFalseTotal
else { 1 0 1
else { 1 0 1
186 1 hm_h2_writer_close(aTHX_ (int)SvIV(*f), SvUV(*g),
187 (int32_t)SvIV(*s), trailers);
188 33 ▶ trailers = NULL;
Branch: 1 branch
BranchTrueFalseTotal
trailers = NULL; 2 31 33
189 }
190 }
191 } else {
192 SV **f = av_fetch(w, 0, 0);
193 SV **i = av_fetch(w, 1, 0);
194 # uncoverable branch false class:ignore_covered_err
195 # uncoverable condition right count:1 class:ignore_covered_err
196 # uncoverable condition right count:2 class:ignore_covered_err
197 if (f && i)
198 hm_stream_finish(aTHX_ (int)SvIV(*f), SvUV(*i), 0, trailers);
199 }
200 if (trailers) SvREFCNT_dec((SV *)trailers);
201 }
202  
203 # The other ending, for a producer that failed part way through a body. Only
204 # the stream-handle form has one: the older two shapes have no verb for it,
205 # and inventing a graceful close here would be exactly the lie this exists to
206 # avoid, so they are left alone and the caller is told nothing happened.
207 IV
208 abort(self)
209 SV *self
210 CODE:
211 {
212 5 AV *w = (AV *)SvRV(self);
213 5 RETVAL = HM_ABI_STREAM_STALE;
214 5 ▶ if (av_len(w) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(w) == 2) { 3 2 5
215 3 SV **h = av_fetch(w, 0, 0);
216 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
217 1 RETVAL = hm_stream_abort_h(aTHX_ INT2PTR(void *, SvIV(*h)));
218 1 sv_setiv(*h, 0); /* freed; a later ->close is a no-op */
219 }
220 }
221 }
222 OUTPUT:
223 RETVAL
« include/hyperman/hm_sa_bus_perl.h ↑ Summary include/hyperman/hm_future.h »