include/fetch/ft_ua.h

filter: Statementstmt 89.7% 418 / 466 Branchbran 55.1% 278 / 504 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.7% 696 / 970 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/backend_epoll.c ↑ Summary include/fetch/ft_ws.h »
1 #ifndef FT_UA_H
2 #define FT_UA_H
3  
4 /* The Fetch user agent, fully in C. A Fetch object is a blessed IV holding an
5 * ft_ua*; new() resolves the event-loop adapter and builds the default header
6 * set / cookie jar / connection pool, and request()/get()/... run the HTTP
7 * exchange - URL parsing, header merge, cookie application, request
8 * serialisation, and redirect following (a Future chain built with the C
9 * Future API in ft_future.h). The foreign event-loop adapters stay in Perl;
10 * everything else here is C.
11 *
12 * Included from Fetch.xs after the ft_loop_from_sv / ft_pool_from_sv /
13 * ft_obj_can statics it depends on. */
14  
15 typedef struct {
16 SV *loop; /* resolved Fetch::Loop adapter */
17 /* Hyperman-direct mode (ft_hm.h): set at UA build when `loop` is a
18 * Fetch::Loop::Hyperman and Hyperman's C ABI resolved; connections then
19 * arm interest/deadlines through the table instead of _ft_arm/_ft_timer.
20 * The adapter (held in `loop`) owns the Hyperman::Loop SV, which keeps
21 * hm_loop valid for the UA's lifetime. */
22 const hm_abi *hm; /* the resolved table, or NULL */
23 void *hm_loop; /* opaque Hyperman loop handle */
24 SV *pool; /* Fetch::_Pool, or NULL when keep_alive is off */
25 SV *headers; /* default headers, a Fetch::Headers */
26 SV *agent; /* User-Agent string */
27 SV *cookie_jar; /* Fetch::CookieJar, or NULL */
28 int tls_verify;
29 int max_redirects;
30 int keep_alive;
31 int simple_response; /* resolve to a raw hash, not a Fetch::Response */
32 double timeout;
33 } ft_ua;
34  
35 263 static ft_ua *ft_ua_of(pTHX_ SV *sv) {
36 263 ▶ if (!(SvROK(sv) && SvIOK(SvRV(sv))))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(sv) && SvIOK(SvRV(sv)))) 263 0 263
if (!(SvROK(sv) && SvIOK(SvRV(sv)))) 0 263 263
37 0 croak("Fetch: not a Fetch user agent");
38 263 return INT2PTR(ft_ua *, SvIV(SvRV(sv)));
39 }
40  
41 /* ---- URL parsing -------------------------------------------------------- */
42  
43 typedef struct { char *scheme, *host, *path; int port; } ft_url;
44  
45 194 static void ft_url_free(ft_url *u) {
46 194 free(u->scheme); free(u->host); free(u->path);
47 194 u->scheme = u->host = u->path = NULL;
48 194 }
49  
50 /* Parse scheme://host[:port][path]. Mirrors Fetch::_parse_url; returns 1 on
51 * success (fields malloc'd), 0 if there is no "://". */
52 194 static int ft_parse_url(const char *url, ft_url *u) {
53 194 const char *sep = strstr(url, "://");
54 const char *h, *p, *rest;
55 size_t i;
56 194 memset(u, 0, sizeof *u);
57 194 ▶ if (!sep) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) return 0; 0 194 194
58 194 u->scheme = ft_lc_dup(url, (STRLEN)(sep - url));
59 194 h = sep + 3;
60 194 p = h;
61 1952 ▶ while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++;
Branch: 5 branches
BranchTrueFalseTotal
while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; 1952 0 1952
while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; 1951 1 1952
while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; 1758 193 1951
while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; 1758 0 1758
while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; 1758 0 1758
62 194 u->host = ft_strdup_n(h, (size_t)(p - h));
63 194 ▶ if (*p == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ':') { 193 1 194
64 193 p++;
65 193 u->port = 0;
66 1142 ▶ while (*p >= '0' && *p <= '9') { u->port = u->port * 10 + (*p - '0'); p++; }
Branch: 2 branches
BranchTrueFalseTotal
while (*p >= '0' && *p <= '9') { u->port = u->port * 10 + (*p - '0'); p++; } 949 193 1142
while (*p >= '0' && *p <= '9') { u->port = u->port * 10 + (*p - '0'); p++; } 949 0 949
67 } else {
68 1 u->port = 0;
69 }
70 194 ▶ if (u->port == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (u->port == 0) 1 193 194
71 1 ▶ u->port = (strcmp(u->scheme, "https") == 0) ? 443 : 80;
Branch: 1 branch
BranchTrueFalseTotal
u->port = (strcmp(u->scheme, "https") == 0) ? 443 : 80; 0 1 1
72 194 rest = p;
73 194 ▶ u->path = (*rest) ? ft_strdup0(rest) : ft_strdup0("/");
Branch: 1 branch
BranchTrueFalseTotal
u->path = (*rest) ? ft_strdup0(rest) : ft_strdup0("/"); 194 0 194
74 194 ▶ if (!u->path[0]) { free(u->path); u->path = ft_strdup0("/"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!u->path[0]) { free(u->path); u->path = ft_strdup0("/"); } 0 194 194
75 (void)i;
76 194 return 1;
77 }
78  
79 /* authority for the Host header / :authority: host, or host:port off-default */
80 18 static char *ft_authority(const char *host, int port, int tls) {
81 18 ▶ if (port == (tls ? 443 : 80)) return ft_strdup0(host);
Branch: 2 branches
BranchTrueFalseTotal
if (port == (tls ? 443 : 80)) return ft_strdup0(host); 7 11 18
if (port == (tls ? 443 : 80)) return ft_strdup0(host); 0 18 18
82 {
83 char buf[300];
84 18 snprintf(buf, sizeof buf, "%s:%d", host, port);
85 18 return ft_strdup0(buf);
86 }
87 }
88  
89 /* Same origin = same scheme, host (case-insensitive) and effective port.
90 * Used to decide whether a redirect may carry credential headers along. */
91 12 static int ft_same_origin(const char *a, const char *b) {
92 ft_url ua, ub;
93 12 int pa = ft_parse_url(a, &ua), pb = ft_parse_url(b, &ub);
94 12 ▶ int same = pa && pb
Branch: 1 branch
BranchTrueFalseTotal
int same = pa && pb 12 0 12
95 12 ▶ && strcmp(ua.scheme, ub.scheme) == 0
Branch: 1 branch
BranchTrueFalseTotal
&& strcmp(ua.scheme, ub.scheme) == 0 12 0 12
96 12 ▶ && strcasecmp(ua.host, ub.host) == 0
Branch: 1 branch
BranchTrueFalseTotal
&& strcasecmp(ua.host, ub.host) == 0 12 0 12
97 24 ▶ && ua.port == ub.port;
Branch: 2 branches
BranchTrueFalseTotal
&& ua.port == ub.port; 12 0 12
&& ua.port == ub.port; 11 1 12
98 12 ft_url_free(&ua); /* memset-zeroed on parse failure, so free is safe */
99 12 ft_url_free(&ub);
100 12 return same;
101 }
102  
103 /* ---- redirect helpers --------------------------------------------------- */
104  
105 /* 307/308 keep method+body; 303 or POST become GET without a body; otherwise
106 * the method (and body) are preserved. Returns the (possibly new) method and
107 * sets *drop_body when the body must be dropped. */
108 12 static const char *ft_redirect_method(const char *method, int status,
109 int *drop_body) {
110 12 *drop_body = 0;
111 12 ▶ if (status == 307 || status == 308) return method;
Branch: 2 branches
BranchTrueFalseTotal
if (status == 307 || status == 308) return method; 11 1 12
if (status == 307 || status == 308) return method; 0 11 11
112 11 ▶ if (status == 303 || strcasecmp(method, "POST") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (status == 303 || strcasecmp(method, "POST") == 0) { 10 1 11
if (status == 303 || strcasecmp(method, "POST") == 0) { 0 10 10
113 1 *drop_body = 1;
114 1 return "GET";
115 }
116 10 return method;
117 }
118  
119 /* Resolve a Location against the request URL (absolute / scheme-relative /
120 * root-relative / path-relative). Returns a fresh SV. */
121 12 static SV *ft_resolve_location(pTHX_ const char *base, SV *loc_sv) {
122 STRLEN ll;
123 12 const char *loc = SvPV_const(loc_sv, ll);
124 ft_url u;
125 char *authority;
126 SV *out;
127  
128 /* absolute: scheme://... */
129 {
130 12 const char *s = loc;
131 12 ▶ if (isALPHA((unsigned char)*s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isALPHA((unsigned char)*s)) { 2 10 12
132 2 const char *q = s + 1;
133 8 ▶ while (*q && (isALNUM((unsigned char)*q) || *q=='+' || *q=='.' || *q=='-')) q++;
Branch: 5 branches
BranchTrueFalseTotal
while (*q && (isALNUM((unsigned char)*q) || *q=='+' || *q=='.' || *q=='-')) q++; 8 0 8
while (*q && (isALNUM((unsigned char)*q) || *q=='+' || *q=='.' || *q=='-')) q++; 6 2 8
while (*q && (isALNUM((unsigned char)*q) || *q=='+' || *q=='.' || *q=='-')) q++; 0 2 2
while (*q && (isALNUM((unsigned char)*q) || *q=='+' || *q=='.' || *q=='-')) q++; 0 2 2
while (*q && (isALNUM((unsigned char)*q) || *q=='+' || *q=='.' || *q=='-')) q++; 0 2 2
134 2 ▶ if (q[0]==':' && q[1]=='/' && q[2]=='/')
Branch: 3 branches
BranchTrueFalseTotal
if (q[0]==':' && q[1]=='/' && q[2]=='/') 2 0 2
if (q[0]==':' && q[1]=='/' && q[2]=='/') 2 0 2
if (q[0]==':' && q[1]=='/' && q[2]=='/') 2 0 2
135 2 return newSVpvn(loc, ll);
136 }
137 }
138 10 ▶ if (!ft_parse_url(base, &u)) return newSVpvn(loc, ll);
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_parse_url(base, &u)) return newSVpvn(loc, ll); 0 10 10
139 10 authority = ft_authority(u.host, u.port, strcmp(u.scheme, "https") == 0);
140  
141 10 ▶ if (ll >= 2 && loc[0]=='/' && loc[1]=='/') { /* scheme-relative */
Branch: 3 branches
BranchTrueFalseTotal
if (ll >= 2 && loc[0]=='/' && loc[1]=='/') { /* scheme-relative */ 10 0 10
if (ll >= 2 && loc[0]=='/' && loc[1]=='/') { /* scheme-relative */ 10 0 10
if (ll >= 2 && loc[0]=='/' && loc[1]=='/') { /* scheme-relative */ 0 10 10
142 0 out = newSVpvf("%s:%.*s", u.scheme, (int)ll, loc);
143 10 ▶ } else if (ll >= 1 && loc[0]=='/') { /* root-relative */
Branch: 2 branches
BranchTrueFalseTotal
} else if (ll >= 1 && loc[0]=='/') { /* root-relative */ 10 0 10
} else if (ll >= 1 && loc[0]=='/') { /* root-relative */ 10 0 10
144 10 out = newSVpvf("%s://%s%.*s", u.scheme, authority, (int)ll, loc);
145 } else { /* path-relative */
146 /* dir = request path up to and including the last '/' before any '?' */
147 0 const char *q = strchr(u.path, '?');
148 0 ▶ size_t plen = q ? (size_t)(q - u.path) : strlen(u.path);
Branch: 1 branch
BranchTrueFalseTotal
size_t plen = q ? (size_t)(q - u.path) : strlen(u.path); 0 0 0
149 0 long last = -1; size_t i;
150 0 ▶ for (i = 0; i < plen; i++) if (u.path[i] == '/') last = (long)i;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < plen; i++) if (u.path[i] == '/') last = (long)i; 0 0 0
for (i = 0; i < plen; i++) if (u.path[i] == '/') last = (long)i; 0 0 0
151 0 out = newSVpvf("%s://%s", u.scheme, authority);
152 0 ▶ if (last <= 0) sv_catpvs(out, "/");
Branch: 1 branch
BranchTrueFalseTotal
if (last <= 0) sv_catpvs(out, "/"); 0 0 0
153 0 else sv_catpvn(out, u.path, (STRLEN)(last + 1));
154 0 sv_catpvn(out, loc, ll);
155 }
156 10 free(authority);
157 10 ft_url_free(&u);
158 10 return out;
159 }
160  
161 /* ---- request serialisation --------------------------------------------- */
162  
163 /* Serialize the HTTP/1.1 request bytes from the merged header list. */
164 160 static SV *ft_build_request(pTHX_ ft_ua *ua, const char *method,
165 const char *host, int port, const char *path,
166 AV *hav, SV *body) {
167 160 SV *req = newSV(256); /* preallocate: most request heads fit, no realloc */
168 SSize_t n, i;
169 160 SvPOK_on(req);
170 160 SvCUR_set(req, 0);
171 160 *SvPVX(req) = '\0';
172 160 sv_catpv(req, method); sv_catpvs(req, " ");
173 160 sv_catpv(req, path); sv_catpvs(req, " HTTP/1.1\r\n");
174 160 ▶ if (!ft_hdr_exists(aTHX_ hav, "Host", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_hdr_exists(aTHX_ hav, "Host", 4)) { 160 0 160
175 160 ▶ if (port == 80 || port == 443) {
Branch: 2 branches
BranchTrueFalseTotal
if (port == 80 || port == 443) { 159 1 160
if (port == 80 || port == 443) { 0 159 159
176 1 sv_catpvs(req, "Host: "); sv_catpv(req, host); sv_catpvs(req, "\r\n");
177 } else {
178 159 sv_catpvf(req, "Host: %s:%d\r\n", host, port);
179 }
180 }
181 160 ▶ if (!ua->keep_alive && !ft_hdr_exists(aTHX_ hav, "Connection", 10))
Branch: 2 branches
BranchTrueFalseTotal
if (!ua->keep_alive && !ft_hdr_exists(aTHX_ hav, "Connection", 10)) 5 155 160
if (!ua->keep_alive && !ft_hdr_exists(aTHX_ hav, "Connection", 10)) 5 0 5
182 5 sv_catpvs(req, "Connection: close\r\n");
183 160 ▶ if (body && SvOK(body)) {
Branch: 2 branches
BranchTrueFalseTotal
if (body && SvOK(body)) { 6 154 160
if (body && SvOK(body)) { 6 0 6
184 STRLEN bl; (void)SvPV(body, bl);
185 6 ▶ if (bl && !ft_hdr_exists(aTHX_ hav, "Content-Length", 14))
Branch: 2 branches
BranchTrueFalseTotal
if (bl && !ft_hdr_exists(aTHX_ hav, "Content-Length", 14)) 6 0 6
if (bl && !ft_hdr_exists(aTHX_ hav, "Content-Length", 14)) 6 0 6
186 6 sv_catpvf(req, "Content-Length: %lu\r\n", (unsigned long)bl);
187 }
188 160 n = av_len(hav) + 1;
189 366 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 206 160 366
190 206 SV **k = av_fetch(hav, i, 0);
191 206 SV **v = av_fetch(hav, i + 1, 0);
192 STRLEN kl, vl;
193 412 ▶ const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, "");
Branch: 2 branches
BranchTrueFalseTotal
const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); 206 0 206
const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); 206 0 206
194 412 ▶ const char *vs = (v && *v) ? SvPV_const(*v, vl) : (vl = 0, "");
Branch: 2 branches
BranchTrueFalseTotal
const char *vs = (v && *v) ? SvPV_const(*v, vl) : (vl = 0, ""); 206 0 206
const char *vs = (v && *v) ? SvPV_const(*v, vl) : (vl = 0, ""); 206 0 206
195 206 sv_catpvn(req, ks, kl); sv_catpvs(req, ": ");
196 206 sv_catpvn(req, vs, vl); sv_catpvs(req, "\r\n");
197 }
198 160 sv_catpvs(req, "\r\n");
199 160 ▶ if (body && SvOK(body)) sv_catsv(req, body);
Branch: 2 branches
BranchTrueFalseTotal
if (body && SvOK(body)) sv_catsv(req, body); 6 154 160
if (body && SvOK(body)) sv_catsv(req, body); 6 0 6
200 160 return req;
201 }
202  
203 /* The settle half of the v2 outbound observer. Attached with on_ready, so it
204 * runs however the hop ended - resolved, failed, or cancelled - and therefore
205 * exactly once per ft_obs_start. A timeout, a refused connection and a DNS
206 * failure all arrive here as a failure rather than as silence, which is the
207 * whole reason a client observer is worth having. The token travels as an IV
208 * in the closure because it is a C pointer the consumer owns and no SV should
209 * ever be holding it. */
210 XS_INTERNAL(ft_obs_done_cb_xs);
211 13 XS_INTERNAL(ft_obs_done_cb_xs) {
212 13 dXSARGS;
213 13 hm_clos *cl = hm_clos_of(aTHX_ cv);
214 ft_obs_tokens *t;
215 SV *f;
216 13 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 13 0 13
if (!cl || items < 1) XSRETURN_EMPTY; 0 13 13
217 13 t = INT2PTR(ft_obs_tokens *, cl->i);
218 13 f = ST(0);
219 13 ▶ if (!t) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) XSRETURN_EMPTY; 0 13 13
220 {
221 /* a settled future carries its value in the same place either way:
222 * the response when it resolved, the error when it did not */
223 13 AV *vals = hmf_values_av(aTHX_ f);
224 13 ▶ SV **vp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **vp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 13 0 13
SV **vp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 13 0 13
225 13 ▶ SV *v = (vp && *vp) ? *vp : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *v = (vp && *vp) ? *vp : NULL; 13 0 13
SV *v = (vp && *vp) ? *vp : NULL; 13 0 13
226 13 ▶ if (hmf_state(aTHX_ f) == HMF_DONE) ft_obs_done(aTHX_ t, v, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ f) == HMF_DONE) ft_obs_done(aTHX_ t, v, NULL); 10 3 13
227 3 else ft_obs_done(aTHX_ t, NULL, v);
228 }
229 13 XSRETURN_EMPTY;
230 }
231  
232 /* ---- response peeking (for redirects/cookies) --------------------------- */
233  
234 142 static int ft_response_status(pTHX_ SV *res) {
235 142 ▶ if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 142 0 142
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 142 0 142
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 142 0 142
236 142 SV **e = hv_fetchs((HV *)SvRV(res), "status", 0);
237 142 ▶ if (e && *e) return (int)SvIV(*e);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) return (int)SvIV(*e); 142 0 142
if (e && *e) return (int)SvIV(*e); 142 0 142
238 }
239 0 return 0;
240 }
241  
242 /* borrowed value SV of the first matching header, or NULL */
243 12 static SV *ft_response_header(pTHX_ SV *res, const char *name, STRLEN nl) {
244 12 ▶ if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 12 0 12
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 12 0 12
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 12 0 12
245 12 SV **hp = hv_fetchs((HV *)SvRV(res), "headers", 0);
246 12 ▶ if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 12 0 12
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 12 0 12
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 12 0 12
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 12 0 12
247 12 AV *av = (AV *)SvRV(*hp);
248 12 SSize_t idx = ft_hdr_find(aTHX_ av, name, nl);
249 12 ▶ if (idx >= 0) { SV **v = av_fetch(av, idx + 1, 0); return (v && *v) ? *v : NULL; }
Branch: 3 branches
BranchTrueFalseTotal
if (idx >= 0) { SV **v = av_fetch(av, idx + 1, 0); return (v && *v) ? *v : NULL; } 12 0 12
if (idx >= 0) { SV **v = av_fetch(av, idx + 1, 0); return (v && *v) ? *v : NULL; } 12 0 12
if (idx >= 0) { SV **v = av_fetch(av, idx + 1, 0); return (v && *v) ? *v : NULL; } 12 0 12
250 }
251 }
252 0 return NULL;
253 }
254  
255 /* ---- loop resolution + object construction ------------------------------ */
256  
257 34 static SV *ft_load_new(pTHX_ const char *class, SV *arg) {
258 SV *obj;
259 34 dSP;
260 int n;
261 34 load_module(PERL_LOADMOD_NOIMPORT, newSVpv(class, 0), NULL);
262 34 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 34 34
263 34 ▶ XPUSHs(sv_2mortal(newSVpv(class, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(class, 0))); 0 34 34
264 34 ▶ if (arg) XPUSHs(arg);
Branch: 2 branches
BranchTrueFalseTotal
if (arg) XPUSHs(arg); 0 34 34
if (arg) XPUSHs(arg); 0 0 0
265 34 PUTBACK;
266 34 n = call_method("new", G_SCALAR);
267 34 SPAGAIN;
268 34 ▶ obj = n ? SvREFCNT_inc(POPs) : newSV(0); /* survive the FREETMPS below */
Branch: 1 branch
BranchTrueFalseTotal
obj = n ? SvREFCNT_inc(POPs) : newSV(0); /* survive the FREETMPS below */ 34 0 34
269 34 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 34 0 34
270 34 return obj;
271 }
272  
273 /* The implicit loop - the one you get when you do not pass `loop` - is a
274 * per-process singleton. Every Fetch->new used to mint its own, so two agents
275 * in one program ran two loops that could not see each other's sockets, and
276 * since install_await writes a process global, whichever agent was built last
277 * owned the await path for both. Sharing one loop also lets independent agents
278 * multiplex: awaiting on one now drives the other's transfers rather than
279 * stalling them for the duration.
280 *
281 * Kept in a package global rather than a C static so that each interpreter
282 * gets its own under ithreads, and rebuilt after a fork - a child inheriting
283 * the parent's kqueue/epoll descriptor shares its readiness state, which is
284 * its own class of hang. */
285 47 static SV *ft_default_loop(pTHX) {
286 47 SV *slot = get_sv("Fetch::Loop::Standalone::DEFAULT", GV_ADD);
287 47 SV *pidv = get_sv("Fetch::Loop::Standalone::DEFAULT_PID", GV_ADD);
288 47 IV pid = (IV)PerlProc_getpid();
289 SV *l;
290  
291 47 ▶ if (SvROK(slot) && SvIOK(pidv) && SvIV(pidv) == pid)
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(slot) && SvIOK(pidv) && SvIV(pidv) == pid) 20 27 47
if (SvROK(slot) && SvIOK(pidv) && SvIV(pidv) == pid) 20 0 20
if (SvROK(slot) && SvIOK(pidv) && SvIV(pidv) == pid) 19 1 20
292 19 return SvREFCNT_inc(slot);
293  
294 28 l = ft_load_new(aTHX_ "Fetch::Loop::Standalone", NULL);
295 28 sv_setsv(slot, l); /* the global holds it for the life of the process */
296 28 sv_setiv(pidv, pid);
297 28 return l; /* owned reference for the caller */
298 }
299  
300 /* Turn whatever was passed as `loop` into a Fetch::Loop adapter (see
301 * Fetch::_resolve_loop). Returns an owned reference. */
302 56 static SV *ft_resolve_loop(pTHX_ SV *loop) {
303 56 ▶ if (!loop || !SvOK(loop))
Branch: 2 branches
BranchTrueFalseTotal
if (!loop || !SvOK(loop)) 9 47 56
if (!loop || !SvOK(loop)) 0 9 9
304 47 return ft_default_loop(aTHX);
305 9 ▶ if (sv_isobject(loop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(loop)) { 9 0 9
306 9 ▶ if (sv_derived_from(loop, "Fetch::Loop::Standalone")
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(loop, "Fetch::Loop::Standalone") 0 9 9
307 0 ▶ || sv_derived_from(loop, "Fetch::Loop"))
Branch: 1 branch
BranchTrueFalseTotal
|| sv_derived_from(loop, "Fetch::Loop")) 0 0 0
308 9 return SvREFCNT_inc(loop);
309 0 ▶ if (sv_derived_from(loop, "IO::Async::Loop"))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(loop, "IO::Async::Loop")) 0 0 0
310 0 return ft_load_new(aTHX_ "Fetch::Loop::IOAsync", loop);
311 0 ▶ if (sv_derived_from(loop, "Hyperman::Loop"))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(loop, "Hyperman::Loop")) 0 0 0
312 0 return ft_load_new(aTHX_ "Fetch::Loop::Hyperman", loop);
313 0 ▶ if (ft_obj_can(aTHX_ loop, "_ft_arm"))
Branch: 1 branch
BranchTrueFalseTotal
if (ft_obj_can(aTHX_ loop, "_ft_arm")) 0 0 0
314 0 return SvREFCNT_inc(loop);
315 0 croak("Fetch: don't know how to drive loop '%s'",
316 sv_reftype(SvRV(loop), 1));
317 }
318 0 ▶ if (SvPOK(loop) && strEQ(SvPV_nolen(loop), "AnyEvent"))
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(loop) && strEQ(SvPV_nolen(loop), "AnyEvent")) 0 0 0
if (SvPOK(loop) && strEQ(SvPV_nolen(loop), "AnyEvent")) 0 0 0
319 0 return ft_load_new(aTHX_ "Fetch::Loop::AnyEvent", NULL);
320 0 croak("Fetch: unrecognised loop '%s'", SvPV_nolen(loop));
321 }
322  
323 /* forward declarations for the redirect chain */
324 static SV *ft_request_once(pTHX_ SV *self_sv, ft_ua *ua, const char *method,
325 const char *url, HV *opt);
326 static SV *ft_follow(pTHX_ SV *self_sv, ft_ua *ua, const char *method,
327 const char *url, HV *opt, IV left);
328  
329 /* ---- cookie-extract continuation (attached like Perl's on_done) --------- */
330  
331 XS_INTERNAL(ft_extract_cb);
332 9 XS_INTERNAL(ft_extract_cb) {
333 9 dXSARGS;
334 9 hm_clos *cl = hm_clos_of(aTHX_ cv);
335 SV *f;
336 9 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 9 0 9
if (!cl || items < 1) XSRETURN_EMPTY; 0 9 9
337 9 f = ST(0);
338 9 ▶ if (hmf_state(aTHX_ f) == HMF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ f) == HMF_DONE) { 9 0 9
339 9 AV *vals = hmf_values_av(aTHX_ f);
340 9 ▶ SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 9 0 9
SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 9 0 9
341 9 ▶ SV *res = rp ? *rp : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *res = rp ? *rp : NULL; 9 0 9
342 9 ▶ if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 9 0 9
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 9 0 9
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 9 0 9
343 9 ft_jar *j = ft_jar_of(aTHX_ cl->a);
344 9 const char *host = SvPV_nolen(cl->b);
345 9 ▶ const char *path = SvPV_nolen(cl->c);
Branch: 1 branch
BranchTrueFalseTotal
const char *path = SvPV_nolen(cl->c); 9 0 9
346 9 SV **hp = hv_fetchs((HV *)SvRV(res), "headers", 0);
347 9 ▶ if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 9 0 9
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 9 0 9
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 9 0 9
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 9 0 9
348 9 AV *av = (AV *)SvRV(*hp);
349 9 SSize_t n = av_len(av) + 1, i;
350 40 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 31 9 40
351 31 SV **k = av_fetch(av, i, 0);
352 31 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 31 0 31
if (k && *k) { 31 0 31
353 STRLEN kl;
354 31 const char *ks = SvPV_const(*k, kl);
355 31 ▶ if (ft_ci_eq(ks, kl, "set-cookie", 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ft_ci_eq(ks, kl, "set-cookie", 10)) { 6 25 31
356 6 SV **v = av_fetch(av, i + 1, 0);
357 6 ▶ if (v && *v) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) { 6 0 6
if (v && *v) { 6 0 6
358 STRLEN vl;
359 6 const char *vs = SvPV_const(*v, vl);
360 6 ▶ if (vl) ft_jar_set_cookie(j, vs, host, path);
Branch: 1 branch
BranchTrueFalseTotal
if (vl) ft_jar_set_cookie(j, vs, host, path); 6 0 6
361 }
362 }
363 }
364 }
365 }
366 }
367 }
368 9 XSRETURN_EMPTY;
369 }
370  
371 /* ---- one request (no redirect) ------------------------------------------ */
372  
373 159 static SV *ft_request_once(pTHX_ SV *self_sv, ft_ua *ua, const char *method,
374 const char *url, HV *opt) {
375 ft_url u;
376 int tls, verify;
377 double timeout;
378 AV *hav, *h2;
379 char *authority;
380 char portbuf[16];
381 SV *req, *hdrs2_rv, *m_sv, *sc_sv, *au_sv, *pa_sv, *f;
382 159 SV *body = NULL, *on_body = NULL, *on_headers = NULL, *json_body = NULL;
383 159 ft_loop *l = NULL; SV *lsv = NULL;
384 ft_pool *pl;
385 159 ft_obs_tokens *obs = NULL;
386 SSize_t n, i;
387  
388 159 ▶ if (!ft_parse_url(url, &u))
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_parse_url(url, &u)) 0 159 159
389 0 croak("Fetch: cannot parse URL '%s'", url);
390 159 ▶ if (strcmp(u.scheme, "http") != 0 && strcmp(u.scheme, "https") != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(u.scheme, "http") != 0 && strcmp(u.scheme, "https") != 0) { 7 152 159
if (strcmp(u.scheme, "http") != 0 && strcmp(u.scheme, "https") != 0) { 0 7 7
391 0 SV *fu = hmf_new(aTHX_ "Fetch::Future");
392 0 SV *e = sv_2mortal(newSVpvf("Fetch: unsupported scheme '%s'\n", u.scheme));
393 0 hmf_settle(aTHX_ fu, HMF_FAILED, &e, 1);
394 0 ft_url_free(&u);
395 0 return fu;
396 }
397 159 tls = (strcmp(u.scheme, "https") == 0);
398 159 verify = ua->tls_verify;
399 159 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 159 0 159
400 159 SV **tv = hv_fetchs(opt, "tls_verify", 0);
401 159 ▶ if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; 7 152 159
if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; 7 0 7
402 }
403  
404 /* merged headers: defaults, overlaid with this request's headers */
405 159 hav = newAV();
406 159 sv_2mortal((SV *)hav);
407 159 ft_hdr_pairs_into(aTHX_ hav, ua->headers);
408 159 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 159 0 159
409 159 SV **oh = hv_fetchs(opt, "headers", 0);
410 159 ▶ if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh);
Branch: 2 branches
BranchTrueFalseTotal
if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); 7 152 159
if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); 7 0 7
411 }
412 /* Redirect crossing origin: drop credential-bearing headers so a token or
413 * explicit cookie set for the original host is never sent to another one
414 * (jar cookies are re-scoped to u.host below, so they stay safe).
415 * _redirect_from carries the previous hop's URL; absent on the first hop. */
416 159 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 159 0 159
417 159 SV **rf = hv_fetchs(opt, "_redirect_from", 0);
418 159 ▶ if (rf && *rf && SvOK(*rf)
Branch: 3 branches
BranchTrueFalseTotal
if (rf && *rf && SvOK(*rf) 12 147 159
if (rf && *rf && SvOK(*rf) 12 0 12
if (rf && *rf && SvOK(*rf) 12 0 12
419 24 ▶ && !ft_same_origin(SvPV_nolen(*rf), url)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !ft_same_origin(SvPV_nolen(*rf), url)) { 1 11 12
420 1 AV *keep = newAV();
421 1 SSize_t hn = av_len(hav) + 1, j;
422 1 sv_2mortal((SV *)keep);
423 3 ▶ for (j = 0; j + 1 < hn; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < hn; j += 2) { 2 1 3
424 2 SV **k = av_fetch(hav, j, 0);
425 2 SV **v = av_fetch(hav, j + 1, 0);
426 4 ▶ STRLEN kl; const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, "");
Branch: 2 branches
BranchTrueFalseTotal
STRLEN kl; const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); 2 0 2
STRLEN kl; const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); 2 0 2
427 2 ▶ if ((kl == 13 && strncasecmp(ks, "Authorization", 13) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if ((kl == 13 && strncasecmp(ks, "Authorization", 13) == 0) 1 1 2
if ((kl == 13 && strncasecmp(ks, "Authorization", 13) == 0) 0 1 1
428 1 ▶ || (kl == 6 && strncasecmp(ks, "Cookie", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
|| (kl == 6 && strncasecmp(ks, "Cookie", 6) == 0) 1 0 1
|| (kl == 6 && strncasecmp(ks, "Cookie", 6) == 0) 0 1 1
429 0 ▶ || (kl == 19 && strncasecmp(ks, "Proxy-Authorization", 19) == 0))
Branch: 2 branches
BranchTrueFalseTotal
|| (kl == 19 && strncasecmp(ks, "Proxy-Authorization", 19) == 0)) 0 0 0
|| (kl == 19 && strncasecmp(ks, "Proxy-Authorization", 19) == 0)) 0 0 0
430 2 continue; /* strip on the cross-origin hop */
431 0 ▶ av_push(keep, k && *k ? newSVsv(*k) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(keep, k && *k ? newSVsv(*k) : newSV(0)); 0 0 0
av_push(keep, k && *k ? newSVsv(*k) : newSV(0)); 0 0 0
432 0 ▶ av_push(keep, v && *v ? newSVsv(*v) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(keep, v && *v ? newSVsv(*v) : newSV(0)); 0 0 0
av_push(keep, v && *v ? newSVsv(*v) : newSV(0)); 0 0 0
433 }
434 1 hav = keep;
435 }
436 }
437 159 ▶ if (!ft_hdr_exists(aTHX_ hav, "User-Agent", 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_hdr_exists(aTHX_ hav, "User-Agent", 10)) { 159 0 159
438 159 av_push(hav, newSVpvs("User-Agent"));
439 159 av_push(hav, newSVsv(ua->agent));
440 }
441 159 ▶ if (ua->cookie_jar && SvOK(ua->cookie_jar)
Branch: 2 branches
BranchTrueFalseTotal
if (ua->cookie_jar && SvOK(ua->cookie_jar) 9 150 159
if (ua->cookie_jar && SvOK(ua->cookie_jar) 9 0 9
442 9 ▶ && !ft_hdr_exists(aTHX_ hav, "Cookie", 6)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !ft_hdr_exists(aTHX_ hav, "Cookie", 6)) { 9 0 9
443 9 char *ck = ft_jar_cookie_str(ft_jar_of(aTHX_ ua->cookie_jar),
444 9 u.host, u.path, tls);
445 9 ▶ if (ck && *ck) {
Branch: 2 branches
BranchTrueFalseTotal
if (ck && *ck) { 6 3 9
if (ck && *ck) { 6 0 6
446 6 av_push(hav, newSVpvs("Cookie"));
447 6 av_push(hav, newSVpv(ck, 0));
448 }
449 9 free(ck);
450 }
451  
452 /* json => $data: encode to the body and default Content-Type. Done before
453 * the h2 list so the header is carried on either protocol. */
454 159 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 159 0 159
455 159 SV **jp = hv_fetchs(opt, "json", 0);
456 159 ▶ if (jp && *jp && SvOK(*jp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (jp && *jp && SvOK(*jp)) { 1 158 159
if (jp && *jp && SvOK(*jp)) { 1 0 1
if (jp && *jp && SvOK(*jp)) { 1 0 1
457 1 json_body = sv_2mortal(ft_json_encode(aTHX_ *jp));
458 1 ▶ if (!ft_hdr_exists(aTHX_ hav, "Content-Type", 12)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_hdr_exists(aTHX_ hav, "Content-Type", 12)) { 1 0 1
459 1 av_push(hav, newSVpvs("Content-Type"));
460 1 av_push(hav, newSVpvs("application/json"));
461 }
462 }
463 }
464  
465 /* v2 outbound observers, here because it is the last moment the header
466 * list is still one list: after this the h2 pass copies it, so a header
467 * added later would ride HTTP/1.1 and vanish over h2. Once per hop, which
468 * is what a redirect chain should look like to anything measuring it. */
469 159 obs = ft_obs_start(aTHX_ method, strlen(method), url, strlen(url), hav);
470  
471 /* The h2 header list is used only if ALPN picks h2, or Alt-Svc moves the
472 * request to h3, and both need TLS, so for cleartext this second pass
473 * over the headers is skipped.
474 *
475 * HTTP/2 and HTTP/3 impose rules on the field list that HTTP/1.1 does
476 * not (RFC 9113 8.2, RFC 9114 4.2), and a server must reset a request
477 * that breaks one. A header that is harmless over HTTP/1.1 can thus kill
478 * the same request over h2, so the list is adjusted rather than copied:
479 *
480 * names are lowercased, an uppercase letter being malformed there
481 * the names ft_h2_omits_header lists are left out
482 * te is sent as "te: trailers", its one permitted value, when the
483 * caller's TE listed trailers, and is left out otherwise. gRPC
484 * needs this, since its status comes back as a trailer
485 * a name starting with ':' is left out. The pseudo-headers are
486 * Fetch's to write, and one in the regular list is malformed */
487 159 h2 = NULL;
488 159 ▶ if (tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (tls) { 7 152 159
489 7 h2 = newAV();
490 7 n = av_len(hav) + 1;
491 14 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 7 7 14
492 7 SV **k = av_fetch(hav, i, 0);
493 7 SV **v = av_fetch(hav, i + 1, 0);
494 STRLEN kl, vl;
495 14 ▶ const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, "");
Branch: 2 branches
BranchTrueFalseTotal
const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); 7 0 7
const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); 7 0 7
496 7 ▶ if (!kl || ks[0] == ':' || ft_h2_omits_header(ks, kl)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!kl || ks[0] == ':' || ft_h2_omits_header(ks, kl)) continue; 7 0 7
if (!kl || ks[0] == ':' || ft_h2_omits_header(ks, kl)) continue; 7 0 7
if (!kl || ks[0] == ':' || ft_h2_omits_header(ks, kl)) continue; 0 7 7
497 7 ▶ if (ft_ci_eq(ks, kl, "te", 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ft_ci_eq(ks, kl, "te", 2)) { 0 7 7
498 0 ▶ const char *vs = (v && *v && SvOK(*v)) ? SvPV_const(*v, vl) : (vl = 0, "");
Branch: 3 branches
BranchTrueFalseTotal
const char *vs = (v && *v && SvOK(*v)) ? SvPV_const(*v, vl) : (vl = 0, ""); 0 0 0
const char *vs = (v && *v && SvOK(*v)) ? SvPV_const(*v, vl) : (vl = 0, ""); 0 0 0
const char *vs = (v && *v && SvOK(*v)) ? SvPV_const(*v, vl) : (vl = 0, ""); 0 0 0
499 0 ▶ if (ft_te_lists_trailers(vs, vl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ft_te_lists_trailers(vs, vl)) { 0 0 0
500 0 av_push(h2, newSVpvs("te"));
501 0 av_push(h2, newSVpvs("trailers"));
502 }
503 0 continue;
504 }
505 {
506 7 char *lc = ft_lc_dup(ks, kl);
507 7 av_push(h2, newSVpvn(lc, kl));
508 7 free(lc);
509 7 ▶ av_push(h2, (v && *v) ? newSVsv(*v) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(h2, (v && *v) ? newSVsv(*v) : newSV(0)); 7 0 7
av_push(h2, (v && *v) ? newSVsv(*v) : newSV(0)); 7 0 7
510 }
511 }
512 }
513  
514 159 timeout = ua->timeout;
515 159 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 159 0 159
516 159 SV **to = hv_fetchs(opt, "timeout", 0);
517 159 ▶ if (to && *to) timeout = SvNV(*to);
Branch: 2 branches
BranchTrueFalseTotal
if (to && *to) timeout = SvNV(*to); 4 155 159
if (to && *to) timeout = SvNV(*to); 4 0 4
518 159 ▶ body = hv_fetchs(opt, "body", 0) ? *hv_fetchs(opt, "body", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
body = hv_fetchs(opt, "body", 0) ? *hv_fetchs(opt, "body", 0) : NULL; 5 154 159
519 159 ▶ on_body = hv_fetchs(opt, "on_body", 0) ? *hv_fetchs(opt, "on_body", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
on_body = hv_fetchs(opt, "on_body", 0) ? *hv_fetchs(opt, "on_body", 0) : NULL; 3 156 159
520 159 ▶ on_headers = hv_fetchs(opt, "on_headers", 0) ? *hv_fetchs(opt, "on_headers", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
on_headers = hv_fetchs(opt, "on_headers", 0) ? *hv_fetchs(opt, "on_headers", 0) : NULL; 0 159 159
521 159 ▶ if (body && !SvOK(body)) body = NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (body && !SvOK(body)) body = NULL; 5 154 159
if (body && !SvOK(body)) body = NULL; 0 5 5
522 }
523 159 ▶ if (json_body) body = json_body; /* json => wins over an explicit body */
Branch: 1 branch
BranchTrueFalseTotal
if (json_body) body = json_body; /* json => wins over an explicit body */ 1 158 159
524  
525 159 req = sv_2mortal(ft_build_request(aTHX_ ua, method, u.host, u.port, u.path,
526 hav, body));
527 159 snprintf(portbuf, sizeof portbuf, "%d", u.port);
528  
529 159 ▶ if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone"))
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone")) 159 0 159
if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone")) 159 0 159
530 159 l = ft_loop_from_sv(aTHX_ ua->loop);
531 else
532 0 lsv = ua->loop;
533 159 ▶ pl = ua->pool ? ft_pool_from_sv(aTHX_ ua->pool) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
pl = ua->pool ? ft_pool_from_sv(aTHX_ ua->pool) : NULL; 154 5 159
534  
535 /* the pseudo-header pieces (method/scheme/:authority/path) and the h2 nva
536 * are h2-only too; pass undef for cleartext so ft_h1_start stores nothing. */
537 159 authority = NULL;
538 159 ▶ if (tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (tls) { 7 152 159
539 7 authority = ft_authority(u.host, u.port, tls);
540 7 m_sv = sv_2mortal(newSVpv(method, 0));
541 7 sc_sv = sv_2mortal(newSVpv(u.scheme, 0));
542 7 au_sv = sv_2mortal(newSVpv(authority, 0));
543 7 pa_sv = sv_2mortal(newSVpv(u.path, 0));
544 7 hdrs2_rv = sv_2mortal(newRV_noinc((SV *)h2));
545 } else {
546 152 m_sv = sc_sv = au_sv = pa_sv = hdrs2_rv = &PL_sv_undef;
547 }
548  
549 {
550 STRLEN rl;
551 159 const char *rb = SvPV_const(req, rl);
552 159 ft_conn_simple_next = ua->simple_response; /* inherited by a fresh conn */
553 159 ft_conn_hm_next = ua->hm;
554 159 ft_conn_hm_loop_next = ua->hm_loop;
555 159 ft_conn_on_headers_next =
556 159 ▶ (on_headers && SvROK(on_headers)) ? on_headers : NULL;
Branch: 2 branches
BranchTrueFalseTotal
(on_headers && SvROK(on_headers)) ? on_headers : NULL; 0 159 159
(on_headers && SvROK(on_headers)) ? on_headers : NULL; 0 0 0
557 /* HTTP/3, when this origin has said it speaks it.
558 *
559 * There is no h3:// scheme, so nothing about the URL can ask for
560 * this: the only way to know is an Alt-Svc header on an earlier
561 * response over TCP (ft_altsvc.h). A cache miss is the ordinary case
562 * for a first request and takes the TCP path below, which is also
563 * what records the header for next time.
564 *
565 * The h3 attempt is not a commitment. If it fails - the libraries are
566 * not built in, the loop has no timer fine enough for loss detection,
567 * the UDP port is blocked, which is common enough on real networks to
568 * be the reason Alt-Svc is advisory - the future comes back failed
569 * rather than throwing, and the request falls back to TCP with
570 * nothing lost but the attempt. */
571 159 f = NULL;
572 if (tls && FT_H3_AVAILABLE) {
573 char origin[300];
574 ft_altsvc *alt;
575 snprintf(origin, sizeof origin, "%s:%d", u.host, u.port);
576 alt = ft_altsvc_get(origin);
577 if (alt) {
578 char h3port[16];
579 SV *hf;
580 snprintf(h3port, sizeof h3port, "%d", alt->port);
581 hf = ft_h3_start(aTHX_ l, lsv, pl, alt->host, h3port, verify,
582 timeout, m_sv, sc_sv, au_sv, pa_sv, hdrs2_rv,
583 body ? body : &PL_sv_undef,
584 on_body ? on_body : &PL_sv_undef);
585 if (hmf_state(aTHX_ hf) == HMF_FAILED) {
586 /* it could not even be started; forget the advertisement
587 * so the next request does not pay for this again */
588 ft_altsvc_forget(origin);
589 SvREFCNT_dec(hf);
590 } else {
591 f = hf;
592 }
593 }
594 }
595 159 ▶ if (!f)
Branch: 1 branch
BranchTrueFalseTotal
if (!f) 159 0 159
596 159 ▶ f = ft_h1_start(aTHX_ l, lsv, pl, u.host, portbuf, rb, rl, tls, verify,
Branch: 2 branches
BranchTrueFalseTotal
f = ft_h1_start(aTHX_ l, lsv, pl, u.host, portbuf, rb, rl, tls, verify, 3 156 159
f = ft_h1_start(aTHX_ l, lsv, pl, u.host, portbuf, rb, rl, tls, verify, 6 153 159
597 timeout, m_sv, sc_sv, au_sv, pa_sv, hdrs2_rv,
598 body ? body : &PL_sv_undef,
599 on_body ? on_body : &PL_sv_undef, NULL);
600 159 ft_conn_on_headers_next = NULL; /* borrowed; do not keep past the call */
601 159 hmf_pin_loop(aTHX_ f, ua->loop); /* only this loop can resolve it */
602 }
603  
604 159 ▶ if (obs) { /* settle the observers when this hop ends, however it ends */
Branch: 1 branch
BranchTrueFalseTotal
if (obs) { /* settle the observers when this hop ends, however it ends */ 13 146 159
605 13 SV *cb = hm_closure(aTHX_ ft_obs_done_cb_xs, NULL, NULL, NULL, NULL,
606 PTR2IV(obs), 0);
607 13 hmf_on_ready(aTHX_ f, cb);
608 13 SvREFCNT_dec(cb);
609 }
610  
611 /* store any Set-Cookie before the caller/redirect-follower runs */
612 159 ▶ if (ua->cookie_jar && SvOK(ua->cookie_jar)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ua->cookie_jar && SvOK(ua->cookie_jar)) { 9 150 159
if (ua->cookie_jar && SvOK(ua->cookie_jar)) { 9 0 9
613 9 SV *hostsv = newSVpv(u.host, 0);
614 9 SV *pathsv = newSVpv(u.path, 0);
615 9 SV *cb = hm_closure(aTHX_ ft_extract_cb, ua->cookie_jar, hostsv, pathsv,
616 NULL, 0, 0);
617 9 SvREFCNT_dec(hostsv);
618 9 SvREFCNT_dec(pathsv);
619 9 hmf_on_ready(aTHX_ f, cb);
620 9 SvREFCNT_dec(cb);
621 }
622  
623 159 free(authority);
624 159 ft_url_free(&u);
625 159 return f;
626 }
627  
628 /* ---- redirect continuation ---------------------------------------------- */
629  
630 XS_INTERNAL(ft_redirect_cb);
631 157 XS_INTERNAL(ft_redirect_cb) {
632 157 dXSARGS;
633 157 hm_clos *cl = hm_clos_of(aTHX_ cv);
634 SV *f, *next, *self_sv;
635 AV *pack;
636 IV left, st;
637 const char *method, *url;
638 157 HV *opt = NULL;
639 157 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 157 0 157
if (!cl || items < 1) XSRETURN_EMPTY; 0 157 157
640 157 f = ST(0);
641 157 next = cl->a;
642 157 self_sv = cl->b;
643 157 pack = (AV *)SvRV(cl->c);
644 157 left = cl->i;
645 157 method = SvPV_nolen(*av_fetch(pack, 0, 0));
646 157 ▶ url = SvPV_nolen(*av_fetch(pack, 1, 0));
Branch: 1 branch
BranchTrueFalseTotal
url = SvPV_nolen(*av_fetch(pack, 1, 0)); 157 0 157
647 {
648 157 SV **op = av_fetch(pack, 2, 0);
649 157 ▶ if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) 157 0 157
if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) 157 0 157
if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) 157 0 157
if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) 157 0 157
650 157 opt = (HV *)SvRV(*op);
651 }
652  
653 157 st = hmf_state(aTHX_ f);
654 157 ▶ if (st != HMF_DONE) { /* propagate failure/cancel */
Branch: 1 branch
BranchTrueFalseTotal
if (st != HMF_DONE) { /* propagate failure/cancel */ 15 142 157
655 15 ▶ if (st == HMF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_FAILED) { 15 0 15
656 15 AV *vals = hmf_values_av(aTHX_ f);
657 15 ▶ SSize_t n = vals ? av_len(vals) + 1 : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = vals ? av_len(vals) + 1 : 0, i; 15 0 15
658 15 AV *out = newAV();
659 30 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 15 30
660 15 SV **e = av_fetch(vals, i, 0);
661 15 ▶ av_push(out, e ? newSVsv(*e) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(out, e ? newSVsv(*e) : newSV(0)); 15 0 15
662 }
663 15 hmf_settle_av(aTHX_ next, HMF_FAILED, out);
664 } else {
665 0 hmf_cancel(aTHX_ next);
666 }
667 15 XSRETURN_EMPTY;
668 }
669  
670 {
671 142 AV *vals = hmf_values_av(aTHX_ f);
672 142 ▶ SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 142 0 142
SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 142 0 142
673 142 ▶ SV *res = rp ? *rp : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *res = rp ? *rp : &PL_sv_undef; 142 0 142
674 142 int status = ft_response_status(aTHX_ res);
675 13 ▶ SV *loc = (status >= 300 && status < 400)
Branch: 1 branch
BranchTrueFalseTotal
SV *loc = (status >= 300 && status < 400) 12 1 13
676 155 ▶ ? ft_response_header(aTHX_ res, "location", 8) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? ft_response_header(aTHX_ res, "location", 8) : NULL; 13 129 142
677  
678 142 ▶ if (!(loc && SvOK(loc) && SvCUR(loc) > 0)) { /* not a redirect */
Branch: 3 branches
BranchTrueFalseTotal
if (!(loc && SvOK(loc) && SvCUR(loc) > 0)) { /* not a redirect */ 12 130 142
if (!(loc && SvOK(loc) && SvCUR(loc) > 0)) { /* not a redirect */ 12 0 12
if (!(loc && SvOK(loc) && SvCUR(loc) > 0)) { /* not a redirect */ 0 12 12
679 130 SV *cp = newSVsv(res);
680 130 hmf_settle(aTHX_ next, HMF_DONE, &cp, 1);
681 130 SvREFCNT_dec(cp);
682 130 XSRETURN_EMPTY;
683 }
684 {
685 int drop_body;
686 12 const char *m2 = ft_redirect_method(method, status, &drop_body);
687 12 SV *u2 = ft_resolve_location(aTHX_ url, loc);
688 12 HV *o2 = newHV();
689 SV *child, *cb2;
690 12 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 12 0 12
691 HE *he;
692 12 hv_iterinit(opt);
693 22 ▶ while ((he = hv_iternext(opt))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opt))) { 10 12 22
694 I32 kl;
695 10 char *k = hv_iterkey(he, &kl);
696 10 (void)hv_store(o2, k, kl, newSVsv(hv_iterval(opt, he)), 0);
697 }
698 }
699 12 ▶ if (drop_body) (void)hv_delete(o2, "body", 4, G_DISCARD);
Branch: 1 branch
BranchTrueFalseTotal
if (drop_body) (void)hv_delete(o2, "body", 4, G_DISCARD); 1 11 12
700 /* record the origin we are leaving so ft_request_once can strip
701 * credential headers if this hop crosses to a different origin */
702 12 (void)hv_store(o2, "_redirect_from", 14, newSVpv(url, 0), 0);
703  
704 24 child = ft_follow(aTHX_ self_sv, ft_ua_of(aTHX_ self_sv),
705 12 m2, SvPV_nolen(u2), o2, left - 1);
706 12 cb2 = hm_closure(aTHX_ hm_xs_chain_cb, next, NULL, NULL, NULL, 0, 0);
707 12 hm_any_on_ready(aTHX_ child, cb2);
708 12 SvREFCNT_dec(cb2);
709 12 SvREFCNT_dec(child);
710 12 SvREFCNT_dec(u2);
711 12 SvREFCNT_dec((SV *)o2);
712 }
713 }
714 12 XSRETURN_EMPTY;
715 }
716  
717 /* ---- follow: request, then chain a redirect if there are hops left ------ */
718  
719 159 static SV *ft_follow(pTHX_ SV *self_sv, ft_ua *ua, const char *method,
720 const char *url, HV *opt, IV left) {
721 159 SV *f = ft_request_once(aTHX_ self_sv, ua, method, url, opt);
722 159 ▶ if (left <= 0) return f;
Branch: 1 branch
BranchTrueFalseTotal
if (left <= 0) return f; 2 157 159
723 {
724 157 SV *next = hmf_new(aTHX_ hmf_class_of(aTHX_ f));
725 157 AV *pack = newAV();
726 SV *pack_rv, *cb;
727 157 av_push(pack, newSVpv(method, 0));
728 157 av_push(pack, newSVpv(url, 0));
729 157 ▶ av_push(pack, opt ? newRV_inc((SV *)opt) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(pack, opt ? newRV_inc((SV *)opt) : newSV(0)); 157 0 157
730 157 pack_rv = newRV_noinc((SV *)pack);
731 157 hmf_set_upstream(aTHX_ next, f);
732 157 cb = hm_closure(aTHX_ ft_redirect_cb, next, self_sv, pack_rv, NULL,
733 left, 0);
734 157 SvREFCNT_dec(pack_rv); /* hm_closure took its own ref */
735 157 hmf_on_ready(aTHX_ f, cb);
736 157 SvREFCNT_dec(cb);
737 157 SvREFCNT_dec(f); /* the connection keeps f alive */
738 157 return next;
739 }
740 }
741  
742 /* ---- verb dispatch ------------------------------------------------------ */
743  
744 147 static SV *ft_dispatch(pTHX_ SV *self_sv, const char *method, const char *url,
745 SV **opt_args, int nopt) {
746 147 HV *opt = newHV();
747 147 ft_ua *ua = ft_ua_of(aTHX_ self_sv);
748 IV max;
749 int i;
750 SV *f;
751 173 ▶ for (i = 0; i + 1 < nopt; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < nopt; i += 2) { 26 147 173
752 STRLEN kl;
753 26 const char *k = SvPV_const(opt_args[i], kl);
754 26 (void)hv_store(opt, k, (I32)kl, newSVsv(opt_args[i + 1]), 0);
755 }
756 147 ▶ if (hv_exists(opt, "max_redirects", 13)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(opt, "max_redirects", 13)) { 2 145 147
757 2 SV **m = hv_fetchs(opt, "max_redirects", 0);
758 2 ▶ max = (m && *m && SvOK(*m)) ? SvIV(*m) : 0;
Branch: 3 branches
BranchTrueFalseTotal
max = (m && *m && SvOK(*m)) ? SvIV(*m) : 0; 2 0 2
max = (m && *m && SvOK(*m)) ? SvIV(*m) : 0; 2 0 2
max = (m && *m && SvOK(*m)) ? SvIV(*m) : 0; 2 0 2
759 } else {
760 145 max = ua->max_redirects;
761 }
762 147 ▶ if (max < 0) max = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (max < 0) max = 0; 0 147 147
763 147 f = ft_follow(aTHX_ self_sv, ua, method, url, opt, max);
764 147 SvREFCNT_dec((SV *)opt);
765 147 return f;
766 }
767  
768 /* ---- WebSocket ---------------------------------------------------------- */
769  
770 /* the ft_conn behind a Fetch::WebSocket (a blessed IV over the connection) */
771 13 static ft_conn *ft_ws_of(pTHX_ SV *sv) {
772 13 ▶ if (!(SvROK(sv) && SvIOK(SvRV(sv))))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(sv) && SvIOK(SvRV(sv)))) 13 0 13
if (!(SvROK(sv) && SvIOK(SvRV(sv)))) 0 13 13
773 0 croak("Fetch::WebSocket: not a websocket");
774 13 return INT2PTR(ft_conn *, SvIV(SvRV(sv)));
775 }
776  
777 /* Open a WebSocket: send the HTTP/1.1 Upgrade handshake with a fresh key and
778 * return a Future resolving to a Fetch::WebSocket once the 101 is verified.
779 * Accepts ws:// wss:// http:// https:// (ws/http cleartext, wss/https TLS). */
780 1 static SV *ft_websocket(pTHX_ ft_ua *ua, const char *url, HV *opt) {
781 ft_url u;
782 int tls, verify;
783 double timeout;
784 AV *hav;
785 char portbuf[16], wskey[25];
786 char *authority;
787 SV *req, *m_sv, *sc_sv, *au_sv, *pa_sv, *empty_rv, *f;
788 1 ft_loop *l = NULL; SV *lsv = NULL;
789  
790 1 ▶ if (!ft_parse_url(url, &u))
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_parse_url(url, &u)) 0 1 1
791 0 croak("Fetch: cannot parse URL '%s'", url);
792 1 ▶ tls = (strcmp(u.scheme, "wss") == 0 || strcmp(u.scheme, "https") == 0);
Branch: 2 branches
BranchTrueFalseTotal
tls = (strcmp(u.scheme, "wss") == 0 || strcmp(u.scheme, "https") == 0); 1 0 1
tls = (strcmp(u.scheme, "wss") == 0 || strcmp(u.scheme, "https") == 0); 0 1 1
793 1 ▶ if (strcmp(u.scheme, "ws") && strcmp(u.scheme, "wss")
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(u.scheme, "ws") && strcmp(u.scheme, "wss") 0 1 1
if (strcmp(u.scheme, "ws") && strcmp(u.scheme, "wss") 0 0 0
794 0 ▶ && strcmp(u.scheme, "http") && strcmp(u.scheme, "https")) {
Branch: 2 branches
BranchTrueFalseTotal
&& strcmp(u.scheme, "http") && strcmp(u.scheme, "https")) { 0 0 0
&& strcmp(u.scheme, "http") && strcmp(u.scheme, "https")) { 0 0 0
795 0 SV *fu = hmf_new(aTHX_ "Fetch::Future");
796 0 SV *e = sv_2mortal(newSVpvf("Fetch: not a websocket URL scheme '%s'\n", u.scheme));
797 0 hmf_settle(aTHX_ fu, HMF_FAILED, &e, 1);
798 0 ft_url_free(&u);
799 0 return fu;
800 }
801 1 ▶ if (tls && u.port == 80) u.port = 443; /* default wss/https port */
Branch: 2 branches
BranchTrueFalseTotal
if (tls && u.port == 80) u.port = 443; /* default wss/https port */ 0 1 1
if (tls && u.port == 80) u.port = 443; /* default wss/https port */ 0 0 0
802  
803 1 verify = ua->tls_verify;
804 1 timeout = ua->timeout;
805 1 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 1 0 1
806 1 SV **tv = hv_fetchs(opt, "tls_verify", 0);
807 1 SV **to = hv_fetchs(opt, "timeout", 0);
808 1 ▶ if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; 0 1 1
if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; 0 0 0
809 1 ▶ if (to && *to) timeout = SvNV(*to);
Branch: 2 branches
BranchTrueFalseTotal
if (to && *to) timeout = SvNV(*to); 0 1 1
if (to && *to) timeout = SvNV(*to); 0 0 0
810 }
811  
812 1 ft_ws_genkey(wskey);
813  
814 1 hav = newAV();
815 1 sv_2mortal((SV *)hav);
816 1 ft_hdr_pairs_into(aTHX_ hav, ua->headers);
817 1 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 1 0 1
818 1 SV **oh = hv_fetchs(opt, "headers", 0);
819 1 ▶ if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh);
Branch: 2 branches
BranchTrueFalseTotal
if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); 0 1 1
if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); 0 0 0
820 }
821 1 ▶ if (!ft_hdr_exists(aTHX_ hav, "User-Agent", 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_hdr_exists(aTHX_ hav, "User-Agent", 10)) { 1 0 1
822 1 av_push(hav, newSVpvs("User-Agent")); av_push(hav, newSVsv(ua->agent));
823 }
824 1 ▶ if (!ft_hdr_exists(aTHX_ hav, "Upgrade", 7)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_hdr_exists(aTHX_ hav, "Upgrade", 7)) { 1 0 1
825 1 av_push(hav, newSVpvs("Upgrade")); av_push(hav, newSVpvs("websocket"));
826 }
827 1 ▶ if (!ft_hdr_exists(aTHX_ hav, "Connection", 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_hdr_exists(aTHX_ hav, "Connection", 10)) { 1 0 1
828 1 av_push(hav, newSVpvs("Connection")); av_push(hav, newSVpvs("Upgrade"));
829 }
830 1 av_push(hav, newSVpvs("Sec-WebSocket-Key")); av_push(hav, newSVpv(wskey, 0));
831 1 av_push(hav, newSVpvs("Sec-WebSocket-Version")); av_push(hav, newSVpvs("13"));
832  
833 1 req = sv_2mortal(ft_build_request(aTHX_ ua, "GET", u.host, u.port, u.path,
834 hav, NULL));
835 1 authority = ft_authority(u.host, u.port, tls);
836 1 snprintf(portbuf, sizeof portbuf, "%d", u.port);
837  
838 1 ▶ if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone"))
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone")) 1 0 1
if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone")) 1 0 1
839 1 l = ft_loop_from_sv(aTHX_ ua->loop);
840 else
841 0 lsv = ua->loop;
842  
843 1 m_sv = sv_2mortal(newSVpvs("GET"));
844 1 sc_sv = sv_2mortal(newSVpv(u.scheme, 0));
845 1 au_sv = sv_2mortal(newSVpv(authority, 0));
846 1 pa_sv = sv_2mortal(newSVpv(u.path, 0));
847 1 empty_rv = sv_2mortal(newRV_noinc((SV *)newAV()));
848  
849 {
850 STRLEN rl;
851 1 const char *rb = SvPV_const(req, rl);
852 1 ft_conn_hm_next = ua->hm;
853 1 ft_conn_hm_loop_next = ua->hm_loop;
854 /* pool = NULL (never pooled); ws_key drives want_ws + the 101 check */
855 1 f = ft_h1_start(aTHX_ l, lsv, NULL, u.host, portbuf, rb, rl, tls, verify,
856 timeout, m_sv, sc_sv, au_sv, pa_sv, empty_rv,
857 &PL_sv_undef, &PL_sv_undef, wskey);
858 1 hmf_pin_loop(aTHX_ f, ua->loop); /* only this loop can resolve it */
859 }
860  
861 1 free(authority);
862 1 ft_url_free(&u);
863 1 return f;
864 }
865  
866 #endif /* FT_UA_H */
« include/fetch/backend_epoll.c ↑ Summary include/fetch/ft_ws.h »