include/punk/punk_ratelimit.h

filter: Statementstmt 97.0% 166 / 171 Branchbran 62.8% 123 / 194 (16 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.9% 289 / 365 (16 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_auth.h ↑ Summary xs/abi.xs »
1 #ifndef PUNK_RATELIMIT_H
2 #define PUNK_RATELIMIT_H
3  
4 /* The rate_limit policy, in C. `rate_limit` (a Punk::App method, forwarded
5 * from the keyword) captures a rule's config into a magic-CV closure and
6 * pushes it onto the before_dispatch chain; the closure body, prl_check_cb,
7 * runs on every matching request with no Perl frame of its own - it reads the
8 * env, builds the key, calls Hyperman's shared arena through the ABI
9 * (punk_hm), and either short-circuits with a 429 or returns nothing.
10 *
11 * Requires punk_context.h (pcx_av / PCX_ENV), punk_app.h (app_hash / hooks),
12 * punk_static.h (punk_closure / punk_clos_cap) and punk_wsconn.h (punk_hm) -
13 * all included before this file in Punk.xs.
14 *
15 * Fails open: with no Hyperman >= ABI v3 under the app, ratelimit_hit is
16 * absent and every request is allowed. */
17  
18 #include <ctype.h>
19 #include <time.h>
20 #include <string.h>
21  
22 /* Capture slots for a rule (an AV owned by the closure). */
23 #define PRL_LIMIT 0 /* IV: requests per window (<= 0 unlimited) */
24 #define PRL_WINDOW 1 /* IV: window seconds */
25 #define PRL_BY 2 /* IV: 0 ip, 1 header, 2 coderef */
26 #define PRL_ENVKEY 3 /* PV: env key for header mode (e.g. HTTP_...) */
27 #define PRL_FOR 4 /* PV: path prefix, or "" for all */
28 #define PRL_TAG 5 /* PV: counter namespace */
29 #define PRL_BYFN 6 /* SV: the coderef for by-mode 2, else undef */
30  
31 /* the most of a header or coderef id that becomes part of the counter key */
32 #define PRL_ID_MAX 256
33  
34 /* The before_dispatch body for one rule. ST(0) is $c; a reference return
35 * short-circuits the request (punk_dispatch.h), so a 429 answers here. */
36 XS_INTERNAL(prl_check_cb);
37 114 XS_INTERNAL(prl_check_cb) {
38 114 dXSARGS;
39 114 AV *cap = punk_clos_cap(aTHX_ cv);
40 SV *c;
41 IV limit, window, by;
42 114 STRLEN elen, flen, tlen, idlen = 0;
43 114 const char *envkey, *forp, *tag, *id = NULL;
44 114 SV *idsv = NULL, *keysv;
45 AV *cav;
46 SV **e;
47 HV *env;
48 const hm_abi *A;
49 114 IV rem = 0, reset = 0;
50 int ok;
51  
52 /*
53 # uncoverable branch true
54 # uncoverable condition left count:1 class:ignore_covered_err
55 # uncoverable condition left count:2 class:ignore_covered_err */
56 114 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 114 0 114
if (!cap || items < 1) XSRETURN_EMPTY; 0 114 114
57 114 c = ST(0);
58  
59 114 limit = SvIV(*av_fetch(cap, PRL_LIMIT, 0));
60 114 window = SvIV(*av_fetch(cap, PRL_WINDOW, 0));
61 114 by = SvIV(*av_fetch(cap, PRL_BY, 0));
62 114 envkey = SvPV(*av_fetch(cap, PRL_ENVKEY, 0), elen);
63 114 ▶ forp = SvPV(*av_fetch(cap, PRL_FOR, 0), flen);
Branch: 1 branch
BranchTrueFalseTotal
forp = SvPV(*av_fetch(cap, PRL_FOR, 0), flen); 114 0 114
64 114 ▶ tag = SvPV(*av_fetch(cap, PRL_TAG, 0), tlen);
Branch: 1 branch
BranchTrueFalseTotal
tag = SvPV(*av_fetch(cap, PRL_TAG, 0), tlen); 114 0 114
65  
66 114 cav = pcx_av(aTHX_ c);
67 114 ▶ e = cav ? av_fetch(cav, PCX_ENV, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
e = cav ? av_fetch(cav, PCX_ENV, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 114 0 114
68 /*
69 # uncoverable branch true
70 # uncoverable condition left count:1 class:ignore_covered_err
71 # uncoverable condition left count:2 class:ignore_covered_err
72 # uncoverable condition left count:3 class:ignore_covered_err
73 # uncoverable condition left count:4 class:ignore_covered_err */
74 114 ▶ if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) XSRETURN_EMPTY;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) XSRETURN_EMPTY; 114 0 114
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) XSRETURN_EMPTY; 114 0 114
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) XSRETURN_EMPTY; 114 0 114
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) XSRETURN_EMPTY; 0 114 114
75 114 env = (HV *)SvRV(*e);
76  
77 /* `for` prefix: only paths under it are limited by this rule */
78 114 ▶ if (flen) {
Branch: 1 branch
BranchTrueFalseTotal
if (flen) { 43 71 114
79 43 SV **p = hv_fetchs(env, "PATH_INFO", 0);
80 43 STRLEN plen = 0;
81 /*
82 # uncoverable condition right count:1 class:ignore_covered_err
83 # uncoverable condition right count:2 class:ignore_covered_err
84 # uncoverable condition right count:3 class:ignore_covered_err */
85 86 ▶ const char *path = (p && *p && SvOK(*p)) ? SvPV(*p, plen) : "";
Branch: 3 branches
BranchTrueFalseTotal
const char *path = (p && *p && SvOK(*p)) ? SvPV(*p, plen) : ""; 43 0 43
const char *path = (p && *p && SvOK(*p)) ? SvPV(*p, plen) : ""; 43 0 43
const char *path = (p && *p && SvOK(*p)) ? SvPV(*p, plen) : ""; 43 0 43
86 43 ▶ if (plen < flen || memcmp(path, forp, flen) != 0) XSRETURN_EMPTY; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (plen < flen || memcmp(path, forp, flen) != 0) XSRETURN_EMPTY; /* # uncoverable condition left count:2 class:ignore_covered_err */ 31 12 43
if (plen < flen || memcmp(path, forp, flen) != 0) XSRETURN_EMPTY; /* # uncoverable condition left count:2 class:ignore_covered_err */ 3 28 31
87 }
88  
89 /* the caller's identity for this rule */
90 99 ▶ if (by == 2) { /* coderef */
Branch: 1 branch
BranchTrueFalseTotal
if (by == 2) { /* coderef */ 13 86 99
91 13 SV **fp = av_fetch(cap, PRL_BYFN, 0);
92 int count;
93 13 ENTER; SAVETMPS;
94 13 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 13 13
95 13 ▶ XPUSHs(c);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(c); 0 13 13
96 13 PUTBACK;
97 /*
98 # uncoverable condition right count:1 class:ignore_covered_err
99 # uncoverable condition right count:2 class:ignore_covered_err */
100 13 ▶ count = call_sv(fp && *fp ? *fp : &PL_sv_undef, G_SCALAR);
Branch: 2 branches
BranchTrueFalseTotal
count = call_sv(fp && *fp ? *fp : &PL_sv_undef, G_SCALAR); 13 0 13
count = call_sv(fp && *fp ? *fp : &PL_sv_undef, G_SCALAR); 13 0 13
101 13 SPAGAIN;
102 13 ▶ idsv = count > 0 ? newSVsv(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
idsv = count > 0 ? newSVsv(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 13 0 13
103 13 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 13 0 13
104 /*
105 # uncoverable branch true
106 # uncoverable condition left count:1 class:ignore_covered_err
107 # uncoverable condition left count:2 class:ignore_covered_err */
108 13 ▶ if (!idsv || !SvOK(idsv)) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; }
Branch: 2 branches
BranchTrueFalseTotal
if (!idsv || !SvOK(idsv)) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; } 13 0 13
if (!idsv || !SvOK(idsv)) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; } 0 13 13
109 13 id = SvPV(idsv, idlen);
110 } else { /* ip (0) or header (1) */
111 15 SV **ev = (by == 1) ? hv_fetch(env, envkey, (I32)elen, 0)
112 86 ▶ : hv_fetchs(env, "REMOTE_ADDR", 0);
Branch: 1 branch
BranchTrueFalseTotal
: hv_fetchs(env, "REMOTE_ADDR", 0); 15 71 86
113 /* A header rule with no header falls back to the address: a client
114 * that simply omits the header is still one client, and a rule that
115 * limited nothing for it was no limit at all. */
116 /*
117 # uncoverable branch true class:ignore_covered_err
118 # uncoverable condition left count:1 class:ignore_covered_err
119 # uncoverable condition left count:2 class:ignore_covered_err
120 # uncoverable condition left count:3 class:ignore_covered_err
121 # uncoverable condition left count:4 class:ignore_covered_err
122 # uncoverable condition left count:5 class:ignore_covered_err */
123 86 ▶ if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev)))
Branch: 5 branches
BranchTrueFalseTotal
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 15 71 86
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 12 3 15
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 12 0 12
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 12 0 12
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 0 12 12
124 3 ev = hv_fetchs(env, "REMOTE_ADDR", 0); /* # uncoverable statement class:ignore_covered_err */
125 /*
126 # uncoverable condition left count:2 class:ignore_covered_err
127 # uncoverable condition left count:3 class:ignore_covered_err */
128 86 ▶ if (!(ev && *ev && SvOK(*ev))) XSRETURN_EMPTY;
Branch: 3 branches
BranchTrueFalseTotal
if (!(ev && *ev && SvOK(*ev))) XSRETURN_EMPTY; 62 24 86
if (!(ev && *ev && SvOK(*ev))) XSRETURN_EMPTY; 62 0 62
if (!(ev && *ev && SvOK(*ev))) XSRETURN_EMPTY; 0 62 62
129 124 id = SvPV(*ev, idlen);
130 }
131 /*
132 # uncoverable branch true
133 # uncoverable condition left count:1 class:ignore_covered_err */
134 75 ▶ if (!idlen) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; }
Branch: 1 branch
BranchTrueFalseTotal
if (!idlen) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; } 0 75 75
135 /* an id is a key in a shared table, not a place to put a header's worth
136 * of bytes per request */
137 /*
138 # uncoverable branch true
139 # uncoverable condition left count:1 class:ignore_covered_err */
140 75 ▶ if (idlen > PRL_ID_MAX) idlen = PRL_ID_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (idlen > PRL_ID_MAX) idlen = PRL_ID_MAX; 0 75 75
141  
142 /* key = "rl\0" tag "\0" id - NUL-joined so rules and ids never collide */
143 75 keysv = sv_2mortal(newSVpvs("rl"));
144 75 sv_catpvn(keysv, "\0", 1);
145 75 sv_catpvn(keysv, tag, tlen);
146 75 sv_catpvn(keysv, "\0", 1);
147 75 sv_catpvn(keysv, id, idlen);
148 75 SvREFCNT_dec(idsv);
149  
150 /* resolved: punk_hm's environment check is a scan of every variable,
151 * per request, and the answer cannot change once the table is found */
152 75 A = punk_hm_resolved(aTHX);
153 /*
154 # uncoverable branch true class:ignore_covered_err
155 # uncoverable condition left count:1 class:ignore_covered_err
156 # uncoverable condition left count:2 class:ignore_covered_err */
157 75 ▶ if (!(A && A->ratelimit_hit)) XSRETURN_EMPTY; /* fail open */
Branch: 2 branches
BranchTrueFalseTotal
if (!(A && A->ratelimit_hit)) XSRETURN_EMPTY; /* fail open */ 69 6 75
if (!(A && A->ratelimit_hit)) XSRETURN_EMPTY; /* fail open */ 0 69 69
158 {
159 STRLEN klen;
160 69 const char *kp = SvPV(keysv, klen);
161 69 ok = A->ratelimit_hit(kp, klen, limit, window, &rem, &reset);
162 }
163 69 ▶ if (ok) XSRETURN_EMPTY; /* within the limit */
Branch: 1 branch
BranchTrueFalseTotal
if (ok) XSRETURN_EMPTY; /* within the limit */ 49 20 69
164  
165 /* over: answer 429 with Retry-After and the X-RateLimit-* headers */
166 {
167 20 long now = (long)time(NULL);
168 20 long retry = (long)reset - now;
169 20 AV *hdr = newAV();
170 20 AV *body = newAV();
171 20 AV *resp = newAV();
172 /*
173 # uncoverable branch true
174 # uncoverable condition left count:1 class:ignore_covered_err */
175 20 ▶ if (retry < 0) retry = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (retry < 0) retry = 0; 0 20 20
176 20 av_push(hdr, newSVpvs("Content-Type"));
177 20 av_push(hdr, newSVpvs("application/problem+json"));
178 20 av_push(hdr, newSVpvs("Retry-After"));
179 20 av_push(hdr, newSViv(retry));
180 20 av_push(hdr, newSVpvs("X-RateLimit-Limit"));
181 20 av_push(hdr, newSViv(limit));
182 20 av_push(hdr, newSVpvs("X-RateLimit-Remaining"));
183 20 av_push(hdr, newSViv(0));
184 20 av_push(hdr, newSVpvs("X-RateLimit-Reset"));
185 20 av_push(hdr, newSViv(reset));
186 20 av_push(body, newSVpvs(
187 "{\"type\":\"about:blank\",\"title\":\"Too Many Requests\","
188 "\"status\":429,\"detail\":\"rate limit exceeded\"}"));
189 20 av_push(resp, newSViv(429));
190 20 av_push(resp, newRV_noinc((SV *)hdr));
191 20 av_push(resp, newRV_noinc((SV *)body));
192 20 ST(0) = sv_2mortal(newRV_noinc((SV *)resp));
193 20 XSRETURN(1);
194 }
195 }
196  
197 /* Build the capture and push a rule's closure onto the app's before_dispatch
198 * chain. Called once, from the rate_limit XSUB. */
199 24 static void prl_install(pTHX_ SV *self, IV limit, IV window, IV by,
200 const char *envkey, STRLEN elen,
201 const char *forp, STRLEN flen,
202 const char *tag, STRLEN tlen, SV *byfn) {
203 24 AV *cap = newAV();
204 SV *closure;
205 HV *hooks;
206 SV **slot;
207  
208 24 av_extend(cap, PRL_BYFN);
209 24 (void)av_store(cap, PRL_LIMIT, newSViv(limit));
210 24 (void)av_store(cap, PRL_WINDOW, newSViv(window));
211 24 (void)av_store(cap, PRL_BY, newSViv(by));
212 /*
213 # uncoverable condition right count:1 class:ignore_covered_err
214 # uncoverable condition right count:2 class:ignore_covered_err */
215 24 ▶ (void)av_store(cap, PRL_ENVKEY, newSVpvn(envkey ? envkey : "", envkey ? elen : 0));
Branch: 2 branches
BranchTrueFalseTotal
(void)av_store(cap, PRL_ENVKEY, newSVpvn(envkey ? envkey : "", envkey ? elen : 0)); 24 0 24
(void)av_store(cap, PRL_ENVKEY, newSVpvn(envkey ? envkey : "", envkey ? elen : 0)); 24 0 24
216 /*
217 # uncoverable condition right count:1 class:ignore_covered_err
218 # uncoverable condition right count:2 class:ignore_covered_err */
219 24 ▶ (void)av_store(cap, PRL_FOR, newSVpvn(forp ? forp : "", forp ? flen : 0));
Branch: 2 branches
BranchTrueFalseTotal
(void)av_store(cap, PRL_FOR, newSVpvn(forp ? forp : "", forp ? flen : 0)); 17 7 24
(void)av_store(cap, PRL_FOR, newSVpvn(forp ? forp : "", forp ? flen : 0)); 17 7 24
220 /*
221 # uncoverable condition right count:1 class:ignore_covered_err
222 # uncoverable condition right count:2 class:ignore_covered_err */
223 24 ▶ (void)av_store(cap, PRL_TAG, newSVpvn(tag ? tag : "", tag ? tlen : 0));
Branch: 2 branches
BranchTrueFalseTotal
(void)av_store(cap, PRL_TAG, newSVpvn(tag ? tag : "", tag ? tlen : 0)); 24 0 24
(void)av_store(cap, PRL_TAG, newSVpvn(tag ? tag : "", tag ? tlen : 0)); 24 0 24
224 /*
225 # uncoverable condition left count:1 class:ignore_covered_err
226 # uncoverable condition left count:2 class:ignore_covered_err
227 # uncoverable condition right count:2 class:ignore_covered_err */
228 24 ▶ (void)av_store(cap, PRL_BYFN, (by == 2 && byfn) ? newSVsv(byfn) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
(void)av_store(cap, PRL_BYFN, (by == 2 && byfn) ? newSVsv(byfn) : newSV(0)); 1 23 24
(void)av_store(cap, PRL_BYFN, (by == 2 && byfn) ? newSVsv(byfn) : newSV(0)); 1 0 1
229  
230 24 closure = punk_closure(aTHX_ prl_check_cb, cap); /* takes cap; +1 coderef */
231 24 hooks = app_hash(aTHX_ app_hv(aTHX_ self), K_HOOKS);
232 24 slot = hv_fetchs(hooks, K_BEFORE_D, 0);
233 /*
234 # uncoverable branch false
235 # uncoverable condition right count:1 class:ignore_covered_err
236 # uncoverable condition right count:2 class:ignore_covered_err
237 # uncoverable condition right count:3 class:ignore_covered_err
238 # uncoverable condition right count:4 class:ignore_covered_err */
239 24 ▶ if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) 24 0 24
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) 24 0 24
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) 24 0 24
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) 24 0 24
240 24 av_push((AV *)SvRV(*slot), closure); /* AV owns it now */
241 else
242 0 SvREFCNT_dec(closure); /* # uncoverable statement */
243 24 }
244  
245 /* "header:X-Api-Key" -> the env key it arrives under, as a mortal SV. */
246 4 static SV *prl_header_envkey(pTHX_ const char *bs, STRLEN bl) {
247 4 SV *k = sv_2mortal(newSVpvs("HTTP_"));
248 STRLEN j;
249 34 ▶ for (j = 7; j < bl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 7; j < bl; j++) { 30 4 34
250 30 char ch = bs[j];
251 30 ▶ ch = (ch == '-') ? '_' : (char)toupper((unsigned char)ch);
Branch: 1 branch
BranchTrueFalseTotal
ch = (ch == '-') ? '_' : (char)toupper((unsigned char)ch); 23 7 30
252 30 sv_catpvn(k, &ch, 1);
253 }
254 4 return k;
255 }
256  
257 /* ---- boot: compile the route-level rate_limit options --------------------- */
258  
259 /* The `rate_limit` route option, compiled the way `validate` is: one guard
260 * closure appended to the record, running the same prl_check_cb the keyword
261 * installs on before_dispatch. A route that declares nothing is untouched and
262 * the whole pass costs nothing.
263 *
264 * The counter namespace defaults to this route's own method and path, so two
265 * routes with the same budget do not spend each other's. Naming a `tag`
266 * shares one deliberately, which is how three routes get one budget between
267 * them:
268 *
269 * my %auth = ( limit => 5, window => 60, tag => 'auth' );
270 * post '/login' => $t, { rate_limit => \%auth };
271 * post '/register' => $t, { rate_limit => \%auth };
272 * post '/forgot' => $t, { rate_limit => \%auth };
273 */
274 8 static void prl_compile_routes(pTHX_ SV *self) {
275 8 HV *h = (HV *)SvRV(self);
276 8 SV **rlp = hv_fetchs(h, K_RL_ROUTES, 0);
277 AV *rls;
278 HV *by;
279 SSize_t i, n;
280  
281 /*
282 # uncoverable branch true
283 # uncoverable condition left count:1 class:ignore_covered_err
284 # uncoverable condition left count:2 class:ignore_covered_err
285 # uncoverable condition left count:3 class:ignore_covered_err
286 # uncoverable condition left count:4 class:ignore_covered_err */
287 8 ▶ if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV)) 8 0 8
if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV)) 8 0 8
if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV)) 8 0 8
if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV)) 0 8 8
288 0 return; /* # uncoverable statement */
289 8 rls = (AV *)SvRV(*rlp);
290 8 n = av_len(rls) + 1;
291 /*
292 # uncoverable branch true
293 # uncoverable condition left count:1 class:ignore_covered_err */
294 8 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 8 8
295 8 by = pk_route_index(aTHX_ self, "rate_limit");
296  
297 20 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 18 2 20
298 18 SV **rp = av_fetch(rls, i, 0);
299 HV *rr, *rec, *spec;
300 SV **m, **p, **sp, **x;
301 SV *where;
302 HE *he;
303 18 IV limit = 60, window = 60, by_mode = 0;
304 18 SV *byfn = NULL, *envkeysv = NULL, *tagsv;
305 18 const char *envkey = "";
306 18 STRLEN elen = 0;
307 AV *cap;
308  
309 /*
310 # uncoverable branch true
311 # uncoverable condition left count:1 class:ignore_covered_err
312 # uncoverable condition left count:2 class:ignore_covered_err
313 # uncoverable condition left count:3 class:ignore_covered_err
314 # uncoverable condition left count:4 class:ignore_covered_err */
315 18 ▶ if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 18 0 18
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 18 0 18
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 18 0 18
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 0 18 18
316 0 continue; /* # uncoverable statement */
317 18 rr = (HV *)SvRV(*rp);
318 18 m = hv_fetchs(rr, K_METHOD, 0);
319 18 p = hv_fetchs(rr, K_PATH, 0);
320 18 sp = hv_fetchs(rr, K_RATE_LIMIT, 0);
321 /*
322 # uncoverable branch true
323 # uncoverable condition left count:1 class:ignore_covered_err
324 # uncoverable condition left count:2 class:ignore_covered_err
325 # uncoverable condition left count:3 class:ignore_covered_err
326 # uncoverable condition left count:4 class:ignore_covered_err
327 # uncoverable condition left count:5 class:ignore_covered_err
328 # uncoverable condition left count:6 class:ignore_covered_err
329 # uncoverable condition left count:7 class:ignore_covered_err
330 # uncoverable condition left count:8 class:ignore_covered_err */
331 18 ▶ if (!(m && *m && p && *p && sp && *sp
Branch: 6 branches
BranchTrueFalseTotal
if (!(m && *m && p && *p && sp && *sp 18 0 18
if (!(m && *m && p && *p && sp && *sp 18 0 18
if (!(m && *m && p && *p && sp && *sp 18 0 18
if (!(m && *m && p && *p && sp && *sp 18 0 18
if (!(m && *m && p && *p && sp && *sp 18 0 18
if (!(m && *m && p && *p && sp && *sp 18 0 18
332 18 ▶ && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
&& SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVHV)) 18 0 18
&& SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVHV)) 0 18 18
333 0 continue; /* # uncoverable statement */
334 18 spec = (HV *)SvRV(*sp);
335  
336 18 where = sv_2mortal(newSVsv(*m));
337 18 sv_catpvs(where, " ");
338 18 sv_catsv(where, *p);
339 18 he = hv_fetch_ent(by, where, 0, 0);
340 /*
341 # uncoverable branch true
342 # uncoverable condition left count:1 class:ignore_covered_err */
343 18 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 0 18 18
344 0 croak("Punk: rate_limit on unknown route %s", SvPV_nolen(where)); /* # uncoverable statement */
345 18 rec = (HV *)SvRV(HeVAL(he));
346  
347 18 x = hv_fetchs(spec, "limit", 0);
348 /*
349 # uncoverable branch false class:ignore_covered_err
350 # uncoverable condition right count:1 class:ignore_covered_err
351 # uncoverable condition right count:2 class:ignore_covered_err */
352 18 ▶ if (x && *x) {
Branch: 2 branches
BranchTrueFalseTotal
if (x && *x) { 16 2 18
if (x && *x) { 16 0 16
353 16 ▶ if (!SvOK(*x) || !looks_like_number(*x)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(*x) || !looks_like_number(*x)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 16 0 16
if (!SvOK(*x) || !looks_like_number(*x)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 15 16
354 1 croak("Punk: rate_limit limit on %s is a count of requests",
355 SvPV_nolen(where));
356 15 limit = SvIV(*x);
357 }
358 17 x = hv_fetchs(spec, "window", 0);
359 17 ▶ if (x && *x) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (x && *x) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 7 17
if (x && *x) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
360 /*
361 # uncoverable condition left count:1 class:ignore_covered_err
362 # uncoverable condition left count:2 class:ignore_covered_err */
363 10 ▶ if (!SvOK(*x) || !looks_like_number(*x) || SvIV(*x) <= 0)
Branch: 3 branches
BranchTrueFalseTotal
if (!SvOK(*x) || !looks_like_number(*x) || SvIV(*x) <= 0) 10 0 10
if (!SvOK(*x) || !looks_like_number(*x) || SvIV(*x) <= 0) 10 0 10
if (!SvOK(*x) || !looks_like_number(*x) || SvIV(*x) <= 0) 2 8 10
364 2 croak("Punk: rate_limit window on %s is a positive number "
365 "of seconds", SvPV_nolen(where));
366 8 window = SvIV(*x);
367 }
368 15 x = hv_fetchs(spec, "by", 0);
369 /*
370 # uncoverable condition right count:2 class:ignore_covered_err
371 # uncoverable condition right count:3 class:ignore_covered_err */
372 15 ▶ if (x && *x && SvOK(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) { 7 8 15
if (x && *x && SvOK(*x)) { 7 0 7
if (x && *x && SvOK(*x)) { 7 0 7
373 7 ▶ if (SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVCV) { 3 4 7
if (SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVCV) { 2 1 3
374 2 by_mode = 2; byfn = *x;
375 }
376 5 ▶ else if (SvROK(*x))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(*x)) 1 4 5
377 1 croak("Punk: rate_limit by on %s is 'ip', 'header:NAME' or "
378 "a coderef", SvPV_nolen(where));
379 else {
380 STRLEN bl;
381 4 const char *bs = SvPV(*x, bl);
382 4 ▶ if (bl > 7 && strnEQ(bs, "header:", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (bl > 7 && strnEQ(bs, "header:", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 2 4
if (bl > 7 && strnEQ(bs, "header:", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
383 2 by_mode = 1;
384 2 envkeysv = prl_header_envkey(aTHX_ bs, bl);
385 2 envkey = SvPV(envkeysv, elen);
386 }
387 /*
388 # uncoverable branch false
389 # uncoverable condition right count:1 class:ignore_covered_err
390 # uncoverable condition left count:2 class:ignore_covered_err
391 # uncoverable condition right count:2 class:ignore_covered_err */
392 2 ▶ else if (!(bl == 2 && memEQ(bs, "ip", 2)))
Branch: 2 branches
BranchTrueFalseTotal
else if (!(bl == 2 && memEQ(bs, "ip", 2))) 0 2 2
else if (!(bl == 2 && memEQ(bs, "ip", 2))) 0 0 0
393 4 croak("Punk: rate_limit by on %s is 'ip', 'header:NAME' "
394 "or a coderef, not '%.*s'", SvPV_nolen(where),
395 (int)bl, bs);
396 }
397 }
398 12 x = hv_fetchs(spec, "tag", 0);
399 /*
400 # uncoverable condition right count:2 class:ignore_covered_err
401 # uncoverable condition right count:3 class:ignore_covered_err
402 # uncoverable condition right count:4 class:ignore_covered_err */
403 12 ▶ if (x && *x && SvOK(*x) && SvCUR(*x)) {
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x) && SvCUR(*x)) { 4 8 12
if (x && *x && SvOK(*x) && SvCUR(*x)) { 4 0 4
if (x && *x && SvOK(*x) && SvCUR(*x)) { 4 0 4
if (x && *x && SvOK(*x) && SvCUR(*x)) { 4 0 4
404 4 tagsv = sv_2mortal(newSVsv(*x));
405 }
406 else {
407 /* this route's own namespace, so one route's retries are not
408 * charged to another with the same limit */
409 8 tagsv = sv_2mortal(newSVpvs("route "));
410 8 sv_catsv(tagsv, where);
411 }
412  
413 12 cap = newAV();
414 12 av_extend(cap, PRL_BYFN);
415 12 (void)av_store(cap, PRL_LIMIT, newSViv(limit));
416 12 (void)av_store(cap, PRL_WINDOW, newSViv(window));
417 12 (void)av_store(cap, PRL_BY, newSViv(by_mode));
418 12 (void)av_store(cap, PRL_ENVKEY, newSVpvn(envkey, elen));
419 12 (void)av_store(cap, PRL_FOR, newSVpvs("")); /* the route is it */
420 12 (void)av_store(cap, PRL_TAG, newSVsv(tagsv));
421 12 ▶ (void)av_store(cap, PRL_BYFN,
Branch: 2 branches
BranchTrueFalseTotal
(void)av_store(cap, PRL_BYFN, 2 10 12
(void)av_store(cap, PRL_BYFN, 2 0 2
422 (by_mode == 2 && byfn) ? newSVsv(byfn) : newSV(0));
423 12 pk_route_guard_push(aTHX_ rec,
424 punk_closure(aTHX_ prl_check_cb, cap));
425 }
426 }
427  
428 #endif /* PUNK_RATELIMIT_H */
« include/punk/punk_auth.h ↑ Summary xs/abi.xs »