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