feersum_psgi.c.inc

filter: Statementstmt 84.3% 936 / 1110 Branchbran 63.0% 705 / 1118 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.6% 1641 / 2228 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« feersum_core.c.inc ↑ Summary feersum_utils.c.inc »
1  
2 // leftmost X-Forwarded-For entry is the original client
3 static SV*
4 18 extract_forwarded_addr(pTHX_ struct feer_req *r)
5 {
6 size_t val_len;
7 18 const char *val = find_header_value(r, "x-forwarded-for", 15, &val_len);
8 18 ▶ if (!val || val_len == 0) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!val || val_len == 0) return NULL; 15 3 18
if (!val || val_len == 0) return NULL; 0 15 15
9  
10 15 ▶ while (val_len > 0 && (*val == ' ' || *val == '\t')) { val++; val_len--; }
Branch: 3 branches
BranchTrueFalseTotal
while (val_len > 0 && (*val == ' ' || *val == '\t')) { val++; val_len--; } 15 0 15
while (val_len > 0 && (*val == ' ' || *val == '\t')) { val++; val_len--; } 0 15 15
while (val_len > 0 && (*val == ' ' || *val == '\t')) { val++; val_len--; } 0 15 15
11 15 ▶ if (val_len == 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (val_len == 0) return NULL; 0 15 15
12  
13 15 size_t ip_len = 0;
14 187 ▶ while (ip_len < val_len && val[ip_len] != ',' && val[ip_len] != ' ') ip_len++;
Branch: 3 branches
BranchTrueFalseTotal
while (ip_len < val_len && val[ip_len] != ',' && val[ip_len] != ' ') ip_len++; 178 9 187
while (ip_len < val_len && val[ip_len] != ',' && val[ip_len] != ' ') ip_len++; 173 5 178
while (ip_len < val_len && val[ip_len] != ',' && val[ip_len] != ' ') ip_len++; 172 1 173
15  
16 15 ▶ if (ip_len == 0 || ip_len > 45) return NULL; // max IPv6 length is 45 chars
Branch: 2 branches
BranchTrueFalseTotal
if (ip_len == 0 || ip_len > 45) return NULL; // max IPv6 length is 45 chars 15 0 15
if (ip_len == 0 || ip_len > 45) return NULL; // max IPv6 length is 45 chars 0 15 15
17  
18 char ip_buf[46];
19 15 memcpy(ip_buf, val, ip_len);
20 15 ip_buf[ip_len] = '\0';
21  
22 struct in_addr addr4;
23 struct in6_addr addr6;
24 15 ▶ if (inet_pton(AF_INET, ip_buf, &addr4) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, ip_buf, &addr4) == 1) { 9 6 15
25 9 return newSVpvn(val, ip_len);
26 }
27 6 ▶ if (inet_pton(AF_INET6, ip_buf, &addr6) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET6, ip_buf, &addr6) == 1) { 2 4 6
28 2 return newSVpvn(val, ip_len);
29 }
30  
31 trace("X-Forwarded-For contains invalid IP: %s\n", ip_buf);
32 4 return NULL;
33 }
34  
35 static SV*
36 17 extract_forwarded_proto(pTHX_ struct feer_req *r)
37 {
38 size_t val_len;
39 17 const char *val = find_header_value(r, "x-forwarded-proto", 17, &val_len);
40 17 ▶ if (!val || val_len == 0) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!val || val_len == 0) return NULL; 7 10 17
if (!val || val_len == 0) return NULL; 0 7 7
41  
42 7 ▶ while (val_len > 0 && (*val == ' ' || *val == '\t')) { val++; val_len--; }
Branch: 3 branches
BranchTrueFalseTotal
while (val_len > 0 && (*val == ' ' || *val == '\t')) { val++; val_len--; } 7 0 7
while (val_len > 0 && (*val == ' ' || *val == '\t')) { val++; val_len--; } 0 7 7
while (val_len > 0 && (*val == ' ' || *val == '\t')) { val++; val_len--; } 0 7 7
43  
44 7 ▶ if (val_len >= 5 && str_case_eq_fixed("https", val, 5) &&
Branch: 2 branches
BranchTrueFalseTotal
if (val_len >= 5 && str_case_eq_fixed("https", val, 5) && 7 0 7
if (val_len >= 5 && str_case_eq_fixed("https", val, 5) && 7 0 7
45 7 ▶ (val_len == 5 || val[5] == ' ' || val[5] == '\t' || val[5] == ','))
Branch: 4 branches
BranchTrueFalseTotal
(val_len == 5 || val[5] == ' ' || val[5] == '\t' || val[5] == ',')) 1 6 7
(val_len == 5 || val[5] == ' ' || val[5] == '\t' || val[5] == ',')) 1 0 1
(val_len == 5 || val[5] == ' ' || val[5] == '\t' || val[5] == ',')) 1 0 1
(val_len == 5 || val[5] == ' ' || val[5] == '\t' || val[5] == ',')) 1 0 1
46 7 return newSVpvs("https");
47 0 ▶ if (val_len >= 4 && str_case_eq_fixed("http", val, 4) &&
Branch: 2 branches
BranchTrueFalseTotal
if (val_len >= 4 && str_case_eq_fixed("http", val, 4) && 0 0 0
if (val_len >= 4 && str_case_eq_fixed("http", val, 4) && 0 0 0
48 0 ▶ (val_len == 4 || val[4] == ' ' || val[4] == '\t' || val[4] == ','))
Branch: 4 branches
BranchTrueFalseTotal
(val_len == 4 || val[4] == ' ' || val[4] == '\t' || val[4] == ',')) 0 0 0
(val_len == 4 || val[4] == ' ' || val[4] == '\t' || val[4] == ',')) 0 0 0
(val_len == 4 || val[4] == ' ' || val[4] == '\t' || val[4] == ',')) 0 0 0
(val_len == 4 || val[4] == ' ' || val[4] == '\t' || val[4] == ',')) 0 0 0
49 0 return newSVpvs("http");
50  
51 0 return NULL;
52 }
53  
54 /* Returns a new SV, or NULL for the default "http". */
55 static SV *
56 1106 feer_determine_url_scheme(pTHX_ struct feer_conn *c)
57 {
58 #ifdef FEERSUM_HAS_H2
59 /* H2 is TLS-only here; :scheme is validated but not consulted. */
60 if (c->is_h2_stream) return newSVpvs("https");
61 #endif
62 #ifdef FEERSUM_HAS_TLS
63 1106 ▶ if (c->tls) return newSVpvs("https");
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) return newSVpvs("https"); 147 959 1106
64 #endif
65 959 ▶ if (c->proxy_ssl) return newSVpvs("https");
Branch: 1 branch
BranchTrueFalseTotal
if (c->proxy_ssl) return newSVpvs("https"); 5 954 959
66 954 ▶ if (c->proxy_proto_version > 0 && c->proxy_dst_port == 443)
Branch: 2 branches
BranchTrueFalseTotal
if (c->proxy_proto_version > 0 && c->proxy_dst_port == 443) 39 915 954
if (c->proxy_proto_version > 0 && c->proxy_dst_port == 443) 5 34 39
67 5 return newSVpvs("https");
68 949 ▶ if (c->cached_use_reverse_proxy && c->req) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->cached_use_reverse_proxy && c->req) { 17 932 949
if (c->cached_use_reverse_proxy && c->req) { 17 0 17
69 17 SV *fwd = extract_forwarded_proto(aTHX_ c->req);
70 17 ▶ if (fwd) return fwd;
Branch: 1 branch
BranchTrueFalseTotal
if (fwd) return fwd; 7 10 17
71 }
72 942 return NULL;
73 }
74  
75 static HV *psgi_env_template = NULL;
76  
77 static void
78 159 feersum_init_psgi_env_constants(pTHX)
79 {
80 159 ▶ if (psgi_env_version) return;
Branch: 1 branch
BranchTrueFalseTotal
if (psgi_env_version) return; 0 159 159
81 159 psgi_env_version = newRV((SV*)psgi_ver);
82 159 psgi_env_errors = newRV((SV*)PL_stderrgv);
83  
84 /* Values are copies: a shared immortal croaks when an app assigns to the key. */
85 159 HV *t = newHV();
86 /* The newHVhv clone inherits the bucket count; 48 covers a typical request. */
87 159 hv_ksplit(t, 48);
88 159 hv_stores(t, "psgi.version", newSVsv(psgi_env_version));
89 159 hv_stores(t, "psgi.errors", newSVsv(psgi_env_errors));
90 159 hv_stores(t, "psgi.run_once", newSVsv(&PL_sv_no));
91 159 hv_stores(t, "psgi.nonblocking", newSVsv(&PL_sv_yes));
92 159 hv_stores(t, "psgi.multithread", newSVsv(&PL_sv_no));
93 159 hv_stores(t, "psgi.streaming", newSVsv(&PL_sv_yes));
94 /* No psgix.input.buffered: psgi.input cannot rewind. */
95 159 hv_stores(t, "psgix.output.buffered", newSVsv(&PL_sv_yes));
96 159 hv_stores(t, "psgix.body.scalar_refs", newSVsv(&PL_sv_yes));
97 159 hv_stores(t, "psgix.output.guard", newSVsv(&PL_sv_yes));
98 159 hv_stores(t, "SCRIPT_NAME", newSVpvs(""));
99 159 psgi_env_template = t;
100 }
101  
102 /* Plain PV only; magic, taint, refs and dualvars must go through newSVsv. */
103 INLINE_UNLESS_DEBUG static SV *
104 8752 feer_copy_pv(pTHX_ SV *src)
105 {
106 STRLEN len;
107 const char *pv;
108 8752 ▶ if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src)
Branch: 10 branches
BranchTrueFalseTotal
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 8752 0 8752
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 2190 6562 8752
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 6562 2190 8752
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 0 6562 6562
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 6562 2190 8752
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 0 6562 6562
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 6562 2190 8752
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 0 6562 6562
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 6562 2190 8752
if (unlikely(!src || !SvPOK(src) || SvIOK(src) || SvNOK(src) 0 6562 6562
109 || SvGMAGICAL(src) || SvROK(src)))
110 2190 return newSVsv(src);
111 6562 pv = SvPV_const(src, len);
112 6562 return newSVpvn_flags(pv, len, SvUTF8(src) ? SVf_UTF8 : 0);
113 }
114  
115 static HV*
116 1095 feersum_build_psgi_env(pTHX)
117 {
118 1095 return newHVhv(psgi_env_template);
119 }
120  
121 static HV*
122 1095 feersum_env(pTHX_ struct feer_conn *c)
123 {
124 HV *e;
125 int i,j;
126 1095 struct feer_req *r = c->req;
127  
128 1095 ▶ if (unlikely(!psgi_env_version))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!psgi_env_version)) 159 936 1095
129 159 feersum_init_psgi_env_constants(aTHX);
130  
131 1095 e = feersum_build_psgi_env(aTHX);
132  
133 trace("generating header (fd %d) %.*s\n",
134 c->fd, (int)r->uri_len, r->uri);
135  
136 // Copies: the listener SVs are readonly and middleware assigns to these keys.
137 {
138 1095 struct feer_listen *conn_lsnr = c->listener;
139 1095 ▶ hv_stores(e, "SERVER_NAME",
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(e, "SERVER_NAME", 1095 0 1095
140 conn_lsnr->server_name ? feer_copy_pv(aTHX_ conn_lsnr->server_name) : newSVpvs(""));
141 1095 ▶ hv_stores(e, "SERVER_PORT",
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(e, "SERVER_PORT", 1095 0 1095
142 conn_lsnr->server_port ? feer_copy_pv(aTHX_ conn_lsnr->server_port) : newSVpvs("0"));
143 }
144 1095 hv_stores(e, "REQUEST_URI", feersum_env_uri(aTHX_ r));
145 #ifdef FEERSUM_HAS_H2
146 /* feersum_env_method* return an owned ref to a shared read-only SV. */
147 {
148 SV *m = feersum_env_method_h2(aTHX_ c, r);
149 hv_stores(e, "REQUEST_METHOD", feer_copy_pv(aTHX_ m));
150 SvREFCNT_dec(m);
151 }
152 if (unlikely(c->is_h2_stream))
153 hv_stores(e, "SERVER_PROTOCOL", newSVpvs("HTTP/2"));
154 else
155 hv_stores(e, "SERVER_PROTOCOL", feer_copy_pv(aTHX_ feersum_env_protocol(aTHX_ r)));
156 #else
157 {
158 1095 SV *m = feersum_env_method(aTHX_ r);
159 1095 hv_stores(e, "REQUEST_METHOD", feer_copy_pv(aTHX_ m));
160 1095 SvREFCNT_dec(m);
161 }
162 1095 hv_stores(e, "SERVER_PROTOCOL", feer_copy_pv(aTHX_ feersum_env_protocol(aTHX_ r)));
163 #endif
164  
165 1095 feersum_set_conn_remote_info(aTHX_ c);
166  
167 1095 ▶ if (c->cached_use_reverse_proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->cached_use_reverse_proxy) { 13 1082 1095
168 13 SV *fwd_addr = extract_forwarded_addr(aTHX_ r);
169 13 ▶ hv_stores(e, "REMOTE_ADDR", fwd_addr ? fwd_addr : feer_copy_pv(aTHX_ c->remote_addr));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(e, "REMOTE_ADDR", fwd_addr ? fwd_addr : feer_copy_pv(aTHX_ c->remote_addr)); 5 8 13
170 } else {
171 1082 hv_stores(e, "REMOTE_ADDR", feer_copy_pv(aTHX_ c->remote_addr));
172 }
173 1095 hv_stores(e, "REMOTE_PORT", feer_copy_pv(aTHX_ c->remote_port));
174  
175 {
176 1095 SV *scheme = feer_determine_url_scheme(aTHX_ c);
177 1095 ▶ hv_stores(e, "psgi.url_scheme", scheme ? scheme : newSVpvs("http"));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(e, "psgi.url_scheme", scheme ? scheme : newSVpvs("http")); 936 159 1095
178 }
179  
180 1095 hv_stores(e, "CONTENT_LENGTH", newSViv(c->expected_cl));
181  
182 1095 hv_stores(e, "psgi.input", new_feer_conn_handle(aTHX_ c, 0));
183  
184 /* A copy: a shared immortal croaks on assignment. */
185 1095 ▶ hv_stores(e, "psgi.multiprocess",
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(e, "psgi.multiprocess", 0 1095 1095
186 newSVsv(c->server->multiprocess ? &PL_sv_yes : &PL_sv_no));
187  
188 /* Check the server's live flag, not a per-conn cache: reading psgix.io
189 * takes the socket, so a native handler must never be handed one. */
190 1095 ▶ if (c->server->request_cb_is_psgi && c->server->psgix_io) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->server->request_cb_is_psgi && c->server->psgix_io) { 709 386 1095
if (c->server->request_cb_is_psgi && c->server->psgix_io) { 708 1 709
191 708 SV *fake_fh = newSViv(c->fd);
192 708 SV *selfref = sv_2mortal(feer_conn_2sv(c));
193 708 sv_magicext(fake_fh, selfref, PERL_MAGIC_ext, &psgix_io_vtbl, NULL, 0);
194 708 hv_stores(e, "psgix.io", fake_fh);
195 }
196  
197 1095 ▶ if (c->trailers) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->trailers) { 0 1095 1095
198 0 hv_stores(e, "psgix.h2.trailers", newRV_inc((SV*)c->trailers));
199 }
200  
201 1095 ▶ if (c->proxy_tlvs) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->proxy_tlvs) { 14 1081 1095
202 /* Copy: the TLV hash is shared by every request and stream on the conn. */
203 14 HV *tlvs = (HV *)SvRV(c->proxy_tlvs);
204 14 HV *copy = newHVhv(tlvs);
205 14 hv_stores(e, "psgix.proxy_tlvs", newRV_noinc((SV *)copy));
206 }
207  
208 1095 ▶ if (likely(!r->path)) feersum_set_path_and_query(aTHX_ r);
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!r->path)) feersum_set_path_and_query(aTHX_ r); 1094 1 1095
209 1095 hv_stores(e, "PATH_INFO", feer_copy_pv(aTHX_ r->path));
210 1095 hv_stores(e, "QUERY_STRING", feer_copy_pv(aTHX_ r->query));
211  
212 1095 char *kbuf = header_key_buf; // already holds the "HTTP_" prefix
213  
214 3165 ▶ for (i=0; i<r->num_headers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<r->num_headers; i++) { 2070 1095 3165
215 2070 struct phr_header *hdr = &(r->headers[i]);
216 // hdr->name is never NULL: obs-fold is rejected at parse time
217 2168 if (unlikely(hdr->name_len == 14) &&
218 98 str_case_eq_fixed("content-length", hdr->name, 14))
219 {
220 // CONTENT_LENGTH was set above; PSGI forbids HTTP_CONTENT_LENGTH
221 98 continue;
222 }
223 1983 else if (unlikely(hdr->name_len == 12) &&
224 11 str_case_eq_fixed("content-type", hdr->name, 12))
225 {
226 8 hv_stores(e, "CONTENT_TYPE", newSVpvn(hdr->value, hdr->value_len));
227 8 continue;
228 }
229 2023 else if (unlikely(hdr->name_len == 17) &&
230 59 str_case_eq_fixed("transfer-encoding", hdr->name, 17))
231 {
232 // Body already de-chunked; TE next to CONTENT_LENGTH is a CL.TE hazard
233 51 continue;
234 }
235  
236 // defense in depth: parse already rejects these with 431
237 1913 ▶ if (unlikely(hdr->name_len > MAX_HEADER_NAME_LEN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(hdr->name_len > MAX_HEADER_NAME_LEN)) { 0 1913 1913
238 trace("skipping oversized header name (len=%zu) on fd %d\n",
239 hdr->name_len, c->fd);
240 0 continue;
241 }
242  
243 1913 size_t klen = 5+hdr->name_len;
244 1913 char *key = kbuf + 5;
245 15938 ▶ for (j=0; j<hdr->name_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j=0; j<hdr->name_len; j++) { 14025 1913 15938
246 14025 *key++ = ascii_upper_dash[(unsigned char)hdr->name[j]];
247 }
248  
249 /* '_' normalises like '-': a "Content_Length:" header must not inject this. */
250 1913 ▶ if (unlikely((klen == 19 && memEQ(kbuf, "HTTP_CONTENT_LENGTH", 19))
Branch: 6 branches
BranchTrueFalseTotal
if (unlikely((klen == 19 && memEQ(kbuf, "HTTP_CONTENT_LENGTH", 19)) 0 1913 1913
if (unlikely((klen == 19 && memEQ(kbuf, "HTTP_CONTENT_LENGTH", 19)) 0 0 0
if (unlikely((klen == 19 && memEQ(kbuf, "HTTP_CONTENT_LENGTH", 19)) 1913 0 1913
if (unlikely((klen == 19 && memEQ(kbuf, "HTTP_CONTENT_LENGTH", 19)) 3 1910 1913
if (unlikely((klen == 19 && memEQ(kbuf, "HTTP_CONTENT_LENGTH", 19)) 0 3 3
if (unlikely((klen == 19 && memEQ(kbuf, "HTTP_CONTENT_LENGTH", 19)) 0 1913 1913
251 || (klen == 17 && memEQ(kbuf, "HTTP_CONTENT_TYPE", 17))))
252 0 continue;
253  
254 1913 SV **fetched = hv_fetch(e, kbuf, klen, 1);
255 trace("adding header to env (fd %d) %.*s: %.*s\n",
256 c->fd, (int)klen, kbuf, (int)hdr->value_len, hdr->value);
257  
258 1913 ▶ if (unlikely(fetched == NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(fetched == NULL)) { 0 1913 1913
259 trace("hv_fetch returned NULL (OOM?) on fd %d\n", c->fd);
260 0 continue;
261 }
262 1913 SV *cur_val = *fetched;
263 1913 ▶ if (unlikely(SvPOK(cur_val))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvPOK(cur_val))) { 2 1911 1913
264 trace("... is multivalue\n");
265 // cookie joins with "; " (RFC 9113 8.2.3)
266 2 ▶ const char *sep = (hdr->name_len == 6
Branch: 1 branch
BranchTrueFalseTotal
const char *sep = (hdr->name_len == 6 0 2 2
267 0 ▶ && str_case_eq_fixed("cookie", hdr->name, 6)) ? "; " : ", ";
Branch: 1 branch
BranchTrueFalseTotal
&& str_case_eq_fixed("cookie", hdr->name, 6)) ? "; " : ", "; 0 0 0
268 2 sv_catpvn(cur_val, sep, 2);
269 2 sv_catpvn(cur_val, hdr->value, hdr->value_len);
270 }
271 else {
272 1911 sv_setpvn(cur_val, hdr->value, hdr->value_len);
273 }
274 }
275  
276 /* absolute-form authority overrides Host (RFC 9112 3.2.2) */
277 {
278 STRLEN authority_len;
279 1095 const char *authority = feer_absolute_authority(r->uri, r->uri_len, &authority_len);
280 1095 ▶ if (unlikely(authority != NULL) && authority_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(authority != NULL) && authority_len > 0) { 5 1090 1095
if (unlikely(authority != NULL) && authority_len > 0) { 5 0 5
281 /* strip userinfo (RFC 3986 3.2) */
282 65 ▶ for (STRLEN i = authority_len; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = authority_len; i > 0; i--) { 60 5 65
283 60 ▶ if (authority[i - 1] == '@') {
Branch: 1 branch
BranchTrueFalseTotal
if (authority[i - 1] == '@') { 0 60 60
284 0 authority += i; authority_len -= i; break;
285 }
286 }
287 5 ▶ if (authority_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (authority_len > 0) 5 0 5
288 5 hv_stores(e, "HTTP_HOST", newSVpvn(authority, authority_len));
289 }
290 }
291  
292 #ifdef FEERSUM_HAS_H2
293 /* :authority overrides Host (RFC 9113 8.3.1) */
294 if (unlikely(c->is_h2_stream)) {
295 struct feer_h2_stream *stream = H2_STREAM_FROM_PC(c);
296 if (stream && stream->h2_authority) {
297 STRLEN alen;
298 const char *aval = SvPV(stream->h2_authority, alen);
299 hv_stores(e, "HTTP_HOST", newSVpvn(aval, alen));
300 }
301 /* Extended CONNECT (RFC 8441) is presented as an H1 Upgrade request. */
302 if (stream && stream->is_tunnel && stream->h2_protocol) {
303 STRLEN plen;
304 const char *pval = SvPV(stream->h2_protocol, plen);
305 hv_stores(e, "HTTP_UPGRADE", newSVpvn(pval, plen));
306 hv_stores(e, "HTTP_CONNECTION", newSVpvs("Upgrade"));
307 hv_stores(e, "psgix.h2.protocol", newSVpvn(pval, plen));
308 hv_stores(e, "psgix.h2.extended_connect", newSViv(1));
309 }
310 }
311 #endif
312  
313 1095 return e;
314 }
315  
316 #define COPY_NORM_HEADER(_str) \
317 for (i = 0; i < r->num_headers; i++) {\
318 struct phr_header *hdr = &(r->headers[i]);\
319 if (unlikely(hdr->name_len > MAX_HEADER_NAME_LEN)) continue;\
320 char *k = kbuf;\
321 for (j = 0; j < hdr->name_len; j++) { char n = hdr->name[j]; *k++ = _str; }\
322 SV** val = hv_fetch(e, kbuf, hdr->name_len, 1);\
323 if (unlikely(!val)) continue;\
324 if (unlikely(SvPOK(*val))) {\
325 const char *sep = (hdr->name_len == 6\
326 && str_case_eq_fixed("cookie", hdr->name, 6)) ? "; " : ", ";\
327 sv_catpvn(*val, sep, 2);\
328 sv_catpvn(*val, hdr->value, hdr->value_len);\
329 } else {\
330 sv_setpvn(*val, hdr->value, hdr->value_len);\
331 }\
332 }\
333 break;
334  
335 static HV*
336 7 feersum_env_headers(pTHX_ struct feer_req *r, int norm)
337 {
338 size_t i; size_t j; HV* e;
339 7 e = newHV();
340 7 ▶ if (r->num_headers > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r->num_headers > 0) 7 0 7
341 7 hv_ksplit(e, r->num_headers);
342 7 char *kbuf = header_key_buf + 5; // skip the "HTTP_" prefix
343 7 switch (norm) {
344 1 case HEADER_NORM_SKIP:
345 26 ▶ COPY_NORM_HEADER(n)
Branch: 7 branches
BranchTrueFalseTotal
COPY_NORM_HEADER(n) 0 3 3
COPY_NORM_HEADER(n) 22 3 25
COPY_NORM_HEADER(n) 0 3 3
COPY_NORM_HEADER(n) 0 3 3
COPY_NORM_HEADER(n) 0 0 0
COPY_NORM_HEADER(n) 0 0 0
COPY_NORM_HEADER(n) 3 1 4
346 2 case HEADER_NORM_LOCASE:
347 71 ▶ COPY_NORM_HEADER(ascii_lower[(unsigned char)n])
Branch: 7 branches
BranchTrueFalseTotal
COPY_NORM_HEADER(ascii_lower[(unsigned char)n]) 0 9 9
COPY_NORM_HEADER(ascii_lower[(unsigned char)n]) 60 9 69
COPY_NORM_HEADER(ascii_lower[(unsigned char)n]) 0 9 9
COPY_NORM_HEADER(ascii_lower[(unsigned char)n]) 2 7 9
COPY_NORM_HEADER(ascii_lower[(unsigned char)n]) 2 0 2
COPY_NORM_HEADER(ascii_lower[(unsigned char)n]) 1 1 2
COPY_NORM_HEADER(ascii_lower[(unsigned char)n]) 9 2 11
348 1 case HEADER_NORM_UPCASE:
349 26 ▶ COPY_NORM_HEADER(ascii_upper[(unsigned char)n])
Branch: 7 branches
BranchTrueFalseTotal
COPY_NORM_HEADER(ascii_upper[(unsigned char)n]) 0 3 3
COPY_NORM_HEADER(ascii_upper[(unsigned char)n]) 22 3 25
COPY_NORM_HEADER(ascii_upper[(unsigned char)n]) 0 3 3
COPY_NORM_HEADER(ascii_upper[(unsigned char)n]) 0 3 3
COPY_NORM_HEADER(ascii_upper[(unsigned char)n]) 0 0 0
COPY_NORM_HEADER(ascii_upper[(unsigned char)n]) 0 0 0
COPY_NORM_HEADER(ascii_upper[(unsigned char)n]) 3 1 4
350 2 case HEADER_NORM_LOCASE_DASH:
351 82 ▶ COPY_NORM_HEADER(ascii_lower_dash[(unsigned char)n])
Branch: 7 branches
BranchTrueFalseTotal
COPY_NORM_HEADER(ascii_lower_dash[(unsigned char)n]) 0 8 8
COPY_NORM_HEADER(ascii_lower_dash[(unsigned char)n]) 72 8 80
COPY_NORM_HEADER(ascii_lower_dash[(unsigned char)n]) 0 8 8
COPY_NORM_HEADER(ascii_lower_dash[(unsigned char)n]) 0 8 8
COPY_NORM_HEADER(ascii_lower_dash[(unsigned char)n]) 0 0 0
COPY_NORM_HEADER(ascii_lower_dash[(unsigned char)n]) 0 0 0
COPY_NORM_HEADER(ascii_lower_dash[(unsigned char)n]) 8 2 10
352 1 case HEADER_NORM_UPCASE_DASH:
353 26 ▶ COPY_NORM_HEADER(ascii_upper_dash[(unsigned char)n])
Branch: 7 branches
BranchTrueFalseTotal
COPY_NORM_HEADER(ascii_upper_dash[(unsigned char)n]) 0 3 3
COPY_NORM_HEADER(ascii_upper_dash[(unsigned char)n]) 22 3 25
COPY_NORM_HEADER(ascii_upper_dash[(unsigned char)n]) 0 3 3
COPY_NORM_HEADER(ascii_upper_dash[(unsigned char)n]) 0 3 3
COPY_NORM_HEADER(ascii_upper_dash[(unsigned char)n]) 0 0 0
COPY_NORM_HEADER(ascii_upper_dash[(unsigned char)n]) 0 0 0
COPY_NORM_HEADER(ascii_upper_dash[(unsigned char)n]) 3 1 4
354 0 default:
355 0 break;
356 }
357 7 return e;
358 }
359  
360 INLINE_UNLESS_DEBUG static SV*
361 10 feersum_env_header(pTHX_ struct feer_req *r, SV *name)
362 {
363 10 SV *result = NULL;
364 STRLEN nlen;
365 10 const char *np = SvPV(name, nlen);
366 10 ▶ const bool is_cookie = (nlen == 6 && str_case_eq_fixed("cookie", np, 6));
Branch: 2 branches
BranchTrueFalseTotal
const bool is_cookie = (nlen == 6 && str_case_eq_fixed("cookie", np, 6)); 2 8 10
const bool is_cookie = (nlen == 6 && str_case_eq_fixed("cookie", np, 6)); 1 1 2
367 52 ▶ for (size_t i = 0; i < r->num_headers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < r->num_headers; i++) { 42 10 52
368 42 struct phr_header *hdr = &(r->headers[i]);
369 42 ▶ if (unlikely(hdr->name_len == nlen
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(hdr->name_len == nlen 16 26 42
if (unlikely(hdr->name_len == nlen 12 4 16
370 && str_case_eq_both(np, hdr->name, nlen))) {
371 12 ▶ if (likely(!result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!result)) { 10 2 12
372 10 result = newSVpvn(hdr->value, hdr->value_len);
373 } else {
374 2 ▶ sv_catpvn(result, is_cookie ? "; " : ", ", 2);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(result, is_cookie ? "; " : ", ", 2); 1 1 2
375 2 sv_catpvn(result, hdr->value, hdr->value_len);
376 }
377 }
378 }
379 10 ▶ return result ? result : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return result ? result : &PL_sv_undef; 10 0 10
380 }
381  
382 /* Returns an owned RV; the glob's scalar slot holds a back-ref that keeps c
383 * alive. On croak the caller still owns fd. */
384 static SV *
385 15 feer_make_io_handle(pTHX_ struct feer_conn *c, int fd, const char *who)
386 {
387 15 dSP;
388 15 SV *sv = newSViv(fd);
389  
390 15 ENTER;
391 15 SAVETMPS;
392 15 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 15 15
393 15 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 15 15
394 15 ▶ mXPUSHs(newSViv(fd));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(fd)); 0 15 15
395 15 PUTBACK;
396  
397 15 call_pv("Feersum::Connection::_raw", G_VOID|G_DISCARD|G_EVAL);
398 15 SPAGAIN;
399  
400 15 ▶ if (unlikely(SvTRUE(ERRSV))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV))) { 15 0 15
if (unlikely(SvTRUE(ERRSV))) { 0 15 15
401 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
402 0 SvREFCNT_dec(sv);
403 0 ▶ croak("Failed to create %s IO handle: %-p", who, ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
croak("Failed to create %s IO handle: %-p", who, ERRSV); 0 0 0
404 }
405 15 ▶ if (unlikely(!SvROK(sv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!SvROK(sv))) { 0 15 15
406 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
407 0 SvREFCNT_dec(sv);
408 0 croak("Failed to create %s IO handle: new_from_fd returned undef", who);
409 }
410  
411 15 SV *io_glob = SvRV(sv);
412 15 GvSV(io_glob) = newRV_inc(c->self);
413  
414 15 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 15 0 15
415 15 LEAVE;
416 15 return sv;
417 }
418  
419 static void
420 15 feer_hand_out_raw_fd(pTHX_ struct feer_conn *c, SV *io_glob, const char *who)
421 {
422 15 ▶ if (likely(c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf))) {
Branch: 4 branches
BranchTrueFalseTotal
if (likely(c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf))) { 15 0 15
if (likely(c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf))) { 15 0 15
if (likely(c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf))) { 15 0 15
if (likely(c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf))) { 10 5 15
423 STRLEN rbuf_len;
424 10 const char *rbuf_ptr = SvPV(c->rbuf, rbuf_len);
425 10 IO *io = GvIOp(io_glob);
426 10 ▶ if (io) {
Branch: 1 branch
BranchTrueFalseTotal
if (io) { 10 0 10
427 10 SSize_t pushed = PerlIO_unread(IoIFP(io), (const void *)rbuf_ptr, rbuf_len);
428 10 ▶ if (likely(pushed == (SSize_t)rbuf_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(pushed == (SSize_t)rbuf_len)) { 10 0 10
429 10 SvCUR_set(c->rbuf, 0);
430 10 *SvPVX(c->rbuf) = '\0';
431 0 ▶ } else if (pushed > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (pushed > 0) { 0 0 0
432 0 sv_chop(c->rbuf, rbuf_ptr + pushed);
433 0 trouble("PerlIO_unread partial in %s: %zd of %"Sz_uf" bytes fd=%d\n",
434 who, pushed, (Sz)rbuf_len, c->fd);
435 } else {
436 0 trouble("PerlIO_unread failed in %s fd=%d\n", who, c->fd);
437 }
438 }
439 else {
440 0 trouble("%s: GvIOp returned NULL fd=%d\n", who, c->fd);
441 }
442 }
443  
444 15 stop_read_watcher(c);
445 15 stop_read_timer(c);
446 15 stop_write_timer(c);
447 // write watcher stays armed to flush outstanding data
448  
449 15 c->io_taken = 1;
450 15 c->fd_given_away = 1; // the handle's destructor closes c->fd now
451 15 }
452  
453 static SV*
454 16 feersum_env_io(pTHX_ struct feer_conn *c)
455 {
456 16 ▶ if (unlikely(c->io_taken))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->io_taken)) 1 15 16
457 1 croak("io() already called on this connection");
458 15 ▶ if (unlikely(c->responding != RESPOND_NOT_STARTED))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->responding != RESPOND_NOT_STARTED)) 0 15 15
459 0 croak("io() cannot be called after send_response/start_response");
460  
461 trace("feersum_env_io for fd=%d\n", c->fd);
462  
463 #ifdef FEERSUM_HAS_H2
464 if (c->is_h2_stream) {
465 struct feer_h2_stream *stream = H2_STREAM_FROM_PC(c);
466 if (!stream || !stream->is_tunnel)
467 croak("io() is not supported on regular HTTP/2 streams");
468 h2_tunnel_auto_accept(aTHX_ c, stream);
469 /* Re-fetch stream: auto_accept -> session_send may have freed it */
470 stream = H2_STREAM_FROM_PC(c);
471 if (!stream)
472 croak("io() tunnel: stream freed during auto-accept");
473 feer_h2_setup_tunnel(aTHX_ stream);
474 if (!stream->tunnel_established)
475 croak("Failed to create tunnel socketpair");
476  
477 SV *sv = feer_make_io_handle(aTHX_ c, stream->tunnel_sv1, "tunnel");
478 /* sv[1] now owned by the IO handle */
479 stream->tunnel_sv1 = -1;
480 c->io_taken = 1;
481 return sv;
482 }
483 #endif
484  
485 #ifdef FEERSUM_HAS_TLS
486 15 ▶ if (c->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) { 9 6 15
487 9 feer_tls_setup_tunnel(c);
488 9 ▶ if (!c->tls_tunnel)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tls_tunnel) 0 9 9
489 0 croak("Failed to create TLS tunnel socketpair");
490  
491 9 SV *sv = feer_make_io_handle(aTHX_ c, c->tls_tunnel_sv1, "TLS tunnel");
492 /* sv[1] now owned by the IO handle */
493 9 c->tls_tunnel_sv1 = -1;
494 9 c->io_taken = 1;
495 9 stop_read_timer(c);
496 9 stop_write_timer(c);
497 /* Keep read watcher active: TLS reads relay to tunnel */
498 9 return sv;
499 }
500 #endif
501  
502 6 SV *sv = feer_make_io_handle(aTHX_ c, c->fd, "raw socket");
503 6 feer_hand_out_raw_fd(aTHX_ c, SvRV(sv), "io()");
504 6 return sv;
505 }
506  
507 static SSize_t
508 8 feersum_return_from_io(pTHX_ struct feer_conn *c, SV *io_sv, const char *func_name)
509 {
510 8 ▶ if (unlikely(!c->io_taken))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->io_taken)) 2 6 8
511 2 croak("%s: called without a preceding io()/psgix.io", func_name);
512 #ifdef FEERSUM_HAS_H2
513 if (unlikely(c->is_h2_stream))
514 croak("%s: not supported on HTTP/2 streams", func_name);
515 #endif
516 #ifdef FEERSUM_HAS_TLS
517 6 ▶ if (c->tls_tunnel)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel) 1 5 6
518 1 croak("%s: not supported on TLS tunnel connections "
519 "(io() over TLS uses a socketpair relay that cannot be returned)",
520 func_name);
521 #endif
522  
523 5 ▶ if (!SvROK(io_sv) || !isGV_with_GP(SvRV(io_sv)))
Branch: 4 branches
BranchTrueFalseTotal
if (!SvROK(io_sv) || !isGV_with_GP(SvRV(io_sv))) 5 0 5
if (!SvROK(io_sv) || !isGV_with_GP(SvRV(io_sv))) 5 0 5
if (!SvROK(io_sv) || !isGV_with_GP(SvRV(io_sv))) 0 5 5
if (!SvROK(io_sv) || !isGV_with_GP(SvRV(io_sv))) 0 0 0
524 0 croak("%s requires a filehandle", func_name);
525  
526 5 GV *gv = (GV *)SvRV(io_sv);
527 5 ▶ IO *io = GvIO(gv);
Branch: 4 branches
BranchTrueFalseTotal
IO *io = GvIO(gv); 5 0 5
IO *io = GvIO(gv); 0 5 5
IO *io = GvIO(gv); 0 0 0
IO *io = GvIO(gv); 5 0 5
528 5 ▶ if (!io || !IoIFP(io))
Branch: 2 branches
BranchTrueFalseTotal
if (!io || !IoIFP(io)) 5 0 5
if (!io || !IoIFP(io)) 0 5 5
529 0 croak("%s: invalid filehandle", func_name);
530  
531 5 PerlIO *fp = IoIFP(io);
532  
533 /* A foreign handle would splice its bytes into rbuf and clear
534 * fd_given_away while the real handle still shares c->fd. */
535 5 ▶ if (PerlIO_fileno(fp) != c->fd)
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_fileno(fp) != c->fd) 0 5 5
536 0 croak("%s: that handle does not wrap this connection's socket",
537 func_name);
538  
539 5 SSize_t cnt = PerlIO_get_cnt(fp);
540 5 ▶ if (cnt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cnt > 0) { 0 5 5
541 // ptr is valid only until the next PerlIO op on fp
542 0 STDCHAR *ptr = PerlIO_get_ptr(fp);
543 0 ▶ if (ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptr) { 0 0 0
544 0 ▶ if (!c->rbuf)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->rbuf) 0 0 0
545 0 c->rbuf = newSV(READ_BUFSZ);
546  
547 0 sv_catpvn(c->rbuf, (const char *)ptr, cnt);
548  
549 0 PerlIO_set_ptrcnt(fp, ptr + cnt, 0);
550  
551 trace("pulled %zd bytes back to feersum fd=%d\n", cnt, c->fd);
552 }
553 }
554  
555 /* dup: the app's handle still owns c->fd and closes it when dropped. */
556 {
557 #ifdef F_DUPFD_CLOEXEC
558 5 int nfd = fcntl(c->fd, F_DUPFD_CLOEXEC, 0);
559 #else
560 int nfd = dup(c->fd);
561 if (nfd >= 0) (void)fcntl(nfd, F_SETFD, FD_CLOEXEC);
562 #endif
563 5 ▶ if (unlikely(nfd < 0))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(nfd < 0)) 0 5 5
564 0 croak("%s: cannot duplicate the connection descriptor: %s",
565 func_name, strerror(errno));
566 /* ev_io_set is undefined on an active watcher */
567 5 int r_active = ev_is_active(&c->read_ev_io);
568 5 int w_active = ev_is_active(&c->write_ev_io);
569 5 ▶ if (r_active) ev_io_stop(feersum_ev_loop, &c->read_ev_io);
Branch: 1 branch
BranchTrueFalseTotal
if (r_active) ev_io_stop(feersum_ev_loop, &c->read_ev_io); 0 5 5
570 5 ▶ if (w_active) ev_io_stop(feersum_ev_loop, &c->write_ev_io);
Branch: 1 branch
BranchTrueFalseTotal
if (w_active) ev_io_stop(feersum_ev_loop, &c->write_ev_io); 0 5 5
571 5 ev_io_set(&c->read_ev_io, nfd, EV_READ);
572 5 ev_io_set(&c->write_ev_io, nfd, EV_WRITE);
573 5 ▶ if (r_active) ev_io_start(feersum_ev_loop, &c->read_ev_io);
Branch: 1 branch
BranchTrueFalseTotal
if (r_active) ev_io_start(feersum_ev_loop, &c->read_ev_io); 0 5 5
574 5 ▶ if (w_active) ev_io_start(feersum_ev_loop, &c->write_ev_io);
Branch: 1 branch
BranchTrueFalseTotal
if (w_active) ev_io_start(feersum_ev_loop, &c->write_ev_io); 0 5 5
575 5 c->fd = nfd;
576 }
577  
578 5 change_responding_state(c, RESPOND_NOT_STARTED);
579 5 change_receiving_state(c, RECEIVE_HEADERS);
580 5 c->expected_cl = 0;
581 5 c->received_cl = 0;
582 5 c->io_taken = 0;
583 5 c->fd_given_away = 0;
584 5 free_request(c);
585  
586 5 ▶ if (c->rbuf && cnt <= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (c->rbuf && cnt <= 0) 5 0 5
if (c->rbuf && cnt <= 0) 5 0 5
587 5 SvCUR_set(c->rbuf, 0);
588  
589 5 start_read_watcher(c);
590 5 c->prev_read_outq = 0;
591 5 restart_read_timer(c);
592 5 restart_header_timer(c);
593  
594 /* Bytes already in rbuf raise no socket event, so parse them now; this
595 * only parses and enqueues, it does not dispatch re-entrantly. */
596 5 ▶ if (c->rbuf && SvCUR(c->rbuf) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->rbuf && SvCUR(c->rbuf) > 0) { 5 0 5
if (c->rbuf && SvCUR(c->rbuf) > 0) { 0 5 5
597 0 c->pipelined = SvCUR(c->rbuf);
598 #ifdef FEERSUM_HAS_TLS
599 0 ▶ if (c->tls)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) 0 0 0
600 0 try_tls_conn_read(feersum_ev_loop, &c->read_ev_io, EV_READ);
601 else
602 #endif
603 0 try_conn_read(feersum_ev_loop, &c->read_ev_io, EV_READ);
604 }
605  
606 5 return cnt > 0 ? cnt : 0;
607 }
608  
609 /* The header passes walk AvARRAY and SvPV with no eval above them. Returns
610 * the AV itself, a mortal plain copy, or NULL with ERRSV set. */
611 static AV *
612 1324 feersum_flatten_headers(pTHX_ AV *headers)
613 {
614 1324 bool needs_copy = SvMAGICAL((SV *)headers) ? 1 : 0;
615 1324 ▶ if (!needs_copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (!needs_copy) { 1320 4 1324
616 1320 SV **arr = AvARRAY(headers);
617 1320 I32 top = AvFILLp(headers);
618 3676 ▶ for (I32 i = 0; i <= top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i <= top; i++) { 2358 1318 3676
619 2358 SV *e = arr[i];
620 2358 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 2358 2358
621 2358 ▶ if (SvMAGICAL(e) || (SvROK(e) && SvAMAGIC(e))) { needs_copy = 1; break; }
Branch: 5 branches
BranchTrueFalseTotal
if (SvMAGICAL(e) || (SvROK(e) && SvAMAGIC(e))) { needs_copy = 1; break; } 2358 0 2358
if (SvMAGICAL(e) || (SvROK(e) && SvAMAGIC(e))) { needs_copy = 1; break; } 2 2356 2358
if (SvMAGICAL(e) || (SvROK(e) && SvAMAGIC(e))) { needs_copy = 1; break; } 2 0 2
if (SvMAGICAL(e) || (SvROK(e) && SvAMAGIC(e))) { needs_copy = 1; break; } 2 0 2
if (SvMAGICAL(e) || (SvROK(e) && SvAMAGIC(e))) { needs_copy = 1; break; } 2 0 2
622 }
623 }
624 1324 ▶ if (likely(!needs_copy)) return headers;
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!needs_copy)) return headers; 1318 6 1324
625  
626 6 dSP;
627 6 ENTER;
628 6 SAVETMPS;
629 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
630 6 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)headers)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)headers))); 0 6 6
631 6 PUTBACK;
632  
633 6 int count = call_pv("Feersum::_flatten_headers", G_SCALAR|G_EVAL);
634 6 SPAGAIN;
635  
636 6 AV *out = NULL;
637 9 ▶ if (likely(!SvTRUE(ERRSV)) && count == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (likely(!SvTRUE(ERRSV)) && count == 1) { 6 0 6
if (likely(!SvTRUE(ERRSV)) && count == 1) { 3 3 6
if (likely(!SvTRUE(ERRSV)) && count == 1) { 3 0 3
638 3 SV *r = POPs;
639 3 ▶ if (IsArrayRef(r)) out = (AV *)SvRV(r);
Branch: 2 branches
BranchTrueFalseTotal
if (IsArrayRef(r)) out = (AV *)SvRV(r); 3 0 3
if (IsArrayRef(r)) out = (AV *)SvRV(r); 3 0 3
640 3 ▶ if (out) SvREFCNT_inc((SV *)out);
Branch: 1 branch
BranchTrueFalseTotal
if (out) SvREFCNT_inc((SV *)out); 3 0 3
641 }
642 3 ▶ else if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) { 3 0 3
643 3 (void)POPs;
644 }
645 6 PUTBACK;
646 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
647 6 LEAVE;
648  
649 6 ▶ if (out) sv_2mortal((SV *)out);
Branch: 1 branch
BranchTrueFalseTotal
if (out) sv_2mortal((SV *)out); 3 3 6
650 6 return out;
651 }
652  
653 /* av_len/av_fetch on a tied AV run FETCHSIZE/FETCH, which can die with no eval
654 * above. Returns the AV itself, a mortal plain copy, or NULL with ERRSV set. */
655 static AV *
656 424 feersum_flatten_magical_av(pTHX_ AV *av)
657 {
658 424 ▶ if (likely(!SvMAGICAL((SV *)av))) return av;
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!SvMAGICAL((SV *)av))) return av; 423 1 424
659  
660 1 dSP;
661 1 ENTER;
662 1 SAVETMPS;
663 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
664 1 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)av)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)av))); 0 1 1
665 1 PUTBACK;
666  
667 1 int count = call_pv("Feersum::_flatten_body", G_SCALAR|G_EVAL);
668 1 SPAGAIN;
669  
670 1 AV *out = NULL;
671 1 ▶ if (likely(!SvTRUE(ERRSV)) && count == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (likely(!SvTRUE(ERRSV)) && count == 1) { 1 0 1
if (likely(!SvTRUE(ERRSV)) && count == 1) { 0 1 1
if (likely(!SvTRUE(ERRSV)) && count == 1) { 0 0 0
672 0 SV *r = POPs;
673 0 ▶ if (IsArrayRef(r)) out = (AV *)SvRV(r);
Branch: 2 branches
BranchTrueFalseTotal
if (IsArrayRef(r)) out = (AV *)SvRV(r); 0 0 0
if (IsArrayRef(r)) out = (AV *)SvRV(r); 0 0 0
674 0 ▶ if (out) SvREFCNT_inc((SV *)out); /* survive FREETMPS below */
Branch: 1 branch
BranchTrueFalseTotal
if (out) SvREFCNT_inc((SV *)out); /* survive FREETMPS below */ 0 0 0
675 }
676 1 ▶ else if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) { 1 0 1
677 1 (void)POPs;
678 }
679 1 PUTBACK;
680 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 0 1
681 1 LEAVE;
682  
683 1 ▶ if (out) sv_2mortal((SV *)out);
Branch: 1 branch
BranchTrueFalseTotal
if (out) sv_2mortal((SV *)out); 0 1 1
684 1 return out;
685 }
686  
687 /* Fire element get-magic under an eval: the body write newSVsv's each element
688 * with no eval above it. Returns the body to use, or NULL with ERRSV set. */
689 static SV *
690 350 feersum_flatten_magical_body(pTHX_ SV *body)
691 {
692 350 AV *av = (AV *)SvRV(body);
693 /* A tied AV dies inside av_len itself, so test the array first. */
694 350 bool magical = SvMAGICAL((SV *)av) ? 1 : 0;
695 350 ▶ if (!magical) {
Branch: 1 branch
BranchTrueFalseTotal
if (!magical) { 340 10 350
696 340 SV **arr = AvARRAY(av);
697 340 I32 top = AvFILLp(av);
698 702 ▶ for (I32 i = 0; i <= top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i <= top; i++) { 394 308 702
699 394 SV *e = arr[i];
700 394 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 394 394
701 394 ▶ if (SvMAGICAL(e)) { magical = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(e)) { magical = 1; break; } 21 373 394
702 /* the writer derefs an RV, so \$tied bites one level down too */
703 373 ▶ if (SvROK(e) && SvMAGICAL(SvRV(e))) { magical = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(e) && SvMAGICAL(SvRV(e))) { magical = 1; break; } 11 362 373
if (SvROK(e) && SvMAGICAL(SvRV(e))) { magical = 1; break; } 11 0 11
704 }
705 }
706 350 ▶ if (likely(!magical)) return body;
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!magical)) return body; 308 42 350
707  
708 42 dSP;
709 42 ENTER;
710 42 SAVETMPS;
711 42 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 42 42
712 42 ▶ XPUSHs(body);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(body); 0 42 42
713 42 PUTBACK;
714 42 int count = call_pv("Feersum::_flatten_body", G_SCALAR|G_EVAL);
715 42 SPAGAIN;
716 42 SV *out = NULL;
717 42 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 42 0 42
718 42 SV *r = POPs;
719 42 ▶ if (!SvTRUE(ERRSV) && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) 42 0 42
if (!SvTRUE(ERRSV) && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) 9 33 42
if (!SvTRUE(ERRSV) && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) 9 0 9
if (!SvTRUE(ERRSV) && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) 9 0 9
720 9 out = newSVsv(r);
721 }
722 42 PUTBACK;
723 42 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 42 0 42
724 42 LEAVE;
725 42 ▶ if (out) return sv_2mortal(out);
Branch: 1 branch
BranchTrueFalseTotal
if (out) return sv_2mortal(out); 9 33 42
726 33 return NULL;
727 }
728  
729 /* The only status parser: feersum_check_psgi_status screens for the croak
730 * feersum_start_response raises, and that path has no G_EVAL above it. */
731 INLINE_UNLESS_DEBUG static UV
732 1869 feer_status_code(pTHX_ SV *message)
733 {
734 1869 UV code = 0;
735 1869 ▶ if (SvIOK(message)) return SvIV(message);
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(message)) return SvIV(message); 1651 218 1869
736 218 STRLEN mlen = SvCUR(message);
737 218 ▶ if (grok_number(SvPVX_const(message), mlen > 3 ? 3 : mlen, &code)
Branch: 1 branch
BranchTrueFalseTotal
if (grok_number(SvPVX_const(message), mlen > 3 ? 3 : mlen, &code) 4 214 218
738 != IS_NUMBER_IN_UV)
739 4 return 0;
740 /* the rest is copied verbatim into the status line, so "200.5" must fail */
741 214 ▶ if (unlikely(mlen > 3 && SvPVX_const(message)[3] != ' '))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(mlen > 3 && SvPVX_const(message)[3] != ' ')) 85 129 214
if (unlikely(mlen > 3 && SvPVX_const(message)[3] != ' ')) 0 85 85
742 0 return 0;
743 214 return code;
744 }
745  
746 /* Same bounds as feersum_start_response; returns NULL or a literal error. */
747 static const char *
748 667 feersum_check_psgi_status(pTHX_ SV *msg)
749 {
750 667 ▶ if (!SvOK(msg) || !(SvIOK(msg) || SvPOK(msg)))
Branch: 3 branches
BranchTrueFalseTotal
if (!SvOK(msg) || !(SvIOK(msg) || SvPOK(msg))) 667 0 667
if (!SvOK(msg) || !(SvIOK(msg) || SvPOK(msg))) 65 602 667
if (!SvOK(msg) || !(SvIOK(msg) || SvPOK(msg))) 0 65 65
751 0 return "PSGI status must be a number or status string";
752 667 UV code = feer_status_code(aTHX_ msg);
753 667 ▶ if (unlikely(!code))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!code)) 1 666 667
754 1 return "PSGI status must start with a positive number";
755 666 ▶ if (unlikely(code > 599 || (code < 200 && code != 101)))
Branch: 4 branches
BranchTrueFalseTotal
if (unlikely(code > 599 || (code < 200 && code != 101))) 648 18 666
if (unlikely(code > 599 || (code < 200 && code != 101))) 36 612 648
if (unlikely(code > 599 || (code < 200 && code != 101))) 36 0 36
if (unlikely(code > 599 || (code < 200 && code != 101))) 36 612 648
756 54 return "PSGI status must be 200-599, or 101";
757 612 return NULL;
758 }
759  
760 /* Framing headers Feersum owns; keep in step with h2_skip_header. Connection
761 * and Upgrade deliberately pass through. */
762 INLINE_UNLESS_DEBUG static bool
763 2202 feer_skip_response_header(const char *hp, STRLEN hlen)
764 {
765 2202 switch (hlen) {
766 0 case 2: return str_case_eq_fixed("te", hp, 2);
767 122 case 10: return str_case_eq_fixed("keep-alive", hp, 10);
768 0 case 16: return str_case_eq_fixed("proxy-connection", hp, 16);
769 2 case 17: return str_case_eq_fixed("transfer-encoding", hp, 17);
770 2078 default: return false;
771 }
772 }
773  
774 /* close and keep-alive are absorbed (the tail emits the canonical header);
775 * anything else, Upgrade included, passes through. Returns 1 to drop. */
776 INLINE_UNLESS_DEBUG static int
777 120 feer_connection_header_drop(struct feer_conn *c, const char *vp, STRLEN vlen,
778 bool apply)
779 {
780 120 ▶ if (feer_header_has_token(vp, vlen, "close", 5)) {
Branch: 1 branch
BranchTrueFalseTotal
if (feer_header_has_token(vp, vlen, "close", 5)) { 120 0 120
781 120 ▶ if (apply) c->is_keepalive = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (apply) c->is_keepalive = 0; 60 60 120
782 120 return 1;
783 }
784 0 return feer_header_has_token(vp, vlen, "keep-alive", 10) ? 1 : 0;
785 }
786  
787 /* Response-splitting screen. Returns NULL or a literal error string: croak
788 * it under a G_EVAL, route it through call_died otherwise. */
789 static const char *
790 1257 feersum_check_response_framing(pTHX_ SV *message, AV *headers)
791 {
792 1257 ▶ if (SvPOK(message)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(message)) { 162 1095 1257
793 STRLEN mlen;
794 162 const char *mp = SvPV(message, mlen);
795 949 ▶ for (STRLEN k = 0; k < mlen; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN k = 0; k < mlen; k++) 789 160 949
796 789 ▶ if (unlikely(mp[k] == '\r' || mp[k] == '\n'))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(mp[k] == '\r' || mp[k] == '\n')) 788 1 789
if (unlikely(mp[k] == '\r' || mp[k] == '\n')) 1 787 788
797 2 return "Status message contains CR or LF";
798 }
799 1255 I32 avl = av_len(headers);
800 1255 SV **ary = AvARRAY(headers);
801 2364 ▶ for (I32 j = 0; j < avl; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 j = 0; j < avl; j += 2) { 1124 1240 2364
802 1124 SV *hdr = ary[j];
803 1124 SV *val = ary[j+1];
804 1124 ▶ if (!hdr || !SvOK(hdr) || !val || !SvOK(val))
Branch: 4 branches
BranchTrueFalseTotal
if (!hdr || !SvOK(hdr) || !val || !SvOK(val)) 1124 0 1124
if (!hdr || !SvOK(hdr) || !val || !SvOK(val)) 1124 0 1124
if (!hdr || !SvOK(hdr) || !val || !SvOK(val)) 1124 0 1124
if (!hdr || !SvOK(hdr) || !val || !SvOK(val)) 0 1124 1124
805 0 continue;
806 STRLEN hlen, vlen;
807 1124 ▶ const char *hp = SvPV(hdr, hlen);
Branch: 1 branch
BranchTrueFalseTotal
const char *hp = SvPV(hdr, hlen); 817 307 1124
808 1124 const char *vp = SvPV(val, vlen);
809 1124 ▶ if (unlikely(hlen == 0))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(hlen == 0)) 0 1124 1124
810 15 return "Response header name is empty";
811 14750 ▶ for (STRLEN k = 0; k < hlen; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN k = 0; k < hlen; k++) { 13629 1121 14750
812 13629 unsigned char ch = (unsigned char)hp[k];
813 13629 ▶ if (unlikely(ch <= 0x20 || ch == 0x7F || ch == ':'))
Branch: 4 branches
BranchTrueFalseTotal
if (unlikely(ch <= 0x20 || ch == 0x7F || ch == ':')) 13627 2 13629
if (unlikely(ch <= 0x20 || ch == 0x7F || ch == ':')) 0 13627 13627
if (unlikely(ch <= 0x20 || ch == 0x7F || ch == ':')) 13627 2 13629
if (unlikely(ch <= 0x20 || ch == 0x7F || ch == ':')) 1 13626 13627
814 2 ▶ return (ch == '\r' || ch == '\n' || ch == ':')
Branch: 2 branches
BranchTrueFalseTotal
return (ch == '\r' || ch == '\n' || ch == ':') 1 1 2
return (ch == '\r' || ch == '\n' || ch == ':') 1 0 1
815 ? "Response header name contains CR, LF, or colon"
816 5 ▶ : "Response header name contains a space or control character";
Branch: 1 branch
BranchTrueFalseTotal
: "Response header name contains a space or control character"; 2 1 3
817 }
818 9363 ▶ for (STRLEN k = 0; k < vlen; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN k = 0; k < vlen; k++) { 8254 1109 9363
819 8254 unsigned char ch = (unsigned char)vp[k];
820 8254 ▶ if (unlikely(ch < 0x20 || ch == 0x7F)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(ch < 0x20 || ch == 0x7F)) { 8242 12 8254
if (unlikely(ch < 0x20 || ch == 0x7F)) { 0 8242 8242
821 12 ▶ if (ch == '\t') continue; /* HTAB is legal in a field value */
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\t') continue; /* HTAB is legal in a field value */ 0 12 12
822 1 ▶ return (ch == '\r' || ch == '\n')
Branch: 1 branch
BranchTrueFalseTotal
return (ch == '\r' || ch == '\n') 1 0 1
823 ? "Response header value contains CR or LF"
824 13 ▶ : "Response header value contains a control character";
Branch: 1 branch
BranchTrueFalseTotal
: "Response header value contains a control character"; 1 11 12
825 }
826 }
827 }
828 1240 return NULL;
829 }
830  
831 static void
832 1215 feersum_start_response (pTHX_ struct feer_conn *c, SV *message, AV *headers,
833 int streaming, bool pre_validated)
834 {
835 const char *ptr;
836  
837 trace("start_response fd=%d streaming=%d\n", c->fd, streaming);
838  
839 /* Precondition: headers already flattened (feersum_flatten_headers). */
840  
841 1215 ▶ if (unlikely(!SvOK(message) || !(SvIOK(message) || SvPOK(message)))) {
Branch: 4 branches
BranchTrueFalseTotal
if (unlikely(!SvOK(message) || !(SvIOK(message) || SvPOK(message)))) { 1215 0 1215
if (unlikely(!SvOK(message) || !(SvIOK(message) || SvPOK(message)))) { 162 1053 1215
if (unlikely(!SvOK(message) || !(SvIOK(message) || SvPOK(message)))) { 0 162 162
if (unlikely(!SvOK(message) || !(SvIOK(message) || SvPOK(message)))) { 0 1215 1215
842 0 croak("Must define an HTTP status code or message");
843 }
844  
845 1215 I32 avl = av_len(headers);
846 1215 ▶ if (unlikely((avl+1) % 2 == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely((avl+1) % 2 == 1)) { 0 1215 1215
847 0 croak("expected even-length array, got %d", avl+1);
848 }
849  
850 1215 ▶ if (!pre_validated) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pre_validated) { 645 570 1215
851 645 const char *err = feersum_check_response_framing(aTHX_ message, headers);
852 645 ▶ if (unlikely(err)) croak("%s", err);
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(err)) croak("%s", err); 9 636 645
853 }
854  
855 /* Set before the H2 dispatch so both transports see it. */
856 1206 ▶ c->no_resp_body = (c->req && c->req->method_len == 4
Branch: 1 branch
BranchTrueFalseTotal
c->no_resp_body = (c->req && c->req->method_len == 4 193 1013 1206
857 2412 ▶ && memcmp(c->req->method, "HEAD", 4) == 0) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(c->req->method, "HEAD", 4) == 0) ? 1 : 0; 1206 0 1206
&& memcmp(c->req->method, "HEAD", 4) == 0) ? 1 : 0; 36 157 193
858  
859 #ifdef FEERSUM_HAS_H2
860 if (unlikely(c->is_h2_stream)) {
861 feersum_h2_start_response(aTHX_ c, message, headers, streaming);
862 return;
863 }
864 #endif
865  
866 1206 ▶ if (unlikely(c->responding != RESPOND_NOT_STARTED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->responding != RESPOND_NOT_STARTED)) { 4 1202 1206
867 4 ▶ if (c->fd < 0 || c->responding == RESPOND_SHUTDOWN)
Branch: 2 branches
BranchTrueFalseTotal
if (c->fd < 0 || c->responding == RESPOND_SHUTDOWN) 4 0 4
if (c->fd < 0 || c->responding == RESPOND_SHUTDOWN) 2 2 4
868 2 croak("connection is already closed");
869 2 croak("already responding?!");
870 }
871 /* Reading psgix.io takes the socket; the app's handle owns the fd now. */
872 1202 ▶ if (unlikely(c->io_taken))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->io_taken)) 0 1202 1202
873 0 croak("can't respond after io()/psgix.io; call return_from_io first");
874  
875 1202 UV code = feer_status_code(aTHX_ message);
876 trace2("starting response fd=%d code=%"UVuf"\n",c->fd,code);
877  
878 // Validate before changing state: call_died cannot send a 500 once responding.
879 1202 ▶ if (unlikely(!code))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!code)) 3 1199 1202
880 3 croak("first parameter is not a number or doesn't start with digits");
881 /* A non-101 1xx is interim and desyncs the client. Bounds must match
882 * h2_build_nva and feersum_check_psgi_status. */
883 1199 ▶ if (unlikely(code > 599 || (code < 200 && code != 101)))
Branch: 4 branches
BranchTrueFalseTotal
if (unlikely(code > 599 || (code < 200 && code != 101))) 1197 2 1199
if (unlikely(code > 599 || (code < 200 && code != 101))) 1 1196 1197
if (unlikely(code > 599 || (code < 200 && code != 101))) 1 0 1
if (unlikely(code > 599 || (code < 200 && code != 101))) 1 1196 1197
884 3 croak("response status must be 200-599, or 101, got %"UVuf, code);
885  
886 1196 ▶ change_responding_state(c, streaming ? RESPOND_STREAMING : RESPOND_NORMAL);
Branch: 1 branch
BranchTrueFalseTotal
change_responding_state(c, streaming ? RESPOND_STREAMING : RESPOND_NORMAL); 317 879 1196
887  
888 1196 ▶ if (likely(!SvPOK(message) || SvCUR(message) == 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if (likely(!SvPOK(message) || SvCUR(message) == 3)) { 150 1046 1196
if (likely(!SvPOK(message) || SvCUR(message) == 3)) { 65 85 150
889 1111 switch (code) {
890 1032 case 200: message = status_200; break;
891 0 case 201: message = status_201; break;
892 27 case 204: message = status_204; break;
893 0 case 301: message = status_301; break;
894 0 case 302: message = status_302; break;
895 24 case 304: message = status_304; break;
896 0 case 400: message = status_400; break;
897 8 case 404: message = status_404; break;
898 0 case 500: message = status_500; break;
899 20 default:
900 20 ptr = http_code_to_msg(code);
901 20 message = sv_2mortal(newSVpvf("%"UVuf" %s",code,ptr));
902 20 break;
903 }
904 }
905  
906 1196 ▶ c->auto_cl = http_status_no_body(code) ? 0 : 1;
Branch: 5 branches
BranchTrueFalseTotal
c->auto_cl = http_status_no_body(code) ? 0 : 1; 1169 27 1196
c->auto_cl = http_status_no_body(code) ? 0 : 1; 1149 20 1169
c->auto_cl = http_status_no_body(code) ? 0 : 1; 1124 25 1149
c->auto_cl = http_status_no_body(code) ? 0 : 1; 1124 0 1124
c->auto_cl = http_status_no_body(code) ? 0 : 1; 1124 0 1124
907 1196 c->app_content_length = 0;
908  
909 /* 304 deliberately keeps the app's Content-Length (RFC 9110 8.6). */
910 1196 ▶ const bool drop_app_cl = (code < 200 || code == 204 || code == 205);
Branch: 3 branches
BranchTrueFalseTotal
const bool drop_app_cl = (code < 200 || code == 204 || code == 205); 1196 0 1196
const bool drop_app_cl = (code < 200 || code == 204 || code == 205); 1169 27 1196
const bool drop_app_cl = (code < 200 || code == 204 || code == 205); 20 1149 1169
911  
912 /* Unlike 204, a 205 is not bodiless by framing rule; it needs an explicit CL. */
913 1196 const bool emit_zero_cl = (code == 205);
914  
915 STRLEN msg_len;
916 1196 const char *msg_ptr = SvPV(message, msg_len);
917 1196 ▶ if (unlikely(SvUTF8(message))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvUTF8(message))) { 0 1196 1196
918 0 SV *t = feer_bytes_mortal(msg_ptr, msg_len);
919 0 msg_ptr = SvPVX(t); msg_len = SvCUR(t);
920 }
921  
922 // Pass 1: size the block and find Content-Length
923 1196 bool has_content_length = 0;
924 1196 bool cl_seen = 0, cl_bad = 0;
925 1196 UV cl_val = 0;
926 1196 STRLEN hdr_total = 9 + msg_len + 2; // "HTTP/1.x " + message + CRLF
927 1196 SV **ary = AvARRAY(headers);
928 2297 ▶ for (I32 i = 0; i < avl; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i < avl; i += 2) { 1101 1196 2297
929 1101 SV *hdr = ary[i];
930 1101 SV *val = ary[i+1];
931 1101 ▶ if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val)))
Branch: 6 branches
BranchTrueFalseTotal
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 1101 0 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 0 1101 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 1101 0 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 0 1101 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 1101 0 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 0 1101 1101
932 234 continue;
933 STRLEN hlen, vlen;
934 1101 const char *hp = SvPV(hdr, hlen);
935 1101 ▶ if (unlikely(feer_skip_response_header(hp, hlen)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(feer_skip_response_header(hp, hlen))) 2 1099 1101
936 2 continue;
937 /* is_keepalive must be settled here: the tail is sized from it below. */
938 1099 ▶ if (unlikely(hlen == 10) && str_case_eq_fixed("connection", hp, 10)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(hlen == 10) && str_case_eq_fixed("connection", hp, 10)) { 60 1039 1099
if (unlikely(hlen == 10) && str_case_eq_fixed("connection", hp, 10)) { 60 0 60
939 STRLEN cvlen;
940 60 const char *cvp = SvPV(val, cvlen);
941 60 ▶ if (feer_connection_header_drop(c, cvp, cvlen, 1))
Branch: 1 branch
BranchTrueFalseTotal
if (feer_connection_header_drop(c, cvp, cvlen, 1)) 60 0 60
942 60 continue;
943 }
944 1039 ▶ if (unlikely(hlen == 14) && str_case_eq_fixed("content-length", hp, 14)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(hlen == 14) && str_case_eq_fixed("content-length", hp, 14)) { 297 742 1039
if (unlikely(hlen == 14) && str_case_eq_fixed("content-length", hp, 14)) { 297 0 297
945 /* HEAD keeps the app's Content-Length: a GET's length, not the empty body's. */
946 297 ▶ if (unlikely(c->no_resp_body) && !unlikely(drop_app_cl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->no_resp_body) && !unlikely(drop_app_cl)) { 18 279 297
if (unlikely(c->no_resp_body) && !unlikely(drop_app_cl)) { 18 0 18
947 18 c->app_content_length = 1;
948 18 has_content_length = 1;
949 }
950 279 ▶ else if ((likely(c->auto_cl) && !streaming) || unlikely(drop_app_cl))
Branch: 3 branches
BranchTrueFalseTotal
else if ((likely(c->auto_cl) && !streaming) || unlikely(drop_app_cl)) 276 3 279
else if ((likely(c->auto_cl) && !streaming) || unlikely(drop_app_cl)) 106 170 276
else if ((likely(c->auto_cl) && !streaming) || unlikely(drop_app_cl)) 2 107 109
951 172 continue;
952 else {
953 107 has_content_length = 1;
954 /* as for a request: 1*DIGIT, OWS allowed, duplicates must agree */
955 STRLEN clen;
956 107 const char *clp = SvPV(val, clen);
957 107 UV v = 0;
958 107 ▶ if (feer_parse_content_length(clp, clen, &v)
Branch: 1 branch
BranchTrueFalseTotal
if (feer_parse_content_length(clp, clen, &v) 107 0 107
959 107 ▶ && v <= (UV)(~(size_t)0) && (!cl_seen || v == cl_val))
Branch: 2 branches
BranchTrueFalseTotal
&& v <= (UV)(~(size_t)0) && (!cl_seen || v == cl_val)) 0 107 107
&& v <= (UV)(~(size_t)0) && (!cl_seen || v == cl_val)) 0 0 0
960 107 cl_val = v;
961 else
962 0 cl_bad = 1;
963 107 cl_seen = 1;
964 }
965 }
966 (void)SvPV(val, vlen);
967 867 hdr_total += hlen + 2 + vlen + 2; // key + ": " + value + CRLF
968 }
969 // HTTP/1.0 streaming without Content-Length can only end the body by closing.
970 1196 ▶ if (streaming && c->is_keepalive && !has_content_length
Branch: 3 branches
BranchTrueFalseTotal
if (streaming && c->is_keepalive && !has_content_length 317 879 1196
if (streaming && c->is_keepalive && !has_content_length 34 283 317
if (streaming && c->is_keepalive && !has_content_length 18 16 34
971 18 ▶ && !c->is_http11 && c->auto_cl)
Branch: 2 branches
BranchTrueFalseTotal
&& !c->is_http11 && c->auto_cl) 1 17 18
&& !c->is_http11 && c->auto_cl) 1 0 1
972 1 c->is_keepalive = 0;
973 // a malformed app Content-Length goes out as-is, unenforced
974 1196 ▶ c->resp_cl_enforced = streaming && c->auto_cl && cl_seen && !cl_bad;
Branch: 4 branches
BranchTrueFalseTotal
c->resp_cl_enforced = streaming && c->auto_cl && cl_seen && !cl_bad; 317 879 1196
c->resp_cl_enforced = streaming && c->auto_cl && cl_seen && !cl_bad; 252 65 317
c->resp_cl_enforced = streaming && c->auto_cl && cl_seen && !cl_bad; 106 146 252
c->resp_cl_enforced = streaming && c->auto_cl && cl_seen && !cl_bad; 106 0 106
975 1196 ▶ c->resp_cl_owed = c->resp_cl_enforced ? (size_t)cl_val : 0;
Branch: 1 branch
BranchTrueFalseTotal
c->resp_cl_owed = c->resp_cl_enforced ? (size_t)cl_val : 0; 106 1090 1196
976  
977 // The tail is sized apart so the re-check after pass 2 can use it.
978 1196 STRLEN tail_len = 0;
979 1196 ▶ if (unlikely(emit_zero_cl))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(emit_zero_cl)) 20 1176 1196
980 20 tail_len += 19; // "Content-Length: 0\r\n"
981 /* Date goes on HTTP/1.0 too (RFC 9110 6.6.1). */
982 1196 tail_len += DATE_HEADER_LENGTH;
983 1196 ▶ if (likely(c->is_http11))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->is_http11)) 1111 85 1196
984 1111 ▶ tail_len += (!c->is_keepalive ? 19 : 0);
Branch: 1 branch
BranchTrueFalseTotal
tail_len += (!c->is_keepalive ? 19 : 0); 859 252 1111
985 85 ▶ else if (c->is_keepalive)
Branch: 1 branch
BranchTrueFalseTotal
else if (c->is_keepalive) 1 84 85
986 1 tail_len += 24; // "Connection: keep-alive\r\n"
987 1196 ▶ if (streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (streaming) { 317 879 1196
988 317 ▶ if (c->is_http11 && !has_content_length && c->auto_cl)
Branch: 3 branches
BranchTrueFalseTotal
if (c->is_http11 && !has_content_length && c->auto_cl) 298 19 317
if (c->is_http11 && !has_content_length && c->auto_cl) 174 124 298
if (c->is_http11 && !has_content_length && c->auto_cl) 109 65 174
989 109 tail_len += 30; // "Transfer-Encoding: chunked\r\n\r\n"
990 else
991 208 tail_len += 2; // terminal CRLF
992 }
993 1196 hdr_total += tail_len;
994  
995 // Pass 2: build the block
996 1196 SV *hdr_sv = newSV(hdr_total + 1);
997 1196 SvPOK_on(hdr_sv);
998 1196 char *p = SvPVX(hdr_sv);
999 1196 STRLEN hdr_alloc = hdr_total;
1000  
1001 1196 ▶ const char *ver = c->is_http11 ? "HTTP/1.1 " : "HTTP/1.0 ";
Branch: 1 branch
BranchTrueFalseTotal
const char *ver = c->is_http11 ? "HTTP/1.1 " : "HTTP/1.0 "; 1111 85 1196
1002 1196 memcpy(p, ver, 9); p += 9;
1003 1196 memcpy(p, msg_ptr, msg_len); p += msg_len;
1004 1196 *p++ = '\r'; *p++ = '\n';
1005  
1006 2297 ▶ for (I32 i = 0; i < avl; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i < avl; i += 2) { 1101 1196 2297
1007 1101 SV *hdr = ary[i];
1008 1101 SV *val = ary[i+1];
1009 1101 ▶ if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val)))
Branch: 6 branches
BranchTrueFalseTotal
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 1101 0 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 0 1101 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 1101 0 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 0 1101 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 1101 0 1101
if (unlikely(!hdr || !SvOK(hdr) || !val || !SvOK(val))) 0 1101 1101
1010 234 continue;
1011 STRLEN hlen, vlen;
1012 1101 const char *hp = SvPV(hdr, hlen);
1013 1101 ▶ if (unlikely(feer_skip_response_header(hp, hlen)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(feer_skip_response_header(hp, hlen))) 2 1099 1101
1014 2 continue;
1015 /* apply=0: a magical value could differ on this second read. */
1016 1099 ▶ if (unlikely(hlen == 10) && str_case_eq_fixed("connection", hp, 10)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(hlen == 10) && str_case_eq_fixed("connection", hp, 10)) { 60 1039 1099
if (unlikely(hlen == 10) && str_case_eq_fixed("connection", hp, 10)) { 60 0 60
1017 STRLEN cvlen;
1018 60 const char *cvp = SvPV(val, cvlen);
1019 60 ▶ if (feer_connection_header_drop(c, cvp, cvlen, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (feer_connection_header_drop(c, cvp, cvlen, 0)) 60 0 60
1020 60 continue;
1021 }
1022 1039 ▶ if (unlikely(hlen == 14) && str_case_eq_fixed("content-length", hp, 14)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(hlen == 14) && str_case_eq_fixed("content-length", hp, 14)) { 297 742 1039
if (unlikely(hlen == 14) && str_case_eq_fixed("content-length", hp, 14)) { 297 0 297
1023 /* mirror of pass 1 */
1024 297 ▶ if (!(unlikely(c->no_resp_body) && !unlikely(drop_app_cl))
Branch: 2 branches
BranchTrueFalseTotal
if (!(unlikely(c->no_resp_body) && !unlikely(drop_app_cl)) 18 279 297
if (!(unlikely(c->no_resp_body) && !unlikely(drop_app_cl)) 0 18 18
1025 279 ▶ && ((likely(c->auto_cl) && !streaming) || unlikely(drop_app_cl)))
Branch: 3 branches
BranchTrueFalseTotal
&& ((likely(c->auto_cl) && !streaming) || unlikely(drop_app_cl))) 276 3 279
&& ((likely(c->auto_cl) && !streaming) || unlikely(drop_app_cl))) 106 170 276
&& ((likely(c->auto_cl) && !streaming) || unlikely(drop_app_cl))) 2 107 109
1026 172 continue;
1027 }
1028 867 const char *vp = SvPV(val, vlen);
1029 /* Downgrading cannot exceed pass 1's sizing: the encoded form is never shorter. */
1030 867 ▶ if (unlikely(SvUTF8(hdr))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvUTF8(hdr))) { 0 867 867
1031 0 SV *t = feer_bytes_mortal(hp, hlen);
1032 0 hp = SvPVX(t); hlen = SvCUR(t);
1033 }
1034 867 ▶ if (unlikely(SvUTF8(val))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvUTF8(val))) { 0 867 867
1035 0 SV *t = feer_bytes_mortal(vp, vlen);
1036 0 vp = SvPVX(t); vlen = SvCUR(t);
1037 }
1038 867 STRLEN need = hlen + 2 + vlen + 2;
1039 // Guard against magical SVs returning longer strings than pass 1
1040 867 ▶ if (unlikely((p - SvPVX(hdr_sv)) + need > hdr_alloc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely((p - SvPVX(hdr_sv)) + need > hdr_alloc)) { 0 867 867
1041 0 STRLEN pos = p - SvPVX(hdr_sv);
1042 0 hdr_alloc = pos + need + 128; // slack covers the tail (<= 105 bytes)
1043 0 ▶ SvGROW(hdr_sv, hdr_alloc + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(hdr_sv, hdr_alloc + 1); 0 0 0
SvGROW(hdr_sv, hdr_alloc + 1); 0 0 0
1044 0 p = SvPVX(hdr_sv) + pos;
1045 }
1046 867 memcpy(p, hp, hlen); p += hlen;
1047 867 *p++ = ':'; *p++ = ' ';
1048 867 memcpy(p, vp, vlen); p += vlen;
1049 867 *p++ = '\r'; *p++ = '\n';
1050 }
1051  
1052 // The 128-byte slack is not reserved (a later header may consume it) and
1053 // the tail is written unchecked, so re-verify at the final position.
1054 {
1055 1196 STRLEN pos = p - SvPVX(hdr_sv);
1056 1196 ▶ if (unlikely(pos + tail_len > hdr_alloc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(pos + tail_len > hdr_alloc)) { 0 1196 1196
1057 0 hdr_alloc = pos + tail_len;
1058 0 ▶ SvGROW(hdr_sv, hdr_alloc + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(hdr_sv, hdr_alloc + 1); 0 0 0
SvGROW(hdr_sv, hdr_alloc + 1); 0 0 0
1059 0 p = SvPVX(hdr_sv) + pos;
1060 }
1061 }
1062  
1063 1196 ▶ if (unlikely(emit_zero_cl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(emit_zero_cl)) { 20 1176 1196
1064 20 memcpy(p, "Content-Length: 0\r\n", 19); p += 19;
1065 }
1066  
1067 1196 memcpy(p, DATE_BUF, DATE_HEADER_LENGTH); p += DATE_HEADER_LENGTH;
1068 1196 ▶ if (likely(c->is_http11)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->is_http11)) { 1111 85 1196
1069 1111 ▶ if (!c->is_keepalive) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->is_keepalive) { 859 252 1111
1070 859 memcpy(p, "Connection: close\r\n", 19); p += 19;
1071 }
1072 85 ▶ } else if (c->is_keepalive) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->is_keepalive) { 1 84 85
1073 1 memcpy(p, "Connection: keep-alive\r\n", 24); p += 24;
1074 }
1075  
1076 1196 ▶ if (streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (streaming) { 317 879 1196
1077 317 ▶ if (c->is_http11 && !has_content_length && c->auto_cl) {
Branch: 3 branches
BranchTrueFalseTotal
if (c->is_http11 && !has_content_length && c->auto_cl) { 298 19 317
if (c->is_http11 && !has_content_length && c->auto_cl) { 174 124 298
if (c->is_http11 && !has_content_length && c->auto_cl) { 109 65 174
1078 109 memcpy(p, "Transfer-Encoding: chunked\r\n\r\n", 30); p += 30;
1079 109 c->use_chunked = 1;
1080 }
1081 else {
1082 208 *p++ = '\r'; *p++ = '\n';
1083 208 c->use_chunked = 0;
1084 }
1085 }
1086  
1087 1196 SvCUR_set(hdr_sv, p - SvPVX(hdr_sv));
1088 1196 *p = '\0';
1089  
1090 {
1091 1196 struct iomatrix *m = next_iomatrix(c);
1092 1196 unsigned idx = m->count++;
1093 1196 m->iov[idx].iov_base = SvPVX(hdr_sv);
1094 1196 m->iov[idx].iov_len = SvCUR(hdr_sv);
1095 1196 m->sv[idx] = hdr_sv;
1096 1196 c->wbuf_len += SvCUR(hdr_sv);
1097 }
1098  
1099 // Not for RESPOND_NORMAL: conn_write_ready writes immediately and would
1100 // finish the response before write_whole_body queues the body.
1101 1196 ▶ if (streaming)
Branch: 1 branch
BranchTrueFalseTotal
if (streaming) 317 879 1196
1102 317 conn_write_ready(c);
1103 1196 }
1104  
1105 static size_t
1106 879 feersum_write_whole_body (pTHX_ struct feer_conn *c, SV *body)
1107 {
1108 size_t RETVAL;
1109 879 bool body_is_string = 0;
1110 STRLEN cur;
1111  
1112 879 ▶ if (c->responding != RESPOND_NORMAL)
Branch: 1 branch
BranchTrueFalseTotal
if (c->responding != RESPOND_NORMAL) 0 879 879
1113 0 croak("can't use write_whole_body when in streaming mode");
1114  
1115 #ifdef FEERSUM_HAS_H2
1116 if (unlikely(c->is_h2_stream)) {
1117 SV *body_sv;
1118 if (!SvOK(body)) {
1119 body_sv = sv_2mortal(newSVpvs(""));
1120 } else if (SvROK(body)) {
1121 SV *refd = SvRV(body);
1122 if (SvOK(refd) && !SvROK(refd)) {
1123 body_sv = refd;
1124 } else if (SvTYPE(refd) == SVt_PVAV) {
1125 AV *ab = (AV*)refd;
1126 body_sv = sv_2mortal(newSVpvs(""));
1127 I32 amax = av_len(ab);
1128 /* Per-element bytes like H1's add_sv_to_wbuf: sv_catsv would
1129 * upgrade the accumulator and re-encode earlier elements. */
1130 for (I32 i = 0; i <= amax; i++) {
1131 SV *sv = fetch_av_normal(aTHX_ ab, i);
1132 if (sv) {
1133 STRLEN el_len;
1134 const char *el = SvPV(sv, el_len);
1135 if (unlikely(SvUTF8(sv))) {
1136 SV *t = feer_bytes_mortal(el, el_len);
1137 el = SvPVX(t); el_len = SvCUR(t);
1138 }
1139 sv_catpvn(body_sv, el, el_len);
1140 }
1141 }
1142 } else {
1143 croak("body must be a scalar, scalar reference or array reference");
1144 }
1145 } else {
1146 body_sv = body;
1147 }
1148 return feersum_h2_write_whole_body(aTHX_ c, body_sv);
1149 }
1150 #endif
1151  
1152 879 ▶ if (!SvOK(body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(body)) { 0 879 879
1153 0 body = sv_2mortal(newSVpvs(""));
1154 0 body_is_string = 1;
1155 }
1156 879 ▶ else if (SvROK(body)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(body)) { 613 266 879
1157 613 SV *refd = SvRV(body);
1158 613 ▶ if (SvOK(refd) && !SvROK(refd)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(refd) && !SvROK(refd)) { 202 411 613
if (SvOK(refd) && !SvROK(refd)) { 202 0 202
1159 202 body = refd;
1160 202 body_is_string = 1;
1161 }
1162 411 ▶ else if (SvTYPE(refd) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(refd) != SVt_PVAV) { 0 411 411
1163 0 croak("body must be a scalar, scalar reference or array reference");
1164 }
1165 }
1166 else {
1167 266 body_is_string = 1;
1168 }
1169  
1170 879 SV *body_scalar = NULL;
1171 879 ▶ if (body_is_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_is_string) { 468 411 879
1172 468 body_scalar = body;
1173 } else {
1174 411 AV *abody = (AV*)SvRV(body);
1175 411 ▶ if (av_len(abody) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(abody) == 0) { 362 49 411
1176 362 SV *elem = fetch_av_normal(aTHX_ abody, 0);
1177 362 ▶ if (elem) body_scalar = elem;
Branch: 1 branch
BranchTrueFalseTotal
if (elem) body_scalar = elem; 362 0 362
1178 }
1179 }
1180  
1181 // Fast path: append CL (and a small body) to the header buffer, one iovec.
1182 879 ▶ if (likely(c->auto_cl) && !unlikely(c->app_content_length)
Branch: 2 branches
BranchTrueFalseTotal
if (likely(c->auto_cl) && !unlikely(c->app_content_length) 872 7 879
if (likely(c->auto_cl) && !unlikely(c->app_content_length) 0 872 872
1183 872 ▶ && body_scalar && c->wbuf_rinq) {
Branch: 2 branches
BranchTrueFalseTotal
&& body_scalar && c->wbuf_rinq) { 45 827 872
&& body_scalar && c->wbuf_rinq) { 0 827 827
1184 827 struct iomatrix *m = (struct iomatrix *)c->wbuf_rinq->prev->ref;
1185 827 unsigned last = m->count - 1;
1186 827 SV *hdr_sv = m->sv[last];
1187  
1188 // the tail entry must be start_response's writable header SV
1189 827 ▶ if (unlikely(!hdr_sv || SvREADONLY(hdr_sv)))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(!hdr_sv || SvREADONLY(hdr_sv))) 827 0 827
if (unlikely(!hdr_sv || SvREADONLY(hdr_sv))) 0 827 827
1190 0 goto whole_body_slow_path;
1191  
1192 STRLEN body_len;
1193 827 const char *body_ptr = SvPV(body_scalar, body_len);
1194 827 ▶ if (unlikely(SvUTF8(body_scalar))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvUTF8(body_scalar))) { 1 826 827
1195 /* bytes on the wire; Content-Length must count the same form */
1196 1 SV *t = feer_bytes_mortal(body_ptr, body_len);
1197 1 body_scalar = t;
1198 1 body_ptr = SvPVX(t); body_len = SvCUR(t);
1199 }
1200 char cl_buf[48];
1201 827 int cl_len = format_content_length(cl_buf, body_len);
1202 827 STRLEN hdr_cur = SvCUR(hdr_sv);
1203  
1204 827 ▶ if (likely(body_len <= 4096)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(body_len <= 4096)) { 816 11 827
1205 // HEAD advertises the body's Content-Length but emits no bytes
1206 816 ▶ STRLEN emit_len = unlikely(c->no_resp_body) ? 0 : body_len;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN emit_len = unlikely(c->no_resp_body) ? 0 : body_len; 808 8 816
1207 816 STRLEN total = hdr_cur + cl_len + emit_len;
1208 816 ▶ SvGROW(hdr_sv, total + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(hdr_sv, total + 1); 816 0 816
SvGROW(hdr_sv, total + 1); 816 0 816
1209 816 char *p = SvPVX(hdr_sv) + hdr_cur;
1210 816 memcpy(p, cl_buf, cl_len); p += cl_len;
1211 816 ▶ if (likely(emit_len)) { memcpy(p, body_ptr, emit_len); p += emit_len; }
Branch: 1 branch
BranchTrueFalseTotal
if (likely(emit_len)) { memcpy(p, body_ptr, emit_len); p += emit_len; } 807 9 816
1212 816 *p = '\0';
1213 816 SvCUR_set(hdr_sv, total);
1214 816 m->iov[last].iov_len = total;
1215 816 c->wbuf_len += cl_len + emit_len;
1216 } else {
1217 11 ▶ SvGROW(hdr_sv, hdr_cur + cl_len + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(hdr_sv, hdr_cur + cl_len + 1); 11 0 11
SvGROW(hdr_sv, hdr_cur + cl_len + 1); 11 0 11
1218 11 memcpy(SvPVX(hdr_sv) + hdr_cur, cl_buf, cl_len);
1219 11 SvCUR_set(hdr_sv, hdr_cur + cl_len);
1220 11 SvPVX(hdr_sv)[hdr_cur + cl_len] = '\0';
1221 11 m->iov[last].iov_len = hdr_cur + cl_len;
1222 11 c->wbuf_len += cl_len;
1223 11 ▶ if (likely(!c->no_resp_body))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!c->no_resp_body)) 5 6 11
1224 5 add_sv_to_wbuf(c, body_scalar);
1225 }
1226 827 m->iov[last].iov_base = SvPVX(hdr_sv);
1227 827 RETVAL = body_len;
1228 }
1229 52 else { whole_body_slow_path:;
1230 SV *cl_sv;
1231 struct iovec *cl_iov;
1232 /* app_content_length: HEAD with the app's own Content-Length already in the block */
1233 52 ▶ if (likely(c->auto_cl) && !unlikely(c->app_content_length))
Branch: 2 branches
BranchTrueFalseTotal
if (likely(c->auto_cl) && !unlikely(c->app_content_length)) 45 7 52
if (likely(c->auto_cl) && !unlikely(c->app_content_length)) 45 0 45
1234 45 add_placeholder_to_wbuf(c, &cl_sv, &cl_iov);
1235 else
1236 7 add_crlf_to_wbuf(c);
1237  
1238 52 ▶ if (unlikely(!c->auto_cl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->auto_cl)) { 7 45 52
1239 7 RETVAL = 0;
1240 }
1241 45 ▶ else if (body_is_string) {
Branch: 1 branch
BranchTrueFalseTotal
else if (body_is_string) { 0 45 45
1242 // HEAD: measure the (downgraded) length, queue nothing
1243 0 ▶ if (unlikely(c->no_resp_body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->no_resp_body)) { 0 0 0
1244 0 const char *bp = SvPV(body, cur);
1245 0 ▶ if (unlikely(SvUTF8(body)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvUTF8(body))) 0 0 0
1246 0 cur = SvCUR(feer_bytes_mortal(bp, cur));
1247 }
1248 else
1249 0 cur = add_sv_to_wbuf(c,body);
1250 0 RETVAL = cur;
1251 }
1252 else {
1253 45 AV *abody = (AV*)SvRV(body);
1254 45 I32 amax = av_len(abody);
1255 45 RETVAL = 0;
1256 138 ▶ for (I32 i = 0; i <= amax; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i <= amax; i++) { 93 45 138
1257 93 SV *sv = fetch_av_normal(aTHX_ abody, i);
1258 93 ▶ if (unlikely(!sv)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!sv)) continue; 1 92 93
1259 92 ▶ if (unlikely(c->no_resp_body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->no_resp_body)) { 0 92 92
1260 0 const char *bp = SvPV(sv, cur);
1261 0 ▶ if (unlikely(SvUTF8(sv)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvUTF8(sv))) 0 0 0
1262 0 cur = SvCUR(feer_bytes_mortal(bp, cur));
1263 }
1264 else
1265 92 cur = add_sv_to_wbuf(c,sv);
1266 trace("body part i=%d sv=%p cur=%"Sz_uf"\n", i, sv, (Sz)cur);
1267 92 RETVAL += cur;
1268 }
1269 }
1270  
1271 52 ▶ if (likely(c->auto_cl) && !unlikely(c->app_content_length)) {
Branch: 2 branches
BranchTrueFalseTotal
if (likely(c->auto_cl) && !unlikely(c->app_content_length)) { 45 7 52
if (likely(c->auto_cl) && !unlikely(c->app_content_length)) { 45 0 45
1272 char cl_buf[48];
1273 45 int cl_len = format_content_length(cl_buf, RETVAL);
1274 45 sv_setpvn(cl_sv, cl_buf, cl_len);
1275 45 update_wbuf_placeholder(c, cl_sv, cl_iov);
1276 }
1277 }
1278  
1279 879 change_responding_state(c, RESPOND_SHUTDOWN);
1280 879 conn_write_ready(c);
1281 879 return RETVAL;
1282 }
1283  
1284 static void
1285 161 call_died (pTHX_ struct feer_conn *c, const char *cb_type)
1286 {
1287 161 dSP;
1288 #if DEBUG >= 1
1289 trace("An error was thrown in the %s callback: %-p\n",cb_type,ERRSV);
1290 #endif
1291 161 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 161 161
1292 161 ▶ mXPUSHs(newSVsv(ERRSV));
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(newSVsv(ERRSV)); 0 161 161
mXPUSHs(newSVsv(ERRSV)); 161 0 161
1293 161 PUTBACK;
1294 161 call_pv("Feersum::DIED", G_DISCARD|G_EVAL|G_VOID);
1295 161 SPAGAIN;
1296  
1297 /* A mid-response die may leave an unterminated block on the wire: no reuse. */
1298 161 ▶ if (unlikely(c->responding > RESPOND_NOT_STARTED
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->responding > RESPOND_NOT_STARTED 5 156 161
if (unlikely(c->responding > RESPOND_NOT_STARTED 4 1 5
1299 && c->responding < RESPOND_SHUTDOWN))
1300 4 c->is_keepalive = 0;
1301  
1302 /* No 500 onto an app-owned stream or mid-response on H1; H2 still needs
1303 * the call, where it becomes RST_STREAM. */
1304 161 ▶ if (!c->io_taken
Branch: 1 branch
BranchTrueFalseTotal
if (!c->io_taken 161 0 161
1305 161 ▶ && (likely(c->responding == RESPOND_NOT_STARTED)
Branch: 1 branch
BranchTrueFalseTotal
&& (likely(c->responding == RESPOND_NOT_STARTED) 156 5 161
1306 #ifdef FEERSUM_HAS_H2
1307 || c->is_h2_stream
1308 #endif
1309 ))
1310 156 respond_with_server_error(c, "Request handler exception\n", 500);
1311 161 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 161 0 161
1312 161 }
1313  
1314 /* Balance the pin new_feer_conn_handle took. On a die, a still-streaming
1315 * writer whose last ref unwound is sealed dirty so the truncation shows. */
1316 static void
1317 1691 feersum_writer_unpin(pTHX_ struct feer_conn *c, bool errored)
1318 {
1319 1691 SV *pin = c->pinned_writer;
1320 1691 ▶ if (likely(!pin)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!pin)) return; 1391 300 1691
1321 300 c->pinned_writer = NULL;
1322 300 ▶ if (unlikely(errored) && SvREFCNT(pin) == 1
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(errored) && SvREFCNT(pin) == 1 1 299 300
if (unlikely(errored) && SvREFCNT(pin) == 1 1 0 1
1323 1 ▶ && c->responding == RESPOND_STREAMING) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->responding == RESPOND_STREAMING) { 1 0 1
1324 trace("callback died mid-stream, sealing dirty fd=%d c=%p\n", c->fd, c);
1325 1 c->is_keepalive = 0; /* also done in call_died; keep both */
1326 1 change_responding_state(c, RESPOND_SHUTDOWN);
1327 1 conn_write_ready(c);
1328 }
1329 300 SvREFCNT_dec(pin);
1330 }
1331  
1332 static void
1333 316 feersum_start_psgi_streaming(pTHX_ struct feer_conn *c, SV *streamer)
1334 {
1335 316 dSP;
1336 bool errored;
1337 316 ENTER;
1338 316 SAVETMPS;
1339 316 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 316 316
1340 316 ▶ mXPUSHs(feer_conn_2sv(c));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(feer_conn_2sv(c)); 0 316 316
1341 316 ▶ XPUSHs(streamer);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(streamer); 0 316 316
1342 316 PUTBACK;
1343 316 c->pin_writer = 1;
1344 316 call_method("_initiate_streaming_psgi", G_DISCARD|G_EVAL|G_VOID);
1345 316 c->pin_writer = 0;
1346 316 SPAGAIN;
1347 316 ▶ errored = SvTRUE(ERRSV) ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
errored = SvTRUE(ERRSV) ? TRUE : FALSE; 316 0 316
1348 316 ▶ if (unlikely(errored)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(errored)) { 0 316 316
1349 0 call_died(aTHX_ c, "PSGI stream initiator");
1350 }
1351 316 feersum_writer_unpin(aTHX_ c, errored);
1352 316 PUTBACK;
1353 316 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 316 0 316
1354 316 LEAVE;
1355 316 }
1356  
1357 static void
1358 13 feersum_close_psgi_body(pTHX_ SV *body)
1359 {
1360 13 dSP;
1361 13 ENTER;
1362 13 SAVETMPS;
1363 13 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 13 13
1364 13 ▶ mXPUSHs(newSVsv(body));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVsv(body)); 0 13 13
1365 13 PUTBACK;
1366 13 call_method("close", G_VOID|G_DISCARD|G_EVAL);
1367 13 SPAGAIN;
1368 13 ▶ if (unlikely(SvTRUE(ERRSV))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV))) { 13 0 13
if (unlikely(SvTRUE(ERRSV))) { 0 13 13
1369 0 ▶ trouble("Couldn't close body IO handle: %-p\n", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
trouble("Couldn't close body IO handle: %-p\n", ERRSV); 0 0 0
1370 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
1371 }
1372 13 PUTBACK;
1373 13 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 13 0 13
1374 13 LEAVE;
1375 13 }
1376  
1377 static void
1378 1653 feersum_release_io_body(pTHX_ struct feer_conn *c)
1379 {
1380 1653 ▶ if (!c->poll_write_cb_is_io_handle || !c->poll_write_cb) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!c->poll_write_cb_is_io_handle || !c->poll_write_cb) return; 13 1640 1653
if (!c->poll_write_cb_is_io_handle || !c->poll_write_cb) return; 0 13 13
1381 13 SV *body = c->poll_write_cb;
1382 13 c->poll_write_cb = NULL;
1383 13 c->poll_write_cb_is_io_handle = 0;
1384 13 feersum_close_psgi_body(aTHX_ body);
1385 13 SvREFCNT_dec(body);
1386 }
1387  
1388 static void
1389 2138 feersum_queue_response_cleanup(struct feer_conn *c)
1390 {
1391 2138 ▶ if (c->response_cleanup_queued
Branch: 1 branch
BranchTrueFalseTotal
if (c->response_cleanup_queued 2138 0 2138
1392 2138 ▶ || (!c->ext_guard
Branch: 1 branch
BranchTrueFalseTotal
|| (!c->ext_guard 1948 190 2138
1393 1948 ▶ && (!c->poll_write_cb_is_io_handle || !c->poll_write_cb))) return;
Branch: 2 branches
BranchTrueFalseTotal
&& (!c->poll_write_cb_is_io_handle || !c->poll_write_cb))) return; 0 1948 1948
&& (!c->poll_write_cb_is_io_handle || !c->poll_write_cb))) return; 0 0 0
1394 190 rinq_push(&c->server->response_cleanup_rinq, c);
1395 190 c->response_cleanup_queued = 1;
1396 190 SvREFCNT_inc_void_NN(c->self);
1397 /* a quiet loop would otherwise never drain the queue */
1398 190 ▶ if (!ev_is_active(&c->server->ei))
Branch: 1 branch
BranchTrueFalseTotal
if (!ev_is_active(&c->server->ei)) 35 155 190
1399 35 ev_idle_start(feersum_ev_loop, &c->server->ei);
1400 }
1401  
1402 static void
1403 190 feersum_drain_response_cleanup(pTHX_ struct feer_server *server)
1404 {
1405 struct feer_conn *c;
1406 380 ▶ while ((c = rinq_shift(&server->response_cleanup_rinq)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((c = rinq_shift(&server->response_cleanup_rinq)) != NULL) { 190 190 380
1407 190 c->response_cleanup_queued = 0;
1408 190 feersum_release_io_body(aTHX_ c);
1409 /* a guard can pin an env -> psgi.input -> c cycle; NULL before dec:
1410 * its DESTROY may touch the connection */
1411 190 SV *guard = c->ext_guard;
1412 190 c->ext_guard = NULL;
1413 190 SvREFCNT_dec(guard);
1414 190 SvREFCNT_dec(c->self); /* queue ref; c may be freed here */
1415 }
1416 190 }
1417  
1418 static void
1419 740 feersum_handle_psgi_response(
1420 pTHX_ struct feer_conn *c, SV *ret, bool can_recurse)
1421 {
1422 740 ▶ if (unlikely(!SvOK(ret) || !SvROK(ret))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(!SvOK(ret) || !SvROK(ret))) { 740 0 740
if (unlikely(!SvOK(ret) || !SvROK(ret))) { 0 740 740
1423 0 ▶ sv_setpvs(ERRSV, "Invalid PSGI response (expected reference)");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "Invalid PSGI response (expected reference)"); 0 0 0
1424 0 call_died(aTHX_ c, "PSGI request");
1425 0 return;
1426 }
1427  
1428 740 ▶ if (unlikely(!IsArrayRef(ret))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(!IsArrayRef(ret))) { 740 0 740
if (unlikely(!IsArrayRef(ret))) { 316 424 740
1429 316 ▶ if (likely(can_recurse)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(can_recurse)) { 316 0 316
1430 trace("PSGI response non-array, c=%p ret=%p\n", c, ret);
1431 316 feersum_start_psgi_streaming(aTHX_ c, ret);
1432 }
1433 else {
1434 0 ▶ sv_setpvs(ERRSV, "PSGI attempt to recurse in a streaming callback");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "PSGI attempt to recurse in a streaming callback"); 0 0 0
1435 0 call_died(aTHX_ c, "PSGI request");
1436 }
1437 316 return;
1438 }
1439  
1440 424 AV *psgi_triplet = (AV*)SvRV(ret);
1441 /* before av_len: a tied FETCHSIZE die has no G_EVAL above it here */
1442 424 psgi_triplet = feersum_flatten_magical_av(aTHX_ psgi_triplet);
1443 424 ▶ if (unlikely(!psgi_triplet)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!psgi_triplet)) { 1 423 424
1444 1 call_died(aTHX_ c, "PSGI request");
1445 1 return;
1446 }
1447 423 ▶ if (unlikely(av_len(psgi_triplet)+1 != 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(av_len(psgi_triplet)+1 != 3)) { 0 423 423
1448 0 ▶ sv_setpvs(ERRSV, "Invalid PSGI array response (expected triplet)");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "Invalid PSGI array response (expected triplet)"); 0 0 0
1449 0 call_died(aTHX_ c, "PSGI request");
1450 0 return;
1451 }
1452  
1453 trace("PSGI response triplet, c=%p av=%p\n", c, psgi_triplet);
1454 423 SV **msg_p = av_fetch(psgi_triplet,0,0);
1455 423 SV **hdrs_p = av_fetch(psgi_triplet,1,0);
1456 423 SV **body_p = av_fetch(psgi_triplet,2,0);
1457 423 ▶ if (unlikely(!msg_p || !hdrs_p || !body_p)) {
Branch: 4 branches
BranchTrueFalseTotal
if (unlikely(!msg_p || !hdrs_p || !body_p)) { 423 0 423
if (unlikely(!msg_p || !hdrs_p || !body_p)) { 0 423 423
if (unlikely(!msg_p || !hdrs_p || !body_p)) { 423 0 423
if (unlikely(!msg_p || !hdrs_p || !body_p)) { 0 423 423
1458 0 ▶ sv_setpvs(ERRSV, "Invalid PSGI array response (NULL element)");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "Invalid PSGI array response (NULL element)"); 0 0 0
1459 0 call_died(aTHX_ c, "PSGI request");
1460 0 return;
1461 }
1462 423 SV *msg = *msg_p;
1463 423 SV *hdrs = *hdrs_p;
1464 423 SV *body = *body_p;
1465  
1466 AV *headers;
1467 423 ▶ if (IsArrayRef(hdrs))
Branch: 2 branches
BranchTrueFalseTotal
if (IsArrayRef(hdrs)) 423 0 423
if (IsArrayRef(hdrs)) 423 0 423
1468 423 headers = (AV*)SvRV(hdrs);
1469 else {
1470 0 ▶ sv_setpvs(ERRSV, "PSGI Headers must be an array-ref");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "PSGI Headers must be an array-ref"); 0 0 0
1471 0 call_died(aTHX_ c, "PSGI request");
1472 0 return;
1473 }
1474 423 headers = feersum_flatten_headers(aTHX_ headers);
1475 423 ▶ if (unlikely(!headers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!headers)) { 3 420 423
1476 3 call_died(aTHX_ c, "PSGI request");
1477 3 return;
1478 }
1479  
1480 /* No G_EVAL is above this path (call_sv's has already exited), so every
1481 * croak feersum_start_response would raise must be screened here. */
1482 420 ▶ if (unlikely((av_len(headers) + 1) % 2 == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely((av_len(headers) + 1) % 2 == 1)) { 1 419 420
1483 1 ▶ sv_setpvs(ERRSV, "PSGI headers must be an even-length array");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "PSGI headers must be an even-length array"); 1 0 1
1484 1 call_died(aTHX_ c, "PSGI request");
1485 1 return;
1486 }
1487 {
1488 419 const char *serr = feersum_check_psgi_status(aTHX_ msg);
1489 419 ▶ if (unlikely(serr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(serr)) { 55 364 419
1490 55 ▶ sv_setpv(ERRSV, serr);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpv(ERRSV, serr); 55 0 55
1491 55 call_died(aTHX_ c, "PSGI request");
1492 55 return;
1493 }
1494 }
1495 /* A psgi.input poll_cb that died mid-handler has already answered. */
1496 364 ▶ if (unlikely(c->responding != RESPOND_NOT_STARTED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->responding != RESPOND_NOT_STARTED)) { 0 364 364
1497 0 ▶ sv_setpvs(ERRSV, "PSGI app returned a response after the response had "
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "PSGI app returned a response after the response had " 0 0 0
1498 "already started");
1499 0 call_died(aTHX_ c, "PSGI request");
1500 0 return;
1501 }
1502 364 ▶ if (unlikely(c->io_taken)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->io_taken)) { 0 364 364
1503 0 ▶ sv_setpvs(ERRSV, "PSGI app took psgix.io and then returned a response; "
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "PSGI app took psgix.io and then returned a response; " 0 0 0
1504 "call return_from_psgix_io first");
1505 0 call_died(aTHX_ c, "PSGI request");
1506 0 return;
1507 }
1508 {
1509 364 const char *err = feersum_check_response_framing(aTHX_ msg, headers);
1510 364 ▶ if (unlikely(err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(err)) { 1 363 364
1511 1 ▶ sv_setpv(ERRSV, err);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpv(ERRSV, err); 1 0 1
1512 1 call_died(aTHX_ c, "PSGI request");
1513 1 return;
1514 }
1515 }
1516  
1517 /* start_response can finish the response itself (H2 reset, bodiless H2
1518 * stream); writing a body then would croak with no G_EVAL above. */
1519 680 ▶ if (likely(IsArrayRef(body))) {
Branch: 2 branches
BranchTrueFalseTotal
if (likely(IsArrayRef(body))) { 363 0 363
if (likely(IsArrayRef(body))) { 350 13 363
1520 350 SV *flat = feersum_flatten_magical_body(aTHX_ body);
1521 350 ▶ if (unlikely(!flat)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!flat)) { 33 317 350
1522 33 call_died(aTHX_ c, "PSGI request");
1523 33 return;
1524 }
1525 317 body = flat;
1526 317 feersum_start_response(aTHX_ c, msg, headers, 0, 1);
1527 317 ▶ if (unlikely(c->responding >= RESPOND_SHUTDOWN)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->responding >= RESPOND_SHUTDOWN)) return; 0 317 317
1528 317 feersum_write_whole_body(aTHX_ c, body);
1529 }
1530 // Only an object, glob or IO answers getline(); screen before start_response
1531 13 ▶ else if (likely(SvROK(body)
Branch: 6 branches
BranchTrueFalseTotal
else if (likely(SvROK(body) 13 0 13
else if (likely(SvROK(body) 11 2 13
else if (likely(SvROK(body) 11 0 11
else if (likely(SvROK(body) 0 13 13
else if (likely(SvROK(body) 0 0 0
else if (likely(SvROK(body) 13 0 13
1532 && (sv_isobject(body)
1533 || SvTYPE(SvRV(body)) == SVt_PVGV
1534 || SvTYPE(SvRV(body)) == SVt_PVIO))) {
1535 13 feersum_start_response(aTHX_ c, msg, headers, 1, 1);
1536 13 ▶ if (unlikely(c->responding >= RESPOND_SHUTDOWN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->responding >= RESPOND_SHUTDOWN)) { 0 13 13
1537 /* H2 HEAD/no-content ends at HEADERS; still close the body */
1538 0 feersum_close_psgi_body(aTHX_ body);
1539 0 return;
1540 }
1541 13 c->poll_write_cb = newSVsv(body);
1542 13 c->poll_write_cb_is_io_handle = 1;
1543 13 conn_write_ready(c);
1544 }
1545 else {
1546 0 ▶ sv_setpvs(ERRSV, "Expected PSGI array-ref or IO::Handle-like body");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, "Expected PSGI array-ref or IO::Handle-like body"); 0 0 0
1547 0 call_died(aTHX_ c, "PSGI request");
1548 0 return;
1549 }
1550 }
1551  
1552 static int
1553 326 feersum_close_handle (pTHX_ struct feer_conn *c, bool is_writer)
1554 {
1555 int RETVAL;
1556 326 ▶ if (is_writer) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_writer) { 305 21 326
1557 trace("close writer fd=%d, c=%p, refcnt=%d\n", c->fd, c, SvREFCNT(c->self));
1558 305 feersum_release_io_body(aTHX_ c);
1559 305 ▶ if (c->poll_write_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_write_cb) { 53 252 305
1560 53 SV *tmp = c->poll_write_cb;
1561 53 c->poll_write_cb = NULL; // NULL before dec: DESTROY can re-enter
1562 53 c->poll_write_cb_is_io_handle = 0;
1563 53 SvREFCNT_dec(tmp);
1564 }
1565 305 ▶ if (c->on_eof_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_eof_cb) { 40 265 305
1566 40 SV *tmp = c->on_eof_cb;
1567 40 c->on_eof_cb = NULL; // NULL before dec: DESTROY can re-enter
1568 40 SvREFCNT_dec(tmp);
1569 }
1570 #ifdef FEERSUM_HAS_H2
1571 if (unlikely(c->is_h2_stream)) {
1572 if (c->responding < RESPOND_SHUTDOWN) {
1573 feersum_h2_close_write(aTHX_ c);
1574 change_responding_state(c, RESPOND_SHUTDOWN);
1575 }
1576 } else
1577 #endif
1578 305 ▶ if (c->responding < RESPOND_SHUTDOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->responding < RESPOND_SHUTDOWN) { 275 30 305
1579 275 finish_wbuf(c);
1580 275 change_responding_state(c, RESPOND_SHUTDOWN);
1581 275 conn_write_ready(c);
1582 }
1583 305 RETVAL = 1;
1584 }
1585 else {
1586 trace("close reader fd=%d, c=%p\n", c->fd, c);
1587 21 ▶ if (c->poll_read_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_read_cb) { 0 21 21
1588 0 SV *tmp = c->poll_read_cb;
1589 0 c->poll_read_cb = NULL;
1590 0 SvREFCNT_dec(tmp);
1591 }
1592 /* Body fully received: leave rbuf (it may hold a pipelined request)
1593 * and the socket alone, or keepalive breaks. */
1594 21 ▶ if (c->receiving == RECEIVE_SHUTDOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->receiving == RECEIVE_SHUTDOWN) { 21 0 21
1595 21 RETVAL = 0;
1596 }
1597 else {
1598 /* Still arriving: the app is abandoning an upload. */
1599 0 ▶ if (c->rbuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->rbuf) { 0 0 0
1600 0 SvREFCNT_dec(c->rbuf);
1601 0 c->rbuf = NULL;
1602 }
1603 /* fd_given_away: a SHUT_RD here would kill the app's own reads */
1604 0 ▶ if (c->fd >= 0 && !c->fd_given_away
Branch: 2 branches
BranchTrueFalseTotal
if (c->fd >= 0 && !c->fd_given_away 0 0 0
if (c->fd >= 0 && !c->fd_given_away 0 0 0
1605 #ifdef FEERSUM_HAS_H2
1606 && !c->is_h2_stream
1607 #endif
1608 )
1609 0 RETVAL = shutdown(c->fd, SHUT_RD);
1610 else
1611 0 RETVAL = -1;
1612 0 change_receiving_state(c, RECEIVE_SHUTDOWN);
1613 }
1614 }
1615  
1616 326 SvREFCNT_dec(c->self); // drop the handle's ref to the conn
1617 326 return RETVAL;
1618 }
1619  
1620 static SV*
1621 209 feersum_conn_guard(pTHX_ struct feer_conn *c, SV *guard)
1622 {
1623 209 ▶ if (guard) {
Branch: 1 branch
BranchTrueFalseTotal
if (guard) { 207 2 209
1624 /* Install before dec: the old guard's DESTROY may register a new one. */
1625 207 SV *old = c->ext_guard;
1626 207 ▶ c->ext_guard = SvOK(guard) ? newSVsv(guard) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c->ext_guard = SvOK(guard) ? newSVsv(guard) : NULL; 207 0 207
1627 207 SvREFCNT_dec(old);
1628 /* H2 HEAD/no-content can close the stream before the guard is set */
1629 207 ▶ if (unlikely(c->fd < 0 && c->ext_guard))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->fd < 0 && c->ext_guard)) 0 207 207
if (unlikely(c->fd < 0 && c->ext_guard)) 0 0 0
1630 0 feersum_queue_response_cleanup(c);
1631 }
1632 209 ▶ return c->ext_guard ? newSVsv(c->ext_guard) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return c->ext_guard ? newSVsv(c->ext_guard) : &PL_sv_undef; 209 0 209
1633 }
1634  
1635 static void
1636 1375 call_request_callback (struct feer_conn *c)
1637 {
1638 dTHX;
1639 1375 dSP;
1640 int flags;
1641 1375 struct feer_server *server = c->server;
1642 1375 c->in_callback++;
1643 1375 SvREFCNT_inc_void_NN(c->self);
1644 1375 server->total_requests++;
1645  
1646 /* req->method/uri point into rbuf, which the next request overwrites */
1647 1375 ▶ if (unlikely(server->access_log_cb_cv != NULL) && likely(c->req != NULL)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(server->access_log_cb_cv != NULL) && likely(c->req != NULL)) { 4 1371 1375
if (unlikely(server->access_log_cb_cv != NULL) && likely(c->req != NULL)) { 4 0 4
1648 4 SvREFCNT_dec(c->log_method);
1649 4 SvREFCNT_dec(c->log_uri);
1650 4 c->log_method = newSVpvn(c->req->method, c->req->method_len);
1651 4 c->log_uri = newSVpvn(c->req->uri, c->req->uri_len);
1652 4 c->req_start = ev_now(feersum_ev_loop);
1653 }
1654  
1655 1375 ▶ if (unlikely(server->max_requests)
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(server->max_requests) 0 1375 1375
1656 0 ▶ && server->total_requests >= server->max_requests
Branch: 1 branch
BranchTrueFalseTotal
&& server->total_requests >= server->max_requests 0 0 0
1657 0 ▶ && !server->shutting_down)
Branch: 1 branch
BranchTrueFalseTotal
&& !server->shutting_down) 0 0 0
1658 {
1659 0 server->max_requests = 0; /* one-shot */
1660 0 server->retire_pending = 1; /* check_cb drains at a loop boundary */
1661 }
1662  
1663 trace("request callback c=%p\n", c);
1664  
1665 1375 ENTER;
1666 1375 SAVETMPS;
1667 1375 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1375 1375
1668  
1669 1375 ▶ if (server->request_cb_is_psgi) {
Branch: 1 branch
BranchTrueFalseTotal
if (server->request_cb_is_psgi) { 709 666 1375
1670 709 HV *env = feersum_env(aTHX_ c);
1671 709 ▶ mXPUSHs(newRV_noinc((SV*)env));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV*)env)); 0 709 709
1672 709 flags = G_EVAL|G_SCALAR;
1673 }
1674 else {
1675 666 ▶ mXPUSHs(feer_conn_2sv(c));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(feer_conn_2sv(c)); 0 666 666
1676 666 flags = G_DISCARD|G_EVAL|G_VOID;
1677 }
1678  
1679 1375 PUTBACK;
1680 1375 c->pin_writer = 1;
1681 1375 int returned = call_sv(server->request_cb_cv, flags);
1682 1375 c->pin_writer = 0;
1683 1375 SPAGAIN;
1684  
1685 trace("called request callback, errsv? %d\n", SvTRUE(ERRSV) ? 1 : 0);
1686  
1687 1375 ▶ bool req_cb_errored = SvTRUE(ERRSV) ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
bool req_cb_errored = SvTRUE(ERRSV) ? TRUE : FALSE; 1375 0 1375
1688 1375 ▶ if (unlikely(req_cb_errored)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(req_cb_errored)) { 27 1348 1375
1689 27 call_died(aTHX_ c, "request");
1690 // call_sv(G_SCALAR) pushed undef before unwinding and call_died ran
1691 // more Perl: re-fetch SP and pop it, or one slot leaks per die.
1692 27 SPAGAIN;
1693 27 ▶ if (returned > 0) SP -= returned;
Branch: 1 branch
BranchTrueFalseTotal
if (returned > 0) SP -= returned; 0 27 27
1694 27 PUTBACK;
1695 27 returned = 0;
1696 }
1697 /* Before the PSGI streamer runs: it pins its own writer. */
1698 1375 feersum_writer_unpin(aTHX_ c, req_cb_errored);
1699  
1700 1375 SV *psgi_response = NULL;
1701 1375 ▶ if (server->request_cb_is_psgi && likely(returned >= 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (server->request_cb_is_psgi && likely(returned >= 1)) { 709 666 1375
if (server->request_cb_is_psgi && likely(returned >= 1)) { 709 0 709
1702 709 psgi_response = POPs;
1703 709 SvREFCNT_inc_void_NN(psgi_response);
1704 }
1705  
1706 trace("leaving request callback\n");
1707 1375 PUTBACK;
1708  
1709 1375 ▶ if (psgi_response) {
Branch: 1 branch
BranchTrueFalseTotal
if (psgi_response) { 709 666 1375
1710 709 feersum_handle_psgi_response(aTHX_ c, psgi_response, 1); // can_recurse
1711 709 SvREFCNT_dec(psgi_response);
1712 }
1713  
1714 1375 c->in_callback--;
1715 1375 SvREFCNT_dec(c->self);
1716  
1717 1375 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1375 0 1375
1718 1375 LEAVE;
1719 1375 }
1720  
1721 static void
1722 1071 call_poll_callback (struct feer_conn *c, bool is_write)
1723 {
1724 dTHX;
1725 1071 dSP;
1726  
1727 1071 ▶ SV *cb = (is_write) ? c->poll_write_cb : c->poll_read_cb;
Branch: 1 branch
BranchTrueFalseTotal
SV *cb = (is_write) ? c->poll_write_cb : c->poll_read_cb; 914 157 1071
1728  
1729 1071 ▶ if (unlikely(cb == NULL)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(cb == NULL)) return; 0 1071 1071
1730  
1731 /* the callback can drop the installing handle's last ref; hold the conn */
1732 1071 SvREFCNT_inc_void_NN(c->self);
1733  
1734 1071 c->in_callback++;
1735  
1736 trace("%s poll callback c=%p cbrv=%p\n",
1737 is_write ? "write" : "read", c, cb);
1738  
1739 1071 ENTER;
1740 1071 SAVETMPS;
1741 1071 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1071 1071
1742 1071 SV *hdl = new_feer_conn_handle(aTHX_ c, is_write);
1743 1071 ▶ mXPUSHs(hdl);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(hdl); 0 1071 1071
1744 1071 PUTBACK;
1745 1071 call_sv(cb, G_DISCARD|G_EVAL|G_VOID);
1746 1071 SPAGAIN;
1747  
1748 trace("called %s poll callback, errsv? %d\n",
1749 is_write ? "write" : "read", SvTRUE(ERRSV) ? 1 : 0);
1750  
1751 1071 ▶ if (unlikely(SvTRUE(ERRSV))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV))) { 1071 0 1071
if (unlikely(SvTRUE(ERRSV))) { 2 1069 1071
1752 /* A callback left installed after a die re-arms and spins forever. */
1753 2 ▶ SV **cb_slot = is_write ? &c->poll_write_cb : &c->poll_read_cb;
Branch: 1 branch
BranchTrueFalseTotal
SV **cb_slot = is_write ? &c->poll_write_cb : &c->poll_read_cb; 2 0 2
1754 2 ▶ if (*cb_slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (*cb_slot) { 2 0 2
1755 2 SV *tmp = *cb_slot;
1756 2 *cb_slot = NULL;
1757 2 ▶ if (is_write) c->poll_write_cb_is_io_handle = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (is_write) c->poll_write_cb_is_io_handle = 0; 2 0 2
1758 2 SvREFCNT_dec(tmp);
1759 }
1760 2 ▶ call_died(aTHX_ c, is_write ? "write poll" : "read poll");
Branch: 1 branch
BranchTrueFalseTotal
call_died(aTHX_ c, is_write ? "write poll" : "read poll"); 2 0 2
1761 /* call_died cannot 500 mid-response; end it rather than hang. */
1762 2 ▶ if (c->responding > RESPOND_NOT_STARTED
Branch: 1 branch
BranchTrueFalseTotal
if (c->responding > RESPOND_NOT_STARTED 2 0 2
1763 2 ▶ && c->responding < RESPOND_SHUTDOWN) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->responding < RESPOND_SHUTDOWN) { 2 0 2
1764 2 c->is_keepalive = 0;
1765 2 change_responding_state(c, RESPOND_SHUTDOWN);
1766 }
1767 }
1768  
1769 // Neutralise the mortal handle before FREETMPS, or its DESTROY closes the
1770 // connection. SvUVX is already 0 if the callback called close().
1771 {
1772 1071 SV *inner = SvRV(hdl);
1773 1071 ▶ if (SvUVX(inner)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUVX(inner)) { 1071 0 1071
1774 1071 SvUVX(inner) = 0;
1775 1071 SvREFCNT_dec(c->self); // balance new_feer_conn_handle's inc
1776 }
1777 }
1778  
1779 trace("leaving %s poll callback\n", is_write ? "write" : "read");
1780 1071 PUTBACK;
1781 1071 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1071 0 1071
1782 1071 LEAVE;
1783  
1784 1071 c->in_callback--;
1785  
1786 1071 SvREFCNT_dec(c->self);
1787 }
1788  
1789 static void
1790 35 call_on_eof_callback (struct feer_conn *c)
1791 {
1792 dTHX;
1793 35 dSP;
1794  
1795 35 SV *cb = c->on_eof_cb;
1796  
1797 35 ▶ if (unlikely(cb == NULL)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(cb == NULL)) return; 0 35 35
1798  
1799 /* the callback can drop the installing handle's last ref; hold the conn */
1800 35 SvREFCNT_inc_void_NN(c->self);
1801  
1802 35 c->in_callback++;
1803 35 c->in_on_eof = 1;
1804  
1805 trace("on_eof callback c=%p cbrv=%p\n", c, cb);
1806  
1807 35 ENTER;
1808 35 SAVETMPS;
1809 35 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 35 35
1810 35 SV *hdl = new_feer_conn_handle(aTHX_ c, 1);
1811 35 ▶ mXPUSHs(hdl);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(hdl); 0 35 35
1812 35 PUTBACK;
1813 35 call_sv(cb, G_DISCARD|G_EVAL|G_VOID);
1814 35 SPAGAIN;
1815  
1816 35 ▶ if (unlikely(SvTRUE(ERRSV))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV))) { 35 0 35
if (unlikely(SvTRUE(ERRSV))) { 1 34 35
1817 /* left installed, a dead callback would refire on the next install */
1818 1 ▶ if (c->on_eof_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_eof_cb) { 0 1 1
1819 0 SV *tmp = c->on_eof_cb;
1820 0 c->on_eof_cb = NULL;
1821 0 SvREFCNT_dec(tmp);
1822 }
1823 1 call_died(aTHX_ c, "on_eof");
1824 /* call_died cannot 500 mid-response; end it rather than hang. */
1825 1 ▶ if (c->responding > RESPOND_NOT_STARTED
Branch: 1 branch
BranchTrueFalseTotal
if (c->responding > RESPOND_NOT_STARTED 1 0 1
1826 1 ▶ && c->responding < RESPOND_SHUTDOWN) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->responding < RESPOND_SHUTDOWN) { 0 1 1
1827 0 c->is_keepalive = 0;
1828 0 change_responding_state(c, RESPOND_SHUTDOWN);
1829 }
1830 }
1831  
1832 // Neutralise the mortal handle before FREETMPS, or its DESTROY closes the
1833 // connection. SvUVX is already 0 if the callback called close().
1834 {
1835 35 SV *inner = SvRV(hdl);
1836 35 ▶ if (SvUVX(inner)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUVX(inner)) { 34 1 35
1837 34 SvUVX(inner) = 0;
1838 34 SvREFCNT_dec(c->self); // balance new_feer_conn_handle's inc
1839 }
1840 }
1841  
1842 trace("leaving on_eof callback\n");
1843 35 PUTBACK;
1844 35 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 35 0 35
1845 35 LEAVE;
1846  
1847 35 c->in_callback--;
1848 35 c->in_on_eof = 0;
1849  
1850 35 SvREFCNT_dec(c->self);
1851 }
1852  
1853 static void
1854 20 pump_io_handle (struct feer_conn *c)
1855 {
1856 dTHX;
1857 20 dSP;
1858  
1859 20 ▶ if (unlikely(c->poll_write_cb == NULL)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->poll_write_cb == NULL)) return; 0 20 20
1860  
1861 20 c->in_callback++;
1862  
1863 trace("pump io handle %d\n", c->fd);
1864  
1865 20 SV *old_rs = PL_rs;
1866 20 ▶ SvREFCNT_inc_simple_void(old_rs);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(old_rs); 20 0 20
1867  
1868 20 ENTER;
1869 20 SAVETMPS;
1870  
1871 /* Mortalise after SAVETMPS, or the FREETMPS below cannot reap it. */
1872 20 PL_rs = sv_2mortal(newRV_noinc(newSViv(IO_PUMP_BUFSZ)));
1873 20 sv_setsv(get_sv("/", GV_ADD), PL_rs);
1874  
1875 // HEAD or a bodiless status: a pumped chunk would go out unframed. Fall
1876 // through returned=0 into the EOF branch, which closes and finishes.
1877 int returned;
1878 20 ▶ if (unlikely(c->no_resp_body || !c->auto_cl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->no_resp_body || !c->auto_cl)) { 20 0 20
if (unlikely(c->no_resp_body || !c->auto_cl)) { 7 13 20
1879 7 returned = 0;
1880 }
1881 else {
1882 13 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 13 13
1883 13 ▶ XPUSHs(c->poll_write_cb);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(c->poll_write_cb); 0 13 13
1884 13 PUTBACK;
1885 13 returned = call_method("getline", G_SCALAR|G_EVAL);
1886 13 SPAGAIN;
1887 }
1888  
1889 trace("called getline on io handle fd=%d errsv=%d returned=%d\n",
1890 c->fd, SvTRUE(ERRSV) ? 1 : 0, returned);
1891  
1892 /* pop before the error exit, which would otherwise leave it on the stack */
1893 20 ▶ SV *ret = (returned > 0) ? POPs : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *ret = (returned > 0) ? POPs : NULL; 13 7 20
1894 20 PUTBACK;
1895  
1896 20 ▶ if (unlikely(SvTRUE(ERRSV))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV))) { 20 0 20
if (unlikely(SvTRUE(ERRSV))) { 0 20 20
1897 /* Restore PL_rs first: Feersum::DIED is Perl code that may read $/ */
1898 0 PL_rs = old_rs;
1899 0 sv_setsv(get_sv("/", GV_ADD), old_rs);
1900 0 call_died(aTHX_ c, "getline on io handle");
1901 0 feersum_release_io_body(aTHX_ c);
1902 0 SPAGAIN;
1903 /* no finish_wbuf: a died body must not look like a clean EOF */
1904 0 change_responding_state(c, RESPOND_SHUTDOWN);
1905 0 goto done_pump_io;
1906 }
1907  
1908 20 ▶ if (ret && SvMAGICAL(ret))
Branch: 2 branches
BranchTrueFalseTotal
if (ret && SvMAGICAL(ret)) 13 7 20
if (ret && SvMAGICAL(ret)) 0 13 13
1909 0 ret = sv_2mortal(newSVsv(ret));
1910  
1911 20 ▶ if (unlikely(!ret || !SvOK(ret))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(!ret || !SvOK(ret))) { 13 7 20
if (unlikely(!ret || !SvOK(ret))) { 6 7 13
1912 13 PUTBACK;
1913 13 feersum_release_io_body(aTHX_ c);
1914 13 SPAGAIN;
1915 13 finish_wbuf(c);
1916 13 change_responding_state(c, RESPOND_SHUTDOWN);
1917  
1918 13 goto done_pump_io;
1919 }
1920  
1921 7 ▶ if (c->use_chunked)
Branch: 1 branch
BranchTrueFalseTotal
if (c->use_chunked) 7 0 7
1922 7 add_chunk_sv_to_wbuf(c, ret);
1923 else
1924 0 add_raw_body_to_wbuf(c, ret);
1925  
1926 20 done_pump_io:
1927 trace("leaving pump io handle %d\n", c->fd);
1928  
1929 // Restore PL_rs before FREETMPS reaps the mortal RV it points at.
1930 20 PL_rs = old_rs;
1931 20 sv_setsv(get_sv("/", GV_ADD), old_rs);
1932 20 SvREFCNT_dec(old_rs);
1933  
1934 20 PUTBACK;
1935 20 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 20 0 20
1936 20 LEAVE;
1937  
1938 20 c->in_callback--;
1939 }
1940  
1941 static int
1942 9 psgix_io_svt_get (pTHX_ SV *sv, MAGIC *mg)
1943 {
1944 9 dSP;
1945  
1946 9 struct feer_conn *c = sv_2feer_conn(mg->mg_obj);
1947 trace("invoking psgix.io magic for fd=%d\n", c->fd);
1948  
1949 9 sv_unmagic(sv, PERL_MAGIC_ext);
1950  
1951 /* io() croaks on these, but magic-get also fires on innocent env reads
1952 * (dumps): decline with undef; only a second takeover warns */
1953 9 ▶ if (unlikely(c->io_taken)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->io_taken)) { 0 9 9
1954 0 trouble("psgix.io: socket already taken on fd=%d\n", c->fd);
1955 0 sv_setsv(sv, &PL_sv_undef);
1956 0 return 0;
1957 }
1958 /* H2 Extended CONNECT responds 200 before the app reads psgix.io */
1959 9 if (
1960 #ifdef FEERSUM_HAS_H2
1961 !c->is_h2_stream &&
1962 #endif
1963 9 ▶ unlikely(c->responding != RESPOND_NOT_STARTED)) {
Branch: 1 branch
BranchTrueFalseTotal
unlikely(c->responding != RESPOND_NOT_STARTED)) { 0 9 9
1964 0 sv_setsv(sv, &PL_sv_undef);
1965 0 return 0;
1966 }
1967  
1968 #ifdef FEERSUM_HAS_H2
1969 if (c->is_h2_stream) {
1970 struct feer_h2_stream *stream = H2_STREAM_FROM_PC(c);
1971 if (!stream || !stream->is_tunnel) {
1972 /* Set undef, not croak: magic-get fires from innocent env reads,
1973 * and the IV left behind would read as a truthy handle. */
1974 trouble("psgix.io: not supported on regular H2 streams fd=%d\n", c->fd);
1975 sv_setsv(sv, &PL_sv_undef);
1976 return 0;
1977 }
1978 h2_tunnel_auto_accept(aTHX_ c, stream);
1979 /* Re-fetch stream: auto_accept -> session_send may have freed it */
1980 stream = H2_STREAM_FROM_PC(c);
1981 if (!stream) {
1982 trouble("psgix.io: stream freed during auto_accept fd=%d\n", c->fd);
1983 sv_setsv(sv, &PL_sv_undef);
1984 return 0;
1985 }
1986 feer_h2_setup_tunnel(aTHX_ stream);
1987 if (!stream->tunnel_established) {
1988 trouble("psgix.io: tunnel setup failed fd=%d\n", c->fd);
1989 struct feer_conn *parent = stream->parent;
1990 SvREFCNT_inc_void_NN(parent->self);
1991 h2_submit_rst(parent->h2_session, stream->stream_id,
1992 NGHTTP2_INTERNAL_ERROR);
1993 feer_h2_session_send(parent);
1994 SvREFCNT_dec(parent->self);
1995 sv_setsv(sv, &PL_sv_undef);
1996 return 0;
1997 }
1998  
1999 ENTER;
2000 SAVETMPS;
2001 PUSHMARK(SP);
2002 XPUSHs(sv);
2003 mXPUSHs(newSViv(stream->tunnel_sv1));
2004 PUTBACK;
2005  
2006 call_pv("Feersum::Connection::_raw", G_VOID|G_DISCARD|G_EVAL);
2007 SPAGAIN;
2008  
2009 /* new_from_fd can return undef without dying (fdopen failure) */
2010 if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) {
2011 call_died(aTHX_ c, "psgix.io H2 tunnel magic");
2012 sv_setsv(sv, &PL_sv_undef);
2013 } else {
2014 SV *io_glob = SvRV(sv);
2015 GvSV(io_glob) = newRV_inc(c->self);
2016 stream->tunnel_sv1 = -1;
2017 }
2018  
2019 c->io_taken = 1;
2020 PUTBACK;
2021 FREETMPS;
2022 LEAVE;
2023 return 0;
2024 }
2025 #endif
2026  
2027 #ifdef FEERSUM_HAS_TLS
2028 9 ▶ if (c->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) { 0 9 9
2029 0 feer_tls_setup_tunnel(c);
2030 0 ▶ if (!c->tls_tunnel) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tls_tunnel) { 0 0 0
2031 0 trouble("psgix.io: TLS tunnel setup failed fd=%d\n", c->fd);
2032 0 sv_setsv(sv, &PL_sv_undef);
2033 0 return 0;
2034 }
2035  
2036 0 ENTER;
2037 0 SAVETMPS;
2038 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
2039 0 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 0 0
2040 0 ▶ mXPUSHs(newSViv(c->tls_tunnel_sv1));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(c->tls_tunnel_sv1)); 0 0 0
2041 0 PUTBACK;
2042  
2043 0 call_pv("Feersum::Connection::_raw", G_VOID|G_DISCARD|G_EVAL);
2044 0 SPAGAIN;
2045  
2046 0 ▶ if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) { 0 0 0
if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) { 0 0 0
if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) { 0 0 0
2047 0 call_died(aTHX_ c, "psgix.io TLS tunnel magic");
2048 0 sv_setsv(sv, &PL_sv_undef);
2049 /* _raw did not take the fd; conn cleanup closes it */
2050 } else {
2051 0 SV *io_glob = SvRV(sv);
2052 0 GvSV(io_glob) = newRV_inc(c->self);
2053 0 c->tls_tunnel_sv1 = -1;
2054 }
2055  
2056 0 c->io_taken = 1;
2057 0 stop_read_timer(c);
2058 0 stop_write_timer(c);
2059 0 PUTBACK;
2060 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
2061 0 LEAVE;
2062 0 return 0;
2063 }
2064 #endif
2065  
2066 9 ENTER;
2067 9 SAVETMPS;
2068  
2069 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
2070 9 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 9 9
2071 9 ▶ mXPUSHs(newSViv(c->fd));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(c->fd)); 0 9 9
2072 9 PUTBACK;
2073  
2074 9 call_pv("Feersum::Connection::_raw", G_VOID|G_DISCARD|G_EVAL);
2075 9 SPAGAIN;
2076  
2077 9 ▶ if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) { 9 0 9
if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) { 9 0 9
if (unlikely(SvTRUE(ERRSV) || !SvROK(sv))) { 0 9 9
2078 0 call_died(aTHX_ c, "psgix.io magic");
2079 0 sv_setsv(sv, &PL_sv_undef);
2080 }
2081 else {
2082 9 SV *io_glob = SvRV(sv);
2083 9 GvSV(io_glob) = newRV_inc(c->self);
2084  
2085 9 feer_hand_out_raw_fd(aTHX_ c, io_glob, "psgix.io");
2086 }
2087  
2088 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 9 0 9
2089 9 LEAVE;
2090  
2091 9 return 0;
2092 }
2093  
2094 #ifdef FEERSUM_HAS_H2
2095 static void
2096 h2_tunnel_auto_accept(pTHX_ struct feer_conn *c, struct feer_h2_stream *stream)
2097 {
2098 if (c->responding != RESPOND_NOT_STARTED)
2099 return;
2100 AV *empty_hdr = newAV();
2101 SV *status_sv = newSVpvs("200");
2102 feersum_start_response(aTHX_ c, status_sv, empty_hdr, 1, 1);
2103 SvREFCNT_dec(status_sv);
2104 SvREFCNT_dec((SV *)empty_hdr);
2105 /* start_response -> session_send may have freed the stream */
2106 stream = H2_STREAM_FROM_PC(c);
2107 if (stream)
2108 stream->tunnel_swallow_response = 1;
2109 }
2110  
2111 /* One getline per pump iteration: draining synchronously never reads a
2112 * WINDOW_UPDATE and buffers the whole body. Returns 0 at EOF. */
2113 static int
2114 pump_h2_io_step(pTHX_ struct feer_conn *c)
2115 {
2116 dSP;
2117 if (unlikely(!c->poll_write_cb)) return 0;
2118  
2119 /* in_callback makes feersum_h2_write_chunk buffer: a flush here re-enters
2120 * this pump through h2_check_stream_poll_cbs and frees the stream. */
2121 c->in_callback++;
2122  
2123 SV *old_rs = PL_rs;
2124 SvREFCNT_inc_simple_void(old_rs);
2125 ENTER;
2126 SAVETMPS;
2127 PL_rs = sv_2mortal(newRV_noinc(newSViv(IO_PUMP_BUFSZ)));
2128 sv_setsv(get_sv("/", GV_ADD), PL_rs);
2129  
2130 PUSHMARK(SP);
2131 XPUSHs(c->poll_write_cb);
2132 PUTBACK;
2133 int returned = call_method("getline", G_SCALAR|G_EVAL);
2134 SPAGAIN;
2135  
2136 /* pop before the error exit, which would otherwise leave it on the stack */
2137 SV *ret = (returned > 0) ? POPs : NULL;
2138 PUTBACK;
2139  
2140 if (unlikely(SvTRUE(ERRSV))) {
2141 PL_rs = old_rs;
2142 sv_setsv(get_sv("/", GV_ADD), old_rs);
2143 call_died(aTHX_ c, "getline on H2 io handle");
2144 feersum_release_io_body(aTHX_ c);
2145 SPAGAIN;
2146 PUTBACK; FREETMPS; LEAVE;
2147 SvREFCNT_dec(old_rs);
2148 c->in_callback--;
2149 feersum_h2_close_write(aTHX_ c);
2150 return 0;
2151 }
2152  
2153 if (ret && SvMAGICAL(ret)) ret = sv_2mortal(newSVsv(ret));
2154  
2155 int more = 1;
2156 if (unlikely(!ret || !SvOK(ret))) { /* undef is EOF; q{} is not */
2157 PUTBACK;
2158 feersum_release_io_body(aTHX_ c);
2159 SPAGAIN;
2160 more = 0;
2161 }
2162 else {
2163 feersum_h2_write_chunk(aTHX_ c, ret);
2164 }
2165  
2166 PL_rs = old_rs;
2167 sv_setsv(get_sv("/", GV_ADD), old_rs);
2168 PUTBACK; FREETMPS; LEAVE;
2169 SvREFCNT_dec(old_rs);
2170  
2171 c->in_callback--;
2172 if (!more) feersum_h2_close_write(aTHX_ c);
2173 return more;
2174 }
2175  
2176 static SV*
2177 feersum_env_method_h2(pTHX_ struct feer_conn *c, struct feer_req *r)
2178 {
2179 if (unlikely(c->is_h2_stream)) {
2180 struct feer_h2_stream *stream = H2_STREAM_FROM_PC(c);
2181 if (stream && stream->is_tunnel && stream->h2_protocol)
2182 return SvREFCNT_inc_simple_NN(method_GET);
2183 }
2184 return feersum_env_method(aTHX_ r);
2185 }
2186 #endif
« feersum_core.c.inc ↑ Summary feersum_utils.c.inc »