Punk.xs

filter: Statementstmt 88.1% 37 / 42 Branchbran 50.0% 30 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.6% 67 / 102 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/upload.xs ↑ Summary xs/headers.xs »
1 /*
2 * Punk.xs - root XS file
3 *
4 * Thin wrapper: resolves the File::Raw::JSON C ABI, includes the C
5 * implementation headers, then pulls in the per-module XS fragments
6 * from xs/ via INCLUDE: (the Chandra / Open::API layout).
7 */
8  
9 #define PERL_NO_GET_CONTEXT
10 #include "EXTERN.h"
11 #include "perl.h"
12 #include "XSUB.h"
13 #include "punk/punk_immortal_probe.h" /* TEMP author probe (-DPUNK_IMMORTAL_PROBE) */
14  
15 #include "punk/punk_compat.h" /* pre-5.16 perl shims; must precede punk/ */
16  
17 #include "frj_abi.h"
18 #include "frx_abi.h"
19  
20 /* ---- the File::Raw::JSON C ABI (JSON response fast path) ------------------ *
21 * Resolved lazily on first use from File::Raw::JSON::_abi_ptr, exactly the
22 * consumer pattern Open::API uses. A mismatch croaks; the Perl caller only
23 * reaches here when the XS tier is active, and File::Raw::JSON is a hard
24 * PREREQ, so this is a startup-environment error, not a request-time one. */
25 static const frj_abi *PUNK_FRJ = NULL;
26 static int PUNK_FRJ_TRIED = 0;
27  
28 109111 static const frj_abi *punk_frj(pTHX) {
29 109111 ▶ if (!PUNK_FRJ && !PUNK_FRJ_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!PUNK_FRJ && !PUNK_FRJ_TRIED) { 91 109020 109111
if (!PUNK_FRJ && !PUNK_FRJ_TRIED) { 91 0 91
30 91 dSP; int count; IV p = 0;
31 91 PUNK_FRJ_TRIED = 1;
32 91 eval_pv("require File::Raw::JSON;", FALSE);
33 /* The require runs arbitrary Perl. If that grew the value stack it was
34 * reallocated, and the SP captured above now points into the freed
35 * block - which the PUTBACK below would publish as PL_stack_sp. */
36 91 SPAGAIN;
37 91 ▶ if (!SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { 91 0 91
if (!SvTRUE(ERRSV)) { 91 0 91
38 91 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 91 91
39 91 count = call_pv("File::Raw::JSON::_abi_ptr", G_SCALAR | G_EVAL);
40 91 SPAGAIN;
41 91 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 91 0 91
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 91 0 91
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 91 0 91
42 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
43 91 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 91 0 91
44 91 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 91 0 91
45 91 const frj_abi *a = INT2PTR(const frj_abi *, p);
46 91 ▶ if (a && a->abi_version >= FRJ_ABI_VERSION) PUNK_FRJ = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= FRJ_ABI_VERSION) PUNK_FRJ = a; 91 0 91
if (a && a->abi_version >= FRJ_ABI_VERSION) PUNK_FRJ = a; 91 0 91
47 }
48 }
49 }
50 109111 ▶ if (!PUNK_FRJ)
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_FRJ) 0 109111 109111
51 0 croak("Punk: the XS JSON path needs File::Raw::JSON with a "
52 "compatible C ABI (FRJ_ABI_VERSION %d)", FRJ_ABI_VERSION);
53 109111 return PUNK_FRJ;
54 }
55  
56 /* ---- the File::Raw::XML C ABI (the XML body path) ------------------------- *
57 * The frj twin. The XML path needs frx_abi's SV bridge - doc_to_sv,
58 * doc_from_sv, node_from_sv, node_to_sv - which is what lets a document cross
59 * between C and Perl without a method call; File::Raw::XML 0.03 is the first
60 * with it and is a hard PREREQ, so a failure here is a startup-environment
61 * error rather than something to fall back from. */
62 static const frx_abi *PUNK_FRX = NULL;
63 static int PUNK_FRX_TRIED = 0;
64  
65 63 static const frx_abi *punk_frx(pTHX) {
66 63 ▶ if (!PUNK_FRX && !PUNK_FRX_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!PUNK_FRX && !PUNK_FRX_TRIED) { 4 59 63
if (!PUNK_FRX && !PUNK_FRX_TRIED) { 4 0 4
67 4 dSP; int count; IV p = 0;
68 4 PUNK_FRX_TRIED = 1;
69 4 eval_pv("require File::Raw::XML;", FALSE);
70 4 SPAGAIN; /* the require may have moved the stack */
71 4 ▶ if (!SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { 4 0 4
if (!SvTRUE(ERRSV)) { 4 0 4
72 4 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 4 4
73 4 count = call_pv("File::Raw::XML::_abi_ptr", G_SCALAR | G_EVAL);
74 4 SPAGAIN;
75 4 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
76 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
77 4 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 4 0 4
78 4 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 4 0 4
79 4 const frx_abi *a = INT2PTR(const frx_abi *, p);
80 4 ▶ if (a && a->abi_version >= FRX_ABI_VERSION) PUNK_FRX = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= FRX_ABI_VERSION) PUNK_FRX = a; 4 0 4
if (a && a->abi_version >= FRX_ABI_VERSION) PUNK_FRX = a; 4 0 4
81 }
82 }
83 }
84 63 ▶ if (!PUNK_FRX)
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_FRX) 0 63 63
85 0 croak("Punk: the XML path needs File::Raw::XML with a compatible "
86 "C ABI (FRX_ABI_VERSION %d)", FRX_ABI_VERSION);
87 63 return PUNK_FRX;
88 }
89  
90 #include "punk/punk_names.h" /* fixed module/slot/key strings, named once */
91 #include "punk/punk_escape.h" /* HTML escaping, shared, and $c->escape_html */
92 #include "punk/punk_xml.h" /* the XML body path, on frx_abi's SV bridge */
93 #include "punk/punk_obs.h" /* the C ABI's observer registry - early,
94 * because punk_context.h and punk_serve.h
95 * both fire from it (pk_abi_impl.h, which
96 * needs the context slots, comes after) */
97 #include "punk/punk_route.h"
98 #include "punk/punk_response.h"
99 #include "punk/punk_request.h"
100 #include "punk/punk_accept.h" /* Accept negotiation for respond_to */
101 #include "punk/punk_multipart.h" /* multipart/form-data + uploads */
102 #include "punk/punk_context.h" /* the per-request context object */
103 #include "punk/pk_abi_impl.h" /* Punk's own C ABI table (needs PCX_*) */
104 #include "punk/punk_obs_selftest.h" /* a C consumer of that table, for t/ */
105 #include "punk/punk_cookie.h" /* build a Set-Cookie value */
106 #include "punk/punk_session.h" /* signed cookie sessions (SHA-256/HMAC) */
107 #include "punk/punk_flash.h" /* one-request messages over the session */
108 #include "punk/punk_ua.h" /* the shared outbound Fetch agent */
109 #include "punk/punk_log.h" /* the level-based logger */
110 #include "punk/punk_dispatch.h" /* the Open::API C ABI consumer */
111 #include "punk/punk_views.h" /* the view-engine registry */
112 #include "punk/punk_scope.h" /* the `under` handle (needs context) */
113 #include "punk/punk_app.h" /* the Punk::App registrar (needs context) */
114 #include "punk/punk_host.h" /* the origin + host allowlist (needs app) */
115 #include "punk/punk_proxy.h" /* reverse-proxy trust (used by serve) */
116 #include "punk/punk_state.h" /* the compiled app as a C struct */
117 #include "punk/punk_serve.h" /* the request dispatcher */
118 #include "punk/punk_ws.h" /* the RFC 6455 frame codec */
119 #include "punk/punk_config.h" /* layered YAML config + resolved secrets */
120 #include "punk/punk_asset.h" /* content-addressed asset URLs (needs sha256) */
121 #include "punk/punk_filecache.h" /* static files by content + their stat: the
122 syscalls were 2/3 of a static hit */
123 #include "punk/punk_static.h" /* the static-file app (a magic-CV closure) */
124 #include "punk/punk_url.h" /* named routes: names, url_for, the filter */
125 #include "punk/punk_sendfile.h" /* $c->send_file: validators + ranges */
126 #include "punk/punk_cget.h" /* conditional GET (needs sendfile+context) */
127 #include "punk/punk_favicon.h" /* GET /favicon.ico, frozen at to_app */
128 #include "punk/punk_oamount.h" /* the `api` mount, boot half (needs static) */
129 #include "punk/punk_dbi.h" /* the shipped DBI model backend */
130 #include "punk/punk_dbiobs.h" /* Punk::DBI, the observed handle */
131 #include "punk/punk_dbq.h" /* the query shapes both backends share */
132 #include "punk/punk_model.h" /* the model tier: DSL, metadata, contract */
133 #include "punk/punk_csrf.h" /* single-use tokens over the session */
134 #include "punk/punk_entropy.h" /* sliced, pid-guarded entropy (needs csrf) */
135 #include "punk/punk_mpstream.h" /* multipart, parsed as it arrives */
136 #include "punk/punk_csp.h" /* CSP nonce + policy (needs entropy) */
137 #include "punk/punk_reqid.h" /* an id per request (uuidv7, buffered) */
138 #include "punk/punk_frozen.h" /* Frozen's C ABI, resolved on first use */
139 #include "punk/punk_plural.h" /* CLDR plural categories (before i18n) */
140 #include "punk/punk_i18n.h" /* the catalogue arena, loaded at boot */
141 #include "punk/punk_lang.h" /* Accept-Language (needs accept + context) */
142 #include "punk/punk_password.h" /* PBKDF2 password hashing (needs session+csrf) */
143 #include "punk/punk_auth.h" /* the auth battery's guard + denial path */
144 #include "punk/punk_validate.h" /* collecting validation, on the jsf ABI */
145 #include "punk/punk_stencil.h" /* the shipped view engine, on Stencil's ABI */
146 #include "punk/punk_markdown.h" /* the `markdown` docs mount (needs static,
147 * stencil and request) */
148 #include "hm_abi.h" /* Hyperman's C ABI, via EU::Depends */
149 #include "punk/punk_wsconn.h" /* the live WebSocket connection */
150 #include "punk/punk_wshandshake.h" /* the upgrade handshake (SHA-1 + base64) */
151 #include "punk/punk_sse.h" /* Server-Sent Events streams */
152 #include "punk/punk_afterres.h" /* after_response: the loop timer needs the
153 ABI, so it lands below hm_abi.h */
154 #include "punk/punk_ratelimit.h" /* rate_limit: a C before_dispatch closure */
155 #include "punk/punk_wsroom.h" /* per-worker pub/sub rooms */
156 #include "punk/punk_cache.h" /* the in-memory cache, bounded by BYTES */
157 #include "punk/punk_cachefile.h" /* ... and the file store, one copy per pool */
158 #include "punk/punk_bus.h" /* ... and across the pool, on hm_abi v5 */
159 #include "punk/punk_cachefront.h" /* Punk::Cache: the tier over both stores */
160 #include "punk/punk_sessionstore.h" /* where a server-side session lives */
161 #include "punk/punk_idem.h" /* Idempotency-Key (needs cache+request) */
162 #include "punk/punk_future.h" /* async result: loop when live, else block */
163 #include "punk/punk_dbil.h" /* the async model backend, on DBIx::Loop's
164 C ABI (needs punk_dbi.h + punk_future.h) */
165 #include "punk/punk_txn.h" /* $c->txn on either backend (needs dbil) */
166 #include "punk/punk_stream.h" /* $c->stream: a body of unknown length
167 (needs wsconn + wshandshake + future) */
168 #include "punk/punk_import.h" /* `use Punk` and the DSL table */
169 #include "punk/punk_cors.h" /* cross-origin: preflight + headers */
170 #include "punk/punk_headers.h" /* security headers on every response */
171 #include "punk/punk_blob.h" /* content-addressed uploads, on Apophis */
172 #include "punk/punk_sitemap.h" /* which routes are URLs (needs route+app) */
173 #include "punk/punk_mrows.h" /* JSONFile + Markdown model backends (needs dbq, markdown, app) */
174 #include "punk/punk_computed.h" /* computed read-only backends (needs dbq, mrows) */
175 #include "punk/punk_health.h" /* /healthz and /readyz (needs response+app) */
176 #include "punk/punk_metrics.h" /* Prometheus /metrics (needs pk_abi_impl+obs) */
177 #include "punk/punk_compile.h" /* the boot compiler (needs static+serve) */
178  
179 /* Every Punk::Router is an IV-ref to a pr_router (new through compile through
180 * match); the whole class is XS. */
181 63885 static pr_router *punk_router_of(pTHX_ SV *self) {
182 63885 ▶ if (!SvROK(self) || !SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || !SvIOK(SvRV(self))) 63885 0 63885
if (!SvROK(self) || !SvIOK(SvRV(self))) 0 63885 63885
183 0 croak("Punk::Router: not a compiled C router handle");
184 63885 return (pr_router *)INT2PTR(void *, SvIV(SvRV(self)));
185 }
186  
187 MODULE = Punk PACKAGE = Punk
188  
189 PROTOTYPES: DISABLE
190  
191 INCLUDE: xs/core.xs
192 INCLUDE: xs/abi.xs
193 INCLUDE: xs/router.xs
194 INCLUDE: xs/request.xs
195 INCLUDE: xs/upload.xs
196 INCLUDE: xs/response.xs
197 INCLUDE: xs/dispatch.xs
198 INCLUDE: xs/context.xs
199 INCLUDE: xs/session.xs
200 INCLUDE: xs/log.xs
201 INCLUDE: xs/scope.xs
202 INCLUDE: xs/app.xs
203 INCLUDE: xs/ratelimit.xs
204 INCLUDE: xs/serve.xs
205 INCLUDE: xs/views.xs
206 INCLUDE: xs/stencil.xs
207 INCLUDE: xs/csrf.xs
208 INCLUDE: xs/reqid.xs
209 INCLUDE: xs/password.xs
210 INCLUDE: xs/auth.xs
211 INCLUDE: xs/validate.xs
212 INCLUDE: xs/cors.xs
213 INCLUDE: xs/headers.xs
214 INCLUDE: xs/websocket.xs
215 INCLUDE: xs/wshandshake.xs
216 INCLUDE: xs/sse.xs
217 INCLUDE: xs/wsroom.xs
218 INCLUDE: xs/future.xs
219 INCLUDE: xs/stream.xs
220 INCLUDE: xs/config.xs
221 INCLUDE: xs/static.xs
222 INCLUDE: xs/sendfile.xs
223 INCLUDE: xs/markdown.xs
224 INCLUDE: xs/model.xs
225 INCLUDE: xs/oamount.xs
226 INCLUDE: xs/dbi.xs
227 INCLUDE: xs/dbil.xs
228 INCLUDE: xs/mrows.xs
229 INCLUDE: xs/computed.xs
230 INCLUDE: xs/txn.xs
231 INCLUDE: xs/cache.xs
232 INCLUDE: xs/blob.xs
233 INCLUDE: xs/sitemap.xs
234 INCLUDE: xs/health.xs
235 INCLUDE: xs/metrics.xs
236 INCLUDE: xs/conditionalget.xs
237 INCLUDE: xs/idempotency.xs
238 INCLUDE: xs/csp.xs
239 INCLUDE: xs/i18n.xs
240 INCLUDE: xs/url.xs
241 INCLUDE: xs/compile.xs
« xs/upload.xs ↑ Summary xs/headers.xs »