xs/ratelimit.xs

filter: Statementstmt 96.6% 29 / 30 Branchbran 72.2% 26 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.3% 55 / 66 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/mrows.xs ↑ Summary include/punk/punk_session.h »
1 MODULE = Punk PACKAGE = Punk::RateLimit
2  
3 PROTOTYPES: DISABLE
4  
5 # The boot half of the `rate_limit` ROUTE option (punk_ratelimit.h): called
6 # from Punk::App::compile_extras when any route carried one. Appends a C guard
7 # closure running the same check the keyword installs on before_dispatch -
8 # before the `validate` guard, so a refused request never parses a body.
9 void
10 _compile_routes(self)
11 SV *self
12 CODE:
13 3 prl_compile_routes(aTHX_ self);
14  
15 MODULE = Punk PACKAGE = Punk::App
16  
17 PROTOTYPES: DISABLE
18  
19 # rate_limit(limit => N, window => S, by => 'ip'|'header:X-Api-Key'|$coderef,
20 # for => '/prefix', tag => 'name')
21 #
22 # The `rate_limit` keyword forwards here. Captures the rule into a magic-CV
23 # closure and pushes it onto before_dispatch (punk_ratelimit.h); the per-request
24 # enforcement then runs entirely in C. lib/Punk/RateLimit.pm is documentation.
25 # Chainable - declare it more than once for layered limits.
26 SV *
27 rate_limit(self, ...)
28 SV *self
29 CODE:
30 {
31 23 IV limit = 60, window = 60, by = 0;
32 23 SV *byfn = NULL;
33 23 const char *envkey = "", *forp = NULL, *tag = NULL;
34 23 STRLEN elen = 0, flen = 0, tlen = 0;
35 23 SV *envkeysv = NULL;
36 int i;
37  
38 23 ▶ if ((items - 1) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2) 0 23 23
39 0 croak("Punk: rate_limit takes a list of key => value options");
40  
41 99 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 76 23 99
42 76 const char *k = SvPV_nolen(ST(i));
43 76 SV *v = ST(i + 1);
44 76 ▶ if (strEQ(k, "limit")) limit = SvOK(v) ? SvIV(v) : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(k, "limit")) limit = SvOK(v) ? SvIV(v) : 0; 23 53 76
if (strEQ(k, "limit")) limit = SvOK(v) ? SvIV(v) : 0; 23 0 23
45 53 ▶ else if (strEQ(k, "window")) window = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "window")) window = SvIV(v); 23 30 53
46 46 ▶ else if (strEQ(k, "for")) { if (SvOK(v)) forp = SvPV(v, flen); }
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "for")) { if (SvOK(v)) forp = SvPV(v, flen); } 16 14 30
else if (strEQ(k, "for")) { if (SvOK(v)) forp = SvPV(v, flen); } 16 0 16
47 20 ▶ else if (strEQ(k, "tag")) { if (SvOK(v)) tag = SvPV(v, tlen); }
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "tag")) { if (SvOK(v)) tag = SvPV(v, tlen); } 6 8 14
else if (strEQ(k, "tag")) { if (SvOK(v)) tag = SvPV(v, tlen); } 6 0 6
48 8 ▶ else if (strEQ(k, "by")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "by")) { 8 0 8
49 8 ▶ if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 1 7 8
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 1 0 1
50 1 by = 2; byfn = v;
51 7 ▶ } else if (SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvOK(v)) { 7 0 7
52 STRLEN bl;
53 7 const char *bs = SvPV(v, bl);
54 7 ▶ if (bl > 7 && strnEQ(bs, "header:", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (bl > 7 && strnEQ(bs, "header:", 7)) { 2 5 7
if (bl > 7 && strnEQ(bs, "header:", 7)) { 2 0 2
55 2 by = 1;
56 2 envkeysv = prl_header_envkey(aTHX_ bs, bl);
57 2 envkey = SvPV(envkeysv, elen);
58 }
59 /* anything else (including 'ip') keeps by = 0 */
60 }
61 }
62 }
63  
64 /* tag default names the counter namespace so two rules never share a
65 * counter: the header env key, or 'fn' / 'ip'. */
66 23 ▶ if (!tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tag) { 17 6 23
67 17 ▶ if (by == 2) { tag = "fn"; tlen = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (by == 2) { tag = "fn"; tlen = 2; } 0 17 17
68 17 ▶ else if (by == 1) { tag = envkey; tlen = elen; }
Branch: 1 branch
BranchTrueFalseTotal
else if (by == 1) { tag = envkey; tlen = elen; } 0 17 17
69 17 else { tag = "ip"; tlen = 2; }
70 }
71  
72 23 prl_install(aTHX_ self, limit, window, by, envkey, elen,
73 forp, flen, tag, tlen, byfn);
74 23 RETVAL = newSVsv(self); /* chainable */
75 }
76 OUTPUT:
77 RETVAL
« xs/mrows.xs ↑ Summary include/punk/punk_session.h »