| 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 |
21 |
| HV *h = app_hv(aTHX_ app); |
| 28 |
21 |
| HV *o = NULL; |
| 29 |
|
| HV *cfg; |
| 30 |
|
| SV **sc; |
| 31 |
|
| PERL_UNUSED_VAR(self); |
| 32 |
|
| |
| 33 |
21 |
▶ | if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
21 |
0 |
21 |
| if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
21 |
0 |
21 |
| if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
21 |
0 |
21 |
|
| 34 |
21 |
| o = (HV *)SvRV(opts); |
| 35 |
21 |
▶ | if (o) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (o) { |
21 |
0 |
21 |
|
| 36 |
|
| static const char *const known[] = |
| 37 |
|
| { "scope", "ttl", "max_record", "lock_wait", NULL }; |
| 38 |
|
| HE *he; |
| 39 |
21 |
| hv_iterinit(o); |
| 40 |
52 |
▶ | while ((he = hv_iternext(o))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(o))) { |
32 |
20 |
52 |
|
| 41 |
32 |
▶ | const char *k = HePV(he, PL_na); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *k = HePV(he, PL_na); |
0 |
32 |
32 |
|
| 42 |
32 |
| int ok = 0, i; |
| 43 |
160 |
▶ | for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; |
31 |
97 |
128 |
| for (i = 0; known[i]; i++) if (strEQ(k, known[i])) ok = 1; |
128 |
32 |
160 |
|
| 44 |
32 |
▶ | if (!ok) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) |
1 |
31 |
32 |
|
| 45 |
1 |
| croak("Punk::Plugin::Idempotency: unknown option '%s'", k); |
| 46 |
|
| } |
| 47 |
|
| } |
| 48 |
20 |
▶ | sc = o ? hv_fetchs(o, "scope", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| sc = o ? hv_fetchs(o, "scope", 0) : NULL; |
20 |
0 |
20 |
|
| 49 |
20 |
▶ | if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
19 |
1 |
20 |
| if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
19 |
0 |
19 |
| if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
19 |
0 |
19 |
| if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
0 |
19 |
19 |
|
| 50 |
1 |
| croak("Punk::Plugin::Idempotency: `scope` is required and must be " |
| 51 |
|
| "a coderef - the stored value is a whole response, so a key " |
| 52 |
|
| "that is not scoped to an account is a way to read somebody " |
| 53 |
|
| "else's (plugin 'Idempotency' => { scope => sub { " |
| 54 |
|
| "$_[0]->current_user->{id} } })"); |
| 55 |
|
| |
| 56 |
19 |
| cfg = newHV(); |
| 57 |
19 |
| (void)hv_stores(cfg, "scope", newSVsv(*sc)); |
| 58 |
|
| { /* 24 hours is the usual default and it is a business decision - |
| 59 |
|
| * how long after a failed request will your client still retry? - |
| 60 |
|
| * so it is an option, not a constant. */ |
| 61 |
19 |
▶ | SV **t = o ? hv_fetchs(o, "ttl", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **t = o ? hv_fetchs(o, "ttl", 0) : NULL; |
19 |
0 |
19 |
|
| 62 |
19 |
▶ | (void)hv_stores(cfg, "ttl", |
Branch: 3 branches
| Branch | True | False | Total |
| (void)hv_stores(cfg, "ttl", |
1 |
18 |
19 |
| (void)hv_stores(cfg, "ttl", |
1 |
0 |
1 |
| (void)hv_stores(cfg, "ttl", |
1 |
0 |
1 |
|
| 63 |
|
| (t && *t && SvOK(*t)) ? newSVnv(SvNV(*t)) : newSVnv(86400)); |
| 64 |
|
| } |
| 65 |
|
| { /* The plugin stores whole responses, so a response bigger than |
| 66 |
|
| * this is served but not recorded (and says so in a header). |
| 67 |
|
| * 1MB default: a JSON answer sits well under it, an export sits |
| 68 |
|
| * well over, and either kind of application can move it. */ |
| 69 |
19 |
▶ | SV **m = o ? hv_fetchs(o, "max_record", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **m = o ? hv_fetchs(o, "max_record", 0) : NULL; |
19 |
0 |
19 |
|
| 70 |
19 |
| IV mr = 1048576; |
| 71 |
19 |
▶ | if (m && *m && SvOK(*m)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (m && *m && SvOK(*m)) { |
4 |
15 |
19 |
| if (m && *m && SvOK(*m)) { |
4 |
0 |
4 |
| if (m && *m && SvOK(*m)) { |
4 |
0 |
4 |
|
| 72 |
4 |
▶ | if (!looks_like_number(*m) || (mr = SvIV(*m)) <= 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!looks_like_number(*m) || (mr = SvIV(*m)) <= 0) |
3 |
1 |
4 |
| if (!looks_like_number(*m) || (mr = SvIV(*m)) <= 0) |
2 |
1 |
3 |
|
| 73 |
3 |
| croak("Punk::Plugin::Idempotency: max_record must be a " |
| 74 |
|
| "positive number of bytes"); |
| 75 |
|
| } |
| 76 |
16 |
| (void)hv_stores(cfg, "max_record", newSViv(mr)); |
| 77 |
|
| } |
| 78 |
|
| { /* How long a request holds the single-flight lock before another |
| 79 |
|
| * may take it for abandoned, and how long a retry waits for the |
| 80 |
|
| * first attempt to finish. It has to outlast the slowest handler |
| 81 |
|
| * behind a key, which is a fact about the application - not the |
| 82 |
|
| * cache store's lock_wait, which is tuned for something else. */ |
| 83 |
16 |
▶ | SV **w = o ? hv_fetchs(o, "lock_wait", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **w = o ? hv_fetchs(o, "lock_wait", 0) : NULL; |
16 |
0 |
16 |
|
| 84 |
16 |
| NV lw = PI_LOCK_WAIT; |
| 85 |
16 |
▶ | if (w && *w && SvOK(*w)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (w && *w && SvOK(*w)) { |
7 |
9 |
16 |
| if (w && *w && SvOK(*w)) { |
7 |
0 |
7 |
| if (w && *w && SvOK(*w)) { |
7 |
0 |
7 |
|
| 86 |
7 |
▶ | if (!looks_like_number(*w) || (lw = SvNV(*w)) <= 0 |
Branch: 2 branches
| Branch | True | False | Total |
| if (!looks_like_number(*w) || (lw = SvNV(*w)) <= 0 |
6 |
1 |
7 |
| if (!looks_like_number(*w) || (lw = SvNV(*w)) <= 0 |
4 |
2 |
6 |
|
| 87 |
4 |
▶ | || Perl_isinfnan(lw)) |
Branch: 1 branch
| Branch | True | False | Total |
| || Perl_isinfnan(lw)) |
2 |
2 |
4 |
|
| 88 |
5 |
| croak("Punk::Plugin::Idempotency: lock_wait must be a " |
| 89 |
|
| "positive, finite number of seconds"); |
| 90 |
|
| } |
| 91 |
11 |
| (void)hv_stores(cfg, "lock_wait", newSVnv(lw)); |
| 92 |
|
| } |
| 93 |
11 |
| (void)hv_stores(h, K_IDEM, newRV_noinc((SV *)cfg)); |
| 94 |
|
| } |