include/punk/punk_ratelimit.h

filter: Statementstmt 95.9% 164 / 171 Branchbran 61.8% 120 / 194 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.8% 284 / 365 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/model.xs ↑ Summary xs/auth.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 94 XS_INTERNAL(prl_check_cb) {
38 94 dXSARGS;
39 94 AV *cap = punk_clos_cap(aTHX_ cv);
40 SV *c;
41 IV limit, window, by;
42 94 STRLEN elen, flen, tlen, idlen = 0;
43 94 const char *envkey, *forp, *tag, *id = NULL;
44 94 SV *idsv = NULL, *keysv;
45 AV *cav;
46 SV **e;
47 HV *env;
48 const hm_abi *A;
49 94 IV rem = 0, reset = 0;
50 int ok;
51  
52 94 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 94 0 94
if (!cap || items < 1) XSRETURN_EMPTY; 0 94 94
53 94 c = ST(0);
54  
55 94 limit = SvIV(*av_fetch(cap, PRL_LIMIT, 0));
56 94 window = SvIV(*av_fetch(cap, PRL_WINDOW, 0));
57 94 by = SvIV(*av_fetch(cap, PRL_BY, 0));
58 94 envkey = SvPV(*av_fetch(cap, PRL_ENVKEY, 0), elen);
59 94 ▶ forp = SvPV(*av_fetch(cap, PRL_FOR, 0), flen);
Branch: 1 branch
BranchTrueFalseTotal
forp = SvPV(*av_fetch(cap, PRL_FOR, 0), flen); 94 0 94
60 94 ▶ tag = SvPV(*av_fetch(cap, PRL_TAG, 0), tlen);
Branch: 1 branch
BranchTrueFalseTotal
tag = SvPV(*av_fetch(cap, PRL_TAG, 0), tlen); 94 0 94
61  
62 94 cav = pcx_av(aTHX_ c);
63 94 ▶ e = cav ? av_fetch(cav, PCX_ENV, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
e = cav ? av_fetch(cav, PCX_ENV, 0) : NULL; 94 0 94
64 94 ▶ 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; 94 0 94
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) XSRETURN_EMPTY; 94 0 94
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) XSRETURN_EMPTY; 94 0 94
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) XSRETURN_EMPTY; 0 94 94
65 94 env = (HV *)SvRV(*e);
66  
67 /* `for` prefix: only paths under it are limited by this rule */
68 94 ▶ if (flen) {
Branch: 1 branch
BranchTrueFalseTotal
if (flen) { 33 61 94
69 33 SV **p = hv_fetchs(env, "PATH_INFO", 0);
70 33 STRLEN plen = 0;
71 66 ▶ const char *path = (p && *p && SvOK(*p)) ? SvPV(*p, plen) : "";
Branch: 3 branches
BranchTrueFalseTotal
const char *path = (p && *p && SvOK(*p)) ? SvPV(*p, plen) : ""; 33 0 33
const char *path = (p && *p && SvOK(*p)) ? SvPV(*p, plen) : ""; 33 0 33
const char *path = (p && *p && SvOK(*p)) ? SvPV(*p, plen) : ""; 33 0 33
72 33 ▶ if (plen < flen || memcmp(path, forp, flen) != 0) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (plen < flen || memcmp(path, forp, flen) != 0) XSRETURN_EMPTY; 28 5 33
if (plen < flen || memcmp(path, forp, flen) != 0) XSRETURN_EMPTY; 3 25 28
73 }
74  
75 /* the caller's identity for this rule */
76 86 ▶ if (by == 2) { /* coderef */
Branch: 1 branch
BranchTrueFalseTotal
if (by == 2) { /* coderef */ 10 76 86
77 10 SV **fp = av_fetch(cap, PRL_BYFN, 0);
78 int count;
79 10 ENTER; SAVETMPS;
80 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
81 10 ▶ XPUSHs(c);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(c); 0 10 10
82 10 PUTBACK;
83 10 ▶ 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); 10 0 10
count = call_sv(fp && *fp ? *fp : &PL_sv_undef, G_SCALAR); 10 0 10
84 10 SPAGAIN;
85 10 ▶ idsv = count > 0 ? newSVsv(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
idsv = count > 0 ? newSVsv(POPs) : NULL; 10 0 10
86 10 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 10 0 10
87 10 ▶ if (!idsv || !SvOK(idsv)) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; }
Branch: 2 branches
BranchTrueFalseTotal
if (!idsv || !SvOK(idsv)) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; } 10 0 10
if (!idsv || !SvOK(idsv)) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; } 0 10 10
88 10 id = SvPV(idsv, idlen);
89 } else { /* ip (0) or header (1) */
90 11 SV **ev = (by == 1) ? hv_fetch(env, envkey, (I32)elen, 0)
91 76 ▶ : hv_fetchs(env, "REMOTE_ADDR", 0);
Branch: 1 branch
BranchTrueFalseTotal
: hv_fetchs(env, "REMOTE_ADDR", 0); 11 65 76
92 /* A header rule with no header falls back to the address: a client
93 * that simply omits the header is still one client, and a rule that
94 * limited nothing for it was no limit at all. */
95 76 ▶ if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev)))
Branch: 5 branches
BranchTrueFalseTotal
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 11 65 76
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 8 3 11
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 8 0 8
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 8 0 8
if (by == 1 && !(ev && *ev && SvOK(*ev) && SvCUR(*ev))) 0 8 8
96 3 ev = hv_fetchs(env, "REMOTE_ADDR", 0);
97 76 ▶ if (!(ev && *ev && SvOK(*ev))) XSRETURN_EMPTY;
Branch: 3 branches
BranchTrueFalseTotal
if (!(ev && *ev && SvOK(*ev))) XSRETURN_EMPTY; 55 21 76
if (!(ev && *ev && SvOK(*ev))) XSRETURN_EMPTY; 55 0 55
if (!(ev && *ev && SvOK(*ev))) XSRETURN_EMPTY; 0 55 55
98 110 id = SvPV(*ev, idlen);
99 }
100 65 ▶ if (!idlen) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; }
Branch: 1 branch
BranchTrueFalseTotal
if (!idlen) { SvREFCNT_dec(idsv); XSRETURN_EMPTY; } 0 65 65
101 /* an id is a key in a shared table, not a place to put a header's worth
102 * of bytes per request */
103 65 ▶ if (idlen > PRL_ID_MAX) idlen = PRL_ID_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (idlen > PRL_ID_MAX) idlen = PRL_ID_MAX; 0 65 65
104  
105 /* key = "rl\0" tag "\0" id - NUL-joined so rules and ids never collide */
106 65 keysv = sv_2mortal(newSVpvs("rl"));
107 65 sv_catpvn(keysv, "\0", 1);
108 65 sv_catpvn(keysv, tag, tlen);
109 65 sv_catpvn(keysv, "\0", 1);
110 65 sv_catpvn(keysv, id, idlen);
111 65 SvREFCNT_dec(idsv);
112  
113 /* resolved: punk_hm's environment check is a scan of every variable,
114 * per request, and the answer cannot change once the table is found */
115 65 A = punk_hm_resolved(aTHX);
116 65 ▶ if (!(A && A->ratelimit_hit)) XSRETURN_EMPTY; /* fail open */
Branch: 2 branches
BranchTrueFalseTotal
if (!(A && A->ratelimit_hit)) XSRETURN_EMPTY; /* fail open */ 59 6 65
if (!(A && A->ratelimit_hit)) XSRETURN_EMPTY; /* fail open */ 0 59 59
117 {
118 STRLEN klen;
119 59 const char *kp = SvPV(keysv, klen);
120 59 ok = A->ratelimit_hit(kp, klen, limit, window, &rem, &reset);
121 }
122 59 ▶ if (ok) XSRETURN_EMPTY; /* within the limit */
Branch: 1 branch
BranchTrueFalseTotal
if (ok) XSRETURN_EMPTY; /* within the limit */ 42 17 59
123  
124 /* over: answer 429 with Retry-After and the X-RateLimit-* headers */
125 {
126 17 long now = (long)time(NULL);
127 17 long retry = (long)reset - now;
128 17 AV *hdr = newAV();
129 17 AV *body = newAV();
130 17 AV *resp = newAV();
131 17 ▶ if (retry < 0) retry = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (retry < 0) retry = 0; 0 17 17
132 17 av_push(hdr, newSVpvs("Content-Type"));
133 17 av_push(hdr, newSVpvs("application/problem+json"));
134 17 av_push(hdr, newSVpvs("Retry-After"));
135 17 av_push(hdr, newSViv(retry));
136 17 av_push(hdr, newSVpvs("X-RateLimit-Limit"));
137 17 av_push(hdr, newSViv(limit));
138 17 av_push(hdr, newSVpvs("X-RateLimit-Remaining"));
139 17 av_push(hdr, newSViv(0));
140 17 av_push(hdr, newSVpvs("X-RateLimit-Reset"));
141 17 av_push(hdr, newSViv(reset));
142 17 av_push(body, newSVpvs(
143 "{\"type\":\"about:blank\",\"title\":\"Too Many Requests\","
144 "\"status\":429,\"detail\":\"rate limit exceeded\"}"));
145 17 av_push(resp, newSViv(429));
146 17 av_push(resp, newRV_noinc((SV *)hdr));
147 17 av_push(resp, newRV_noinc((SV *)body));
148 17 ST(0) = sv_2mortal(newRV_noinc((SV *)resp));
149 17 XSRETURN(1);
150 }
151 }
152  
153 /* Build the capture and push a rule's closure onto the app's before_dispatch
154 * chain. Called once, from the rate_limit XSUB. */
155 23 static void prl_install(pTHX_ SV *self, IV limit, IV window, IV by,
156 const char *envkey, STRLEN elen,
157 const char *forp, STRLEN flen,
158 const char *tag, STRLEN tlen, SV *byfn) {
159 23 AV *cap = newAV();
160 SV *closure;
161 HV *hooks;
162 SV **slot;
163  
164 23 av_extend(cap, PRL_BYFN);
165 23 (void)av_store(cap, PRL_LIMIT, newSViv(limit));
166 23 (void)av_store(cap, PRL_WINDOW, newSViv(window));
167 23 (void)av_store(cap, PRL_BY, newSViv(by));
168 23 ▶ (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)); 23 0 23
(void)av_store(cap, PRL_ENVKEY, newSVpvn(envkey ? envkey : "", envkey ? elen : 0)); 23 0 23
169 23 ▶ (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)); 16 7 23
(void)av_store(cap, PRL_FOR, newSVpvn(forp ? forp : "", forp ? flen : 0)); 16 7 23
170 23 ▶ (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)); 23 0 23
(void)av_store(cap, PRL_TAG, newSVpvn(tag ? tag : "", tag ? tlen : 0)); 23 0 23
171 23 ▶ (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 22 23
(void)av_store(cap, PRL_BYFN, (by == 2 && byfn) ? newSVsv(byfn) : newSV(0)); 1 0 1
172  
173 23 closure = punk_closure(aTHX_ prl_check_cb, cap); /* takes cap; +1 coderef */
174 23 hooks = app_hash(aTHX_ app_hv(aTHX_ self), K_HOOKS);
175 23 slot = hv_fetchs(hooks, K_BEFORE_D, 0);
176 23 ▶ if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) 23 0 23
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) 23 0 23
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) 23 0 23
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) 23 0 23
177 23 av_push((AV *)SvRV(*slot), closure); /* AV owns it now */
178 else
179 0 SvREFCNT_dec(closure);
180 23 }
181  
182 /* "header:X-Api-Key" -> the env key it arrives under, as a mortal SV. */
183 3 static SV *prl_header_envkey(pTHX_ const char *bs, STRLEN bl) {
184 3 SV *k = sv_2mortal(newSVpvs("HTTP_"));
185 STRLEN j;
186 24 ▶ for (j = 7; j < bl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 7; j < bl; j++) { 21 3 24
187 21 char ch = bs[j];
188 21 ▶ ch = (ch == '-') ? '_' : (char)toupper((unsigned char)ch);
Branch: 1 branch
BranchTrueFalseTotal
ch = (ch == '-') ? '_' : (char)toupper((unsigned char)ch); 16 5 21
189 21 sv_catpvn(k, &ch, 1);
190 }
191 3 return k;
192 }
193  
194 /* ---- boot: compile the route-level rate_limit options --------------------- */
195  
196 /* The `rate_limit` route option, compiled the way `validate` is: one guard
197 * closure appended to the record, running the same prl_check_cb the keyword
198 * installs on before_dispatch. A route that declares nothing is untouched and
199 * the whole pass costs nothing.
200 *
201 * The counter namespace defaults to this route's own method and path, so two
202 * routes with the same budget do not spend each other's. Naming a `tag`
203 * shares one deliberately, which is how three routes get one budget between
204 * them:
205 *
206 * my %auth = ( limit => 5, window => 60, tag => 'auth' );
207 * post '/login' => $t, { rate_limit => \%auth };
208 * post '/register' => $t, { rate_limit => \%auth };
209 * post '/forgot' => $t, { rate_limit => \%auth };
210 */
211 3 static void prl_compile_routes(pTHX_ SV *self) {
212 3 HV *h = (HV *)SvRV(self);
213 3 SV **rlp = hv_fetchs(h, K_RL_ROUTES, 0);
214 AV *rls;
215 HV *by;
216 SSize_t i, n;
217  
218 3 ▶ if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV)) 3 0 3
if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV)) 3 0 3
if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV)) 3 0 3
if (!(rlp && *rlp && SvROK(*rlp) && SvTYPE(SvRV(*rlp)) == SVt_PVAV)) 0 3 3
219 0 return;
220 3 rls = (AV *)SvRV(*rlp);
221 3 n = av_len(rls) + 1;
222 3 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 3 3
223 3 by = pk_route_index(aTHX_ self, "rate_limit");
224  
225 11 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 1 11
226 10 SV **rp = av_fetch(rls, i, 0);
227 HV *rr, *rec, *spec;
228 SV **m, **p, **sp, **x;
229 SV *where;
230 HE *he;
231 10 IV limit = 60, window = 60, by_mode = 0;
232 10 SV *byfn = NULL, *envkeysv = NULL, *tagsv;
233 10 const char *envkey = "";
234 10 STRLEN elen = 0;
235 AV *cap;
236  
237 10 ▶ if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 10 0 10
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 10 0 10
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 10 0 10
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 0 10 10
238 0 continue;
239 10 rr = (HV *)SvRV(*rp);
240 10 m = hv_fetchs(rr, K_METHOD, 0);
241 10 p = hv_fetchs(rr, K_PATH, 0);
242 10 sp = hv_fetchs(rr, K_RATE_LIMIT, 0);
243 10 ▶ if (!(m && *m && p && *p && sp && *sp
Branch: 6 branches
BranchTrueFalseTotal
if (!(m && *m && p && *p && sp && *sp 10 0 10
if (!(m && *m && p && *p && sp && *sp 10 0 10
if (!(m && *m && p && *p && sp && *sp 10 0 10
if (!(m && *m && p && *p && sp && *sp 10 0 10
if (!(m && *m && p && *p && sp && *sp 10 0 10
if (!(m && *m && p && *p && sp && *sp 10 0 10
244 10 ▶ && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
&& SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVHV)) 10 0 10
&& SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVHV)) 0 10 10
245 0 continue;
246 10 spec = (HV *)SvRV(*sp);
247  
248 10 where = sv_2mortal(newSVsv(*m));
249 10 sv_catpvs(where, " ");
250 10 sv_catsv(where, *p);
251 10 he = hv_fetch_ent(by, where, 0, 0);
252 10 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 0 10 10
253 0 croak("Punk: rate_limit on unknown route %s", SvPV_nolen(where));
254 10 rec = (HV *)SvRV(HeVAL(he));
255  
256 10 x = hv_fetchs(spec, "limit", 0);
257 10 ▶ if (x && *x) {
Branch: 2 branches
BranchTrueFalseTotal
if (x && *x) { 8 2 10
if (x && *x) { 8 0 8
258 8 ▶ if (!SvOK(*x) || !looks_like_number(*x))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(*x) || !looks_like_number(*x)) 8 0 8
if (!SvOK(*x) || !looks_like_number(*x)) 0 8 8
259 0 croak("Punk: rate_limit limit on %s is a count of requests",
260 SvPV_nolen(where));
261 8 limit = SvIV(*x);
262 }
263 10 x = hv_fetchs(spec, "window", 0);
264 10 ▶ if (x && *x) {
Branch: 2 branches
BranchTrueFalseTotal
if (x && *x) { 5 5 10
if (x && *x) { 5 0 5
265 5 ▶ if (!SvOK(*x) || !looks_like_number(*x) || SvIV(*x) <= 0)
Branch: 3 branches
BranchTrueFalseTotal
if (!SvOK(*x) || !looks_like_number(*x) || SvIV(*x) <= 0) 5 0 5
if (!SvOK(*x) || !looks_like_number(*x) || SvIV(*x) <= 0) 5 0 5
if (!SvOK(*x) || !looks_like_number(*x) || SvIV(*x) <= 0) 1 4 5
266 1 croak("Punk: rate_limit window on %s is a positive number "
267 "of seconds", SvPV_nolen(where));
268 4 window = SvIV(*x);
269 }
270 9 x = hv_fetchs(spec, "by", 0);
271 9 ▶ if (x && *x && SvOK(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) { 3 6 9
if (x && *x && SvOK(*x)) { 3 0 3
if (x && *x && SvOK(*x)) { 3 0 3
272 3 ▶ if (SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVCV) { 1 2 3
if (SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVCV) { 1 0 1
273 1 by_mode = 2; byfn = *x;
274 }
275 2 ▶ else if (SvROK(*x))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(*x)) 0 2 2
276 0 croak("Punk: rate_limit by on %s is 'ip', 'header:NAME' or "
277 "a coderef", SvPV_nolen(where));
278 else {
279 STRLEN bl;
280 2 const char *bs = SvPV(*x, bl);
281 2 ▶ if (bl > 7 && strnEQ(bs, "header:", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (bl > 7 && strnEQ(bs, "header:", 7)) { 1 1 2
if (bl > 7 && strnEQ(bs, "header:", 7)) { 1 0 1
282 1 by_mode = 1;
283 1 envkeysv = prl_header_envkey(aTHX_ bs, bl);
284 1 envkey = SvPV(envkeysv, elen);
285 }
286 1 ▶ else if (!(bl == 2 && memEQ(bs, "ip", 2)))
Branch: 2 branches
BranchTrueFalseTotal
else if (!(bl == 2 && memEQ(bs, "ip", 2))) 0 1 1
else if (!(bl == 2 && memEQ(bs, "ip", 2))) 0 0 0
287 2 croak("Punk: rate_limit by on %s is 'ip', 'header:NAME' "
288 "or a coderef, not '%.*s'", SvPV_nolen(where),
289 (int)bl, bs);
290 }
291 }
292 8 x = hv_fetchs(spec, "tag", 0);
293 8 ▶ if (x && *x && SvOK(*x) && SvCUR(*x)) {
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x) && SvCUR(*x)) { 2 6 8
if (x && *x && SvOK(*x) && SvCUR(*x)) { 2 0 2
if (x && *x && SvOK(*x) && SvCUR(*x)) { 2 0 2
if (x && *x && SvOK(*x) && SvCUR(*x)) { 2 0 2
294 2 tagsv = sv_2mortal(newSVsv(*x));
295 }
296 else {
297 /* this route's own namespace, so one route's retries are not
298 * charged to another with the same limit */
299 6 tagsv = sv_2mortal(newSVpvs("route "));
300 6 sv_catsv(tagsv, where);
301 }
302  
303 8 cap = newAV();
304 8 av_extend(cap, PRL_BYFN);
305 8 (void)av_store(cap, PRL_LIMIT, newSViv(limit));
306 8 (void)av_store(cap, PRL_WINDOW, newSViv(window));
307 8 (void)av_store(cap, PRL_BY, newSViv(by_mode));
308 8 (void)av_store(cap, PRL_ENVKEY, newSVpvn(envkey, elen));
309 8 (void)av_store(cap, PRL_FOR, newSVpvs("")); /* the route is it */
310 8 (void)av_store(cap, PRL_TAG, newSVsv(tagsv));
311 8 ▶ (void)av_store(cap, PRL_BYFN,
Branch: 2 branches
BranchTrueFalseTotal
(void)av_store(cap, PRL_BYFN, 1 7 8
(void)av_store(cap, PRL_BYFN, 1 0 1
312 (by_mode == 2 && byfn) ? newSVsv(byfn) : newSV(0));
313 8 pk_route_guard_push(aTHX_ rec,
314 punk_closure(aTHX_ prl_check_cb, cap));
315 }
316 }
317  
318 #endif /* PUNK_RATELIMIT_H */
« xs/model.xs ↑ Summary xs/auth.xs »