| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
|
| /* |
| 63 |
|
| # uncoverable branch false |
| 64 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 65 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 66 |
4 |
▶ | if ((n = A->node_from_sv(aTHX_ sv, dp)) != NULL && *dp) return n; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 67 |
0 |
| *dp = NULL; /* # uncoverable statement */ |
| 68 |
0 |
| return NULL; /* # uncoverable statement */ |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| /* Is this a Document or a Node? The question punk_coerce and finalize ask of |
| 72 |
|
| * every reference before handing it to the JSON encoder. */ |
| 73 |
32 |
| static int punk_xml_is(pTHX_ SV *sv) { |
| 74 |
|
| frx_doc *d; |
| 75 |
|
| int is_doc; |
| 76 |
32 |
| return punk_xml_apex(aTHX_ sv, &d, &is_doc) != NULL; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* The markup of a Document or Node as bytes (+1, no character flag - which |
| 80 |
|
| * is what a response body wants), or NULL with a mortal message in *errp. |
| 81 |
|
| * |
| 82 |
|
| * `write` reports a refusal by returning NULL and filling an frx_err; it does |
| 83 |
|
| * not croak. That matters more than it looks: punk_coerce runs from loop |
| 84 |
|
| * callbacks with no G_EVAL around them, so an exception raised here would |
| 85 |
|
| * leave Punk entirely rather than becoming a 500. */ |
| 86 |
19 |
| static SV *punk_xml_bytes(pTHX_ SV *sv, SV **errp) { |
| 87 |
|
| const frx_abi *A; |
| 88 |
19 |
| frx_doc *d = NULL; |
| 89 |
|
| const frx_node *apex; |
| 90 |
|
| frx_write_opts o; |
| 91 |
|
| frx_err err; |
| 92 |
|
| SV *out; |
| 93 |
19 |
| int is_doc = 0; |
| 94 |
|
| |
| 95 |
|
| /* |
| 96 |
|
| # uncoverable branch false |
| 97 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 98 |
19 |
▶ | if (errp) *errp = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errp) *errp = NULL; |
19 |
0 |
19 |
|
| 99 |
19 |
| apex = punk_xml_apex(aTHX_ sv, &d, &is_doc); |
| 100 |
|
| /* |
| 101 |
|
| # uncoverable branch true |
| 102 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 103 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 104 |
19 |
▶ | if (!apex || !d) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!apex || !d) { |
19 |
0 |
19 |
| if (!apex || !d) { |
0 |
19 |
19 |
|
| 105 |
|
| /* |
| 106 |
|
| # uncoverable statement |
| 107 |
|
| # uncoverable branch true |
| 108 |
|
| # uncoverable branch false |
| 109 |
|
| # uncoverable condition left count:1 |
| 110 |
|
| # uncoverable condition right count:1 */ |
| 111 |
0 |
▶ | if (errp) *errp = sv_2mortal(newSVpvs( |
Branch: 1 branch
| Branch | True | False | Total |
| if (errp) *errp = sv_2mortal(newSVpvs( |
0 |
0 |
0 |
|
| 112 |
|
| "Punk: xml: not a File::Raw::XML::Document or ::Node")); |
| 113 |
0 |
| return NULL; /* # uncoverable statement */ |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
19 |
| A = punk_frx(aTHX); |
| 117 |
19 |
| A->write_opts_init(&o); |
| 118 |
|
| /* a fragment carries neither, matching what ->to_string does for a node */ |
| 119 |
19 |
▶ | if (!is_doc) { o.declaration = 0; o.doctype = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_doc) { o.declaration = 0; o.doctype = 0; } |
2 |
17 |
19 |
|
| 120 |
|
| |
| 121 |
19 |
| memset(&err, 0, sizeof err); |
| 122 |
|
| /* (A->write), with the member parenthesised: on a PERL_IMPLICIT_SYS perl |
| 123 |
|
| * - Strawberry - iperlsys.h makes `write` a function-like macro, and |
| 124 |
|
| * A->write(...) does not compile. frx_abi.h says so where it declares it. */ |
| 125 |
19 |
| out = (A->write)(aTHX_ apex, d, &o, &err); |
| 126 |
|
| /* |
| 127 |
|
| # uncoverable branch true |
| 128 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 129 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 130 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 131 |
19 |
▶ | if (!out && errp) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!out && errp) |
0 |
19 |
19 |
| if (!out && errp) |
0 |
0 |
0 |
|
| 132 |
0 |
▶ | *errp = sv_2mortal(newSVpvf("Punk: xml: %s", /* # uncoverable statement */ |
Branch: 1 branch
| Branch | True | False | Total |
| *errp = sv_2mortal(newSVpvf("Punk: xml: %s", /* # uncoverable statement */ |
0 |
0 |
0 |
|
| 133 |
|
| err.what ? err.what : "the document could not be written")); |
| 134 |
19 |
| return out; |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
|
| /* Bytes to a blessed File::Raw::XML::Document (+1), croaking with the |
| 138 |
|
| * parser's own message on a refusal - which is what $c->req->json does for |
| 139 |
|
| * malformed JSON, and the two bodies should not answer differently. |
| 140 |
|
| * |
| 141 |
|
| * `parse` is the strict entry and frx_abi.h guarantees it can never mean |
| 142 |
|
| * anything else: no document type declaration is accepted wherever it |
| 143 |
|
| * stands, which removes external entities, parameter entities, the external |
| 144 |
|
| * DTD fetch, XXE and the billion laughs as a class rather than as a setting. |
| 145 |
|
| * There is no options argument here and there must never be one - a profile |
| 146 |
|
| * or a resolver reachable from a request is exactly the switch that would |
| 147 |
|
| * give all of that back. */ |
| 148 |
6 |
| static SV *punk_xml_parse(pTHX_ SV *bytes) { |
| 149 |
6 |
| const frx_abi *A = punk_frx(aTHX); |
| 150 |
6 |
| SV *err = NULL; /* mortal, owned by parse; never freed here */ |
| 151 |
|
| STRLEN len; |
| 152 |
6 |
| const char *p = SvPV_const(bytes, len); |
| 153 |
6 |
| frx_doc *d = A->parse(aTHX_ p, len, NULL, &err); |
| 154 |
6 |
▶ | if (!d) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d) { |
2 |
4 |
6 |
|
| 155 |
|
| /* |
| 156 |
|
| # uncoverable branch false |
| 157 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 158 |
2 |
▶ | if (err) croak_sv(err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) croak_sv(err); |
2 |
0 |
2 |
|
| 159 |
0 |
| croak("Punk: xml: the request body is not well-formed XML"); /* # uncoverable statement */ |
| 160 |
|
| } |
| 161 |
4 |
| return A->doc_to_sv(aTHX_ d); /* takes the document; magic frees it */ |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
|
| #endif /* PUNK_XML_H */ |