| 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 |
254 |
| static ft_ua *ft_ua_of(pTHX_ SV *sv) { |
| 36 |
254 |
▶ | if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
254 |
0 |
254 |
| if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
0 |
254 |
254 |
|
| 37 |
0 |
| croak("Fetch: not a Fetch user agent"); |
| 38 |
254 |
| 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 |
186 |
| static void ft_url_free(ft_url *u) { |
| 46 |
186 |
| free(u->scheme); free(u->host); free(u->path); |
| 47 |
186 |
| u->scheme = u->host = u->path = NULL; |
| 48 |
186 |
| } |
| 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 |
186 |
| static int ft_parse_url(const char *url, ft_url *u) { |
| 53 |
186 |
| const char *sep = strstr(url, "://"); |
| 54 |
|
| const char *h, *p, *rest; |
| 55 |
|
| size_t i; |
| 56 |
186 |
| memset(u, 0, sizeof *u); |
| 57 |
186 |
▶ | if (!sep) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sep) return 0; |
0 |
186 |
186 |
|
| 58 |
186 |
| u->scheme = ft_lc_dup(url, (STRLEN)(sep - url)); |
| 59 |
186 |
| h = sep + 3; |
| 60 |
186 |
| p = h; |
| 61 |
1872 |
▶ | while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; |
Branch: 5 branches
| Branch | True | False | Total |
| while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; |
1872 |
0 |
1872 |
| while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; |
1871 |
1 |
1872 |
| while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; |
1686 |
185 |
1871 |
| while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; |
1686 |
0 |
1686 |
| while (*p && *p != '/' && *p != ':' && *p != '?' && *p != '#') p++; |
1686 |
0 |
1686 |
|
| 62 |
186 |
| u->host = ft_strdup_n(h, (size_t)(p - h)); |
| 63 |
186 |
▶ | if (*p == ':') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ':') { |
185 |
1 |
186 |
|
| 64 |
185 |
| p++; |
| 65 |
185 |
| u->port = 0; |
| 66 |
1094 |
▶ | while (*p >= '0' && *p <= '9') { u->port = u->port * 10 + (*p - '0'); p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p >= '0' && *p <= '9') { u->port = u->port * 10 + (*p - '0'); p++; } |
909 |
185 |
1094 |
| while (*p >= '0' && *p <= '9') { u->port = u->port * 10 + (*p - '0'); p++; } |
909 |
0 |
909 |
|
| 67 |
|
| } else { |
| 68 |
1 |
| u->port = 0; |
| 69 |
|
| } |
| 70 |
186 |
▶ | if (u->port == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (u->port == 0) |
1 |
185 |
186 |
|
| 71 |
1 |
▶ | u->port = (strcmp(u->scheme, "https") == 0) ? 443 : 80; |
Branch: 1 branch
| Branch | True | False | Total |
| u->port = (strcmp(u->scheme, "https") == 0) ? 443 : 80; |
0 |
1 |
1 |
|
| 72 |
186 |
| rest = p; |
| 73 |
186 |
▶ | u->path = (*rest) ? ft_strdup0(rest) : ft_strdup0("/"); |
Branch: 1 branch
| Branch | True | False | Total |
| u->path = (*rest) ? ft_strdup0(rest) : ft_strdup0("/"); |
186 |
0 |
186 |
|
| 74 |
186 |
▶ | if (!u->path[0]) { free(u->path); u->path = ft_strdup0("/"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!u->path[0]) { free(u->path); u->path = ft_strdup0("/"); } |
0 |
186 |
186 |
|
| 75 |
|
| (void)i; |
| 76 |
186 |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| int same = pa && pb |
12 |
0 |
12 |
|
| 95 |
12 |
▶ | && strcmp(ua.scheme, ub.scheme) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| && strcmp(ua.scheme, ub.scheme) == 0 |
12 |
0 |
12 |
|
| 96 |
12 |
▶ | && strcasecmp(ua.host, ub.host) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| && strcasecmp(ua.host, ub.host) == 0 |
12 |
0 |
12 |
|
| 97 |
24 |
▶ | && ua.port == ub.port; |
Branch: 2 branches
| Branch | True | False | Total |
| && 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| } 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
152 |
| 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 |
152 |
| SV *req = newSV(256); /* preallocate: most request heads fit, no realloc */ |
| 168 |
|
| SSize_t n, i; |
| 169 |
152 |
| SvPOK_on(req); |
| 170 |
152 |
| SvCUR_set(req, 0); |
| 171 |
152 |
| *SvPVX(req) = '\0'; |
| 172 |
152 |
| sv_catpv(req, method); sv_catpvs(req, " "); |
| 173 |
152 |
| sv_catpv(req, path); sv_catpvs(req, " HTTP/1.1\r\n"); |
| 174 |
152 |
▶ | if (!ft_hdr_exists(aTHX_ hav, "Host", 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_hdr_exists(aTHX_ hav, "Host", 4)) { |
152 |
0 |
152 |
|
| 175 |
152 |
▶ | if (port == 80 || port == 443) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (port == 80 || port == 443) { |
151 |
1 |
152 |
| if (port == 80 || port == 443) { |
0 |
151 |
151 |
|
| 176 |
1 |
| sv_catpvs(req, "Host: "); sv_catpv(req, host); sv_catpvs(req, "\r\n"); |
| 177 |
|
| } else { |
| 178 |
151 |
| sv_catpvf(req, "Host: %s:%d\r\n", host, port); |
| 179 |
|
| } |
| 180 |
|
| } |
| 181 |
152 |
▶ | if (!ua->keep_alive && !ft_hdr_exists(aTHX_ hav, "Connection", 10)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ua->keep_alive && !ft_hdr_exists(aTHX_ hav, "Connection", 10)) |
5 |
147 |
152 |
| if (!ua->keep_alive && !ft_hdr_exists(aTHX_ hav, "Connection", 10)) |
5 |
0 |
5 |
|
| 182 |
5 |
| sv_catpvs(req, "Connection: close\r\n"); |
| 183 |
152 |
▶ | if (body && SvOK(body)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (body && SvOK(body)) { |
6 |
146 |
152 |
| 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
| Branch | True | False | Total |
| 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 |
152 |
| n = av_len(hav) + 1; |
| 189 |
350 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
198 |
152 |
350 |
|
| 190 |
198 |
| SV **k = av_fetch(hav, i, 0); |
| 191 |
198 |
| SV **v = av_fetch(hav, i + 1, 0); |
| 192 |
|
| STRLEN kl, vl; |
| 193 |
396 |
▶ | const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); |
Branch: 2 branches
| Branch | True | False | Total |
| const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); |
198 |
0 |
198 |
| const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); |
198 |
0 |
198 |
|
| 194 |
396 |
▶ | const char *vs = (v && *v) ? SvPV_const(*v, vl) : (vl = 0, ""); |
Branch: 2 branches
| Branch | True | False | Total |
| const char *vs = (v && *v) ? SvPV_const(*v, vl) : (vl = 0, ""); |
198 |
0 |
198 |
| const char *vs = (v && *v) ? SvPV_const(*v, vl) : (vl = 0, ""); |
198 |
0 |
198 |
|
| 195 |
198 |
| sv_catpvn(req, ks, kl); sv_catpvs(req, ": "); |
| 196 |
198 |
| sv_catpvn(req, vs, vl); sv_catpvs(req, "\r\n"); |
| 197 |
|
| } |
| 198 |
152 |
| sv_catpvs(req, "\r\n"); |
| 199 |
152 |
▶ | if (body && SvOK(body)) sv_catsv(req, body); |
Branch: 2 branches
| Branch | True | False | Total |
| if (body && SvOK(body)) sv_catsv(req, body); |
6 |
146 |
152 |
| if (body && SvOK(body)) sv_catsv(req, body); |
6 |
0 |
6 |
|
| 200 |
152 |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
135 |
| static int ft_response_status(pTHX_ SV *res) { |
| 235 |
135 |
▶ | if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { |
135 |
0 |
135 |
| if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { |
135 |
0 |
135 |
| if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { |
135 |
0 |
135 |
|
| 236 |
135 |
| SV **e = hv_fetchs((HV *)SvRV(res), "status", 0); |
| 237 |
135 |
▶ | if (e && *e) return (int)SvIV(*e); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && *e) return (int)SvIV(*e); |
135 |
0 |
135 |
| if (e && *e) return (int)SvIV(*e); |
135 |
0 |
135 |
|
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
33 |
| static SV *ft_load_new(pTHX_ const char *class, SV *arg) { |
| 258 |
|
| SV *obj; |
| 259 |
33 |
| dSP; |
| 260 |
|
| int n; |
| 261 |
33 |
| load_module(PERL_LOADMOD_NOIMPORT, newSVpv(class, 0), NULL); |
| 262 |
33 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
33 |
33 |
|
| 263 |
33 |
▶ | XPUSHs(sv_2mortal(newSVpv(class, 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(class, 0))); |
0 |
33 |
33 |
|
| 264 |
33 |
▶ | if (arg) XPUSHs(arg); |
Branch: 2 branches
| Branch | True | False | Total |
| if (arg) XPUSHs(arg); |
0 |
33 |
33 |
| if (arg) XPUSHs(arg); |
0 |
0 |
0 |
|
| 265 |
33 |
| PUTBACK; |
| 266 |
33 |
| n = call_method("new", G_SCALAR); |
| 267 |
33 |
| SPAGAIN; |
| 268 |
33 |
▶ | obj = n ? SvREFCNT_inc(POPs) : newSV(0); /* survive the FREETMPS below */ |
Branch: 1 branch
| Branch | True | False | Total |
| obj = n ? SvREFCNT_inc(POPs) : newSV(0); /* survive the FREETMPS below */ |
33 |
0 |
33 |
|
| 269 |
33 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
33 |
0 |
33 |
|
| 270 |
33 |
| 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 |
46 |
| static SV *ft_default_loop(pTHX) { |
| 286 |
46 |
| SV *slot = get_sv("Fetch::Loop::Standalone::DEFAULT", GV_ADD); |
| 287 |
46 |
| SV *pidv = get_sv("Fetch::Loop::Standalone::DEFAULT_PID", GV_ADD); |
| 288 |
46 |
| IV pid = (IV)PerlProc_getpid(); |
| 289 |
|
| SV *l; |
| 290 |
|
| |
| 291 |
46 |
▶ | if (SvROK(slot) && SvIOK(pidv) && SvIV(pidv) == pid) |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvROK(slot) && SvIOK(pidv) && SvIV(pidv) == pid) |
20 |
26 |
46 |
| 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 |
27 |
| l = ft_load_new(aTHX_ "Fetch::Loop::Standalone", NULL); |
| 295 |
27 |
| sv_setsv(slot, l); /* the global holds it for the life of the process */ |
| 296 |
27 |
| sv_setiv(pidv, pid); |
| 297 |
27 |
| 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 |
55 |
| static SV *ft_resolve_loop(pTHX_ SV *loop) { |
| 303 |
55 |
▶ | if (!loop || !SvOK(loop)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!loop || !SvOK(loop)) |
9 |
46 |
55 |
| if (!loop || !SvOK(loop)) |
0 |
9 |
9 |
|
| 304 |
46 |
| return ft_default_loop(aTHX); |
| 305 |
9 |
▶ | if (sv_isobject(loop)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_isobject(loop)) { |
9 |
0 |
9 |
|
| 306 |
9 |
▶ | if (sv_derived_from(loop, "Fetch::Loop::Standalone") |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(loop, "Fetch::Loop::Standalone") |
0 |
9 |
9 |
|
| 307 |
0 |
▶ | || sv_derived_from(loop, "Fetch::Loop")) |
Branch: 1 branch
| Branch | True | False | Total |
| || 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| SV *res = rp ? *rp : NULL; |
9 |
0 |
9 |
|
| 342 |
9 |
▶ | if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
151 |
| 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 |
151 |
| SV *body = NULL, *on_body = NULL, *on_headers = NULL, *json_body = NULL; |
| 383 |
151 |
| ft_loop *l = NULL; SV *lsv = NULL; |
| 384 |
|
| ft_pool *pl; |
| 385 |
151 |
| ft_obs_tokens *obs = NULL; |
| 386 |
|
| SSize_t n, i; |
| 387 |
|
| |
| 388 |
151 |
▶ | if (!ft_parse_url(url, &u)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_parse_url(url, &u)) |
0 |
151 |
151 |
|
| 389 |
0 |
| croak("Fetch: cannot parse URL '%s'", url); |
| 390 |
151 |
▶ | if (strcmp(u.scheme, "http") != 0 && strcmp(u.scheme, "https") != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (strcmp(u.scheme, "http") != 0 && strcmp(u.scheme, "https") != 0) { |
7 |
144 |
151 |
| 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 |
151 |
| tls = (strcmp(u.scheme, "https") == 0); |
| 398 |
151 |
| verify = ua->tls_verify; |
| 399 |
151 |
▶ | if (opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) { |
151 |
0 |
151 |
|
| 400 |
151 |
| SV **tv = hv_fetchs(opt, "tls_verify", 0); |
| 401 |
151 |
▶ | if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; |
7 |
144 |
151 |
| if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; |
7 |
0 |
7 |
|
| 402 |
|
| } |
| 403 |
|
| |
| 404 |
|
| /* merged headers: defaults, overlaid with this request's headers */ |
| 405 |
151 |
| hav = newAV(); |
| 406 |
151 |
| sv_2mortal((SV *)hav); |
| 407 |
151 |
| ft_hdr_pairs_into(aTHX_ hav, ua->headers); |
| 408 |
151 |
▶ | if (opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) { |
151 |
0 |
151 |
|
| 409 |
151 |
| SV **oh = hv_fetchs(opt, "headers", 0); |
| 410 |
151 |
▶ | if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); |
Branch: 2 branches
| Branch | True | False | Total |
| if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); |
7 |
144 |
151 |
| 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 |
151 |
▶ | if (opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) { |
151 |
0 |
151 |
|
| 417 |
151 |
| SV **rf = hv_fetchs(opt, "_redirect_from", 0); |
| 418 |
151 |
▶ | if (rf && *rf && SvOK(*rf) |
Branch: 3 branches
| Branch | True | False | Total |
| if (rf && *rf && SvOK(*rf) |
12 |
139 |
151 |
| 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
| Branch | True | False | Total |
| && !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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| || (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
| Branch | True | False | Total |
| || (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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
151 |
▶ | if (!ft_hdr_exists(aTHX_ hav, "User-Agent", 10)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_hdr_exists(aTHX_ hav, "User-Agent", 10)) { |
151 |
0 |
151 |
|
| 438 |
151 |
| av_push(hav, newSVpvs("User-Agent")); |
| 439 |
151 |
| av_push(hav, newSVsv(ua->agent)); |
| 440 |
|
| } |
| 441 |
151 |
▶ | if (ua->cookie_jar && SvOK(ua->cookie_jar) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ua->cookie_jar && SvOK(ua->cookie_jar) |
9 |
142 |
151 |
| if (ua->cookie_jar && SvOK(ua->cookie_jar) |
9 |
0 |
9 |
|
| 442 |
9 |
▶ | && !ft_hdr_exists(aTHX_ hav, "Cookie", 6)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && !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
| Branch | True | False | Total |
| 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 |
151 |
▶ | if (opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) { |
151 |
0 |
151 |
|
| 455 |
151 |
| SV **jp = hv_fetchs(opt, "json", 0); |
| 456 |
151 |
▶ | if (jp && *jp && SvOK(*jp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (jp && *jp && SvOK(*jp)) { |
1 |
150 |
151 |
| 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
| Branch | True | False | Total |
| 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 |
151 |
| obs = ft_obs_start(aTHX_ method, strlen(method), url, strlen(url), hav); |
| 470 |
|
| |
| 471 |
|
| /* h2 header list (lowercased names, minus hop-by-hop/length fields): only |
| 472 |
|
| * consumed if ALPN negotiates h2, which can only happen on TLS. Skip the |
| 473 |
|
| * whole thing - a full second pass over the headers - for cleartext. */ |
| 474 |
151 |
| h2 = NULL; |
| 475 |
151 |
▶ | if (tls) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tls) { |
7 |
144 |
151 |
|
| 476 |
7 |
| h2 = newAV(); |
| 477 |
7 |
| n = av_len(hav) + 1; |
| 478 |
14 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
7 |
7 |
14 |
|
| 479 |
7 |
| SV **k = av_fetch(hav, i, 0); |
| 480 |
7 |
| SV **v = av_fetch(hav, i + 1, 0); |
| 481 |
|
| STRLEN kl; |
| 482 |
14 |
▶ | const char *ks = (k && *k) ? SvPV_const(*k, kl) : (kl = 0, ""); |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 483 |
7 |
▶ | if (ft_ci_eq(ks, kl, "host", 4) || ft_ci_eq(ks, kl, "connection", 10) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ft_ci_eq(ks, kl, "host", 4) || ft_ci_eq(ks, kl, "connection", 10) |
7 |
0 |
7 |
| if (ft_ci_eq(ks, kl, "host", 4) || ft_ci_eq(ks, kl, "connection", 10) |
7 |
0 |
7 |
|
| 484 |
7 |
▶ | || ft_ci_eq(ks, kl, "content-length", 14) |
Branch: 1 branch
| Branch | True | False | Total |
| || ft_ci_eq(ks, kl, "content-length", 14) |
7 |
0 |
7 |
|
| 485 |
7 |
▶ | || ft_ci_eq(ks, kl, "transfer-encoding", 17)) |
Branch: 1 branch
| Branch | True | False | Total |
| || ft_ci_eq(ks, kl, "transfer-encoding", 17)) |
0 |
7 |
7 |
|
| 486 |
0 |
| continue; |
| 487 |
|
| { |
| 488 |
7 |
| char *lc = ft_lc_dup(ks, kl); |
| 489 |
7 |
| av_push(h2, newSVpvn(lc, kl)); |
| 490 |
7 |
| free(lc); |
| 491 |
7 |
▶ | av_push(h2, (v && *v) ? newSVsv(*v) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(h2, (v && *v) ? newSVsv(*v) : newSV(0)); |
7 |
0 |
7 |
| av_push(h2, (v && *v) ? newSVsv(*v) : newSV(0)); |
7 |
0 |
7 |
|
| 492 |
|
| } |
| 493 |
|
| } |
| 494 |
|
| } |
| 495 |
|
| |
| 496 |
151 |
| timeout = ua->timeout; |
| 497 |
151 |
▶ | if (opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) { |
151 |
0 |
151 |
|
| 498 |
151 |
| SV **to = hv_fetchs(opt, "timeout", 0); |
| 499 |
151 |
▶ | if (to && *to) timeout = SvNV(*to); |
Branch: 2 branches
| Branch | True | False | Total |
| if (to && *to) timeout = SvNV(*to); |
4 |
147 |
151 |
| if (to && *to) timeout = SvNV(*to); |
4 |
0 |
4 |
|
| 500 |
151 |
▶ | body = hv_fetchs(opt, "body", 0) ? *hv_fetchs(opt, "body", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| body = hv_fetchs(opt, "body", 0) ? *hv_fetchs(opt, "body", 0) : NULL; |
5 |
146 |
151 |
|
| 501 |
151 |
▶ | on_body = hv_fetchs(opt, "on_body", 0) ? *hv_fetchs(opt, "on_body", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| on_body = hv_fetchs(opt, "on_body", 0) ? *hv_fetchs(opt, "on_body", 0) : NULL; |
2 |
149 |
151 |
|
| 502 |
151 |
▶ | on_headers = hv_fetchs(opt, "on_headers", 0) ? *hv_fetchs(opt, "on_headers", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| on_headers = hv_fetchs(opt, "on_headers", 0) ? *hv_fetchs(opt, "on_headers", 0) : NULL; |
0 |
151 |
151 |
|
| 503 |
151 |
▶ | if (body && !SvOK(body)) body = NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (body && !SvOK(body)) body = NULL; |
5 |
146 |
151 |
| if (body && !SvOK(body)) body = NULL; |
0 |
5 |
5 |
|
| 504 |
|
| } |
| 505 |
151 |
▶ | if (json_body) body = json_body; /* json => wins over an explicit body */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (json_body) body = json_body; /* json => wins over an explicit body */ |
1 |
150 |
151 |
|
| 506 |
|
| |
| 507 |
151 |
| req = sv_2mortal(ft_build_request(aTHX_ ua, method, u.host, u.port, u.path, |
| 508 |
|
| hav, body)); |
| 509 |
151 |
| snprintf(portbuf, sizeof portbuf, "%d", u.port); |
| 510 |
|
| |
| 511 |
151 |
▶ | if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone")) |
151 |
0 |
151 |
| if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone")) |
151 |
0 |
151 |
|
| 512 |
151 |
| l = ft_loop_from_sv(aTHX_ ua->loop); |
| 513 |
|
| else |
| 514 |
0 |
| lsv = ua->loop; |
| 515 |
151 |
▶ | pl = ua->pool ? ft_pool_from_sv(aTHX_ ua->pool) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| pl = ua->pool ? ft_pool_from_sv(aTHX_ ua->pool) : NULL; |
146 |
5 |
151 |
|
| 516 |
|
| |
| 517 |
|
| /* the pseudo-header pieces (method/scheme/:authority/path) and the h2 nva |
| 518 |
|
| * are h2-only too; pass undef for cleartext so ft_h1_start stores nothing. */ |
| 519 |
151 |
| authority = NULL; |
| 520 |
151 |
▶ | if (tls) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tls) { |
7 |
144 |
151 |
|
| 521 |
7 |
| authority = ft_authority(u.host, u.port, tls); |
| 522 |
7 |
| m_sv = sv_2mortal(newSVpv(method, 0)); |
| 523 |
7 |
| sc_sv = sv_2mortal(newSVpv(u.scheme, 0)); |
| 524 |
7 |
| au_sv = sv_2mortal(newSVpv(authority, 0)); |
| 525 |
7 |
| pa_sv = sv_2mortal(newSVpv(u.path, 0)); |
| 526 |
7 |
| hdrs2_rv = sv_2mortal(newRV_noinc((SV *)h2)); |
| 527 |
|
| } else { |
| 528 |
144 |
| m_sv = sc_sv = au_sv = pa_sv = hdrs2_rv = &PL_sv_undef; |
| 529 |
|
| } |
| 530 |
|
| |
| 531 |
|
| { |
| 532 |
|
| STRLEN rl; |
| 533 |
151 |
| const char *rb = SvPV_const(req, rl); |
| 534 |
151 |
| ft_conn_simple_next = ua->simple_response; /* inherited by a fresh conn */ |
| 535 |
151 |
| ft_conn_hm_next = ua->hm; |
| 536 |
151 |
| ft_conn_hm_loop_next = ua->hm_loop; |
| 537 |
151 |
| ft_conn_on_headers_next = |
| 538 |
151 |
▶ | (on_headers && SvROK(on_headers)) ? on_headers : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| (on_headers && SvROK(on_headers)) ? on_headers : NULL; |
0 |
151 |
151 |
| (on_headers && SvROK(on_headers)) ? on_headers : NULL; |
0 |
0 |
0 |
|
| 539 |
|
| /* HTTP/3, when this origin has said it speaks it. |
| 540 |
|
| * |
| 541 |
|
| * There is no h3:// scheme, so nothing about the URL can ask for |
| 542 |
|
| * this: the only way to know is an Alt-Svc header on an earlier |
| 543 |
|
| * response over TCP (ft_altsvc.h). A cache miss is the ordinary case |
| 544 |
|
| * for a first request and takes the TCP path below, which is also |
| 545 |
|
| * what records the header for next time. |
| 546 |
|
| * |
| 547 |
|
| * The h3 attempt is not a commitment. If it fails - the libraries are |
| 548 |
|
| * not built in, the loop has no timer fine enough for loss detection, |
| 549 |
|
| * the UDP port is blocked, which is common enough on real networks to |
| 550 |
|
| * be the reason Alt-Svc is advisory - the future comes back failed |
| 551 |
|
| * rather than throwing, and the request falls back to TCP with |
| 552 |
|
| * nothing lost but the attempt. */ |
| 553 |
151 |
| f = NULL; |
| 554 |
|
| if (tls && FT_H3_AVAILABLE) { |
| 555 |
|
| char origin[300]; |
| 556 |
|
| ft_altsvc *alt; |
| 557 |
|
| snprintf(origin, sizeof origin, "%s:%d", u.host, u.port); |
| 558 |
|
| alt = ft_altsvc_get(origin); |
| 559 |
|
| if (alt) { |
| 560 |
|
| char h3port[16]; |
| 561 |
|
| SV *hf; |
| 562 |
|
| snprintf(h3port, sizeof h3port, "%d", alt->port); |
| 563 |
|
| hf = ft_h3_start(aTHX_ l, lsv, pl, alt->host, h3port, verify, |
| 564 |
|
| timeout, m_sv, sc_sv, au_sv, pa_sv, hdrs2_rv, |
| 565 |
|
| body ? body : &PL_sv_undef, |
| 566 |
|
| on_body ? on_body : &PL_sv_undef); |
| 567 |
|
| if (hmf_state(aTHX_ hf) == HMF_FAILED) { |
| 568 |
|
| /* it could not even be started; forget the advertisement |
| 569 |
|
| * so the next request does not pay for this again */ |
| 570 |
|
| ft_altsvc_forget(origin); |
| 571 |
|
| SvREFCNT_dec(hf); |
| 572 |
|
| } else { |
| 573 |
|
| f = hf; |
| 574 |
|
| } |
| 575 |
|
| } |
| 576 |
|
| } |
| 577 |
151 |
▶ | if (!f) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) |
151 |
0 |
151 |
|
| 578 |
151 |
▶ | f = ft_h1_start(aTHX_ l, lsv, pl, u.host, portbuf, rb, rl, tls, verify, |
Branch: 2 branches
| Branch | True | False | Total |
| f = ft_h1_start(aTHX_ l, lsv, pl, u.host, portbuf, rb, rl, tls, verify, |
2 |
149 |
151 |
| f = ft_h1_start(aTHX_ l, lsv, pl, u.host, portbuf, rb, rl, tls, verify, |
6 |
145 |
151 |
|
| 579 |
|
| timeout, m_sv, sc_sv, au_sv, pa_sv, hdrs2_rv, |
| 580 |
|
| body ? body : &PL_sv_undef, |
| 581 |
|
| on_body ? on_body : &PL_sv_undef, NULL); |
| 582 |
151 |
| ft_conn_on_headers_next = NULL; /* borrowed; do not keep past the call */ |
| 583 |
151 |
| hmf_pin_loop(aTHX_ f, ua->loop); /* only this loop can resolve it */ |
| 584 |
|
| } |
| 585 |
|
| |
| 586 |
151 |
▶ | if (obs) { /* settle the observers when this hop ends, however it ends */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (obs) { /* settle the observers when this hop ends, however it ends */ |
13 |
138 |
151 |
|
| 587 |
13 |
| SV *cb = hm_closure(aTHX_ ft_obs_done_cb_xs, NULL, NULL, NULL, NULL, |
| 588 |
|
| PTR2IV(obs), 0); |
| 589 |
13 |
| hmf_on_ready(aTHX_ f, cb); |
| 590 |
13 |
| SvREFCNT_dec(cb); |
| 591 |
|
| } |
| 592 |
|
| |
| 593 |
|
| /* store any Set-Cookie before the caller/redirect-follower runs */ |
| 594 |
151 |
▶ | if (ua->cookie_jar && SvOK(ua->cookie_jar)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ua->cookie_jar && SvOK(ua->cookie_jar)) { |
9 |
142 |
151 |
| if (ua->cookie_jar && SvOK(ua->cookie_jar)) { |
9 |
0 |
9 |
|
| 595 |
9 |
| SV *hostsv = newSVpv(u.host, 0); |
| 596 |
9 |
| SV *pathsv = newSVpv(u.path, 0); |
| 597 |
9 |
| SV *cb = hm_closure(aTHX_ ft_extract_cb, ua->cookie_jar, hostsv, pathsv, |
| 598 |
|
| NULL, 0, 0); |
| 599 |
9 |
| SvREFCNT_dec(hostsv); |
| 600 |
9 |
| SvREFCNT_dec(pathsv); |
| 601 |
9 |
| hmf_on_ready(aTHX_ f, cb); |
| 602 |
9 |
| SvREFCNT_dec(cb); |
| 603 |
|
| } |
| 604 |
|
| |
| 605 |
151 |
| free(authority); |
| 606 |
151 |
| ft_url_free(&u); |
| 607 |
151 |
| return f; |
| 608 |
|
| } |
| 609 |
|
| |
| 610 |
|
| /* ---- redirect continuation ---------------------------------------------- */ |
| 611 |
|
| |
| 612 |
|
| XS_INTERNAL(ft_redirect_cb); |
| 613 |
149 |
| XS_INTERNAL(ft_redirect_cb) { |
| 614 |
149 |
| dXSARGS; |
| 615 |
149 |
| hm_clos *cl = hm_clos_of(aTHX_ cv); |
| 616 |
|
| SV *f, *next, *self_sv; |
| 617 |
|
| AV *pack; |
| 618 |
|
| IV left, st; |
| 619 |
|
| const char *method, *url; |
| 620 |
149 |
| HV *opt = NULL; |
| 621 |
149 |
▶ | if (!cl || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cl || items < 1) XSRETURN_EMPTY; |
149 |
0 |
149 |
| if (!cl || items < 1) XSRETURN_EMPTY; |
0 |
149 |
149 |
|
| 622 |
149 |
| f = ST(0); |
| 623 |
149 |
| next = cl->a; |
| 624 |
149 |
| self_sv = cl->b; |
| 625 |
149 |
| pack = (AV *)SvRV(cl->c); |
| 626 |
149 |
| left = cl->i; |
| 627 |
149 |
| method = SvPV_nolen(*av_fetch(pack, 0, 0)); |
| 628 |
149 |
▶ | url = SvPV_nolen(*av_fetch(pack, 1, 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| url = SvPV_nolen(*av_fetch(pack, 1, 0)); |
149 |
0 |
149 |
|
| 629 |
|
| { |
| 630 |
149 |
| SV **op = av_fetch(pack, 2, 0); |
| 631 |
149 |
▶ | if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) |
149 |
0 |
149 |
| if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) |
149 |
0 |
149 |
| if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) |
149 |
0 |
149 |
| if (op && *op && SvROK(*op) && SvTYPE(SvRV(*op)) == SVt_PVHV) |
149 |
0 |
149 |
|
| 632 |
149 |
| opt = (HV *)SvRV(*op); |
| 633 |
|
| } |
| 634 |
|
| |
| 635 |
149 |
| st = hmf_state(aTHX_ f); |
| 636 |
149 |
▶ | if (st != HMF_DONE) { /* propagate failure/cancel */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (st != HMF_DONE) { /* propagate failure/cancel */ |
14 |
135 |
149 |
|
| 637 |
14 |
▶ | if (st == HMF_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st == HMF_FAILED) { |
14 |
0 |
14 |
|
| 638 |
14 |
| AV *vals = hmf_values_av(aTHX_ f); |
| 639 |
14 |
▶ | SSize_t n = vals ? av_len(vals) + 1 : 0, i; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t n = vals ? av_len(vals) + 1 : 0, i; |
14 |
0 |
14 |
|
| 640 |
14 |
| AV *out = newAV(); |
| 641 |
28 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
14 |
14 |
28 |
|
| 642 |
14 |
| SV **e = av_fetch(vals, i, 0); |
| 643 |
14 |
▶ | av_push(out, e ? newSVsv(*e) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(out, e ? newSVsv(*e) : newSV(0)); |
14 |
0 |
14 |
|
| 644 |
|
| } |
| 645 |
14 |
| hmf_settle_av(aTHX_ next, HMF_FAILED, out); |
| 646 |
|
| } else { |
| 647 |
0 |
| hmf_cancel(aTHX_ next); |
| 648 |
|
| } |
| 649 |
14 |
| XSRETURN_EMPTY; |
| 650 |
|
| } |
| 651 |
|
| |
| 652 |
|
| { |
| 653 |
135 |
| AV *vals = hmf_values_av(aTHX_ f); |
| 654 |
135 |
▶ | SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; |
135 |
0 |
135 |
| SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; |
135 |
0 |
135 |
|
| 655 |
135 |
▶ | SV *res = rp ? *rp : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *res = rp ? *rp : &PL_sv_undef; |
135 |
0 |
135 |
|
| 656 |
135 |
| int status = ft_response_status(aTHX_ res); |
| 657 |
13 |
▶ | SV *loc = (status >= 300 && status < 400) |
Branch: 1 branch
| Branch | True | False | Total |
| SV *loc = (status >= 300 && status < 400) |
12 |
1 |
13 |
|
| 658 |
148 |
▶ | ? ft_response_header(aTHX_ res, "location", 8) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? ft_response_header(aTHX_ res, "location", 8) : NULL; |
13 |
122 |
135 |
|
| 659 |
|
| |
| 660 |
135 |
▶ | if (!(loc && SvOK(loc) && SvCUR(loc) > 0)) { /* not a redirect */ |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(loc && SvOK(loc) && SvCUR(loc) > 0)) { /* not a redirect */ |
12 |
123 |
135 |
| 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 |
|
| 661 |
123 |
| SV *cp = newSVsv(res); |
| 662 |
123 |
| hmf_settle(aTHX_ next, HMF_DONE, &cp, 1); |
| 663 |
123 |
| SvREFCNT_dec(cp); |
| 664 |
123 |
| XSRETURN_EMPTY; |
| 665 |
|
| } |
| 666 |
|
| { |
| 667 |
|
| int drop_body; |
| 668 |
12 |
| const char *m2 = ft_redirect_method(method, status, &drop_body); |
| 669 |
12 |
| SV *u2 = ft_resolve_location(aTHX_ url, loc); |
| 670 |
12 |
| HV *o2 = newHV(); |
| 671 |
|
| SV *child, *cb2; |
| 672 |
12 |
▶ | if (opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) { |
12 |
0 |
12 |
|
| 673 |
|
| HE *he; |
| 674 |
12 |
| hv_iterinit(opt); |
| 675 |
22 |
▶ | while ((he = hv_iternext(opt))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(opt))) { |
10 |
12 |
22 |
|
| 676 |
|
| I32 kl; |
| 677 |
10 |
| char *k = hv_iterkey(he, &kl); |
| 678 |
10 |
| (void)hv_store(o2, k, kl, newSVsv(hv_iterval(opt, he)), 0); |
| 679 |
|
| } |
| 680 |
|
| } |
| 681 |
12 |
▶ | if (drop_body) (void)hv_delete(o2, "body", 4, G_DISCARD); |
Branch: 1 branch
| Branch | True | False | Total |
| if (drop_body) (void)hv_delete(o2, "body", 4, G_DISCARD); |
1 |
11 |
12 |
|
| 682 |
|
| /* record the origin we are leaving so ft_request_once can strip |
| 683 |
|
| * credential headers if this hop crosses to a different origin */ |
| 684 |
12 |
| (void)hv_store(o2, "_redirect_from", 14, newSVpv(url, 0), 0); |
| 685 |
|
| |
| 686 |
24 |
| child = ft_follow(aTHX_ self_sv, ft_ua_of(aTHX_ self_sv), |
| 687 |
12 |
| m2, SvPV_nolen(u2), o2, left - 1); |
| 688 |
12 |
| cb2 = hm_closure(aTHX_ hm_xs_chain_cb, next, NULL, NULL, NULL, 0, 0); |
| 689 |
12 |
| hm_any_on_ready(aTHX_ child, cb2); |
| 690 |
12 |
| SvREFCNT_dec(cb2); |
| 691 |
12 |
| SvREFCNT_dec(child); |
| 692 |
12 |
| SvREFCNT_dec(u2); |
| 693 |
12 |
| SvREFCNT_dec((SV *)o2); |
| 694 |
|
| } |
| 695 |
|
| } |
| 696 |
12 |
| XSRETURN_EMPTY; |
| 697 |
|
| } |
| 698 |
|
| |
| 699 |
|
| /* ---- follow: request, then chain a redirect if there are hops left ------ */ |
| 700 |
|
| |
| 701 |
151 |
| static SV *ft_follow(pTHX_ SV *self_sv, ft_ua *ua, const char *method, |
| 702 |
|
| const char *url, HV *opt, IV left) { |
| 703 |
151 |
| SV *f = ft_request_once(aTHX_ self_sv, ua, method, url, opt); |
| 704 |
151 |
▶ | if (left <= 0) return f; |
Branch: 1 branch
| Branch | True | False | Total |
| if (left <= 0) return f; |
2 |
149 |
151 |
|
| 705 |
|
| { |
| 706 |
149 |
| SV *next = hmf_new(aTHX_ hmf_class_of(aTHX_ f)); |
| 707 |
149 |
| AV *pack = newAV(); |
| 708 |
|
| SV *pack_rv, *cb; |
| 709 |
149 |
| av_push(pack, newSVpv(method, 0)); |
| 710 |
149 |
| av_push(pack, newSVpv(url, 0)); |
| 711 |
149 |
▶ | av_push(pack, opt ? newRV_inc((SV *)opt) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(pack, opt ? newRV_inc((SV *)opt) : newSV(0)); |
149 |
0 |
149 |
|
| 712 |
149 |
| pack_rv = newRV_noinc((SV *)pack); |
| 713 |
149 |
| hmf_set_upstream(aTHX_ next, f); |
| 714 |
149 |
| cb = hm_closure(aTHX_ ft_redirect_cb, next, self_sv, pack_rv, NULL, |
| 715 |
|
| left, 0); |
| 716 |
149 |
| SvREFCNT_dec(pack_rv); /* hm_closure took its own ref */ |
| 717 |
149 |
| hmf_on_ready(aTHX_ f, cb); |
| 718 |
149 |
| SvREFCNT_dec(cb); |
| 719 |
149 |
| SvREFCNT_dec(f); /* the connection keeps f alive */ |
| 720 |
149 |
| return next; |
| 721 |
|
| } |
| 722 |
|
| } |
| 723 |
|
| |
| 724 |
|
| /* ---- verb dispatch ------------------------------------------------------ */ |
| 725 |
|
| |
| 726 |
139 |
| static SV *ft_dispatch(pTHX_ SV *self_sv, const char *method, const char *url, |
| 727 |
|
| SV **opt_args, int nopt) { |
| 728 |
139 |
| HV *opt = newHV(); |
| 729 |
139 |
| ft_ua *ua = ft_ua_of(aTHX_ self_sv); |
| 730 |
|
| IV max; |
| 731 |
|
| int i; |
| 732 |
|
| SV *f; |
| 733 |
164 |
▶ | for (i = 0; i + 1 < nopt; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < nopt; i += 2) { |
25 |
139 |
164 |
|
| 734 |
|
| STRLEN kl; |
| 735 |
25 |
| const char *k = SvPV_const(opt_args[i], kl); |
| 736 |
25 |
| (void)hv_store(opt, k, (I32)kl, newSVsv(opt_args[i + 1]), 0); |
| 737 |
|
| } |
| 738 |
139 |
▶ | if (hv_exists(opt, "max_redirects", 13)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(opt, "max_redirects", 13)) { |
2 |
137 |
139 |
|
| 739 |
2 |
| SV **m = hv_fetchs(opt, "max_redirects", 0); |
| 740 |
2 |
▶ | max = (m && *m && SvOK(*m)) ? SvIV(*m) : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 741 |
|
| } else { |
| 742 |
137 |
| max = ua->max_redirects; |
| 743 |
|
| } |
| 744 |
139 |
▶ | if (max < 0) max = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (max < 0) max = 0; |
0 |
139 |
139 |
|
| 745 |
139 |
| f = ft_follow(aTHX_ self_sv, ua, method, url, opt, max); |
| 746 |
139 |
| SvREFCNT_dec((SV *)opt); |
| 747 |
139 |
| return f; |
| 748 |
|
| } |
| 749 |
|
| |
| 750 |
|
| /* ---- WebSocket ---------------------------------------------------------- */ |
| 751 |
|
| |
| 752 |
|
| /* the ft_conn behind a Fetch::WebSocket (a blessed IV over the connection) */ |
| 753 |
13 |
| static ft_conn *ft_ws_of(pTHX_ SV *sv) { |
| 754 |
13 |
▶ | if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
13 |
0 |
13 |
| if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
0 |
13 |
13 |
|
| 755 |
0 |
| croak("Fetch::WebSocket: not a websocket"); |
| 756 |
13 |
| return INT2PTR(ft_conn *, SvIV(SvRV(sv))); |
| 757 |
|
| } |
| 758 |
|
| |
| 759 |
|
| /* Open a WebSocket: send the HTTP/1.1 Upgrade handshake with a fresh key and |
| 760 |
|
| * return a Future resolving to a Fetch::WebSocket once the 101 is verified. |
| 761 |
|
| * Accepts ws:// wss:// http:// https:// (ws/http cleartext, wss/https TLS). */ |
| 762 |
1 |
| static SV *ft_websocket(pTHX_ ft_ua *ua, const char *url, HV *opt) { |
| 763 |
|
| ft_url u; |
| 764 |
|
| int tls, verify; |
| 765 |
|
| double timeout; |
| 766 |
|
| AV *hav; |
| 767 |
|
| char portbuf[16], wskey[25]; |
| 768 |
|
| char *authority; |
| 769 |
|
| SV *req, *m_sv, *sc_sv, *au_sv, *pa_sv, *empty_rv, *f; |
| 770 |
1 |
| ft_loop *l = NULL; SV *lsv = NULL; |
| 771 |
|
| |
| 772 |
1 |
▶ | if (!ft_parse_url(url, &u)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_parse_url(url, &u)) |
0 |
1 |
1 |
|
| 773 |
0 |
| croak("Fetch: cannot parse URL '%s'", url); |
| 774 |
1 |
▶ | tls = (strcmp(u.scheme, "wss") == 0 || strcmp(u.scheme, "https") == 0); |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 775 |
1 |
▶ | if (strcmp(u.scheme, "ws") && strcmp(u.scheme, "wss") |
Branch: 2 branches
| Branch | True | False | Total |
| if (strcmp(u.scheme, "ws") && strcmp(u.scheme, "wss") |
0 |
1 |
1 |
| if (strcmp(u.scheme, "ws") && strcmp(u.scheme, "wss") |
0 |
0 |
0 |
|
| 776 |
0 |
▶ | && strcmp(u.scheme, "http") && strcmp(u.scheme, "https")) { |
Branch: 2 branches
| Branch | True | False | Total |
| && strcmp(u.scheme, "http") && strcmp(u.scheme, "https")) { |
0 |
0 |
0 |
| && strcmp(u.scheme, "http") && strcmp(u.scheme, "https")) { |
0 |
0 |
0 |
|
| 777 |
0 |
| SV *fu = hmf_new(aTHX_ "Fetch::Future"); |
| 778 |
0 |
| SV *e = sv_2mortal(newSVpvf("Fetch: not a websocket URL scheme '%s'\n", u.scheme)); |
| 779 |
0 |
| hmf_settle(aTHX_ fu, HMF_FAILED, &e, 1); |
| 780 |
0 |
| ft_url_free(&u); |
| 781 |
0 |
| return fu; |
| 782 |
|
| } |
| 783 |
1 |
▶ | if (tls && u.port == 80) u.port = 443; /* default wss/https port */ |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 784 |
|
| |
| 785 |
1 |
| verify = ua->tls_verify; |
| 786 |
1 |
| timeout = ua->timeout; |
| 787 |
1 |
▶ | if (opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) { |
1 |
0 |
1 |
|
| 788 |
1 |
| SV **tv = hv_fetchs(opt, "tls_verify", 0); |
| 789 |
1 |
| SV **to = hv_fetchs(opt, "timeout", 0); |
| 790 |
1 |
▶ | if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; |
0 |
1 |
1 |
| if (tv && *tv) verify = SvTRUE(*tv) ? 1 : 0; |
0 |
0 |
0 |
|
| 791 |
1 |
▶ | if (to && *to) timeout = SvNV(*to); |
Branch: 2 branches
| Branch | True | False | Total |
| if (to && *to) timeout = SvNV(*to); |
0 |
1 |
1 |
| if (to && *to) timeout = SvNV(*to); |
0 |
0 |
0 |
|
| 792 |
|
| } |
| 793 |
|
| |
| 794 |
1 |
| ft_ws_genkey(wskey); |
| 795 |
|
| |
| 796 |
1 |
| hav = newAV(); |
| 797 |
1 |
| sv_2mortal((SV *)hav); |
| 798 |
1 |
| ft_hdr_pairs_into(aTHX_ hav, ua->headers); |
| 799 |
1 |
▶ | if (opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) { |
1 |
0 |
1 |
|
| 800 |
1 |
| SV **oh = hv_fetchs(opt, "headers", 0); |
| 801 |
1 |
▶ | if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); |
Branch: 2 branches
| Branch | True | False | Total |
| if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); |
0 |
1 |
1 |
| if (oh && *oh) ft_hdr_merge(aTHX_ hav, *oh); |
0 |
0 |
0 |
|
| 802 |
|
| } |
| 803 |
1 |
▶ | if (!ft_hdr_exists(aTHX_ hav, "User-Agent", 10)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_hdr_exists(aTHX_ hav, "User-Agent", 10)) { |
1 |
0 |
1 |
|
| 804 |
1 |
| av_push(hav, newSVpvs("User-Agent")); av_push(hav, newSVsv(ua->agent)); |
| 805 |
|
| } |
| 806 |
1 |
▶ | if (!ft_hdr_exists(aTHX_ hav, "Upgrade", 7)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_hdr_exists(aTHX_ hav, "Upgrade", 7)) { |
1 |
0 |
1 |
|
| 807 |
1 |
| av_push(hav, newSVpvs("Upgrade")); av_push(hav, newSVpvs("websocket")); |
| 808 |
|
| } |
| 809 |
1 |
▶ | if (!ft_hdr_exists(aTHX_ hav, "Connection", 10)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_hdr_exists(aTHX_ hav, "Connection", 10)) { |
1 |
0 |
1 |
|
| 810 |
1 |
| av_push(hav, newSVpvs("Connection")); av_push(hav, newSVpvs("Upgrade")); |
| 811 |
|
| } |
| 812 |
1 |
| av_push(hav, newSVpvs("Sec-WebSocket-Key")); av_push(hav, newSVpv(wskey, 0)); |
| 813 |
1 |
| av_push(hav, newSVpvs("Sec-WebSocket-Version")); av_push(hav, newSVpvs("13")); |
| 814 |
|
| |
| 815 |
1 |
| req = sv_2mortal(ft_build_request(aTHX_ ua, "GET", u.host, u.port, u.path, |
| 816 |
|
| hav, NULL)); |
| 817 |
1 |
| authority = ft_authority(u.host, u.port, tls); |
| 818 |
1 |
| snprintf(portbuf, sizeof portbuf, "%d", u.port); |
| 819 |
|
| |
| 820 |
1 |
▶ | if (sv_isobject(ua->loop) && sv_derived_from(ua->loop, "Fetch::Loop::Standalone")) |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 821 |
1 |
| l = ft_loop_from_sv(aTHX_ ua->loop); |
| 822 |
|
| else |
| 823 |
0 |
| lsv = ua->loop; |
| 824 |
|
| |
| 825 |
1 |
| m_sv = sv_2mortal(newSVpvs("GET")); |
| 826 |
1 |
| sc_sv = sv_2mortal(newSVpv(u.scheme, 0)); |
| 827 |
1 |
| au_sv = sv_2mortal(newSVpv(authority, 0)); |
| 828 |
1 |
| pa_sv = sv_2mortal(newSVpv(u.path, 0)); |
| 829 |
1 |
| empty_rv = sv_2mortal(newRV_noinc((SV *)newAV())); |
| 830 |
|
| |
| 831 |
|
| { |
| 832 |
|
| STRLEN rl; |
| 833 |
1 |
| const char *rb = SvPV_const(req, rl); |
| 834 |
1 |
| ft_conn_hm_next = ua->hm; |
| 835 |
1 |
| ft_conn_hm_loop_next = ua->hm_loop; |
| 836 |
|
| /* pool = NULL (never pooled); ws_key drives want_ws + the 101 check */ |
| 837 |
1 |
| f = ft_h1_start(aTHX_ l, lsv, NULL, u.host, portbuf, rb, rl, tls, verify, |
| 838 |
|
| timeout, m_sv, sc_sv, au_sv, pa_sv, empty_rv, |
| 839 |
|
| &PL_sv_undef, &PL_sv_undef, wskey); |
| 840 |
1 |
| hmf_pin_loop(aTHX_ f, ua->loop); /* only this loop can resolve it */ |
| 841 |
|
| } |
| 842 |
|
| |
| 843 |
1 |
| free(authority); |
| 844 |
1 |
| ft_url_free(&u); |
| 845 |
1 |
| return f; |
| 846 |
|
| } |
| 847 |
|
| |
| 848 |
|
| #endif /* FT_UA_H */ |