| 1 |
|
| /* punk_static.h - serving files from a directory. |
| 2 |
|
| * |
| 3 |
|
| * `static '/static' => 'root/static'` compiles to a PSGI coderef: a magic |
| 4 |
|
| * CV carrying the directory it serves (the closure pattern Open::API's |
| 5 |
|
| * oa_plack.h uses), whose whole request path is here - method check, the |
| 6 |
|
| * traversal guard, stat, the conditional-request comparison and the header |
| 7 |
|
| * block. The body is a real filehandle, so the server can stream or |
| 8 |
|
| * sendfile it rather than slurping the file into memory. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #ifndef PUNK_STATIC_H |
| 12 |
|
| #define PUNK_STATIC_H |
| 13 |
|
| |
| 14 |
|
| /* ---- closures: a CV carrying captured SVs -------------------------------- */ |
| 15 |
|
| |
| 16 |
|
| typedef struct punk_clos { AV *cap; } punk_clos; |
| 17 |
|
| |
| 18 |
2350 |
| static int punk_clos_free(pTHX_ SV *sv, MAGIC *mg) { |
| 19 |
2350 |
| punk_clos *c = (punk_clos *)mg->mg_ptr; |
| 20 |
|
| PERL_UNUSED_ARG(sv); |
| 21 |
|
| /* |
| 22 |
|
| # uncoverable branch false |
| 23 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 24 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 25 |
2350 |
▶ | if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
2350 |
0 |
2350 |
| if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
2350 |
0 |
2350 |
|
| 26 |
2350 |
| return 0; |
| 27 |
|
| } |
| 28 |
|
| static MGVTBL punk_clos_vtbl = { NULL, NULL, NULL, NULL, punk_clos_free, |
| 29 |
|
| NULL, NULL, NULL }; |
| 30 |
|
| |
| 31 |
62139 |
| static SV *punk_closure(pTHX_ XSUBADDR_t body, AV *cap) { |
| 32 |
62139 |
| CV *cv = (CV *)newXS(NULL, body, __FILE__); |
| 33 |
|
| punk_clos *c; |
| 34 |
62139 |
| Newxz(c, 1, punk_clos); |
| 35 |
62139 |
| c->cap = cap; /* takes ownership */ |
| 36 |
62139 |
| sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &punk_clos_vtbl, (char *)c, 0); |
| 37 |
62139 |
| return newRV_noinc((SV *)cv); |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| /* The same, but installed into a named glob rather than handed back as a |
| 41 |
|
| * coderef: for keywords, where a croak or a stack trace should name the |
| 42 |
|
| * keyword and the class it landed in rather than __ANON__. */ |
| 43 |
|
| static CV *punk_closure_named(pTHX_ const char *name, XSUBADDR_t body, AV *cap) |
| 44 |
|
| PERL_UNUSED_DECL; |
| 45 |
50 |
| static CV *punk_closure_named(pTHX_ const char *name, XSUBADDR_t body, AV *cap) { |
| 46 |
50 |
| CV *cv = newXS((char *)name, body, (char *)__FILE__); |
| 47 |
|
| punk_clos *c; |
| 48 |
50 |
| Newxz(c, 1, punk_clos); |
| 49 |
50 |
| c->cap = cap; /* takes ownership */ |
| 50 |
50 |
| sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &punk_clos_vtbl, (char *)c, 0); |
| 51 |
50 |
| return cv; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
349298 |
| static AV *punk_clos_cap(pTHX_ CV *cv) { |
| 55 |
349298 |
| MAGIC *mg = mg_findext((SV *)cv, PERL_MAGIC_ext, &punk_clos_vtbl); |
| 56 |
349298 |
▶ | return mg ? ((punk_clos *)mg->mg_ptr)->cap : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| return mg ? ((punk_clos *)mg->mg_ptr)->cap : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
349298 |
0 |
349298 |
|
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| /* ---- content types -------------------------------------------------------- */ |
| 60 |
|
| |
| 61 |
151 |
| static const char *ps_content_type(const char *path, STRLEN len) { |
| 62 |
|
| static const struct { const char *ext; const char *type; } map[] = { |
| 63 |
|
| { "html", "text/html; charset=utf-8" }, |
| 64 |
|
| { "htm", "text/html; charset=utf-8" }, |
| 65 |
|
| { "css", "text/css; charset=utf-8" }, |
| 66 |
|
| { "js", "application/javascript; charset=utf-8" }, |
| 67 |
|
| { "mjs", "application/javascript; charset=utf-8" }, |
| 68 |
|
| { "json", "application/json" }, |
| 69 |
|
| { "txt", "text/plain; charset=utf-8" }, |
| 70 |
|
| { "xml", "application/xml" }, |
| 71 |
|
| { "svg", "image/svg+xml" }, |
| 72 |
|
| { "png", "image/png" }, |
| 73 |
|
| { "jpg", "image/jpeg" }, |
| 74 |
|
| { "jpeg", "image/jpeg" }, |
| 75 |
|
| { "gif", "image/gif" }, |
| 76 |
|
| { "ico", "image/x-icon" }, |
| 77 |
|
| { "webp", "image/webp" }, |
| 78 |
|
| { "avif", "image/avif" }, |
| 79 |
|
| { "woff", "font/woff" }, |
| 80 |
|
| { "woff2","font/woff2" }, |
| 81 |
|
| { "ttf", "font/ttf" }, |
| 82 |
|
| { "otf", "font/otf" }, |
| 83 |
|
| { "pdf", "application/pdf" }, |
| 84 |
|
| { "wasm", "application/wasm" }, |
| 85 |
|
| { "map", "application/json" }, |
| 86 |
|
| { "mp4", "video/mp4" }, |
| 87 |
|
| { "webm", "video/webm" }, |
| 88 |
|
| { "mp3", "audio/mpeg" }, |
| 89 |
|
| { "zip", "application/zip" }, |
| 90 |
|
| { NULL, NULL } |
| 91 |
|
| }; |
| 92 |
151 |
| const char *dot = NULL; |
| 93 |
|
| STRLEN i; |
| 94 |
|
| char ext[16]; |
| 95 |
151 |
| int n = 0; |
| 96 |
|
| /* |
| 97 |
|
| # uncoverable branch false |
| 98 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 99 |
624 |
▶ | for (i = len; i > 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = len; i > 0; i--) { |
624 |
0 |
624 |
|
| 100 |
624 |
▶ | if (path[i - 1] == '/') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (path[i - 1] == '/') break; |
2 |
622 |
624 |
|
| 101 |
622 |
▶ | if (path[i - 1] == '.') { dot = path + i; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (path[i - 1] == '.') { dot = path + i; break; } |
149 |
473 |
622 |
|
| 102 |
|
| } |
| 103 |
151 |
▶ | if (!dot) return "application/octet-stream"; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dot) return "application/octet-stream"; |
2 |
149 |
151 |
|
| 104 |
|
| /* index, not *dot++: toLOWER is a macro that evaluates its argument |
| 105 |
|
| * more than once, so a side effect inside it runs more than once */ |
| 106 |
615 |
▶ | while (dot + n < path + len && n < (int)sizeof(ext) - 1) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (dot + n < path + len && n < (int)sizeof(ext) - 1) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
466 |
149 |
615 |
| while (dot + n < path + len && n < (int)sizeof(ext) - 1) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
466 |
0 |
466 |
|
| 107 |
466 |
▶ | ext[n] = (char)toLOWER((U8)dot[n]); |
Branch: 1 branch
| Branch | True | False | Total |
| ext[n] = (char)toLOWER((U8)dot[n]); |
0 |
466 |
466 |
|
| 108 |
466 |
| n++; |
| 109 |
|
| } |
| 110 |
149 |
| ext[n] = '\0'; |
| 111 |
1331 |
▶ | for (i = 0; map[i].ext; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; map[i].ext; i++) |
1318 |
13 |
1331 |
|
| 112 |
1318 |
▶ | if (strEQ(ext, map[i].ext)) return map[i].type; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(ext, map[i].ext)) return map[i].type; |
136 |
1182 |
1318 |
|
| 113 |
13 |
| return "application/octet-stream"; |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| /* ---- helpers -------------------------------------------------------------- */ |
| 117 |
|
| |
| 118 |
|
| /* An RFC 7231 IMF-fixdate, in C: strftime would drag the locale in, and |
| 119 |
|
| * these names are fixed by the spec in any case. */ |
| 120 |
185 |
| static void ps_http_date(char *out, size_t outlen, time_t when) { |
| 121 |
|
| static const char *const days[] = { "Sun","Mon","Tue","Wed","Thu", |
| 122 |
|
| "Fri","Sat" }; |
| 123 |
|
| static const char *const months[] = { "Jan","Feb","Mar","Apr","May","Jun", |
| 124 |
|
| "Jul","Aug","Sep","Oct","Nov","Dec" }; |
| 125 |
|
| struct tm tm; |
| 126 |
|
| #ifdef HAS_GMTIME_R |
| 127 |
|
| /* |
| 128 |
|
| # uncoverable branch true |
| 129 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 130 |
|
| if (!gmtime_r(&when, &tm)) { |
| 131 |
|
| #else |
| 132 |
185 |
| struct tm *tp = gmtime(&when); |
| 133 |
185 |
▶ | if (tp) tm = *tp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tp) tm = *tp; |
185 |
0 |
185 |
|
| 134 |
|
| else { |
| 135 |
|
| #endif |
| 136 |
|
| /* out of range for the C library (a validator that returned |
| 137 |
|
| * nonsense): the epoch, rather than whatever was on the stack */ |
| 138 |
0 |
| time_t zero = 0; /* # uncoverable statement */ |
| 139 |
|
| #ifdef HAS_GMTIME_R |
| 140 |
|
| gmtime_r(&zero, &tm); /* # uncoverable statement */ |
| 141 |
|
| #else |
| 142 |
0 |
| tm = *gmtime(&zero); |
| 143 |
|
| #endif |
| 144 |
|
| } |
| 145 |
185 |
| my_snprintf(out, outlen, "%s, %02d %s %04d %02d:%02d:%02d GMT", |
| 146 |
185 |
| days[tm.tm_wday % 7], tm.tm_mday, months[tm.tm_mon % 12], |
| 147 |
185 |
| tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 148 |
185 |
| } |
| 149 |
|
| |
| 150 |
|
| /* A short text response (405/404), built like any other triplet. */ |
| 151 |
34 |
| static SV *ps_plain(pTHX_ IV status, const char *msg, |
| 152 |
|
| const char *hk, const char *hv) { |
| 153 |
34 |
| AV *resp = newAV(), *headers = newAV(), *body = newAV(); |
| 154 |
34 |
| STRLEN len = strlen(msg); |
| 155 |
34 |
| av_push(headers, newSVpvs("Content-Type")); |
| 156 |
34 |
| av_push(headers, newSVpvs("text/plain; charset=utf-8")); |
| 157 |
34 |
| av_push(headers, newSVpvs("Content-Length")); |
| 158 |
34 |
| av_push(headers, newSViv((IV)len)); |
| 159 |
34 |
▶ | if (hk) { av_push(headers, newSVpv(hk, 0)); av_push(headers, newSVpv(hv, 0)); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hk) { av_push(headers, newSVpv(hk, 0)); av_push(headers, newSVpv(hv, 0)); } |
3 |
31 |
34 |
|
| 160 |
34 |
| av_push(body, newSVpvn(msg, len)); |
| 161 |
34 |
| av_push(resp, newSViv(status)); |
| 162 |
34 |
| av_push(resp, newRV_noinc((SV *)headers)); |
| 163 |
34 |
| av_push(resp, newRV_noinc((SV *)body)); |
| 164 |
34 |
| return newRV_noinc((SV *)resp); |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| /* Any '..' segment, an absolute-looking escape or a NUL is a 404 rather |
| 168 |
|
| * than something to normalise: a request that contains one is not asking |
| 169 |
|
| * for a file we serve. */ |
| 170 |
193 |
| static int ps_path_unsafe(const char *p, STRLEN len) { |
| 171 |
193 |
| STRLEN i, seg = 0; |
| 172 |
|
| /* |
| 173 |
|
| # uncoverable branch true |
| 174 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 175 |
193 |
▶ | if (memchr(p, '\0', len)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memchr(p, '\0', len)) return 1; |
0 |
193 |
193 |
|
| 176 |
2006 |
▶ | for (i = 0; i <= len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= len; i++) { |
1823 |
183 |
2006 |
|
| 177 |
|
| /* A backslash ends a segment too. On this side of the world it is an |
| 178 |
|
| * ordinary filename byte and "..\.." traverses nothing, but Windows |
| 179 |
|
| * reads it as a separator, so a guard that only splits on '/' sees one |
| 180 |
|
| * harmless segment where the OS sees two levels up. Splitting on both |
| 181 |
|
| * costs a comparison and does not depend on which platform the build |
| 182 |
|
| * is for - the wrong place to find out is a port. */ |
| 183 |
1823 |
▶ | if (i == len || p[i] == '/' || p[i] == '\\') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (i == len || p[i] == '/' || p[i] == '\\') { |
1640 |
183 |
1823 |
| if (i == len || p[i] == '/' || p[i] == '\\') { |
1397 |
243 |
1640 |
| if (i == len || p[i] == '/' || p[i] == '\\') { |
5 |
1392 |
1397 |
|
| 184 |
431 |
| STRLEN n = i - seg; |
| 185 |
|
| /* |
| 186 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 187 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 188 |
431 |
▶ | if (n == 2 && p[seg] == '.' && p[seg + 1] == '.') return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (n == 2 && p[seg] == '.' && p[seg + 1] == '.') return 1; |
10 |
421 |
431 |
| if (n == 2 && p[seg] == '.' && p[seg + 1] == '.') return 1; |
10 |
0 |
10 |
| if (n == 2 && p[seg] == '.' && p[seg + 1] == '.') return 1; |
10 |
0 |
10 |
|
| 189 |
421 |
| seg = i + 1; |
| 190 |
|
| } |
| 191 |
|
| } |
| 192 |
183 |
| return 0; |
| 193 |
|
| } |
| 194 |
|
| |
| 195 |
|
| /* Does any segment begin with a dot? A dotfile is where a tree keeps what it |
| 196 |
|
| * was never meant to publish - .env, .git, .htaccess - so a mount refuses |
| 197 |
|
| * them unless it was told otherwise. `.well-known` is the one name with a |
| 198 |
|
| * dot that exists to be served (RFC 8615), and `well_known` lets it through; |
| 199 |
|
| * what is under it is still checked segment by segment. A bare `.` names the |
| 200 |
|
| * directory it is in and hides nothing; `..` is ps_path_unsafe's. */ |
| 201 |
166 |
| static int ps_path_dotted(const char *p, STRLEN len, int well_known) { |
| 202 |
166 |
| STRLEN i, seg = 0; |
| 203 |
1759 |
▶ | for (i = 0; i <= len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= len; i++) { |
1605 |
154 |
1759 |
|
| 204 |
1605 |
▶ | if (i == len || p[i] == '/' || p[i] == '\\') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (i == len || p[i] == '/' || p[i] == '\\') { |
1443 |
162 |
1605 |
| if (i == len || p[i] == '/' || p[i] == '\\') { |
1238 |
205 |
1443 |
| if (i == len || p[i] == '/' || p[i] == '\\') { |
1 |
1237 |
1238 |
|
| 205 |
368 |
| STRLEN n = i - seg; |
| 206 |
368 |
▶ | if (n > 1 && p[seg] == '.' /* # uncoverable condition left count:5 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 1 && p[seg] == '.' /* # uncoverable condition left count:5 class:ignore_covered_err */ |
168 |
200 |
368 |
| if (n > 1 && p[seg] == '.' /* # uncoverable condition left count:5 class:ignore_covered_err */ |
16 |
152 |
168 |
|
| 207 |
16 |
▶ | && !(well_known && n == 11 |
Branch: 2 branches
| Branch | True | False | Total |
| && !(well_known && n == 11 |
13 |
3 |
16 |
| && !(well_known && n == 11 |
4 |
9 |
13 |
|
| 208 |
4 |
▶ | && memEQ(p + seg, ".well-known", 11))) |
Branch: 1 branch
| Branch | True | False | Total |
| && memEQ(p + seg, ".well-known", 11))) |
0 |
4 |
4 |
|
| 209 |
12 |
| return 1; |
| 210 |
356 |
| seg = i + 1; |
| 211 |
|
| } |
| 212 |
|
| } |
| 213 |
154 |
| return 0; |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
|
| /* ---- directories ----------------------------------------------------------- |
| 217 |
|
| * A directory is not a file, so by itself it is a 404. Two options give it |
| 218 |
|
| * one more chance first: `index` names a file inside it to serve in its |
| 219 |
|
| * place, and `list` renders what it holds. |
| 220 |
|
| * |
| 221 |
|
| * The directory is served WHERE IT WAS ASKED FOR - there is no 301 to the |
| 222 |
|
| * trailing-slash form. Both spellings arrive here (`/about/` with the slash |
| 223 |
|
| * PATH_INFO carried, `/about` without), and both name the same directory; |
| 224 |
|
| * redirecting one to the other would spend a round trip to say so. */ |
| 225 |
|
| |
| 226 |
|
| /* `file` names a directory: append the index filename, with the separator |
| 227 |
|
| * only when the path does not already end in one. Returns the new length, or |
| 228 |
|
| * 0 when it would not fit. `idx` is a filename - the constructor rejects one |
| 229 |
|
| * with a separator in it - so nothing here can escape the directory. */ |
| 230 |
18 |
| static STRLEN ps_dir_index(char *file, STRLEN flen, |
| 231 |
|
| const char *idx, STRLEN ilen) { |
| 232 |
18 |
▶ | int slash = (flen > 0 && file[flen - 1] == '/') ? 0 : 1; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| int slash = (flen > 0 && file[flen - 1] == '/') ? 0 : 1; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
18 |
0 |
18 |
| int slash = (flen > 0 && file[flen - 1] == '/') ? 0 : 1; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
4 |
14 |
18 |
|
| 233 |
|
| /* |
| 234 |
|
| # uncoverable branch true |
| 235 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 236 |
18 |
▶ | if (flen + slash + ilen > MAXPATHLEN) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flen + slash + ilen > MAXPATHLEN) return 0; |
0 |
18 |
18 |
|
| 237 |
18 |
▶ | if (slash) file[flen++] = '/'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slash) file[flen++] = '/'; |
4 |
14 |
18 |
|
| 238 |
18 |
| memcpy(file + flen, idx, ilen); |
| 239 |
18 |
| file[flen + ilen] = '\0'; |
| 240 |
18 |
| return flen + ilen; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
|
| /* A filename is not markup and it is not a URL. Two escapes, because the one |
| 244 |
|
| * name goes into both a href and the text of the link. */ |
| 245 |
115 |
| static void ps_html_esc(pTHX_ SV *out, const char *s, STRLEN len) { |
| 246 |
115 |
| punk_html_escape(aTHX_ out, s, len); |
| 247 |
115 |
| } |
| 248 |
|
| |
| 249 |
89 |
| static void ps_url_esc(pTHX_ SV *out, const char *s, STRLEN len) { |
| 250 |
|
| static const char hex[] = "0123456789ABCDEF"; |
| 251 |
89 |
| STRLEN i, run = 0; |
| 252 |
636 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
547 |
89 |
636 |
|
| 253 |
547 |
| unsigned char c = (unsigned char)s[i]; |
| 254 |
|
| char buf[3]; |
| 255 |
|
| /* |
| 256 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 257 |
|
| # uncoverable condition left count:4 class:ignore_covered_err |
| 258 |
|
| # uncoverable condition left count:6 class:ignore_covered_err |
| 259 |
|
| # uncoverable condition left count:7 class:ignore_covered_err */ |
| 260 |
547 |
▶ | if (isALPHA(c) || isDIGIT(c) || c == '-' || c == '_' || c == '.' |
Branch: 5 branches
| Branch | True | False | Total |
| if (isALPHA(c) || isDIGIT(c) || c == '-' || c == '_' || c == '.' |
79 |
468 |
547 |
| if (isALPHA(c) || isDIGIT(c) || c == '-' || c == '_' || c == '.' |
79 |
0 |
79 |
| if (isALPHA(c) || isDIGIT(c) || c == '-' || c == '_' || c == '.' |
69 |
10 |
79 |
| if (isALPHA(c) || isDIGIT(c) || c == '-' || c == '_' || c == '.' |
69 |
0 |
69 |
| if (isALPHA(c) || isDIGIT(c) || c == '-' || c == '_' || c == '.' |
28 |
41 |
69 |
|
| 261 |
547 |
▶ | || c == '~' || c == '/') { run++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| || c == '~' || c == '/') { run++; continue; } |
28 |
0 |
28 |
| || c == '~' || c == '/') { run++; continue; } |
0 |
28 |
28 |
|
| 262 |
28 |
▶ | if (run) sv_catpvn(out, s + i - run, run); |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) sv_catpvn(out, s + i - run, run); |
19 |
9 |
28 |
|
| 263 |
28 |
| run = 0; |
| 264 |
28 |
| buf[0] = '%'; buf[1] = hex[c >> 4]; buf[2] = hex[c & 15]; |
| 265 |
28 |
| sv_catpvn(out, buf, 3); |
| 266 |
|
| } |
| 267 |
|
| /* |
| 268 |
|
| # uncoverable branch false |
| 269 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 270 |
89 |
▶ | if (run) sv_catpvn(out, s + len - run, run); |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) sv_catpvn(out, s + len - run, run); |
89 |
0 |
89 |
|
| 271 |
89 |
| } |
| 272 |
|
| |
| 273 |
|
| /* The listing itself. The directory is read through PerlDir_*, not opendir: |
| 274 |
|
| * there is no ambient DIR on native Windows, and perl's own layer is already |
| 275 |
|
| * the shim - the same idiom the model scan in punk_compile.h uses. |
| 276 |
|
| * |
| 277 |
|
| * `no-store`, because a listing is stale the moment anything in the |
| 278 |
|
| * directory changes and there is no cheap validator for "what this directory |
| 279 |
|
| * holds" the way there is for one file's bytes. */ |
| 280 |
21 |
| static SV *ps_listing(pTHX_ HV *env, const char *file, STRLEN flen, |
| 281 |
|
| const char *urlpath, STRLEN ulen, int is_head, |
| 282 |
|
| int dotfiles) { |
| 283 |
21 |
| DIR *dh = PerlDir_open(file); |
| 284 |
|
| Direntry_t *de; |
| 285 |
|
| AV *ents, *resp, *headers, *body; |
| 286 |
|
| SV *html; |
| 287 |
|
| SSize_t i, n; |
| 288 |
|
| char child[MAXPATHLEN + 1]; |
| 289 |
21 |
| const char *pre = NULL; |
| 290 |
21 |
| STRLEN prel = 0; |
| 291 |
21 |
▶ | int at_root = (ulen == 1 && urlpath[0] == '/'); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| int at_root = (ulen == 1 && urlpath[0] == '/'); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
11 |
10 |
21 |
| int at_root = (ulen == 1 && urlpath[0] == '/'); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
11 |
0 |
11 |
|
| 292 |
|
| |
| 293 |
|
| /* |
| 294 |
|
| # uncoverable branch true |
| 295 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 296 |
21 |
▶ | if (!dh) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dh) return NULL; |
0 |
21 |
21 |
|
| 297 |
|
| |
| 298 |
|
| /* The hrefs are relative, and a relative href resolves against the |
| 299 |
|
| * directory of the URL the browser asked for - which is this directory |
| 300 |
|
| * only when that URL ended in a slash. Asked for without one, `/sub` |
| 301 |
|
| * resolves `x.txt` to `/x.txt`, a level too high. So the links carry the |
| 302 |
|
| * last segment themselves in that spelling. At the root of the mount |
| 303 |
|
| * PATH_INFO is `/` either way and only the request line can say which |
| 304 |
|
| * was asked for; the segment is then the mount's own, from SCRIPT_NAME. */ |
| 305 |
21 |
▶ | if (!at_root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!at_root) { |
10 |
11 |
21 |
|
| 306 |
10 |
▶ | if (ulen && urlpath[ulen - 1] != '/') { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (ulen && urlpath[ulen - 1] != '/') { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
10 |
0 |
10 |
| if (ulen && urlpath[ulen - 1] != '/') { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
3 |
7 |
10 |
|
| 307 |
3 |
| pre = urlpath + ulen; |
| 308 |
13 |
▶ | while (pre > urlpath && pre[-1] != '/') pre--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (pre > urlpath && pre[-1] != '/') pre--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
13 |
0 |
13 |
| while (pre > urlpath && pre[-1] != '/') pre--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
10 |
3 |
13 |
|
| 309 |
3 |
| prel = (STRLEN)(urlpath + ulen - pre); |
| 310 |
|
| } |
| 311 |
|
| } |
| 312 |
|
| else { |
| 313 |
11 |
| SV **ru = hv_fetchs(env, "REQUEST_URI", 0); |
| 314 |
11 |
| SV **sn = hv_fetchs(env, "SCRIPT_NAME", 0); |
| 315 |
|
| /* |
| 316 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 317 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 318 |
|
| # uncoverable condition right count:4 class:ignore_covered_err |
| 319 |
|
| # uncoverable condition right count:5 class:ignore_covered_err |
| 320 |
|
| # uncoverable condition right count:6 class:ignore_covered_err */ |
| 321 |
11 |
▶ | if (ru && *ru && SvOK(*ru) && sn && *sn && SvOK(*sn)) { |
Branch: 6 branches
| Branch | True | False | Total |
| if (ru && *ru && SvOK(*ru) && sn && *sn && SvOK(*sn)) { |
6 |
5 |
11 |
| if (ru && *ru && SvOK(*ru) && sn && *sn && SvOK(*sn)) { |
6 |
0 |
6 |
| if (ru && *ru && SvOK(*ru) && sn && *sn && SvOK(*sn)) { |
6 |
0 |
6 |
| if (ru && *ru && SvOK(*ru) && sn && *sn && SvOK(*sn)) { |
6 |
0 |
6 |
| if (ru && *ru && SvOK(*ru) && sn && *sn && SvOK(*sn)) { |
6 |
0 |
6 |
| if (ru && *ru && SvOK(*ru) && sn && *sn && SvOK(*sn)) { |
6 |
0 |
6 |
|
| 322 |
|
| STRLEN rl, sl; |
| 323 |
6 |
| const char *r = SvPV_const(*ru, rl); |
| 324 |
6 |
▶ | const char *s = SvPV_const(*sn, sl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *s = SvPV_const(*sn, sl); |
6 |
0 |
6 |
|
| 325 |
6 |
| const char *q = (const char *)memchr(r, '?', rl); |
| 326 |
6 |
▶ | if (q) rl = (STRLEN)(q - r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (q) rl = (STRLEN)(q - r); |
1 |
5 |
6 |
|
| 327 |
|
| /* |
| 328 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 329 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 330 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 331 |
6 |
▶ | if (rl && r[rl - 1] != '/' && sl && s[sl - 1] != '/') { |
Branch: 4 branches
| Branch | True | False | Total |
| if (rl && r[rl - 1] != '/' && sl && s[sl - 1] != '/') { |
6 |
0 |
6 |
| if (rl && r[rl - 1] != '/' && sl && s[sl - 1] != '/') { |
3 |
3 |
6 |
| if (rl && r[rl - 1] != '/' && sl && s[sl - 1] != '/') { |
3 |
0 |
3 |
| if (rl && r[rl - 1] != '/' && sl && s[sl - 1] != '/') { |
3 |
0 |
3 |
|
| 332 |
3 |
| pre = s + sl; |
| 333 |
18 |
▶ | while (pre > s && pre[-1] != '/') pre--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (pre > s && pre[-1] != '/') pre--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
18 |
0 |
18 |
| while (pre > s && pre[-1] != '/') pre--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
15 |
3 |
18 |
|
| 334 |
3 |
| prel = (STRLEN)(s + sl - pre); |
| 335 |
|
| } |
| 336 |
|
| } |
| 337 |
|
| } |
| 338 |
|
| |
| 339 |
21 |
| ents = (AV *)sv_2mortal((SV *)newAV()); |
| 340 |
161 |
▶ | while ((de = PerlDir_read(dh))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((de = PerlDir_read(dh))) { |
140 |
21 |
161 |
|
| 341 |
140 |
| const char *nm = de->d_name; |
| 342 |
140 |
▶ | if (nm[0] == '.' && (nm[1] == '\0' /* # uncoverable condition right count:4 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (nm[0] == '.' && (nm[1] == '\0' /* # uncoverable condition right count:4 class:ignore_covered_err */ |
82 |
58 |
140 |
| if (nm[0] == '.' && (nm[1] == '\0' /* # uncoverable condition right count:4 class:ignore_covered_err */ |
61 |
21 |
82 |
|
| 343 |
82 |
▶ | || (nm[1] == '.' && nm[2] == '\0'))) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| || (nm[1] == '.' && nm[2] == '\0'))) continue; |
21 |
40 |
61 |
| || (nm[1] == '.' && nm[2] == '\0'))) continue; |
21 |
0 |
21 |
|
| 344 |
|
| /* what the mount will not serve it does not advertise */ |
| 345 |
98 |
▶ | if (nm[0] == '.' && !dotfiles && !strEQ(nm, ".well-known")) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (nm[0] == '.' && !dotfiles && !strEQ(nm, ".well-known")) continue; |
40 |
58 |
98 |
| if (nm[0] == '.' && !dotfiles && !strEQ(nm, ".well-known")) continue; |
36 |
4 |
40 |
| if (nm[0] == '.' && !dotfiles && !strEQ(nm, ".well-known")) continue; |
28 |
8 |
36 |
|
| 346 |
70 |
| av_push(ents, newSVpv(nm, 0)); |
| 347 |
|
| } |
| 348 |
21 |
| PerlDir_close(dh); |
| 349 |
21 |
| n = av_len(ents) + 1; |
| 350 |
21 |
▶ | if (n > 1) sortsv(AvARRAY(ents), (STRLEN)n, Perl_sv_cmp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 1) sortsv(AvARRAY(ents), (STRLEN)n, Perl_sv_cmp); |
18 |
3 |
21 |
|
| 351 |
|
| |
| 352 |
21 |
| html = sv_2mortal(newSVpvs( |
| 353 |
|
| "<!doctype html>\n<meta charset=\"utf-8\">\n" |
| 354 |
|
| "<meta name=\"viewport\" content=\"width=device-width\">\n<title>")); |
| 355 |
21 |
| sv_catpvs(html, "Index of "); |
| 356 |
21 |
| ps_html_esc(aTHX_ html, urlpath, ulen); |
| 357 |
21 |
| sv_catpvs(html, "</title>\n<h1>Index of "); |
| 358 |
21 |
| ps_html_esc(aTHX_ html, urlpath, ulen); |
| 359 |
21 |
| sv_catpvs(html, "</h1>\n<ul>\n"); |
| 360 |
|
| |
| 361 |
|
| /* The parent, except at the root of the mount, where there is none to |
| 362 |
|
| * offer that is still inside it. */ |
| 363 |
21 |
▶ | if (!at_root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!at_root) { |
10 |
11 |
21 |
|
| 364 |
10 |
▶ | if (prel) sv_catpvs(html, "<li><a href=\".\">../</a></li>\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (prel) sv_catpvs(html, "<li><a href=\".\">../</a></li>\n"); |
3 |
7 |
10 |
|
| 365 |
7 |
| else sv_catpvs(html, "<li><a href=\"..\">../</a></li>\n"); |
| 366 |
|
| } |
| 367 |
|
| |
| 368 |
91 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
70 |
21 |
91 |
|
| 369 |
70 |
| SV **ep = av_fetch(ents, i, 0); |
| 370 |
|
| STRLEN el; |
| 371 |
|
| const char *es; |
| 372 |
70 |
| int isdir = 0; |
| 373 |
|
| /* |
| 374 |
|
| # uncoverable branch true |
| 375 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 376 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 377 |
70 |
▶ | if (!ep || !*ep) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ep || !*ep) continue; |
70 |
0 |
70 |
| if (!ep || !*ep) continue; |
0 |
70 |
70 |
|
| 378 |
70 |
| es = SvPV_const(*ep, el); |
| 379 |
|
| /* |
| 380 |
|
| # uncoverable branch false |
| 381 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 382 |
70 |
▶ | if (flen + 1 + el <= MAXPATHLEN) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flen + 1 + el <= MAXPATHLEN) { |
70 |
0 |
70 |
|
| 383 |
|
| Stat_t st; |
| 384 |
70 |
| memcpy(child, file, flen); |
| 385 |
70 |
| child[flen] = '/'; |
| 386 |
70 |
| memcpy(child + flen + 1, es, el); |
| 387 |
70 |
| child[flen + 1 + el] = '\0'; |
| 388 |
70 |
▶ | if (PerlLIO_stat(child, &st) == 0 && S_ISDIR(st.st_mode)) isdir = 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (PerlLIO_stat(child, &st) == 0 && S_ISDIR(st.st_mode)) isdir = 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
70 |
0 |
70 |
| if (PerlLIO_stat(child, &st) == 0 && S_ISDIR(st.st_mode)) isdir = 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
39 |
31 |
70 |
|
| 389 |
|
| } |
| 390 |
70 |
| sv_catpvs(html, "<li><a href=\""); |
| 391 |
70 |
▶ | if (prel) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (prel) { |
19 |
51 |
70 |
|
| 392 |
19 |
| ps_url_esc(aTHX_ html, pre, prel); |
| 393 |
19 |
| sv_catpvs(html, "/"); |
| 394 |
|
| } |
| 395 |
70 |
| ps_url_esc(aTHX_ html, es, el); |
| 396 |
70 |
▶ | if (isdir) sv_catpvs(html, "/"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (isdir) sv_catpvs(html, "/"); |
39 |
31 |
70 |
|
| 397 |
70 |
| sv_catpvs(html, "\">"); |
| 398 |
70 |
| ps_html_esc(aTHX_ html, es, el); |
| 399 |
70 |
▶ | if (isdir) sv_catpvs(html, "/"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (isdir) sv_catpvs(html, "/"); |
39 |
31 |
70 |
|
| 400 |
70 |
| sv_catpvs(html, "</a></li>\n"); |
| 401 |
|
| } |
| 402 |
21 |
| sv_catpvs(html, "</ul>\n"); |
| 403 |
|
| |
| 404 |
21 |
| resp = newAV(); headers = newAV(); body = newAV(); |
| 405 |
21 |
| av_push(headers, newSVpvs("Content-Type")); |
| 406 |
21 |
| av_push(headers, newSVpvs("text/html; charset=utf-8")); |
| 407 |
21 |
| av_push(headers, newSVpvs("Content-Length")); |
| 408 |
21 |
| av_push(headers, newSViv((IV)SvCUR(html))); |
| 409 |
21 |
| av_push(headers, newSVpvs("Cache-Control")); |
| 410 |
21 |
| av_push(headers, newSVpvs("no-store")); |
| 411 |
21 |
▶ | if (!is_head) av_push(body, newSVsv(html)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_head) av_push(body, newSVsv(html)); |
20 |
1 |
21 |
|
| 412 |
21 |
| av_push(resp, newSViv(200)); |
| 413 |
21 |
| av_push(resp, newRV_noinc((SV *)headers)); |
| 414 |
21 |
| av_push(resp, newRV_noinc((SV *)body)); |
| 415 |
21 |
| return newRV_noinc((SV *)resp); |
| 416 |
|
| } |
| 417 |
|
| |
| 418 |
|
| /* ---- serving one file ------------------------------------------------------ |
| 419 |
|
| * The stat / conditional-request / range / header / body half of a static |
| 420 |
|
| * response, split out so the markdown mount can serve the images and other |
| 421 |
|
| * assets sitting alongside its documents with exactly this behaviour rather |
| 422 |
|
| * than a second implementation that drifts from it. Since 0.13 it is the |
| 423 |
|
| * send_file decision core with a fixed option block - the implementation |
| 424 |
|
| * lives in punk_sendfile.h (included right after this header), which is |
| 425 |
|
| * what gives static files their ETag, 304-on-If-None-Match and 206/416 |
| 426 |
|
| * range answers; the If-Modified-Since exact-match convention is one |
| 427 |
|
| * branch of that core. |
| 428 |
|
| * |
| 429 |
|
| * Returns the finished triplet (+1 owned), or NULL when `file` is not a |
| 430 |
|
| * regular file or cannot be opened - the caller decides what its own 404 |
| 431 |
|
| * looks like. The path is used as given: the traversal guard belongs to |
| 432 |
|
| * whoever built it out of untrusted input. */ |
| 433 |
|
| |
| 434 |
|
| /* What a caller may say about freshness. Borrowed, never owned. */ |
| 435 |
|
| typedef struct ps_fopts { |
| 436 |
|
| SV *cache_control; /* the header value, or NULL for none */ |
| 437 |
|
| } ps_fopts; |
| 438 |
|
| |
| 439 |
|
| static SV *ps_serve_file_opt(pTHX_ HV *env, const char *file, STRLEN flen, |
| 440 |
|
| int is_head, ps_fopts *fo); |
| 441 |
|
| |
| 442 |
|
| /* The same with nothing to say about freshness - what a caller that has no |
| 443 |
|
| * cache policy of its own (the markdown mount's assets) wants. */ |
| 444 |
|
| static SV *ps_serve_file(pTHX_ HV *env, const char *file, STRLEN flen, |
| 445 |
|
| int is_head); |
| 446 |
|
| |
| 447 |
|
| /* ---- the app --------------------------------------------------------------- |
| 448 |
|
| * The PSGI coderef's body: a magic CV whose capture is the mount's whole |
| 449 |
|
| * configuration. Everything a request needs happens here, with no Perl |
| 450 |
|
| * frame at all. */ |
| 451 |
|
| |
| 452 |
|
| enum { |
| 453 |
|
| PSC_DIR = 0, /* the directory served, no trailing slash */ |
| 454 |
|
| PSC_CC = 1, /* Cache-Control for a plain URL, or undef */ |
| 455 |
|
| PSC_CC_IMM = 2, /* ... and for a URL whose digest checked out */ |
| 456 |
|
| PSC_CACHE = 3, /* the digest cache (hashref), or undef: no */ |
| 457 |
|
| /* fingerprinting on this mount */ |
| 458 |
|
| PSC_DEV = 4, /* re-stat cached digests (development) */ |
| 459 |
|
| PSC_INDEX = 5, /* the index filename, or undef: no directory index */ |
| 460 |
|
| PSC_LIST = 6, /* list a directory that has no index file */ |
| 461 |
|
| PSC_DOT = 7 /* serve and list dotfiles */ |
| 462 |
|
| }; |
| 463 |
|
| |
| 464 |
962 |
| static SV *psc_cap(pTHX_ AV *cap, int slot) { |
| 465 |
962 |
| SV **s = av_fetch(cap, slot, 0); |
| 466 |
|
| /* |
| 467 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 468 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 469 |
962 |
▶ | return (s && *s && SvOK(*s)) ? *s : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (s && *s && SvOK(*s)) ? *s : NULL; |
962 |
0 |
962 |
| return (s && *s && SvOK(*s)) ? *s : NULL; |
962 |
0 |
962 |
| return (s && *s && SvOK(*s)) ? *s : NULL; |
95 |
867 |
962 |
|
| 470 |
|
| } |
| 471 |
|
| |
| 472 |
|
| /* The mount's answer (+1) for a GET or HEAD of `path` under its directory. |
| 473 |
|
| * The XSUB below reads the method and PATH_INFO from the env and calls this; |
| 474 |
|
| * the dispatcher calls it directly for a mount it recognises as its own, |
| 475 |
|
| * with the sub-path it already has, and so skips the SCRIPT_NAME/PATH_INFO |
| 476 |
|
| * rewrite and the Perl frame. */ |
| 477 |
154 |
| static SV *punk_static_in(pTHX_ AV *cap, HV *env, const char *path, |
| 478 |
|
| STRLEN plen, int is_head) { |
| 479 |
|
| SV *dirsv, *cachesv; |
| 480 |
|
| const char *dir; |
| 481 |
|
| STRLEN dlen, flen; |
| 482 |
|
| char file[MAXPATHLEN + 1]; |
| 483 |
|
| SV *resp; |
| 484 |
|
| ps_fopts fo; |
| 485 |
|
| |
| 486 |
154 |
| dirsv = *av_fetch(cap, 0, 0); |
| 487 |
154 |
| dir = SvPV_const(dirsv, dlen); |
| 488 |
154 |
| Zero(&fo, 1, ps_fopts); |
| 489 |
154 |
| fo.cache_control = psc_cap(aTHX_ cap, PSC_CC); |
| 490 |
154 |
| cachesv = psc_cap(aTHX_ cap, PSC_CACHE); |
| 491 |
|
| |
| 492 |
154 |
▶ | if (ps_path_unsafe(path, plen) || dlen + plen > MAXPATHLEN /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (ps_path_unsafe(path, plen) || dlen + plen > MAXPATHLEN /* # uncoverable condition left count:2 class:ignore_covered_err */ |
146 |
8 |
154 |
| if (ps_path_unsafe(path, plen) || dlen + plen > MAXPATHLEN /* # uncoverable condition left count:2 class:ignore_covered_err */ |
146 |
0 |
146 |
|
| 493 |
146 |
▶ | || (!psc_cap(aTHX_ cap, PSC_DOT) && ps_path_dotted(path, plen, 1))) |
Branch: 2 branches
| Branch | True | False | Total |
| || (!psc_cap(aTHX_ cap, PSC_DOT) && ps_path_dotted(path, plen, 1))) |
141 |
5 |
146 |
| || (!psc_cap(aTHX_ cap, PSC_DOT) && ps_path_dotted(path, plen, 1))) |
9 |
132 |
141 |
|
| 494 |
17 |
| return ps_plain(aTHX_ 404, "Not Found", NULL, NULL); |
| 495 |
137 |
| memcpy(file, dir, dlen); |
| 496 |
137 |
| memcpy(file + dlen, path, plen); |
| 497 |
137 |
| file[dlen + plen] = '\0'; |
| 498 |
137 |
| flen = dlen + plen; |
| 499 |
|
| |
| 500 |
|
| /* A content-addressed URL: `/app.9f3a1c2b0d4e5f60.css` is the file |
| 501 |
|
| * `/app.css`, and gets a year plus `immutable` when the digest it names |
| 502 |
|
| * is the digest that file now has. |
| 503 |
|
| * |
| 504 |
|
| * The literal path is tried first, so a file genuinely called |
| 505 |
|
| * `app.9f3a1c2b0d4e5f60.css` - a build tool's output, checked in under |
| 506 |
|
| * that name - still serves as itself. That costs one extra stat, and |
| 507 |
|
| * only on a request whose path parses as fingerprinted. */ |
| 508 |
|
| /* |
| 509 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 510 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 511 |
137 |
▶ | if (cachesv && SvROK(cachesv) && SvTYPE(SvRV(cachesv)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (cachesv && SvROK(cachesv) && SvTYPE(SvRV(cachesv)) == SVt_PVHV) { |
6 |
131 |
137 |
| if (cachesv && SvROK(cachesv) && SvTYPE(SvRV(cachesv)) == SVt_PVHV) { |
6 |
0 |
6 |
| if (cachesv && SvROK(cachesv) && SvTYPE(SvRV(cachesv)) == SVt_PVHV) { |
6 |
0 |
6 |
|
| 512 |
|
| char base[MAXPATHLEN + 1], digest[PA_DIGEST_LEN + 1]; |
| 513 |
|
| STRLEN blen; |
| 514 |
|
| Stat_t st; |
| 515 |
|
| /* |
| 516 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 517 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 518 |
6 |
▶ | if ((blen = pa_defingerprint(file, flen, base, digest)) != 0 |
Branch: 1 branch
| Branch | True | False | Total |
| if ((blen = pa_defingerprint(file, flen, base, digest)) != 0 |
6 |
0 |
6 |
|
| 519 |
6 |
▶ | && (!pfc_stat(aTHX_ file, flen, &st) || !S_ISREG(st.st_mode))) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (!pfc_stat(aTHX_ file, flen, &st) || !S_ISREG(st.st_mode))) { |
1 |
5 |
6 |
| && (!pfc_stat(aTHX_ file, flen, &st) || !S_ISREG(st.st_mode))) { |
0 |
1 |
1 |
|
| 520 |
5 |
| SV *have = pa_digest_cached(aTHX_ (HV *)SvRV(cachesv), base, blen, |
| 521 |
5 |
| psc_cap(aTHX_ cap, PSC_DEV) ? 1 : 0); |
| 522 |
5 |
| memcpy(file, base, blen + 1); |
| 523 |
5 |
| flen = blen; |
| 524 |
|
| /* A digest that does not match is a URL from an older deploy: |
| 525 |
|
| * the current bytes are still the right answer, but `immutable` |
| 526 |
|
| * would be a lie about a URL whose meaning has already changed |
| 527 |
|
| * once. It revalidates instead. */ |
| 528 |
5 |
▶ | if (have && SvCUR(have) == PA_DIGEST_LEN /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (have && SvCUR(have) == PA_DIGEST_LEN /* # uncoverable condition right count:2 class:ignore_covered_err */ |
4 |
1 |
5 |
| if (have && SvCUR(have) == PA_DIGEST_LEN /* # uncoverable condition right count:2 class:ignore_covered_err */ |
4 |
0 |
4 |
|
| 529 |
4 |
▶ | && memEQ(SvPVX(have), digest, PA_DIGEST_LEN)) |
Branch: 1 branch
| Branch | True | False | Total |
| && memEQ(SvPVX(have), digest, PA_DIGEST_LEN)) |
3 |
1 |
4 |
|
| 530 |
3 |
| fo.cache_control = psc_cap(aTHX_ cap, PSC_CC_IMM); |
| 531 |
|
| } |
| 532 |
|
| } |
| 533 |
|
| |
| 534 |
|
| /* A directory, on a mount that has something to do with one. Both |
| 535 |
|
| * spellings land here - `/about/` and `/about` name the same directory - |
| 536 |
|
| * and the index is tried before the listing, because a directory holding |
| 537 |
|
| * an index.html is that file's URL and nothing else. |
| 538 |
|
| * |
| 539 |
|
| * One extra stat, and only when an option asked for it: a mount with |
| 540 |
|
| * neither runs exactly the code it ran before. */ |
| 541 |
|
| { |
| 542 |
137 |
| SV *idxsv = psc_cap(aTHX_ cap, PSC_INDEX); |
| 543 |
137 |
| SV *listsv = psc_cap(aTHX_ cap, PSC_LIST); |
| 544 |
|
| Stat_t dst; |
| 545 |
137 |
▶ | if ((idxsv || listsv) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((idxsv || listsv) |
116 |
21 |
137 |
| if ((idxsv || listsv) |
26 |
90 |
116 |
|
| 546 |
47 |
▶ | && pfc_stat(aTHX_ file, flen, &dst) && S_ISDIR(dst.st_mode)) { |
Branch: 2 branches
| Branch | True | False | Total |
| && pfc_stat(aTHX_ file, flen, &dst) && S_ISDIR(dst.st_mode)) { |
45 |
2 |
47 |
| && pfc_stat(aTHX_ file, flen, &dst) && S_ISDIR(dst.st_mode)) { |
35 |
10 |
45 |
|
| 547 |
35 |
| STRLEN dirlen = flen; |
| 548 |
35 |
| int served = 0; |
| 549 |
35 |
▶ | if (idxsv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (idxsv) { |
18 |
17 |
35 |
|
| 550 |
|
| STRLEN ilen; |
| 551 |
18 |
| const char *idx = SvPV_const(idxsv, ilen); |
| 552 |
18 |
| STRLEN n = ps_dir_index(file, flen, idx, ilen); |
| 553 |
|
| Stat_t ist; |
| 554 |
|
| /* |
| 555 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 556 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 557 |
18 |
▶ | if (n && pfc_stat(aTHX_ file, n, &ist) && S_ISREG(ist.st_mode)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (n && pfc_stat(aTHX_ file, n, &ist) && S_ISREG(ist.st_mode)) { |
18 |
0 |
18 |
| if (n && pfc_stat(aTHX_ file, n, &ist) && S_ISREG(ist.st_mode)) { |
13 |
5 |
18 |
| if (n && pfc_stat(aTHX_ file, n, &ist) && S_ISREG(ist.st_mode)) { |
13 |
0 |
13 |
|
| 558 |
13 |
| flen = n; |
| 559 |
13 |
| served = 1; |
| 560 |
|
| } |
| 561 |
5 |
| else { file[dirlen] = '\0'; flen = dirlen; } |
| 562 |
|
| } |
| 563 |
35 |
▶ | if (!served) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!served) { |
22 |
13 |
35 |
|
| 564 |
|
| /* No index, or none wanted. A listing if this mount renders |
| 565 |
|
| * them; otherwise fall through, and the 404 below stands. */ |
| 566 |
22 |
▶ | if (listsv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (listsv) { |
21 |
1 |
22 |
|
| 567 |
21 |
| resp = ps_listing(aTHX_ env, file, flen, path, plen, |
| 568 |
|
| is_head, |
| 569 |
21 |
| psc_cap(aTHX_ cap, PSC_DOT) ? 1 : 0); |
| 570 |
|
| /* |
| 571 |
|
| # uncoverable branch false |
| 572 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 573 |
21 |
▶ | if (resp) return resp; |
Branch: 1 branch
| Branch | True | False | Total |
| if (resp) return resp; |
21 |
0 |
21 |
|
| 574 |
|
| } |
| 575 |
|
| } |
| 576 |
|
| } |
| 577 |
|
| } |
| 578 |
|
| |
| 579 |
116 |
| resp = ps_serve_file_opt(aTHX_ env, file, flen, is_head, &fo); |
| 580 |
116 |
▶ | if (!resp) resp = ps_plain(aTHX_ 404, "Not Found", NULL, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!resp) resp = ps_plain(aTHX_ 404, "Not Found", NULL, NULL); |
14 |
102 |
116 |
|
| 581 |
116 |
| return resp; |
| 582 |
|
| } |
| 583 |
|
| |
| 584 |
|
| XS_INTERNAL(punk_static_cb); |
| 585 |
71 |
| XS_INTERNAL(punk_static_cb) { |
| 586 |
71 |
| dXSARGS; |
| 587 |
71 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 588 |
|
| HV *env; |
| 589 |
|
| SV **e; |
| 590 |
|
| const char *path, *method; |
| 591 |
71 |
| STRLEN plen, mlen = 3; |
| 592 |
71 |
| int is_head = 0; |
| 593 |
|
| |
| 594 |
|
| /* |
| 595 |
|
| # uncoverable branch true |
| 596 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 597 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 598 |
71 |
▶ | if (!cap || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 1) XSRETURN_EMPTY; |
71 |
0 |
71 |
| if (!cap || items < 1) XSRETURN_EMPTY; |
0 |
71 |
71 |
|
| 599 |
|
| /* |
| 600 |
|
| # uncoverable branch true |
| 601 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 602 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 603 |
71 |
▶ | if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) |
71 |
0 |
71 |
| if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) |
0 |
71 |
71 |
|
| 604 |
0 |
| croak("Punk::Static: the app takes a PSGI env hashref"); /* # uncoverable statement */ |
| 605 |
71 |
| env = (HV *)SvRV(ST(0)); |
| 606 |
|
| |
| 607 |
71 |
| e = hv_fetchs(env, "REQUEST_METHOD", 0); |
| 608 |
|
| /* |
| 609 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 610 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 611 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 612 |
142 |
▶ | method = (e && *e && SvOK(*e)) ? SvPV_const(*e, mlen) : "GET"; |
Branch: 3 branches
| Branch | True | False | Total |
| method = (e && *e && SvOK(*e)) ? SvPV_const(*e, mlen) : "GET"; |
71 |
0 |
71 |
| method = (e && *e && SvOK(*e)) ? SvPV_const(*e, mlen) : "GET"; |
71 |
0 |
71 |
| method = (e && *e && SvOK(*e)) ? SvPV_const(*e, mlen) : "GET"; |
71 |
0 |
71 |
|
| 613 |
71 |
▶ | if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1; |
4 |
67 |
71 |
| if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1; |
2 |
2 |
4 |
|
| 614 |
69 |
▶ | else if (!(mlen == 3 && memEQ(method, "GET", 3))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!(mlen == 3 && memEQ(method, "GET", 3))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
67 |
2 |
69 |
| else if (!(mlen == 3 && memEQ(method, "GET", 3))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
67 |
67 |
|
| 615 |
2 |
| ST(0) = sv_2mortal(ps_plain(aTHX_ 405, "Method Not Allowed", |
| 616 |
|
| "Allow", "GET, HEAD")); |
| 617 |
2 |
| XSRETURN(1); |
| 618 |
|
| } |
| 619 |
|
| |
| 620 |
69 |
| e = hv_fetchs(env, "PATH_INFO", 0); |
| 621 |
|
| /* |
| 622 |
|
| # uncoverable branch false |
| 623 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 624 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 625 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 626 |
138 |
▶ | if (e && *e && SvOK(*e)) path = SvPV_const(*e, plen); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) path = SvPV_const(*e, plen); |
69 |
0 |
69 |
| if (e && *e && SvOK(*e)) path = SvPV_const(*e, plen); |
69 |
0 |
69 |
| if (e && *e && SvOK(*e)) path = SvPV_const(*e, plen); |
69 |
0 |
69 |
|
| 627 |
0 |
| else { path = "/"; plen = 1; } /* # uncoverable statement */ |
| 628 |
|
| |
| 629 |
69 |
| ST(0) = sv_2mortal(punk_static_in(aTHX_ cap, env, path, plen, is_head)); |
| 630 |
69 |
| XSRETURN(1); |
| 631 |
|
| } |
| 632 |
|
| |
| 633 |
|
| /* Is this mount coderef the plain static closure above, with no directory |
| 634 |
|
| * index or listing (the two things that read SCRIPT_NAME)? Then the |
| 635 |
|
| * dispatcher may call punk_static_in directly. Hands back the capture. */ |
| 636 |
236 |
| static int punk_static_fast_ok(pTHX_ SV *mapp, AV **cap_out) { |
| 637 |
|
| CV *cv; |
| 638 |
|
| AV *cap; |
| 639 |
|
| /* |
| 640 |
|
| # uncoverable branch true |
| 641 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 642 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 643 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 644 |
236 |
▶ | if (!(mapp && SvROK(mapp) && SvTYPE(SvRV(mapp)) == SVt_PVCV)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(mapp && SvROK(mapp) && SvTYPE(SvRV(mapp)) == SVt_PVCV)) return 0; |
236 |
0 |
236 |
| if (!(mapp && SvROK(mapp) && SvTYPE(SvRV(mapp)) == SVt_PVCV)) return 0; |
236 |
0 |
236 |
| if (!(mapp && SvROK(mapp) && SvTYPE(SvRV(mapp)) == SVt_PVCV)) return 0; |
0 |
236 |
236 |
|
| 645 |
236 |
| cv = (CV *)SvRV(mapp); |
| 646 |
236 |
▶ | if (!CvISXSUB(cv) || CvXSUB(cv) != punk_static_cb) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!CvISXSUB(cv) || CvXSUB(cv) != punk_static_cb) return 0; |
213 |
23 |
236 |
| if (!CvISXSUB(cv) || CvXSUB(cv) != punk_static_cb) return 0; |
122 |
91 |
213 |
|
| 647 |
91 |
| cap = punk_clos_cap(aTHX_ cv); |
| 648 |
91 |
▶ | if (!cap || psc_cap(aTHX_ cap, PSC_INDEX) || psc_cap(aTHX_ cap, PSC_LIST)) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cap || psc_cap(aTHX_ cap, PSC_INDEX) || psc_cap(aTHX_ cap, PSC_LIST)) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
91 |
0 |
91 |
| if (!cap || psc_cap(aTHX_ cap, PSC_INDEX) || psc_cap(aTHX_ cap, PSC_LIST)) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
88 |
3 |
91 |
| if (!cap || psc_cap(aTHX_ cap, PSC_INDEX) || psc_cap(aTHX_ cap, PSC_LIST)) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
3 |
85 |
88 |
|
| 649 |
6 |
| return 0; |
| 650 |
85 |
| *cap_out = cap; |
| 651 |
85 |
| return 1; |
| 652 |
|
| } |
| 653 |
|
| |
| 654 |
|
| /* ---- the other direction: naming a file ------------------------------------ |
| 655 |
|
| * Given a static mount's coderef and a path within it, the content-addressed |
| 656 |
|
| * URL for that path: `/app.css` -> `/app.9f3a1c2b0d4e5f60.css`. NULL when |
| 657 |
|
| * this mount does not fingerprint, when the path has no extension to put a |
| 658 |
|
| * digest in front of, or when the file cannot be read - in every one of |
| 659 |
|
| * those the caller wants the URL it already had, which still works. */ |
| 660 |
14 |
| static SV *pa_asset_url(pTHX_ SV *appsv, SV *relsv) { |
| 661 |
|
| CV *appcv; |
| 662 |
|
| AV *cap; |
| 663 |
|
| SV *cachesv, *digest; |
| 664 |
|
| const char *dir, *rel; |
| 665 |
|
| STRLEN dlen, rlen; |
| 666 |
|
| char file[MAXPATHLEN + 1]; |
| 667 |
|
| |
| 668 |
|
| /* |
| 669 |
|
| # uncoverable branch true |
| 670 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 671 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 672 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 673 |
14 |
▶ | if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVCV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVCV)) |
14 |
0 |
14 |
| if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVCV)) |
14 |
0 |
14 |
| if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVCV)) |
0 |
14 |
14 |
|
| 674 |
0 |
| return NULL; /* # uncoverable statement */ |
| 675 |
14 |
| appcv = (CV *)SvRV(appsv); |
| 676 |
14 |
| cap = punk_clos_cap(aTHX_ appcv); |
| 677 |
|
| /* |
| 678 |
|
| # uncoverable branch true |
| 679 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 680 |
14 |
▶ | if (!cap) return NULL; /* not one of our closures */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cap) return NULL; /* not one of our closures */ |
0 |
14 |
14 |
|
| 681 |
14 |
| cachesv = psc_cap(aTHX_ cap, PSC_CACHE); |
| 682 |
|
| /* |
| 683 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 684 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 685 |
14 |
▶ | if (!(cachesv && SvROK(cachesv) && SvTYPE(SvRV(cachesv)) == SVt_PVHV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(cachesv && SvROK(cachesv) && SvTYPE(SvRV(cachesv)) == SVt_PVHV)) |
12 |
2 |
14 |
| if (!(cachesv && SvROK(cachesv) && SvTYPE(SvRV(cachesv)) == SVt_PVHV)) |
12 |
0 |
12 |
| if (!(cachesv && SvROK(cachesv) && SvTYPE(SvRV(cachesv)) == SVt_PVHV)) |
0 |
12 |
12 |
|
| 686 |
2 |
| return NULL; |
| 687 |
|
| |
| 688 |
24 |
▶ | dir = SvPV_const(*av_fetch(cap, PSC_DIR, 0), dlen); |
Branch: 1 branch
| Branch | True | False | Total |
| dir = SvPV_const(*av_fetch(cap, PSC_DIR, 0), dlen); |
12 |
0 |
12 |
|
| 689 |
12 |
| rel = SvPV_const(relsv, rlen); |
| 690 |
|
| /* |
| 691 |
|
| # uncoverable branch true |
| 692 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 693 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 694 |
12 |
▶ | if (!rlen || rel[0] != '/') return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!rlen || rel[0] != '/') return NULL; |
12 |
0 |
12 |
| if (!rlen || rel[0] != '/') return NULL; |
0 |
12 |
12 |
|
| 695 |
|
| /* |
| 696 |
|
| # uncoverable branch true |
| 697 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 698 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 699 |
12 |
▶ | if (ps_path_unsafe(rel, rlen) || dlen + rlen > MAXPATHLEN) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ps_path_unsafe(rel, rlen) || dlen + rlen > MAXPATHLEN) return NULL; |
12 |
0 |
12 |
| if (ps_path_unsafe(rel, rlen) || dlen + rlen > MAXPATHLEN) return NULL; |
0 |
12 |
12 |
|
| 700 |
12 |
| memcpy(file, dir, dlen); |
| 701 |
12 |
| memcpy(file + dlen, rel, rlen); |
| 702 |
12 |
| file[dlen + rlen] = '\0'; |
| 703 |
|
| |
| 704 |
12 |
| digest = pa_digest_cached(aTHX_ (HV *)SvRV(cachesv), file, dlen + rlen, |
| 705 |
12 |
| psc_cap(aTHX_ cap, PSC_DEV) ? 1 : 0); |
| 706 |
12 |
▶ | if (!digest || SvCUR(digest) != PA_DIGEST_LEN) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!digest || SvCUR(digest) != PA_DIGEST_LEN) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
11 |
1 |
12 |
| if (!digest || SvCUR(digest) != PA_DIGEST_LEN) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
11 |
11 |
|
| 707 |
11 |
| return pa_fingerprint(aTHX_ rel, rlen, SvPVX(digest)); |
| 708 |
|
| } |
| 709 |
|
| |
| 710 |
|
| /* $c->asset's half: the compiled mount table, longest prefix first (compile |
| 711 |
|
| * sorted it), and the first static mount whose prefix this URL sits under |
| 712 |
|
| * gets to name it. A URL under no static mount, or under one that does not |
| 713 |
|
| * fingerprint, comes back unchanged. */ |
| 714 |
11 |
| static SV *pa_asset_for(pTHX_ AV *mounts, SV *url) { |
| 715 |
|
| SSize_t i, n; |
| 716 |
|
| STRLEN ulen; |
| 717 |
11 |
| const char *u = SvPV_const(url, ulen); |
| 718 |
|
| /* |
| 719 |
|
| # uncoverable branch true |
| 720 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 721 |
11 |
▶ | if (!mounts) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mounts) return NULL; |
0 |
11 |
11 |
|
| 722 |
11 |
| n = av_len(mounts) + 1; |
| 723 |
12 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
11 |
1 |
12 |
|
| 724 |
11 |
| SV **mp = av_fetch(mounts, i, 0); |
| 725 |
|
| HV *m; |
| 726 |
|
| SV **pp, **ap; |
| 727 |
|
| STRLEN pl; |
| 728 |
|
| const char *p; |
| 729 |
|
| /* |
| 730 |
|
| # uncoverable branch true class:ignore_covered_err |
| 731 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 732 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 733 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 734 |
12 |
▶ | if (!(mp && *mp && SvROK(*mp))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(mp && *mp && SvROK(*mp))) continue; |
11 |
0 |
11 |
| if (!(mp && *mp && SvROK(*mp))) continue; |
11 |
0 |
11 |
| if (!(mp && *mp && SvROK(*mp))) continue; |
0 |
11 |
11 |
|
| 735 |
11 |
| m = (HV *)SvRV(*mp); |
| 736 |
11 |
| pp = hv_fetchs(m, K_PREFIX, 0); |
| 737 |
11 |
| ap = hv_fetchs(m, K_APP, 0); |
| 738 |
|
| /* |
| 739 |
|
| # uncoverable branch true |
| 740 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 741 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 742 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 743 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 744 |
11 |
▶ | if (!(pp && *pp && ap && *ap)) continue; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(pp && *pp && ap && *ap)) continue; |
11 |
0 |
11 |
| if (!(pp && *pp && ap && *ap)) continue; |
11 |
0 |
11 |
| if (!(pp && *pp && ap && *ap)) continue; |
11 |
0 |
11 |
| if (!(pp && *pp && ap && *ap)) continue; |
0 |
11 |
11 |
|
| 745 |
11 |
| p = SvPV_const(*pp, pl); |
| 746 |
|
| /* |
| 747 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 748 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 749 |
11 |
▶ | if (ulen <= pl || memNE(u, p, pl) || u[pl] != '/') continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (ulen <= pl || memNE(u, p, pl) || u[pl] != '/') continue; |
11 |
0 |
11 |
| if (ulen <= pl || memNE(u, p, pl) || u[pl] != '/') continue; |
10 |
1 |
11 |
| if (ulen <= pl || memNE(u, p, pl) || u[pl] != '/') continue; |
0 |
10 |
10 |
|
| 750 |
|
| { |
| 751 |
|
| /* the first prefix match decides, even when it declines: a |
| 752 |
|
| * shorter mount further down would resolve this path against a |
| 753 |
|
| * different directory, and dispatch would never send the |
| 754 |
|
| * request there anyway */ |
| 755 |
10 |
| SV *rel = sv_2mortal(newSVpvn(u + pl, ulen - pl)); |
| 756 |
10 |
| SV *fp = pa_asset_url(aTHX_ *ap, rel); |
| 757 |
|
| SV *out; |
| 758 |
15 |
▶ | if (!fp) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fp) return NULL; |
5 |
5 |
10 |
|
| 759 |
5 |
| out = newSVpvn(p, pl); |
| 760 |
5 |
| sv_catsv(out, fp); |
| 761 |
5 |
| SvREFCNT_dec(fp); |
| 762 |
5 |
| return out; |
| 763 |
|
| } |
| 764 |
|
| } |
| 765 |
1 |
| return NULL; |
| 766 |
|
| } |
| 767 |
|
| |
| 768 |
|
| /* The same thing as a template filter - `{% "/static/app.css" | asset %}` - |
| 769 |
|
| * so a layout can name an asset without the handler having to pass the URL |
| 770 |
|
| * in. A magic CV over the application, registered on the shipped Stencil |
| 771 |
|
| * engine at boot; the mount table is read at call time, so it does not |
| 772 |
|
| * matter that the views are compiled before the mounts. |
| 773 |
|
| * |
| 774 |
|
| * The captured application is a WEAK reference: the app owns the view |
| 775 |
|
| * registry, which owns the engine, which owns this filter, and a strong |
| 776 |
|
| * one would close that loop and keep the whole application alive forever. */ |
| 777 |
|
| XS_INTERNAL(pa_asset_filter_cb); |
| 778 |
2 |
| XS_INTERNAL(pa_asset_filter_cb) { |
| 779 |
2 |
| dXSARGS; |
| 780 |
2 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 781 |
2 |
| SV *app, *out = NULL; |
| 782 |
|
| |
| 783 |
|
| /* |
| 784 |
|
| # uncoverable branch true |
| 785 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 786 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 787 |
2 |
▶ | if (!cap || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 1) XSRETURN_EMPTY; |
2 |
0 |
2 |
| if (!cap || items < 1) XSRETURN_EMPTY; |
0 |
2 |
2 |
|
| 788 |
2 |
| app = *av_fetch(cap, 0, 0); |
| 789 |
|
| /* |
| 790 |
|
| # uncoverable branch false |
| 791 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 792 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 793 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 794 |
2 |
▶ | if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { |
2 |
0 |
2 |
| if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { |
2 |
0 |
2 |
| if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { |
2 |
0 |
2 |
|
| 795 |
2 |
| SV *mp = app_get(aTHX_ (HV *)SvRV(app), K_MOUNTS_C); |
| 796 |
|
| /* |
| 797 |
|
| # uncoverable branch false |
| 798 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 799 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 800 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 801 |
2 |
▶ | if (mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV) |
2 |
0 |
2 |
| if (mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV) |
2 |
0 |
2 |
| if (mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV) |
2 |
0 |
2 |
|
| 802 |
2 |
| out = pa_asset_for(aTHX_ (AV *)SvRV(mp), ST(0)); |
| 803 |
|
| } |
| 804 |
2 |
▶ | if (out) ST(0) = sv_2mortal(out); |
Branch: 1 branch
| Branch | True | False | Total |
| if (out) ST(0) = sv_2mortal(out); |
1 |
1 |
2 |
|
| 805 |
2 |
| XSRETURN(1); |
| 806 |
|
| } |
| 807 |
|
| |
| 808 |
64 |
| static SV *pa_asset_filter(pTHX_ SV *app) { |
| 809 |
64 |
| AV *cap = newAV(); |
| 810 |
64 |
| SV *weak = newSVsv(app); |
| 811 |
64 |
| sv_rvweaken(weak); |
| 812 |
64 |
| av_push(cap, weak); |
| 813 |
64 |
| return punk_closure(aTHX_ pa_asset_filter_cb, cap); |
| 814 |
|
| } |
| 815 |
|
| |
| 816 |
|
| #endif /* PUNK_STATIC_H */ |