feersum_psgi.c.inc

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