include/punk/punk_xml.h

filter: Statementstmt 85.7% 36 / 42 Branchbran 59.3% 19 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.3% 55 / 74 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_filecache.h ↑ Summary xs/password.xs »
1 #ifndef PUNK_XML_H
2 #define PUNK_XML_H
3  
4 /* The XML body path: a request body parsed into a File::Raw::XML::Document,
5 * and a Document or Node handed back as a response.
6 *
7 * Punk maps nothing between XML and Perl data. JSON's model is Perl's, so
8 * `return { ok => 1 }` has one obvious encoding; XML's is not, and every
9 * convention for elements-versus-attributes, ordering, mixed content and
10 * repeated elements is wrong for somebody's schema. What is here is the two
11 * ends only: bytes in to a tree, a tree out to bytes. The application walks
12 * it or XPaths it.
13 *
14 * All of it runs over frx_abi.h's SV bridge (version 2), so a document
15 * crosses between C and Perl without a method call: `parse` and `doc_to_sv`
16 * on the way in, `doc_from_sv`/`node_from_sv` and `write` on the way out.
17 *
18 * Needs punk_frx() from Punk.xs. */
19  
20 /* One spelling, used by $c->xml, Punk::Response::finalize and punk_coerce.
21 * The charset is explicit because a Node is written without an XML
22 * declaration, so a fragment would otherwise leave its encoding to RFC 7303's
23 * default; it also matches the text/html and text/plain defaults beside it.
24 * Negotiation is unaffected - pa_match compares type and subtype only. */
25 #define PK_XML_CT "application/xml; charset=utf-8"
26  
27 /* Could this SV be one of File::Raw::XML's objects at all?
28 *
29 * punk_frx() requires File::Raw::XML on its first call, and an application
30 * that never touches XML must not pay that just because it returned a hash
31 * reference. Until the module is loaded there is nothing that could have
32 * blessed a Document or a Node, so a missing stash is a definitive no and
33 * this stays a null pointer compare for every JSON response ever sent. */
34 51 static int punk_xml_possible(pTHX_ SV *sv) {
35 51 ▶ if (!SvROK(sv) || !SvOBJECT(SvRV(sv))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || !SvOBJECT(SvRV(sv))) return 0; 41 10 51
if (!SvROK(sv) || !SvOBJECT(SvRV(sv))) return 0; 3 38 41
36 38 return gv_stashpvs("File::Raw::XML::Document", 0) != NULL;
37 }
38  
39 /* The apex to serialise and the document it belongs to, or NULL when this is
40 * not a Document or a Node. The two _from_sv entries answer NULL rather than
41 * croaking for anything that is not theirs, which is what makes them usable
42 * as the type test.
43 *
44 * A Document's apex is its document node, not its root element: that is what
45 * carries the top-level comments and processing instructions, and what the
46 * writer needs to emit the declaration and the DOCTYPE. */
47 51 static const frx_node *punk_xml_apex(pTHX_ SV *sv, frx_doc **dp, int *is_doc) {
48 const frx_abi *A;
49 frx_doc *d;
50 const frx_node *n;
51  
52 51 *dp = NULL;
53 51 *is_doc = 0;
54 51 ▶ if (!punk_xml_possible(aTHX_ sv)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!punk_xml_possible(aTHX_ sv)) return NULL; 13 38 51
55  
56 38 A = punk_frx(aTHX);
57 38 ▶ if ((d = A->doc_from_sv(aTHX_ sv)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((d = A->doc_from_sv(aTHX_ sv)) != NULL) { 34 4 38
58 34 *dp = d;
59 34 *is_doc = 1;
60 34 return A->document(d);
61 }
62 4 ▶ if ((n = A->node_from_sv(aTHX_ sv, dp)) != NULL && *dp) return n;
Branch: 2 branches
BranchTrueFalseTotal
if ((n = A->node_from_sv(aTHX_ sv, dp)) != NULL && *dp) return n; 4 0 4
if ((n = A->node_from_sv(aTHX_ sv, dp)) != NULL && *dp) return n; 4 0 4
63 0 *dp = NULL;
64 0 return NULL;
65 }
66  
67 /* Is this a Document or a Node? The question punk_coerce and finalize ask of
68 * every reference before handing it to the JSON encoder. */
69 32 static int punk_xml_is(pTHX_ SV *sv) {
70 frx_doc *d;
71 int is_doc;
72 32 return punk_xml_apex(aTHX_ sv, &d, &is_doc) != NULL;
73 }
74  
75 /* The markup of a Document or Node as bytes (+1, no character flag - which
76 * is what a response body wants), or NULL with a mortal message in *errp.
77 *
78 * `write` reports a refusal by returning NULL and filling an frx_err; it does
79 * not croak. That matters more than it looks: punk_coerce runs from loop
80 * callbacks with no G_EVAL around them, so an exception raised here would
81 * leave Punk entirely rather than becoming a 500. */
82 19 static SV *punk_xml_bytes(pTHX_ SV *sv, SV **errp) {
83 const frx_abi *A;
84 19 frx_doc *d = NULL;
85 const frx_node *apex;
86 frx_write_opts o;
87 frx_err err;
88 SV *out;
89 19 int is_doc = 0;
90  
91 19 ▶ if (errp) *errp = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (errp) *errp = NULL; 19 0 19
92 19 apex = punk_xml_apex(aTHX_ sv, &d, &is_doc);
93 19 ▶ if (!apex || !d) {
Branch: 2 branches
BranchTrueFalseTotal
if (!apex || !d) { 19 0 19
if (!apex || !d) { 0 19 19
94 0 ▶ if (errp) *errp = sv_2mortal(newSVpvs(
Branch: 1 branch
BranchTrueFalseTotal
if (errp) *errp = sv_2mortal(newSVpvs( 0 0 0
95 "Punk: xml: not a File::Raw::XML::Document or ::Node"));
96 0 return NULL;
97 }
98  
99 19 A = punk_frx(aTHX);
100 19 A->write_opts_init(&o);
101 /* a fragment carries neither, matching what ->to_string does for a node */
102 19 ▶ if (!is_doc) { o.declaration = 0; o.doctype = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!is_doc) { o.declaration = 0; o.doctype = 0; } 2 17 19
103  
104 19 memset(&err, 0, sizeof err);
105 /* (A->write), with the member parenthesised: on a PERL_IMPLICIT_SYS perl
106 * - Strawberry - iperlsys.h makes `write` a function-like macro, and
107 * A->write(...) does not compile. frx_abi.h says so where it declares it. */
108 19 out = (A->write)(aTHX_ apex, d, &o, &err);
109 19 ▶ if (!out && errp)
Branch: 2 branches
BranchTrueFalseTotal
if (!out && errp) 0 19 19
if (!out && errp) 0 0 0
110 0 ▶ *errp = sv_2mortal(newSVpvf("Punk: xml: %s",
Branch: 1 branch
BranchTrueFalseTotal
*errp = sv_2mortal(newSVpvf("Punk: xml: %s", 0 0 0
111 err.what ? err.what : "the document could not be written"));
112 19 return out;
113 }
114  
115 /* Bytes to a blessed File::Raw::XML::Document (+1), croaking with the
116 * parser's own message on a refusal - which is what $c->req->json does for
117 * malformed JSON, and the two bodies should not answer differently.
118 *
119 * `parse` is the strict entry and frx_abi.h guarantees it can never mean
120 * anything else: no document type declaration is accepted wherever it
121 * stands, which removes external entities, parameter entities, the external
122 * DTD fetch, XXE and the billion laughs as a class rather than as a setting.
123 * There is no options argument here and there must never be one - a profile
124 * or a resolver reachable from a request is exactly the switch that would
125 * give all of that back. */
126 6 static SV *punk_xml_parse(pTHX_ SV *bytes) {
127 6 const frx_abi *A = punk_frx(aTHX);
128 6 SV *err = NULL; /* mortal, owned by parse; never freed here */
129 STRLEN len;
130 6 const char *p = SvPV_const(bytes, len);
131 6 frx_doc *d = A->parse(aTHX_ p, len, NULL, &err);
132 6 ▶ if (!d) {
Branch: 1 branch
BranchTrueFalseTotal
if (!d) { 2 4 6
133 2 ▶ if (err) croak_sv(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) croak_sv(err); 2 0 2
134 0 croak("Punk: xml: the request body is not well-formed XML");
135 }
136 4 return A->doc_to_sv(aTHX_ d); /* takes the document; magic frees it */
137 }
138  
139 #endif /* PUNK_XML_H */
« include/punk/punk_filecache.h ↑ Summary xs/password.xs »