xs/idempotency.xs

filter: Statementstmt 100% 33 / 33 Branchbran 69.3% 44 / 62 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 77 / 95 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_cors.h ↑ Summary include/punk/punk_filecache.h »
1 MODULE = Punk PACKAGE = Punk::Plugin::Idempotency
2  
3 PROTOTYPES: DISABLE
4  
5 # register($app, \%opts)
6 #
7 # `scope` is REQUIRED and is the whole security argument. The stored value is
8 # a WHOLE RESPONSE - somebody's order, with their address in it - so if two
9 # accounts can produce one cache key, this plugin is a way to read other
10 # people's responses by guessing a UUID, on exactly the endpoints worth
11 # reading.
12 #
13 # The plugin cannot supply it. A Punk application may authenticate through a
14 # session, an `auth` identity, an OAuth2 token or an API key - four this
15 # workspace already ships - and inventing a default means picking one and
16 # being silently wrong for the rest. Same shape as Punk::Plugin::Sitemap
17 # refusing to start without `base`, and for the same reason: a default here
18 # would be a security decision made by the framework on the application's
19 # behalf.
20 void
21 register(self, app, opts = &PL_sv_undef)
22 SV *self
23 SV *app
24 SV *opts
25 CODE:
26 {
27 39 HV *h = app_hv(aTHX_ app);
28 39 HV *o = NULL;
29 HV *cfg;
30 SV **sc;
31 PERL_UNUSED_VAR(self);
32  
33 39 ▶ # uncoverable branch false
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false 39 0 39
# uncoverable branch false 39 0 39
# uncoverable branch false 39 0 39
34 39 # uncoverable condition right count:1 class:ignore_covered_err
35 39 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 39 0 39
36 # uncoverable condition right count:3 class:ignore_covered_err
37 if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
38 o = (HV *)SvRV(opts);
39 39 # uncoverable branch false
40 94 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 56 38 94
41 56 ▶ if (o) {
Branch: 1 branch
BranchTrueFalseTotal
if (o) { 0 56 56
42 56 static const char *const known[] =
43 280 ▶ { "scope", "ttl", "max_record", "lock_wait", NULL };
Branch: 2 branches
BranchTrueFalseTotal
{ "scope", "ttl", "max_record", "lock_wait", NULL }; 55 169 224
{ "scope", "ttl", "max_record", "lock_wait", NULL }; 224 56 280
44 56 ▶ HE *he;
Branch: 1 branch
BranchTrueFalseTotal
HE *he; 1 55 56
45 1 hv_iterinit(o);
46 while ((he = hv_iternext(o))) {
47 const char *k = HePV(he, PL_na);
48 38 ▶ int ok = 0, i;
Branch: 1 branch
BranchTrueFalseTotal
int ok = 0, i; 38 0 38
49 38 ▶ for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1;
Branch: 4 branches
BranchTrueFalseTotal
for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; 37 1 38
for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; 37 0 37
for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; 37 0 37
for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; 0 37 37
50 1 if (!ok)
51 croak("Punk::Plugin::Idempotency: unknown option '%s'", k);
52 }
53 }
54 sc = o ? hv_fetchs(o, "scope", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
55 # uncoverable condition left count:2 class:ignore_covered_err
56 37 # uncoverable condition left count:3 class:ignore_covered_err
57 37 # uncoverable condition left count:4 class:ignore_covered_err
58 if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV))
59 croak("Punk::Plugin::Idempotency: `scope` is required and must be "
60 "a coderef - the stored value is a whole response, so a key "
61 37 ▶ "that is not scoped to an account is a way to read somebody "
Branch: 1 branch
BranchTrueFalseTotal
"that is not scoped to an account is a way to read somebody " 37 0 37
62 37 ▶ "else's (plugin 'Idempotency' => { scope => sub { "
Branch: 3 branches
BranchTrueFalseTotal
"else's (plugin 'Idempotency' => { scope => sub { " 1 36 37
"else's (plugin 'Idempotency' => { scope => sub { " 1 0 1
"else's (plugin 'Idempotency' => { scope => sub { " 1 0 1
63 "$_[0]->current_user->{id} } })");
64  
65 cfg = newHV();
66 (void)hv_stores(cfg, "scope", newSVsv(*sc));
67 { /* 24 hours is the usual default and it is a business decision -
68 * how long after a failed request will your client still retry? -
69 37 ▶ * so it is an option, not a constant. */
Branch: 1 branch
BranchTrueFalseTotal
* so it is an option, not a constant. */ 37 0 37
70 37 SV **t = o ? hv_fetchs(o, "ttl", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
71 37 ▶ (void)hv_stores(cfg, "ttl",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(cfg, "ttl", 4 33 37
(void)hv_stores(cfg, "ttl", 4 0 4
(void)hv_stores(cfg, "ttl", 4 0 4
72 4 ▶ (t && *t && SvOK(*t)) ? newSVnv(SvNV(*t)) : newSVnv(86400));
Branch: 2 branches
BranchTrueFalseTotal
(t && *t && SvOK(*t)) ? newSVnv(SvNV(*t)) : newSVnv(86400)); 3 1 4
(t && *t && SvOK(*t)) ? newSVnv(SvNV(*t)) : newSVnv(86400)); 2 1 3
73 3 }
74 { /* The plugin stores whole responses, so a response bigger than
75 * this is served but not recorded (and says so in a header).
76 34 * 1MB default: a JSON answer sits well under it, an export sits
77 * well over, and either kind of application can move it. */
78 SV **m = o ? hv_fetchs(o, "max_record", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
79 IV mr = 1048576;
80 # uncoverable condition right count:2 class:ignore_covered_err
81 # uncoverable condition right count:3 class:ignore_covered_err
82 if (m && *m && SvOK(*m)) {
83 34 ▶ if (!looks_like_number(*m) || (mr = SvIV(*m)) <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (!looks_like_number(*m) || (mr = SvIV(*m)) <= 0) 34 0 34
84 34 croak("Punk::Plugin::Idempotency: max_record must be a "
85 34 ▶ "positive number of bytes");
Branch: 3 branches
BranchTrueFalseTotal
"positive number of bytes"); 13 21 34
"positive number of bytes"); 13 0 13
"positive number of bytes"); 13 0 13
86 13 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 12 1 13
} 10 2 12
87 10 ▶ (void)hv_stores(cfg, "max_record", newSViv(mr));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(cfg, "max_record", newSViv(mr)); 2 8 10
88 5 }
89 { /* How long a request holds the single-flight lock before another
90 * may take it for abandoned, and how long a retry waits for the
91 29 * first attempt to finish. It has to outlast the slowest handler
92 * behind a key, which is a fact about the application - not the
93 29 * cache store's lock_wait, which is tuned for something else. */
94 SV **w = o ? hv_fetchs(o, "lock_wait", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
95 NV lw = PI_LOCK_WAIT;
96 # uncoverable condition right count:2 class:ignore_covered_err
97 # uncoverable condition right count:3 class:ignore_covered_err
98 if (w && *w && SvOK(*w)) {
99 if (!looks_like_number(*w) || (lw = SvNV(*w)) <= 0
100 || Perl_isinfnan(lw))
101 croak("Punk::Plugin::Idempotency: lock_wait must be a "
102 "positive, finite number of seconds");
103 }
104 (void)hv_stores(cfg, "lock_wait", newSVnv(lw));
105 }
106 (void)hv_stores(h, K_IDEM, newRV_noinc((SV *)cfg));
107 }
« include/punk/punk_cors.h ↑ Summary include/punk/punk_filecache.h »