| 1 |
|
| /* punk_route.h - the C web router. |
| 2 |
|
| * |
| 3 |
|
| * Compiled once at to_app from the frozen Perl route table. The whole match |
| 4 |
|
| * path now lives here: an exact-match table for static routes, typed-segment |
| 5 |
|
| * records for dynamic ones (:param / *splat, held in one malloc'd arena and |
| 6 |
|
| * walked with memcmp - no regex engine), and the 405 Allow computation on a |
| 7 |
|
| * miss. lib/Punk/Router.pm is documentation only; t/0111 pins this router's |
| 8 |
|
| * behaviour. The dynamic walk is linear in declaration order (first match |
| 9 |
|
| * wins) and costs about 6 ns per record scanned, measured on dyn50. |
| 10 |
|
| * |
| 11 |
|
| * Every record carries `idx`, its position in the one Perl array of compiled |
| 12 |
|
| * records (code + guards) the dispatcher indexes into; a static hit, a |
| 13 |
|
| * dynamic hit and the reference tier all speak that same index. |
| 14 |
|
| * |
| 15 |
|
| * Semantics mirror the Perl exactly: :param captures one non-empty segment, |
| 16 |
|
| * *splat captures the non-empty remainder including slashes, HEAD matches GET, |
| 17 |
|
| * ANY matches every method, and Allow lists the methods a path answers under. |
| 18 |
|
| */ |
| 19 |
|
| |
| 20 |
|
| #ifndef PUNK_ROUTE_H |
| 21 |
|
| #define PUNK_ROUTE_H |
| 22 |
|
| |
| 23 |
|
| #define PR_LIT 0 |
| 24 |
|
| #define PR_PARAM 1 |
| 25 |
|
| #define PR_SPLAT 2 |
| 26 |
|
| |
| 27 |
|
| #define PR_MAX_SEGS 64 |
| 28 |
|
| |
| 29 |
|
| typedef struct { |
| 30 |
|
| int type; |
| 31 |
|
| char *lit; /* PR_LIT: the bytes to match */ |
| 32 |
|
| STRLEN litlen; |
| 33 |
|
| SV *name; /* PR_PARAM / PR_SPLAT: capture name (owned) */ |
| 34 |
|
| } pr_seg; |
| 35 |
|
| |
| 36 |
|
| typedef struct { |
| 37 |
|
| char *method; /* uppercase (kept even for ANY, for Allow) */ |
| 38 |
|
| STRLEN mlen; |
| 39 |
|
| int is_any; /* matches every method */ |
| 40 |
|
| int is_get; /* HEAD falls back onto these */ |
| 41 |
|
| int nsegs; |
| 42 |
|
| int splat; /* last segment is a splat */ |
| 43 |
|
| pr_seg *segs; |
| 44 |
|
| IV idx; /* position in the Perl @recs array */ |
| 45 |
|
| } pr_rec; |
| 46 |
|
| |
| 47 |
|
| typedef struct { |
| 48 |
|
| AV *raw; /* accumulation: { method, path, target, guards } |
| 49 |
|
| * hashrefs pushed by add(), unresolved */ |
| 50 |
|
| int compiled; |
| 51 |
|
| int n; |
| 52 |
|
| pr_rec *recs; /* dynamic records */ |
| 53 |
|
| int nrecs; /* entries in `records` (and in dyn_of) */ |
| 54 |
|
| IV *dyn_of; /* record index -> its position in recs, or -1 for |
| 55 |
|
| * a static route. `recs` is indexed by dynamic |
| 56 |
|
| * position and `records` by record index, and |
| 57 |
|
| * named routes (punk_url.h) arrive holding the |
| 58 |
|
| * second and needing the first. Filled in the one |
| 59 |
|
| * loop that fills recs; nothing per request. */ |
| 60 |
|
| HV *statics; /* "METHOD path" -> IV record index */ |
| 61 |
|
| HV *static_paths; /* path -> AV of uppercase method SVs (for Allow) */ |
| 62 |
|
| AV *records; /* per-route { code, guards, method, path } hashrefs, |
| 63 |
|
| * indexed by the record index match returns */ |
| 64 |
|
| } pr_router; |
| 65 |
|
| |
| 66 |
1137 |
| static pr_router *pr_new(pTHX) { |
| 67 |
|
| pr_router *rt; |
| 68 |
1137 |
| Newxz(rt, 1, pr_router); |
| 69 |
1137 |
| rt->raw = newAV(); |
| 70 |
1137 |
| return rt; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| /* the methods an ANY route answers */ |
| 74 |
|
| static const char *const PR_ANY_METHODS[] = |
| 75 |
|
| { "GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS" }; |
| 76 |
|
| #define PR_N_ANY 7 |
| 77 |
|
| |
| 78 |
|
| /* Split a path on '/' (skipping the leading one) into borrowed segment |
| 79 |
|
| * pointers/lengths. Returns the count, or -1 if it does not start with '/' |
| 80 |
|
| * or overflows PR_MAX_SEGS. */ |
| 81 |
107832 |
| static int pr_split(const char *path, STRLEN plen, |
| 82 |
|
| const char **seg_p, STRLEN *seg_l) { |
| 83 |
107832 |
| int nsegs = 0; |
| 84 |
|
| STRLEN j; |
| 85 |
|
| const char *start; |
| 86 |
107832 |
▶ | if (!plen || path[0] != '/') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!plen || path[0] != '/') return -1; |
107832 |
0 |
107832 |
| if (!plen || path[0] != '/') return -1; |
0 |
107832 |
107832 |
|
| 87 |
107832 |
| start = path + 1; |
| 88 |
1070912 |
▶ | for (j = 1; j <= plen; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 1; j <= plen; j++) { |
963080 |
107832 |
1070912 |
|
| 89 |
963080 |
▶ | if (j == plen || path[j] == '/') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (j == plen || path[j] == '/') { |
855248 |
107832 |
963080 |
| if (j == plen || path[j] == '/') { |
105333 |
749915 |
855248 |
|
| 90 |
213165 |
▶ | if (nsegs == PR_MAX_SEGS) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nsegs == PR_MAX_SEGS) return -1; |
0 |
213165 |
213165 |
|
| 91 |
213165 |
| seg_p[nsegs] = start; |
| 92 |
213165 |
| seg_l[nsegs] = (STRLEN)((path + j) - start); |
| 93 |
213165 |
| nsegs++; |
| 94 |
213165 |
| start = path + j + 1; |
| 95 |
|
| } |
| 96 |
|
| } |
| 97 |
107832 |
| return nsegs; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| /* Do a record's segments match these request segments? Method-agnostic, no |
| 101 |
|
| * captures - the shared core of hit matching and the Allow scan. */ |
| 102 |
107609 |
| static int pr_segs_match(pr_rec *r, const char *path, STRLEN plen, |
| 103 |
|
| const char **seg_p, STRLEN *seg_l, int nsegs) { |
| 104 |
|
| int k; |
| 105 |
107609 |
▶ | if (r->splat ? (nsegs < r->nsegs) : (nsegs != r->nsegs)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (r->splat ? (nsegs < r->nsegs) : (nsegs != r->nsegs)) return 0; |
32 |
107577 |
107609 |
| if (r->splat ? (nsegs < r->nsegs) : (nsegs != r->nsegs)) return 0; |
364 |
107245 |
107609 |
|
| 106 |
319629 |
▶ | for (k = 0; k < r->nsegs; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < r->nsegs; k++) { |
212448 |
107181 |
319629 |
|
| 107 |
212448 |
| pr_seg *s = &r->segs[k]; |
| 108 |
212448 |
▶ | if (s->type == PR_LIT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->type == PR_LIT) { |
105240 |
107208 |
212448 |
|
| 109 |
105240 |
▶ | if (seg_l[k] != s->litlen |
Branch: 1 branch
| Branch | True | False | Total |
| if (seg_l[k] != s->litlen |
105221 |
19 |
105240 |
|
| 110 |
105221 |
▶ | || (s->litlen && memNE(seg_p[k], s->lit, s->litlen))) |
Branch: 2 branches
| Branch | True | False | Total |
| || (s->litlen && memNE(seg_p[k], s->lit, s->litlen))) |
105221 |
0 |
105221 |
| || (s->litlen && memNE(seg_p[k], s->lit, s->litlen))) |
21 |
105200 |
105221 |
|
| 111 |
40 |
| return 0; |
| 112 |
|
| } |
| 113 |
107208 |
▶ | else if (s->type == PR_PARAM) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (s->type == PR_PARAM) { |
107201 |
7 |
107208 |
|
| 114 |
107201 |
▶ | if (!seg_l[k]) return 0; /* [^/]+ needs one char */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!seg_l[k]) return 0; /* [^/]+ needs one char */ |
17 |
107184 |
107201 |
|
| 115 |
|
| } |
| 116 |
|
| else { /* PR_SPLAT (always last): non-empty remainder */ |
| 117 |
7 |
| return ((STRLEN)((path + plen) - seg_p[k])) ? 1 : 0; |
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
107181 |
| return 1; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
107550 |
| static int pr_method_ok(pr_rec *r, const char *method, STRLEN mlen, int is_head) { |
| 124 |
107550 |
▶ | if (r->is_any) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->is_any) return 1; |
0 |
107550 |
107550 |
|
| 125 |
107550 |
▶ | if (mlen == r->mlen && memEQ(method, r->method, mlen)) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mlen == r->mlen && memEQ(method, r->method, mlen)) return 1; |
107389 |
161 |
107550 |
| if (mlen == r->mlen && memEQ(method, r->method, mlen)) return 1; |
107270 |
119 |
107389 |
|
| 126 |
280 |
▶ | if (is_head && r->is_get) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (is_head && r->is_get) return 1; |
2 |
278 |
280 |
| if (is_head && r->is_get) return 1; |
2 |
0 |
2 |
|
| 127 |
278 |
| return 0; |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
|
| /* Parse one route path into a record's typed segments. */ |
| 131 |
98 |
| static void pr_parse_path(pTHX_ pr_rec *r, const char *p, STRLEN pl) { |
| 132 |
|
| const char *seg_start; |
| 133 |
|
| STRLEN j; |
| 134 |
98 |
| int ns = 0, k; |
| 135 |
1147 |
▶ | for (j = 1; j <= pl; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 1; j <= pl; j++) |
1049 |
98 |
1147 |
|
| 136 |
1049 |
▶ | if (j == pl || p[j] == '/') ns++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (j == pl || p[j] == '/') ns++; |
951 |
98 |
1049 |
| if (j == pl || p[j] == '/') ns++; |
105 |
846 |
951 |
|
| 137 |
98 |
▶ | if (ns > PR_MAX_SEGS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ns > PR_MAX_SEGS) |
0 |
98 |
98 |
|
| 138 |
0 |
| croak("punk router: '%.*s' has more than %d segments", |
| 139 |
|
| (int)pl, p, PR_MAX_SEGS); |
| 140 |
98 |
| r->nsegs = ns; |
| 141 |
98 |
▶ | Newxz(r->segs, ns ? ns : 1, pr_seg); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(r->segs, ns ? ns : 1, pr_seg); |
98 |
0 |
98 |
|
| 142 |
98 |
| seg_start = p + 1; |
| 143 |
1147 |
▶ | for (k = 0, j = 1; j <= pl; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0, j = 1; j <= pl; j++) { |
1049 |
98 |
1147 |
|
| 144 |
1049 |
▶ | if (j == pl || p[j] == '/') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (j == pl || p[j] == '/') { |
951 |
98 |
1049 |
| if (j == pl || p[j] == '/') { |
105 |
846 |
951 |
|
| 145 |
203 |
| STRLEN slen = (STRLEN)((p + j) - seg_start); |
| 146 |
203 |
| pr_seg *s = &r->segs[k]; |
| 147 |
203 |
▶ | if (slen > 1 && seg_start[0] == ':') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (slen > 1 && seg_start[0] == ':') { |
197 |
6 |
203 |
| if (slen > 1 && seg_start[0] == ':') { |
98 |
99 |
197 |
|
| 148 |
98 |
| s->type = PR_PARAM; |
| 149 |
98 |
| s->name = newSVpvn(seg_start + 1, slen - 1); |
| 150 |
|
| } |
| 151 |
105 |
▶ | else if (slen > 1 && seg_start[0] == '*') { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (slen > 1 && seg_start[0] == '*') { |
99 |
6 |
105 |
| else if (slen > 1 && seg_start[0] == '*') { |
4 |
95 |
99 |
|
| 152 |
4 |
| s->type = PR_SPLAT; |
| 153 |
4 |
| s->name = newSVpvn(seg_start + 1, slen - 1); |
| 154 |
4 |
▶ | if (k != ns - 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (k != ns - 1) |
0 |
4 |
4 |
|
| 155 |
0 |
| croak("punk router: *splat must be the last segment " |
| 156 |
|
| "in '%.*s'", (int)pl, p); |
| 157 |
4 |
| r->splat = 1; |
| 158 |
|
| } |
| 159 |
|
| else { |
| 160 |
101 |
| s->type = PR_LIT; |
| 161 |
101 |
| s->lit = savepvn(seg_start, slen); |
| 162 |
101 |
| s->litlen = slen; |
| 163 |
|
| } |
| 164 |
203 |
| k++; |
| 165 |
203 |
| seg_start = p + j + 1; |
| 166 |
|
| } |
| 167 |
|
| } |
| 168 |
98 |
| } |
| 169 |
|
| |
| 170 |
|
| /* Insert one static "METHOD path" -> idx into the exact table and the |
| 171 |
|
| * per-path Allow list, croaking on a duplicate. */ |
| 172 |
61360 |
| static void pr_add_static(pTHX_ pr_router *rt, HV *seen, |
| 173 |
|
| const char *m, STRLEN ml, |
| 174 |
|
| const char *p, STRLEN pl, IV idx) { |
| 175 |
61360 |
| STRLEN kl = ml + 1 + pl; |
| 176 |
61360 |
| SV *key = sv_2mortal(newSV(kl + 1)); |
| 177 |
|
| char *kbuf; |
| 178 |
|
| SV **slot; |
| 179 |
61360 |
| SvPOK_on(key); |
| 180 |
61360 |
| kbuf = SvPVX(key); |
| 181 |
61360 |
| memcpy(kbuf, m, ml); kbuf[ml] = ' '; |
| 182 |
61360 |
| memcpy(kbuf + ml + 1, p, pl); kbuf[kl] = '\0'; |
| 183 |
61360 |
| SvCUR_set(key, kl); |
| 184 |
61360 |
▶ | if (hv_exists_ent(seen, key, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists_ent(seen, key, 0)) |
2 |
61358 |
61360 |
|
| 185 |
2 |
| croak("Punk::Router: duplicate route %.*s %.*s", |
| 186 |
|
| (int)ml, m, (int)pl, p); |
| 187 |
61358 |
| (void)hv_store_ent(seen, key, PUNK_SET_TRUE, 0); |
| 188 |
61358 |
| (void)hv_store_ent(rt->statics, key, newSViv(idx), 0); |
| 189 |
61358 |
| slot = hv_fetch(rt->static_paths, p, (I32)pl, 1); |
| 190 |
61358 |
▶ | if (!(*slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(*slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
61358 |
0 |
61358 |
| if (!(*slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
133 |
61225 |
61358 |
| if (!(*slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
0 |
133 |
133 |
|
| 191 |
61225 |
| sv_setsv(*slot, sv_2mortal(newRV_noinc((SV *)newAV()))); |
| 192 |
61358 |
| av_push((AV *)SvRV(*slot), newSVpvn(m, ml)); |
| 193 |
61358 |
| } |
| 194 |
|
| |
| 195 |
|
| /* Compile the whole route table in C from one AV of resolved routes - |
| 196 |
|
| * each [ METHOD, path, code, guards ]. Classifies static vs dynamic (a ':' |
| 197 |
|
| * or '*' anywhere makes it dynamic), expands ANY static routes per method, |
| 198 |
|
| * rejects duplicates and a misplaced *splat, parses dynamic paths into typed |
| 199 |
|
| * segments, and builds the exact table, the per-path Allow lists, the |
| 200 |
|
| * dynamic records and the per-route { code, guards, method, path } record |
| 201 |
|
| * hashrefs the dispatcher indexes into. The only work left in Perl is |
| 202 |
|
| * resolving the targets to those coderefs. */ |
| 203 |
|
| /* Call the app's Perl $resolve->($target, $desc) to turn a route target or |
| 204 |
|
| * guard into a coderef. Returns the resolved SV (+1). A bad target croaks |
| 205 |
|
| * inside $resolve, unwinding to_app - the boot-time contract. */ |
| 206 |
61380 |
| static SV *pr_resolve(pTHX_ SV *resolve, SV *target, SV *desc) { |
| 207 |
61380 |
| dSP; |
| 208 |
|
| SV *ret; |
| 209 |
|
| int count; |
| 210 |
61380 |
| ENTER; SAVETMPS; |
| 211 |
61380 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
61380 |
61380 |
|
| 212 |
61380 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
61380 |
61380 |
|
| 213 |
61380 |
| PUSHs(target); |
| 214 |
61380 |
| PUSHs(desc); |
| 215 |
61380 |
| PUTBACK; |
| 216 |
61380 |
| count = call_sv(resolve, G_SCALAR); |
| 217 |
61375 |
| SPAGAIN; |
| 218 |
61375 |
▶ | ret = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| ret = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
61375 |
0 |
61375 |
|
| 219 |
61375 |
| PUTBACK; |
| 220 |
61375 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
61375 |
0 |
61375 |
|
| 221 |
61375 |
| return ret; |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| /* One { method, path, target, guards } record -> a resolved |
| 225 |
|
| * [ method, path, code, \@guards ] route pushed onto `routes`. */ |
| 226 |
61343 |
| static void pr_add_route(pTHX_ AV *routes, SV *resolve, HV *rec) { |
| 227 |
61343 |
| SV **m = hv_fetchs(rec, "method", 0); |
| 228 |
61343 |
| SV **p = hv_fetchs(rec, "path", 0); |
| 229 |
61343 |
| SV **t = hv_fetchs(rec, "target", 0); |
| 230 |
61343 |
| SV **g = hv_fetchs(rec, "guards", 0); |
| 231 |
61343 |
▶ | SV *mv = (m && *m) ? *m : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *mv = (m && *m) ? *m : &PL_sv_undef; |
61343 |
0 |
61343 |
| SV *mv = (m && *m) ? *m : &PL_sv_undef; |
61343 |
0 |
61343 |
|
| 232 |
61343 |
▶ | SV *pv = (p && *p) ? *p : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| SV *pv = (p && *p) ? *p : &PL_sv_undef; |
61343 |
0 |
61343 |
| SV *pv = (p && *p) ? *p : &PL_sv_undef; |
61343 |
0 |
61343 |
|
| 233 |
61343 |
| AV *route = newAV(); |
| 234 |
61343 |
| AV *gav = newAV(); |
| 235 |
122686 |
▶ | SV *desc = sv_2mortal(newSVpvf("route %s %s", |
Branch: 1 branch
| Branch | True | False | Total |
| SV *desc = sv_2mortal(newSVpvf("route %s %s", |
61343 |
0 |
61343 |
|
| 236 |
|
| SvPV_nolen(mv), SvPV_nolen(pv))); |
| 237 |
61343 |
| av_push(route, newSVsv(mv)); |
| 238 |
61343 |
| av_push(route, newSVsv(pv)); |
| 239 |
61343 |
▶ | av_push(route, pr_resolve(aTHX_ resolve, (t && *t) ? *t : &PL_sv_undef, desc)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(route, pr_resolve(aTHX_ resolve, (t && *t) ? *t : &PL_sv_undef, desc)); |
61343 |
0 |
61343 |
| av_push(route, pr_resolve(aTHX_ resolve, (t && *t) ? *t : &PL_sv_undef, desc)); |
61343 |
0 |
61343 |
|
| 240 |
61338 |
▶ | if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { |
61330 |
8 |
61338 |
| if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { |
61330 |
0 |
61330 |
| if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { |
61330 |
0 |
61330 |
| if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { |
61330 |
0 |
61330 |
|
| 241 |
61330 |
| AV *gs = (AV *)SvRV(*g); |
| 242 |
61330 |
| SSize_t i, n = av_len(gs) + 1; |
| 243 |
61330 |
| SV *gdesc = sv_2mortal(newSVpvf("guard on %s", SvPV_nolen(pv))); |
| 244 |
61367 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
37 |
61330 |
61367 |
|
| 245 |
37 |
| SV **gp = av_fetch(gs, i, 0); |
| 246 |
37 |
▶ | av_push(gav, pr_resolve(aTHX_ resolve, |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(gav, pr_resolve(aTHX_ resolve, |
37 |
0 |
37 |
| av_push(gav, pr_resolve(aTHX_ resolve, |
37 |
0 |
37 |
|
| 247 |
|
| (gp && *gp) ? *gp : &PL_sv_undef, gdesc)); |
| 248 |
|
| } |
| 249 |
|
| } |
| 250 |
61338 |
| av_push(route, newRV_noinc((SV *)gav)); |
| 251 |
61338 |
| av_push(routes, newRV_noinc((SV *)route)); |
| 252 |
61338 |
| } |
| 253 |
|
| |
| 254 |
654 |
| static void pr_build(pTHX_ pr_router *rt, AV *routes) { |
| 255 |
654 |
| int n = (int)(av_len(routes) + 1), i, dn = 0, di = 0; |
| 256 |
654 |
| HV *seen = (HV *)sv_2mortal((SV *)newHV()); |
| 257 |
654 |
| rt->statics = newHV(); |
| 258 |
654 |
| rt->static_paths = newHV(); |
| 259 |
654 |
| rt->records = newAV(); |
| 260 |
654 |
| rt->nrecs = n; |
| 261 |
654 |
▶ | if (n > 0) av_extend(rt->records, n - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) av_extend(rt->records, n - 1); |
534 |
120 |
654 |
|
| 262 |
654 |
▶ | Newx(rt->dyn_of, n ? n : 1, IV); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(rt->dyn_of, n ? n : 1, IV); |
534 |
120 |
654 |
|
| 263 |
61992 |
▶ | for (i = 0; i < n; i++) rt->dyn_of[i] = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) rt->dyn_of[i] = -1; |
61338 |
654 |
61992 |
|
| 264 |
|
| |
| 265 |
61992 |
▶ | for (i = 0; i < n; i++) { /* count dynamics for the record array */ |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { /* count dynamics for the record array */ |
61338 |
654 |
61992 |
|
| 266 |
61338 |
| SV **e = av_fetch(routes, i, 0); |
| 267 |
|
| AV *t; SV **psv; STRLEN pl; const char *p; |
| 268 |
61338 |
▶ | if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) |
61338 |
0 |
61338 |
| if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) |
61338 |
0 |
61338 |
| if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) |
61338 |
0 |
61338 |
| if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) |
0 |
61338 |
61338 |
|
| 269 |
0 |
| croak("Punk::Router::_compile: route %d is not an arrayref", i); |
| 270 |
61338 |
| t = (AV *)SvRV(*e); |
| 271 |
61338 |
| psv = av_fetch(t, 1, 0); |
| 272 |
61338 |
| p = SvPV_const(*psv, pl); |
| 273 |
61338 |
▶ | if (memchr(p, ':', pl) || memchr(p, '*', pl)) dn++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (memchr(p, ':', pl) || memchr(p, '*', pl)) dn++; |
61244 |
94 |
61338 |
| if (memchr(p, ':', pl) || memchr(p, '*', pl)) dn++; |
4 |
61240 |
61244 |
|
| 274 |
|
| } |
| 275 |
654 |
▶ | Newxz(rt->recs, dn ? dn : 1, pr_rec); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(rt->recs, dn ? dn : 1, pr_rec); |
74 |
580 |
654 |
|
| 276 |
|
| |
| 277 |
61990 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
61338 |
652 |
61990 |
|
| 278 |
61338 |
| SV **e = av_fetch(routes, i, 0); |
| 279 |
61338 |
| AV *t = (AV *)SvRV(*e); |
| 280 |
61338 |
| SV **msv = av_fetch(t, 0, 0), **psv = av_fetch(t, 1, 0); |
| 281 |
61338 |
| SV **csv = av_fetch(t, 2, 0), **gsv = av_fetch(t, 3, 0); |
| 282 |
|
| STRLEN ml, pl; |
| 283 |
61338 |
| const char *m = SvPV_const(*msv, ml); |
| 284 |
61338 |
▶ | const char *p = SvPV_const(*psv, pl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *p = SvPV_const(*psv, pl); |
61338 |
0 |
61338 |
|
| 285 |
61338 |
▶ | int is_any = (ml == 3 && memEQ(m, "ANY", 3)); |
Branch: 2 branches
| Branch | True | False | Total |
| int is_any = (ml == 3 && memEQ(m, "ANY", 3)); |
61161 |
177 |
61338 |
| int is_any = (ml == 3 && memEQ(m, "ANY", 3)); |
20 |
61141 |
61161 |
|
| 286 |
61338 |
| HV *rec = newHV(); |
| 287 |
|
| |
| 288 |
61338 |
▶ | (void)hv_stores(rec, "code", csv && *csv ? SvREFCNT_inc(*csv) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_stores(rec, "code", csv && *csv ? SvREFCNT_inc(*csv) : newSV(0)); |
61338 |
0 |
61338 |
| (void)hv_stores(rec, "code", csv && *csv ? SvREFCNT_inc(*csv) : newSV(0)); |
61338 |
0 |
61338 |
|
| 289 |
61338 |
▶ | (void)hv_stores(rec, "guards", gsv && *gsv ? SvREFCNT_inc(*gsv) : newRV_noinc((SV *)newAV())); |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_stores(rec, "guards", gsv && *gsv ? SvREFCNT_inc(*gsv) : newRV_noinc((SV *)newAV())); |
61338 |
0 |
61338 |
| (void)hv_stores(rec, "guards", gsv && *gsv ? SvREFCNT_inc(*gsv) : newRV_noinc((SV *)newAV())); |
61338 |
0 |
61338 |
|
| 290 |
61338 |
| (void)hv_stores(rec, "method", newSVpvn(m, ml)); |
| 291 |
61338 |
| (void)hv_stores(rec, "path", newSVpvn(p, pl)); |
| 292 |
61338 |
| (void)av_store(rt->records, i, newRV_noinc((SV *)rec)); |
| 293 |
|
| |
| 294 |
61338 |
▶ | if (!memchr(p, ':', pl) && !memchr(p, '*', pl)) { /* static */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!memchr(p, ':', pl) && !memchr(p, '*', pl)) { /* static */ |
61244 |
94 |
61338 |
| if (!memchr(p, ':', pl) && !memchr(p, '*', pl)) { /* static */ |
61240 |
4 |
61244 |
|
| 295 |
61240 |
▶ | if (is_any) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_any) { |
20 |
61220 |
61240 |
|
| 296 |
|
| int k; |
| 297 |
160 |
▶ | for (k = 0; k < PR_N_ANY; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < PR_N_ANY; k++) |
140 |
20 |
160 |
|
| 298 |
140 |
| pr_add_static(aTHX_ rt, seen, PR_ANY_METHODS[k], |
| 299 |
140 |
| strlen(PR_ANY_METHODS[k]), p, pl, (IV)i); |
| 300 |
|
| } |
| 301 |
61220 |
| else pr_add_static(aTHX_ rt, seen, m, ml, p, pl, (IV)i); |
| 302 |
|
| } |
| 303 |
|
| else { /* dynamic */ |
| 304 |
98 |
| pr_rec *r = &rt->recs[di]; |
| 305 |
98 |
| rt->dyn_of[i] = (IV)di; |
| 306 |
98 |
| di++; |
| 307 |
98 |
| r->method = savepvn(m, ml); |
| 308 |
98 |
| r->mlen = ml; |
| 309 |
98 |
| r->is_any = is_any; |
| 310 |
98 |
▶ | r->is_get = (ml == 3 && memEQ(m, "GET", 3)); |
Branch: 2 branches
| Branch | True | False | Total |
| r->is_get = (ml == 3 && memEQ(m, "GET", 3)); |
94 |
4 |
98 |
| r->is_get = (ml == 3 && memEQ(m, "GET", 3)); |
92 |
2 |
94 |
|
| 311 |
98 |
| r->idx = (IV)i; |
| 312 |
98 |
| pr_parse_path(aTHX_ r, p, pl); /* croaks on a misplaced *splat */ |
| 313 |
|
| } |
| 314 |
|
| } |
| 315 |
652 |
| rt->n = di; |
| 316 |
652 |
| rt->compiled = 1; |
| 317 |
652 |
| } |
| 318 |
|
| |
| 319 |
1137 |
| static void pr_free(pTHX_ pr_router *rt) { |
| 320 |
|
| int i, k; |
| 321 |
1137 |
▶ | if (!rt) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rt) return; |
0 |
1137 |
1137 |
|
| 322 |
1137 |
▶ | if (rt->raw) SvREFCNT_dec((SV *)rt->raw); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rt->raw) SvREFCNT_dec((SV *)rt->raw); |
1137 |
0 |
1137 |
|
| 323 |
1235 |
▶ | for (i = 0; i < rt->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < rt->n; i++) { |
98 |
1137 |
1235 |
|
| 324 |
98 |
| pr_rec *r = &rt->recs[i]; |
| 325 |
301 |
▶ | for (k = 0; k < r->nsegs; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < r->nsegs; k++) { |
203 |
98 |
301 |
|
| 326 |
203 |
▶ | if (r->segs[k].lit) Safefree(r->segs[k].lit); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->segs[k].lit) Safefree(r->segs[k].lit); |
101 |
102 |
203 |
|
| 327 |
203 |
▶ | if (r->segs[k].name) SvREFCNT_dec(r->segs[k].name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->segs[k].name) SvREFCNT_dec(r->segs[k].name); |
102 |
101 |
203 |
|
| 328 |
|
| } |
| 329 |
98 |
| Safefree(r->segs); |
| 330 |
98 |
▶ | if (r->method) Safefree(r->method); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->method) Safefree(r->method); |
98 |
0 |
98 |
|
| 331 |
|
| } |
| 332 |
1137 |
| Safefree(rt->recs); |
| 333 |
1137 |
| Safefree(rt->dyn_of); |
| 334 |
1137 |
▶ | if (rt->statics) SvREFCNT_dec((SV *)rt->statics); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rt->statics) SvREFCNT_dec((SV *)rt->statics); |
654 |
483 |
1137 |
|
| 335 |
1137 |
▶ | if (rt->static_paths) SvREFCNT_dec((SV *)rt->static_paths); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rt->static_paths) SvREFCNT_dec((SV *)rt->static_paths); |
654 |
483 |
1137 |
|
| 336 |
1137 |
▶ | if (rt->records) SvREFCNT_dec((SV *)rt->records); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rt->records) SvREFCNT_dec((SV *)rt->records); |
654 |
483 |
1137 |
|
| 337 |
1137 |
| Safefree(rt); |
| 338 |
|
| } |
| 339 |
|
| |
| 340 |
|
| /* Static exact match: the record index, or -1. HEAD falls back to GET. */ |
| 341 |
219534 |
| static IV pr_static_match(pTHX_ pr_router *rt, |
| 342 |
|
| const char *method, STRLEN mlen, |
| 343 |
|
| const char *path, STRLEN plen) { |
| 344 |
|
| char buf[256], *kbuf; |
| 345 |
219534 |
| STRLEN kl = mlen + 1 + plen; |
| 346 |
|
| SV **e; |
| 347 |
219534 |
| IV idx = -1; |
| 348 |
219534 |
▶ | kbuf = kl + 1 <= sizeof buf ? buf : (char *)safemalloc(kl + 1); |
Branch: 1 branch
| Branch | True | False | Total |
| kbuf = kl + 1 <= sizeof buf ? buf : (char *)safemalloc(kl + 1); |
219533 |
1 |
219534 |
|
| 349 |
219534 |
| memcpy(kbuf, method, mlen); kbuf[mlen] = ' '; |
| 350 |
219534 |
| memcpy(kbuf + mlen + 1, path, plen); |
| 351 |
219534 |
| e = hv_fetch(rt->statics, kbuf, (I32)kl, 0); |
| 352 |
219534 |
▶ | if (!e && mlen == 4 && memEQ(method, "HEAD", 4)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!e && mlen == 4 && memEQ(method, "HEAD", 4)) { |
107808 |
111726 |
219534 |
| if (!e && mlen == 4 && memEQ(method, "HEAD", 4)) { |
155 |
107653 |
107808 |
| if (!e && mlen == 4 && memEQ(method, "HEAD", 4)) { |
19 |
136 |
155 |
|
| 353 |
19 |
| memcpy(kbuf, "GET", 3); kbuf[3] = ' '; |
| 354 |
19 |
| memcpy(kbuf + 4, path, plen); |
| 355 |
19 |
| e = hv_fetch(rt->statics, kbuf, (I32)(4 + plen), 0); |
| 356 |
|
| } |
| 357 |
219534 |
▶ | if (e && *e) idx = SvIV(*e); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && *e) idx = SvIV(*e); |
111741 |
107793 |
219534 |
| if (e && *e) idx = SvIV(*e); |
111741 |
0 |
111741 |
|
| 358 |
219534 |
▶ | if (kbuf != buf) safefree(kbuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (kbuf != buf) safefree(kbuf); |
1 |
219533 |
219534 |
|
| 359 |
219534 |
| return idx; |
| 360 |
|
| } |
| 361 |
|
| |
| 362 |
|
| /* Dynamic hit: the record index (its @recs idx) and fills caps, or -1. */ |
| 363 |
107501 |
| static IV pr_match(pTHX_ pr_router *rt, |
| 364 |
|
| const char *method, STRLEN mlen, |
| 365 |
|
| const char *path, STRLEN plen, HV *caps) { |
| 366 |
|
| const char *seg_p[PR_MAX_SEGS]; |
| 367 |
|
| STRLEN seg_l[PR_MAX_SEGS]; |
| 368 |
107501 |
| int nsegs = pr_split(path, plen, seg_p, seg_l); |
| 369 |
107501 |
▶ | int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); |
Branch: 2 branches
| Branch | True | False | Total |
| int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); |
119 |
107382 |
107501 |
| int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); |
2 |
117 |
119 |
|
| 370 |
|
| int i, c; |
| 371 |
107501 |
▶ | if (nsegs < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nsegs < 0) return -1; |
0 |
107501 |
107501 |
|
| 372 |
107874 |
▶ | for (i = 0; i < rt->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < rt->n; i++) { |
107550 |
324 |
107874 |
|
| 373 |
107550 |
| pr_rec *r = &rt->recs[i]; |
| 374 |
107550 |
▶ | if (!pr_method_ok(r, method, mlen, is_head)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pr_method_ok(r, method, mlen, is_head)) continue; |
278 |
107272 |
107550 |
|
| 375 |
107272 |
▶ | if (!pr_segs_match(r, path, plen, seg_p, seg_l, nsegs)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pr_segs_match(r, path, plen, seg_p, seg_l, nsegs)) continue; |
95 |
107177 |
107272 |
|
| 376 |
319532 |
▶ | for (c = 0; c < r->nsegs; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < r->nsegs; c++) { |
212355 |
107177 |
319532 |
|
| 377 |
212355 |
| pr_seg *s = &r->segs[c]; |
| 378 |
212355 |
▶ | if (s->type == PR_PARAM) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->type == PR_PARAM) |
107175 |
105180 |
212355 |
|
| 379 |
107175 |
| (void)hv_store_ent(caps, s->name, |
| 380 |
|
| newSVpvn(seg_p[c], seg_l[c]), 0); |
| 381 |
105180 |
▶ | else if (s->type == PR_SPLAT) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (s->type == PR_SPLAT) |
5 |
105175 |
105180 |
|
| 382 |
5 |
| (void)hv_store_ent(caps, s->name, |
| 383 |
|
| newSVpvn(seg_p[c], (STRLEN)((path + plen) - seg_p[c])), 0); |
| 384 |
|
| } |
| 385 |
107177 |
| return r->idx; |
| 386 |
|
| } |
| 387 |
324 |
| return -1; |
| 388 |
|
| } |
| 389 |
|
| |
| 390 |
|
| /* The 405 Allow set for a path the request method missed: every method the |
| 391 |
|
| * path answers under (static exact + dynamic, method-agnostic), minus the |
| 392 |
|
| * request method. Pushes sorted method SVs into `allow`; returns the count. */ |
| 393 |
331 |
| static int pr_allow(pTHX_ pr_router *rt, |
| 394 |
|
| const char *method, STRLEN mlen, |
| 395 |
|
| const char *path, STRLEN plen, AV *allow) { |
| 396 |
|
| const char *seg_p[PR_MAX_SEGS]; |
| 397 |
|
| STRLEN seg_l[PR_MAX_SEGS]; |
| 398 |
331 |
| int nsegs = pr_split(path, plen, seg_p, seg_l); |
| 399 |
331 |
| HV *seen = (HV *)sv_2mortal((SV *)newHV()); |
| 400 |
|
| SV **sp; |
| 401 |
|
| int i; |
| 402 |
|
| |
| 403 |
331 |
| sp = hv_fetch(rt->static_paths, path, (I32)plen, 0); |
| 404 |
331 |
▶ | if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { |
30 |
301 |
331 |
| if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { |
30 |
0 |
30 |
| if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { |
30 |
0 |
30 |
| if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { |
30 |
0 |
30 |
|
| 405 |
30 |
| AV *ms = (AV *)SvRV(*sp); |
| 406 |
30 |
| SSize_t j, n = av_len(ms) + 1; |
| 407 |
69 |
▶ | for (j = 0; j < n; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < n; j++) { |
39 |
30 |
69 |
|
| 408 |
39 |
| SV **mv = av_fetch(ms, j, 0); |
| 409 |
39 |
▶ | if (mv && *mv) (void)hv_store_ent(seen, *mv, PUNK_SET_TRUE, 0); |
Branch: 2 branches
| Branch | True | False | Total |
| if (mv && *mv) (void)hv_store_ent(seen, *mv, PUNK_SET_TRUE, 0); |
39 |
0 |
39 |
| if (mv && *mv) (void)hv_store_ent(seen, *mv, PUNK_SET_TRUE, 0); |
39 |
0 |
39 |
|
| 410 |
|
| } |
| 411 |
|
| } |
| 412 |
331 |
▶ | if (nsegs >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nsegs >= 0) { |
331 |
0 |
331 |
|
| 413 |
668 |
▶ | for (i = 0; i < rt->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < rt->n; i++) { |
337 |
331 |
668 |
|
| 414 |
337 |
| pr_rec *r = &rt->recs[i]; |
| 415 |
337 |
▶ | if (!pr_segs_match(r, path, plen, seg_p, seg_l, nsegs)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pr_segs_match(r, path, plen, seg_p, seg_l, nsegs)) continue; |
328 |
9 |
337 |
|
| 416 |
9 |
| (void)hv_store(seen, r->method, (I32)r->mlen, PUNK_SET_TRUE, 0); |
| 417 |
|
| } |
| 418 |
|
| } |
| 419 |
|
| /* the request method is not "other" */ |
| 420 |
331 |
| (void)hv_delete(seen, method, (I32)mlen, 0); |
| 421 |
|
| |
| 422 |
|
| { |
| 423 |
331 |
| SSize_t na = 0; |
| 424 |
|
| HE *he; |
| 425 |
331 |
| hv_iterinit(seen); |
| 426 |
374 |
▶ | while ((he = hv_iternext(seen))) { av_push(allow, newSVsv(hv_iterkeysv(he))); na++; } |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(seen))) { av_push(allow, newSVsv(hv_iterkeysv(he))); na++; } |
43 |
331 |
374 |
|
| 427 |
|
| /* sort the Allow list, matching the Perl `sort keys %allow` */ |
| 428 |
331 |
▶ | if (na > 1) sortsv(AvARRAY(allow), (STRLEN)na, Perl_sv_cmp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (na > 1) sortsv(AvARRAY(allow), (STRLEN)na, Perl_sv_cmp); |
10 |
321 |
331 |
|
| 429 |
331 |
| return (int)na; |
| 430 |
|
| } |
| 431 |
|
| } |
| 432 |
|
| |
| 433 |
|
| #endif /* PUNK_ROUTE_H */ |