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 xs/future.xs »
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 8 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 24 IV limit = 60, window = 60, by = 0;
32 24 SV *byfn = NULL;
33 24 const char *envkey = "", *forp = NULL, *tag = NULL;
34 24 STRLEN elen = 0, flen = 0, tlen = 0;
35 24 SV *envkeysv = NULL;
36 int i;
37  
38 24 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 24 24
39 0 # uncoverable condition left count:1 class:ignore_covered_err
40 if ((items - 1) % 2)
41 103 ▶ croak("Punk: rate_limit takes a list of key => value options"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: rate_limit takes a list of key => value options"); /* # uncoverable statement */ 79 24 103
42 79  
43 79 for (i = 1; i + 1 < items; i += 2) {
44 79 ▶ const char *k = SvPV_nolen(ST(i));
Branch: 2 branches
BranchTrueFalseTotal
const char *k = SvPV_nolen(ST(i)); 24 55 79
const char *k = SvPV_nolen(ST(i)); 24 0 24
45 55 ▶ SV *v = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
SV *v = ST(i + 1); 24 31 55
46 48 ▶ if (strEQ(k, "limit")) limit = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(k, "limit")) limit = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 14 31
if (strEQ(k, "limit")) limit = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 0 17
47 20 ▶ else if (strEQ(k, "window")) window = SvIV(v);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "window")) window = SvIV(v); 6 8 14
else if (strEQ(k, "window")) window = SvIV(v); 6 0 6
48 8 ▶ # uncoverable branch false count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false count:1 class:ignore_covered_err 8 0 8
49 8 ▶ # uncoverable branch true count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true count:2 class:ignore_covered_err 1 7 8
# uncoverable branch true count:2 class:ignore_covered_err 1 0 1
50 1 # uncoverable branch true count:3 class:ignore_covered_err
51 7 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 7 0 7
52 # uncoverable condition right count:2 class:ignore_covered_err
53 7 else if (strEQ(k, "for")) { if (SvOK(v)) forp = SvPV(v, flen); }
54 7 ▶ # uncoverable statement class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable statement class:ignore_covered_err 2 5 7
# uncoverable statement class:ignore_covered_err 2 0 2
55 2 # uncoverable branch true count:1 class:ignore_covered_err
56 2 # uncoverable branch false count:1 class:ignore_covered_err
57 2 # uncoverable branch true count:2 class:ignore_covered_err
58 # uncoverable branch false count:2 class:ignore_covered_err
59 # uncoverable branch true count:3 class:ignore_covered_err
60 # uncoverable branch false count:3 class:ignore_covered_err
61 # uncoverable condition left count:1 class:ignore_covered_err
62 # uncoverable condition right count:1 class:ignore_covered_err
63 # uncoverable condition left count:2 class:ignore_covered_err
64 # uncoverable condition right count:2 class:ignore_covered_err
65 else if (strEQ(k, "tag")) { if (SvOK(v)) tag = SvPV(v, tlen); }
66 24 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 18 6 24
67 18 ▶ # uncoverable statement class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement class:ignore_covered_err 0 18 18
68 18 ▶ # uncoverable branch true class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true class:ignore_covered_err 0 18 18
69 18 # uncoverable condition left count:1 class:ignore_covered_err
70 # uncoverable condition right count:1 class:ignore_covered_err
71 else if (strEQ(k, "by")) {
72 24 # uncoverable statement class:ignore_covered_err
73 # uncoverable branch true class:ignore_covered_err
74 24 # uncoverable branch false class:ignore_covered_err
75 # uncoverable condition left count:1 class:ignore_covered_err
76 # uncoverable condition right count:1 class:ignore_covered_err
77 # uncoverable condition left count:2 class:ignore_covered_err
78 # uncoverable condition right count:2 class:ignore_covered_err
79 if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) {
80 by = 2; byfn = v; /* # uncoverable statement class:ignore_covered_err */
81 # uncoverable branch false class:ignore_covered_err
82 # uncoverable statement class:ignore_covered_err
83 # uncoverable branch true class:ignore_covered_err
84 # uncoverable condition left count:1 class:ignore_covered_err
85 # uncoverable condition right count:1 class:ignore_covered_err
86 } else if (SvOK(v)) {
87 STRLEN bl;
88 const char *bs = SvPV(v, bl); /* # uncoverable statement class:ignore_covered_err */
89 # uncoverable statement class:ignore_covered_err
90 # uncoverable branch true class:ignore_covered_err
91 # uncoverable branch false class:ignore_covered_err
92 # uncoverable condition left count:1 class:ignore_covered_err
93 # uncoverable condition right count:1 class:ignore_covered_err
94 # uncoverable condition left count:2 class:ignore_covered_err
95 # uncoverable condition right count:2 class:ignore_covered_err
96 if (bl > 7 && strnEQ(bs, "header:", 7)) {
97 by = 1; /* # uncoverable statement class:ignore_covered_err */
98 envkeysv = prl_header_envkey(aTHX_ bs, bl); /* # uncoverable statement class:ignore_covered_err */
99 envkey = SvPV(envkeysv, elen); /* # uncoverable statement class:ignore_covered_err */
100 }
101 /* anything else (including 'ip') keeps by = 0 */
102 }
103 }
104 }
105  
106 /* tag default names the counter namespace so two rules never share a
107 * counter: the header env key, or 'fn' / 'ip'. */
108 # uncoverable branch false class:ignore_covered_err
109 # uncoverable condition right count:1 class:ignore_covered_err
110 if (!tag) {
111 # uncoverable branch true
112 # uncoverable condition left count:1 class:ignore_covered_err
113 if (by == 2) { tag = "fn"; tlen = 2; }
114 # uncoverable branch true
115 # uncoverable condition left count:1 class:ignore_covered_err
116 else if (by == 1) { tag = envkey; tlen = elen; }
117 else { tag = "ip"; tlen = 2; }
118 }
119  
120 prl_install(aTHX_ self, limit, window, by, envkey, elen,
121 forp, flen, tag, tlen, byfn);
122 RETVAL = newSVsv(self); /* chainable */
123 }
124 OUTPUT:
125 RETVAL
« xs/mrows.xs ↑ Summary xs/future.xs »