include/punk/punk_ua.h

filter: Statementstmt 87.7% 79 / 90 Branchbran 56.0% 74 / 132 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.9% 153 / 222 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_compile.h ↑ Summary include/punk/punk_plural.h »
1 /* punk_ua.h - the shared outbound user agent behind $c->ua.
2 *
3 * One Fetch agent per worker, not one per request. An agent owns a keep-alive
4 * connection pool, its DNS state and (when asked for) a cookie jar; building
5 * one per request would throw all of that away and pay a fresh TCP and TLS
6 * handshake every time, which is the opposite of the reason to reach for
7 * Fetch. So $c->ua is a lazy accessor onto the application's agent, and the
8 * context slot only memoises that lookup for the request it belongs to.
9 *
10 * The pid is stored beside the agent and checked on every fetch, exactly as
11 * punk_dbi.h does for a database handle: an agent built before a fork would
12 * otherwise have every worker sharing one set of sockets. A worker that finds
13 * a foreign pid builds its own.
14 *
15 * The agent binds to the worker's Hyperman loop when there is one, so an
16 * outbound call rides the same loop serving inbound requests and the worker
17 * keeps answering while it is in flight. Outside a worker - a test, a script,
18 * punk console - Hyperman->loop is undef, Fetch falls back to its own
19 * standalone loop and ->get blocks. That is the same degradation $c->timer
20 * already documents.
21 *
22 * Perl headers and punk_context.h (pcx_call_meth) must be in scope.
23 */
24  
25 #ifndef PUNK_UA_H
26 #define PUNK_UA_H
27  
28 /* The config the `ua` keyword froze for one agent, or NULL when that name was
29 * never declared. An undeclared default is not an error - an app that never
30 * says `ua` still gets a default agent - but an undeclared NAME is, and
31 * pua_shared turns this into that croak. */
32 45 static HV *pua_cfg(pTHX_ SV *app, const char *name, STRLEN nlen) {
33 SV **s, **e;
34 HV *uas;
35 45 ▶ if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 45 0 45
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 45 0 45
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 0 45 45
36 45 s = hv_fetchs((HV *)SvRV(app), K_UA, 0);
37 45 ▶ if (!(s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!(s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)) return NULL; 45 0 45
if (!(s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)) return NULL; 45 0 45
if (!(s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)) return NULL; 45 0 45
if (!(s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)) return NULL; 0 45 45
38 45 uas = (HV *)SvRV(*s);
39 45 e = hv_fetch(uas, name, (I32)nlen, 0);
40 44 ▶ return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 44 0 44
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 44 0 44
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 44 0 44
41 89 ▶ ? (HV *)SvRV(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*e) : NULL; 44 1 45
42 }
43  
44 /* Is this key one of Fetch's own constructor options?
45 *
46 * Needed to read a punk.yml `ua:` block, where a nested mapping is how a
47 * second agent is named - except that `headers` is an option whose value is
48 * itself a mapping. Deciding on the key rather than the value shape keeps
49 * `headers: {...}` an option and anything else nested a name. */
50 0 static int pua_is_known_opt(pTHX_ const char *k, STRLEN kl) {
51 static const char *opts[] = {
52 "headers", "agent", "timeout", "tls_verify", "max_redirects",
53 "keep_alive", "pool_size", "cookie_jar", "simple_response", NULL
54 };
55 int i;
56 PERL_UNUSED_CONTEXT;
57 0 ▶ for (i = 0; opts[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; opts[i]; i++) 0 0 0
58 0 ▶ if (strlen(opts[i]) == kl && memEQ(k, opts[i], kl)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(opts[i]) == kl && memEQ(k, opts[i], kl)) return 1; 0 0 0
if (strlen(opts[i]) == kl && memEQ(k, opts[i], kl)) return 1; 0 0 0
59 0 return 0;
60 }
61  
62 /* What cookie_jar was asked for. A jar belongs to the agent, so a jar on the
63 * worker-shared agent is shared by every request that worker serves - which is
64 * a cross-request leak the moment the cookies identify an end user rather than
65 * the application itself. So the setting decides which agent a request gets:
66 *
67 * PUA_JAR_NONE no jar (default): the shared agent, as-is.
68 * PUA_JAR_REQUEST cookie_jar => 1: Fetch->clone(cookie_jar => 1) per
69 * request. Its own jar, so cookies cannot outlive the
70 * request that collected them, over the shared connection
71 * pool and loop - a clone exists precisely so this costs
72 * neither.
73 * PUA_JAR_SHARED cookie_jar => 'shared': one jar on the shared agent, for
74 * an upstream that authenticates THIS APPLICATION and where
75 * a persistent session cookie is the point. Spelled out in
76 * full so nobody arrives here by accident.
77 */
78 #define PUA_JAR_NONE 0
79 #define PUA_JAR_REQUEST 1
80 #define PUA_JAR_SHARED 2
81  
82 39 static int pua_jar_mode(pTHX_ HV *cfg) {
83 39 ▶ SV **j = cfg ? hv_fetchs(cfg, "cookie_jar", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **j = cfg ? hv_fetchs(cfg, "cookie_jar", 0) : NULL; 39 0 39
84 39 ▶ if (!(j && *j && SvOK(*j) && SvTRUE(*j))) return PUA_JAR_NONE;
Branch: 4 branches
BranchTrueFalseTotal
if (!(j && *j && SvOK(*j) && SvTRUE(*j))) return PUA_JAR_NONE; 11 28 39
if (!(j && *j && SvOK(*j) && SvTRUE(*j))) return PUA_JAR_NONE; 11 0 11
if (!(j && *j && SvOK(*j) && SvTRUE(*j))) return PUA_JAR_NONE; 11 0 11
if (!(j && *j && SvOK(*j) && SvTRUE(*j))) return PUA_JAR_NONE; 0 11 11
85 11 ▶ if (SvPOK(*j)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(*j)) { 3 8 11
86 3 STRLEN l; const char *p = SvPV_const(*j, l);
87 3 ▶ if (l == 6 && memEQ(p, "shared", 6)) return PUA_JAR_SHARED;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 6 && memEQ(p, "shared", 6)) return PUA_JAR_SHARED; 3 0 3
if (l == 6 && memEQ(p, "shared", 6)) return PUA_JAR_SHARED; 3 0 3
88 }
89 8 return PUA_JAR_REQUEST;
90 }
91  
92 /* The worker's Hyperman::Loop, or NULL outside a worker.
93 *
94 * Deliberately does not load Hyperman. It is a prerequisite, so it is
95 * installed, but that is not the same as loaded - and if it has not been
96 * loaded in this process then by definition no worker loop is running here, so
97 * there is nothing to bind to and requiring it would only cost a script or a
98 * test the load. An unloaded Hyperman and a loaded one sitting outside a
99 * worker are the same answer: no loop. */
100 10 static SV *pua_loop(pTHX) {
101 SV *l;
102 10 ▶ if (!gv_stashpvs("Hyperman", 0)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!gv_stashpvs("Hyperman", 0)) return NULL; 0 10 10
103 10 l = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Hyperman")), "loop",
104 NULL, 0, 1);
105 10 ▶ if (l && SvOK(l)) return l;
Branch: 2 branches
BranchTrueFalseTotal
if (l && SvOK(l)) return l; 10 0 10
if (l && SvOK(l)) return l; 0 10 10
106 10 ▶ if (l) SvREFCNT_dec(l);
Branch: 1 branch
BranchTrueFalseTotal
if (l) SvREFCNT_dec(l); 10 0 10
107 10 return NULL;
108 }
109  
110 /* Fetch->new(%cfg, loop => $loop). Returns a +1 agent, or croaks the way any
111 * other boot-time misconfiguration does. */
112 10 static SV *pua_build(pTHX_ SV *app, const char *name, STRLEN nlen) {
113 HV *cfg;
114 SV *loop;
115 SV **argv;
116 SV *agent;
117 10 int n = 0, cap;
118 HE *e;
119  
120 /* Loaded on first use rather than at boot: an application that never
121 * reaches for $c->ua should not pay for the agent's dependencies. */
122 10 ▶ if (!gv_stashpvs("Fetch", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gv_stashpvs("Fetch", 0)) { 0 10 10
123 0 ▶ if (!pk_require_once(aTHX_ "Fetch", FALSE))
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_require_once(aTHX_ "Fetch", FALSE)) 0 0 0
124 0 ▶ croak("Punk: $c->ua needs Fetch: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: $c->ua needs Fetch: %s", SvPV_nolen(ERRSV)); 0 0 0
125 }
126  
127 10 cfg = pua_cfg(aTHX_ app, name, nlen);
128 10 loop = pua_loop(aTHX);
129  
130 10 ▶ cap = (cfg ? (int)HvUSEDKEYS(cfg) * 2 : 0) + 4;
Branch: 2 branches
BranchTrueFalseTotal
cap = (cfg ? (int)HvUSEDKEYS(cfg) * 2 : 0) + 4; 10 0 10
cap = (cfg ? (int)HvUSEDKEYS(cfg) * 2 : 0) + 4; 0 10 10
131 10 Newx(argv, cap, SV *);
132 10 ▶ if (cfg) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg) { 10 0 10
133 10 int mode = pua_jar_mode(aTHX_ cfg);
134 10 hv_iterinit(cfg);
135 25 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 15 10 25
136 STRLEN kl;
137 15 ▶ const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(e, kl); 0 15 15
138 /* cookie_jar never reaches the shared agent verbatim: in
139 * per-request mode the jar belongs to the clone, and 'shared' is
140 * Punk's spelling, not one Fetch knows. */
141 15 ▶ if (kl == 10 && memEQ(k, "cookie_jar", 10)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 10 && memEQ(k, "cookie_jar", 10)) { 3 12 15
if (kl == 10 && memEQ(k, "cookie_jar", 10)) { 3 0 3
142 3 ▶ if (mode == PUA_JAR_SHARED) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == PUA_JAR_SHARED) { 1 2 3
143 1 argv[n++] = sv_2mortal(newSVpvs("cookie_jar"));
144 1 argv[n++] = sv_2mortal(newSViv(1));
145 }
146 3 continue;
147 }
148 12 argv[n++] = sv_2mortal(newSVpvn(k, kl));
149 12 argv[n++] = sv_2mortal(newSVsv(hv_iterval(cfg, e)));
150 }
151 }
152 10 ▶ if (loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop) { 0 10 10
153 0 argv[n++] = sv_2mortal(newSVpvs("loop"));
154 0 argv[n++] = sv_2mortal(loop); /* pua_loop handed us +1 */
155 }
156 10 agent = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Fetch")), "new",
157 argv, n, 1);
158 10 Safefree(argv);
159 10 ▶ if (!(agent && SvROK(agent)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(agent && SvROK(agent))) 10 0 10
if (!(agent && SvROK(agent))) 0 10 10
160 0 croak("Punk: $c->ua could not build a Fetch agent");
161 10 return agent;
162 }
163  
164 /* This worker's shared agent for one name, built on first use in it. Borrowed
165 * - the app holds the reference. Each name gets its own agent, so two
166 * upstreams do not share a pool, a timeout or a set of default headers. */
167 30 static SV *pua_shared(pTHX_ SV *app, const char *name, STRLEN nlen) {
168 HV *h, *cache, *box;
169 SV **slot, **pid, **a;
170  
171 30 ▶ if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) 30 0 30
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) 30 0 30
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) 0 30 30
172 0 croak("Punk: $c->ua needs the compiled application");
173 30 h = (HV *)SvRV(app);
174  
175 /* A name that was never declared is a mistake worth naming, unlike the
176 * default, which an application is entitled never to configure. */
177 30 ▶ if (!(nlen == sizeof(K_DEFAULT) - 1 && memEQ(name, K_DEFAULT, nlen))
Branch: 2 branches
BranchTrueFalseTotal
if (!(nlen == sizeof(K_DEFAULT) - 1 && memEQ(name, K_DEFAULT, nlen)) 29 1 30
if (!(nlen == sizeof(K_DEFAULT) - 1 && memEQ(name, K_DEFAULT, nlen)) 5 24 29
178 6 ▶ && !pua_cfg(aTHX_ app, name, nlen))
Branch: 1 branch
BranchTrueFalseTotal
&& !pua_cfg(aTHX_ app, name, nlen)) 1 5 6
179 1 croak("Punk: $c->ua('%.*s') is not a configured agent; declare it "
180 "with `ua %.*s => { ... }`", (int)nlen, name, (int)nlen, name);
181  
182 29 slot = hv_fetchs(h, K_UA_AGENT, 0);
183 29 ▶ if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) 22 7 29
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) 22 0 22
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) 22 0 22
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) 22 0 22
184 22 cache = (HV *)SvRV(*slot);
185 else {
186 7 cache = newHV();
187 7 (void)hv_stores(h, K_UA_AGENT, newRV_noinc((SV *)cache));
188 }
189  
190 29 slot = hv_fetch(cache, name, (I32)nlen, 0);
191 29 ▶ if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) { 20 9 29
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) { 20 0 20
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) { 20 0 20
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) { 20 0 20
192 20 box = (HV *)SvRV(*slot);
193 20 pid = hv_fetchs(box, "pid", 0);
194 20 ▶ if (pid && *pid && SvOK(*pid)
Branch: 3 branches
BranchTrueFalseTotal
if (pid && *pid && SvOK(*pid) 20 0 20
if (pid && *pid && SvOK(*pid) 20 0 20
if (pid && *pid && SvOK(*pid) 20 0 20
195 20 ▶ && SvIV(*pid) == (IV)PerlProc_getpid()) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvIV(*pid) == (IV)PerlProc_getpid()) { 19 1 20
196 19 a = hv_fetchs(box, "agent", 0);
197 19 ▶ if (a && *a && SvROK(*a)) return *a;
Branch: 3 branches
BranchTrueFalseTotal
if (a && *a && SvROK(*a)) return *a; 19 0 19
if (a && *a && SvROK(*a)) return *a; 19 0 19
if (a && *a && SvROK(*a)) return *a; 19 0 19
198 }
199 /* a foreign pid: this worker was forked from whoever built that one,
200 * so drop it rather than share its sockets */
201 }
202  
203 10 box = newHV();
204 10 (void)hv_stores(box, "agent", pua_build(aTHX_ app, name, nlen));
205 10 (void)hv_stores(box, "pid", newSViv((IV)PerlProc_getpid()));
206 10 (void)hv_store(cache, name, (I32)nlen, newRV_noinc((SV *)box), 0);
207 10 a = hv_fetchs(box, "agent", 0);
208 10 ▶ return (a && *a) ? *a : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
return (a && *a) ? *a : &PL_sv_undef; 10 0 10
return (a && *a) ? *a : &PL_sv_undef; 10 0 10
209 }
210  
211 /* The agent this request should use, +1 to the caller.
212 *
213 * With a per-request jar that is a clone of the shared one: its own jar, over
214 * the shared connection pool and loop, so isolation costs neither. Otherwise
215 * it is the shared agent itself. Either way the context memoises it, so a
216 * handler calling $c->ua twice gets one agent and one jar. */
217 30 static SV *pua_agent(pTHX_ SV *app, const char *name, STRLEN nlen) {
218 30 SV *shared = pua_shared(aTHX_ app, name, nlen);
219 SV *clone;
220 SV *argv[2];
221  
222 29 ▶ if (pua_jar_mode(aTHX_ pua_cfg(aTHX_ app, name, nlen)) != PUA_JAR_REQUEST)
Branch: 1 branch
BranchTrueFalseTotal
if (pua_jar_mode(aTHX_ pua_cfg(aTHX_ app, name, nlen)) != PUA_JAR_REQUEST) 23 6 29
223 23 return SvREFCNT_inc(shared);
224  
225 6 argv[0] = sv_2mortal(newSVpvs("cookie_jar"));
226 6 argv[1] = sv_2mortal(newSViv(1));
227 6 clone = pcx_call_meth(aTHX_ shared, "clone", argv, 2, 1);
228 6 ▶ if (!(clone && SvROK(clone)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(clone && SvROK(clone))) 6 0 6
if (!(clone && SvROK(clone))) 0 6 6
229 0 croak("Punk: $c->ua could not clone the agent for this request "
230 "(Fetch 0.11 or later is needed for a per-request cookie jar)");
231 6 return clone;
232 }
233  
234 #endif /* PUNK_UA_H */
« include/punk/punk_compile.h ↑ Summary include/punk/punk_plural.h »